// Parser Diagnostics
//===----------------------------------------------------------------------===//
-DIAG(w_no_declarators, WARNING,
- "declaration does not declare anything")
DIAG(w_asm_qualifier_ignored, WARNING,
"ignored %0 qualifier on asm")
DIAG(err_anonymous_union_with_storage_spec, ERROR,
"anonymous union at class scope must not have a storage specifier")
DIAG(err_anonymous_struct_not_member, ERROR,
- "anonymous structs and classes must be class members")
+ "anonymous %select{structs|structs and classes}0 must be %select{struct or union|class}0 members")
+DIAG(err_anonymous_union_not_member, ERROR,
+ "anonymous unions must be struct or union members")
DIAG(err_anonymous_union_member_redecl, ERROR,
"member of anonymous union redeclares %0")
DIAG(err_anonymous_struct_member_redecl, ERROR,
/// ParsedFreeStandingDeclSpec - This method is invoked when a declspec with
/// no declarator (e.g. "struct foo;") is parsed.
Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) {
- // FIXME: Isn't that more of a parser diagnostic than a sema diagnostic?
+ TagDecl *Tag
+ = dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
+ if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) {
+ if (!Record->getDeclName() && Record->isDefinition() &&
+ DS.getStorageClassSpec() != DeclSpec::SCS_typedef)
+ return BuildAnonymousStructOrUnion(S, DS, Record);
+
+ // Microsoft allows unnamed struct/union fields. Don't complain
+ // about them.
+ // FIXME: Should we support Microsoft's extensions in this area?
+ if (Record->getDeclName() && getLangOptions().Microsoft)
+ return Tag;
+ }
+
if (!DS.isMissingDeclaratorOk()) {
// FIXME: This diagnostic is emitted even when various previous
// errors occurred (see e.g. test/Sema/decl-invalid.c). However,
return 0;
}
- TagDecl *Tag
- = dyn_cast_or_null<TagDecl>(static_cast<Decl *>(DS.getTypeRep()));
- if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) {
- if (!Record->getDeclName() && Record->isDefinition() &&
- !Record->isInvalidDecl())
- return BuildAnonymousStructOrUnion(S, DS, Record);
- }
-
return Tag;
}
}
} else {
// FIXME: Check GNU C semantics
+ if (Record->isUnion() && !Owner->isRecord()) {
+ Diag(Record->getLocation(), diag::err_anonymous_union_not_member)
+ << (int)getLangOptions().CPlusPlus;
+ Invalid = true;
+ }
}
if (!Record->isUnion() && !Owner->isRecord()) {
- Diag(Record->getLocation(), diag::err_anonymous_struct_not_member);
+ Diag(Record->getLocation(), diag::err_anonymous_struct_not_member)
+ << (int)getLangOptions().CPlusPlus;
Invalid = true;
}
// Inject the members of the anonymous struct/union into the owning
// context and into the identifier resolver chain for name lookup
// purposes.
- Invalid = Invalid || InjectAnonymousStructOrUnionMembers(S, Owner, Record);
+ if (InjectAnonymousStructOrUnionMembers(S, Owner, Record))
+ Invalid = true;
// Mark this as an anonymous struct/union type. Note that we do not
// do this until after we have already checked and injected the
case DeclSpec::TST_enum: Kind = TagDecl::TK_enum; break;
}
+ DeclContext *SearchDC = CurContext;
DeclContext *DC = CurContext;
DeclContext *LexicalContext = CurContext;
ScopedDecl *PrevDecl = 0;
// with C structs, unions, and enums when looking for a matching
// tag declaration or definition. See the similar lookup tweak
// in Sema::LookupDecl; is there a better way to deal with this?
- while (isa<RecordDecl>(DC) || isa<EnumDecl>(DC))
- DC = DC->getParent();
+ while (isa<RecordDecl>(SearchDC) || isa<EnumDecl>(SearchDC))
+ SearchDC = SearchDC->getParent();
}
}
// If this is a use of a previous tag, or if the tag is already declared
// in the same scope (so that the definition/declaration completes or
// rementions the tag), reuse the decl.
- if (TK == TK_Reference || isDeclInScope(PrevDecl, DC, S)) {
+ if (TK == TK_Reference || isDeclInScope(PrevDecl, SearchDC, S)) {
// Make sure that this wasn't declared as an enum and now used as a
// struct or something similar.
if (PrevTagDecl->getTagKind() != Kind) {
// PrevDecl. If it's NULL, we have a new definition.
} else {
// PrevDecl is a namespace.
- if (isDeclInScope(PrevDecl, DC, S)) {
+ if (isDeclInScope(PrevDecl, SearchDC, S)) {
// The tag name clashes with a namespace name, issue an error and
// recover by making this tag be anonymous.
Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
// C structs and unions.
// Find the context where we'll be declaring the tag.
+ // FIXME: We would like to maintain the current DeclContext as the
+ // lexical context,
while (DC->isRecord())
DC = DC->getParent();
LexicalContext = DC;
CurContext = OldContext;
} else
PushOnScopeChains(New, S);
- } else if (getLangOptions().CPlusPlus) {
- // FIXME: We also want to do this for C, but if this tag is
- // defined within a structure CurContext will point to the context
- // enclosing the structure, and we would end up inserting the tag
- // type into the wrong place.
+ } else {
LexicalContext->addDecl(Context, New);
}
--- /dev/null
+// RUN: clang -fsyntax-only -verify %s
+struct X {
+ union {
+ float f3;
+ double d2;
+ } named;
+
+ union {
+ int i;
+ float f;
+
+ union {
+ float f2;
+ double d;
+ };
+ };
+
+ struct {
+ int a;
+ float b;
+ };
+};
+
+void test_unqual_references(struct X x, const struct X xc) {
+ x.i = 0;
+ x.f = 0.0;
+ x.f2 = x.f;
+ x.d = x.f;
+ x.f3 = 0; // expected-error{{no member named 'f3'}}
+ x.a = 0;
+
+ xc.d = 0.0; // expected-error{{read-only variable is not assignable}}
+ xc.f = 0; // expected-error{{read-only variable is not assignable}}
+ xc.a = 0; // expected-error{{read-only variable is not assignable}}
+}
+
+
+struct Redecl {
+ int x; // expected-note{{previous declaration is here}}
+ struct y { };
+
+ union {
+ int x; // expected-error{{member of anonymous union redeclares 'x'}}
+ float y;
+ double z; // expected-note{{previous declaration is here}}
+ double zz; // expected-note{{previous declaration is here}}
+ };
+
+ int z; // expected-error{{duplicate member 'z'}}
+ void zz(); // expected-error{{duplicate member 'zz'}} \
+ // expected-error{{field 'zz' declared as a function}}
+};
+
+union { // expected-error{{anonymous unions must be struct or union members}}
+ int int_val;
+ float float_val;
+};
+
+static union { // expected-error{{anonymous unions must be struct or union members}}
+ int int_val2;
+ float float_val2;
+};
+
+void f() {
+ int_val2 = 0; // expected-error{{use of undeclared identifier}}
+ float_val2 = 0.0; // expected-error{{use of undeclared identifier}}
+}
+
+void g() {
+ union { // expected-error{{anonymous unions must be struct or union members}}
+ int i;
+ float f2;
+ };
+ i = 0; // expected-error{{use of undeclared identifier}}
+ f2 = 0.0; // expected-error{{use of undeclared identifier}}
+}
+
+// <rdar://problem/6483159>
+struct s0 { union { int f0; }; };
+
+// <rdar://problem/6481130>
+typedef struct { }; // expected-error{{declaration does not declare anything}}