[cfe-commits] r66303 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.def lib/Sema/SemaDecl.cpp test/Sema/anonymous-struct-union.c

Douglas Gregor dgregor at apple.com
Fri Mar 6 15:06:59 PST 2009


Author: dgregor
Date: Fri Mar  6 17:06:59 2009
New Revision: 66303

URL: http://llvm.org/viewvc/llvm-project?rev=66303&view=rev
Log:
Use the 'declaration does not declare anything' error when we see an anonymous struct/union declaration outside of a struct or union in C

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/anonymous-struct-union.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def?rev=66303&r1=66302&r2=66303&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.def Fri Mar  6 17:06:59 2009
@@ -1215,8 +1215,6 @@
      "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,

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=66303&r1=66302&r2=66303&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Mar  6 17:06:59 2009
@@ -922,8 +922,14 @@
 
   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 @@
           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)

Modified: cfe/trunk/test/Sema/anonymous-struct-union.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/anonymous-struct-union.c?rev=66303&r1=66302&r2=66303&view=diff

==============================================================================
--- cfe/trunk/test/Sema/anonymous-struct-union.c (original)
+++ cfe/trunk/test/Sema/anonymous-struct-union.c Fri Mar  6 17:06:59 2009
@@ -50,12 +50,12 @@
   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 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 @@
     int f0; // expected-error{{member of anonymous union redeclares 'f0'}}
   };
 };
+
+struct {}; // expected-error{{declaration does not declare anything}}





More information about the cfe-commits mailing list