diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-03-06 23:06:59 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-03-06 23:06:59 +0000 |
commit | a71c129fb8052a143cbf548963a8db2150b0078e (patch) | |
tree | d8b30db92ff1c3483d2a52d8ffff57e2e777ce05 | |
parent | a6e27790acb6d06be6c1113ea94165c4b7e056cd (diff) |
Use the 'declaration does not declare anything' error when we see an anonymous struct/union declaration outside of a struct or union in C
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66303 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.def | 2 | ||||
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 19 | ||||
-rw-r--r-- | test/Sema/anonymous-struct-union.c | 8 |
3 files changed, 14 insertions, 15 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.def b/include/clang/Basic/DiagnosticSemaKinds.def index 953d3e3429..ce40a9bfab 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.def +++ b/include/clang/Basic/DiagnosticSemaKinds.def @@ -1215,8 +1215,6 @@ 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 %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, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 50113ee4b7..5e7ce52ee8 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -922,8 +922,14 @@ Sema::DeclTy *Sema::ParsedFreeStandingDeclSpec(Scope *S, DeclSpec &DS) { if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Tag)) { if (!Record->getDeclName() && Record->isDefinition() && - DS.getStorageClassSpec() != DeclSpec::SCS_typedef) - return BuildAnonymousStructOrUnion(S, DS, Record); + DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { + if (getLangOptions().CPlusPlus || + Record->getDeclContext()->isRecord()) + return BuildAnonymousStructOrUnion(S, DS, Record); + + Diag(DS.getSourceRange().getBegin(), diag::err_no_declarators) + << DS.getSourceRange(); + } // Microsoft allows unnamed struct/union fields. Don't complain // about them. @@ -1102,14 +1108,7 @@ Sema::DeclTy *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS, Invalid = true; } } - } 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) diff --git a/test/Sema/anonymous-struct-union.c b/test/Sema/anonymous-struct-union.c index 4e699ab2ff..d8a445c649 100644 --- a/test/Sema/anonymous-struct-union.c +++ b/test/Sema/anonymous-struct-union.c @@ -50,12 +50,12 @@ struct Redecl { void zz(); // expected-error{{duplicate member 'zz'}} }; -union { // expected-error{{anonymous unions must be struct or union members}} +union { // expected-error{{declaration does not declare anything}} int int_val; float float_val; }; -static union { // expected-error{{anonymous unions must be struct or union members}} +static union { // expected-error{{declaration does not declare anything}} int int_val2; float float_val2; }; @@ -66,7 +66,7 @@ void f() { } void g() { - union { // expected-error{{anonymous unions must be struct or union members}} + union { // expected-error{{declaration does not declare anything}} int i; float f2; }; @@ -87,3 +87,5 @@ struct s1 { int f0; // expected-error{{member of anonymous union redeclares 'f0'}} }; }; + +struct {}; // expected-error{{declaration does not declare anything}} |