]> granicus.if.org Git - clang/commitdiff
Add a -pedantic warning: an anonymous union within an anonymous union is not
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 28 Jan 2013 00:54:05 +0000 (00:54 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 28 Jan 2013 00:54:05 +0000 (00:54 +0000)
permitted in standard C++, despite being silently accepted by many (all?) major
C++ implementations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173643 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/SemaCXX/anonymous-union.cpp
test/SemaCXX/constant-expression-cxx11.cpp

index fbaa2c5168f4daab5174b307a473376140a069d9..54b8c6f64c1dbaa8e5e1c81daeec363d66d3d724 100644 (file)
@@ -5344,6 +5344,9 @@ def err_anonymous_record_with_type : Error<
 def ext_anonymous_record_with_type : Extension<
   "types declared in an anonymous %select{struct|union}0 are a Microsoft "
   "extension">, InGroup<Microsoft>;
+def ext_anonymous_record_with_anonymous_type : Extension<
+  "nested anonymous types are an extension">,
+  InGroup<DiagGroup<"nested-anon-types">>;
 def err_anonymous_record_with_function : Error<
   "functions cannot be declared in an anonymous %select{struct|union}0">;
 def err_anonymous_record_with_static : Error<
index 136d12ebc317531943ee038d4c9b9025d0233031..a5f39a82f18839ed254063dd5632de79b89c3814 100644 (file)
@@ -3198,6 +3198,12 @@ Decl *Sema::BuildAnonymousStructOrUnion(Scope *S, DeclSpec &DS,
               << (int)Record->isUnion();
             Invalid = true;
           }
+        } else {
+          // This is an anonymous type definition within another anonymous type.
+          // This is a popular extension, provided by Plan9, MSVC and GCC, but
+          // not part of standard C++.
+          Diag(MemRecord->getLocation(),
+               diag::ext_anonymous_record_with_anonymous_type);
         }
       } else if (isa<AccessSpecDecl>(*Mem)) {
         // Any access specifier is fine.
index 2dd7ab86a88d76a011d0af0f5bb028a543135d23..e77f52514733435f04973e5325b53b771094b1fb 100644 (file)
@@ -9,7 +9,7 @@ struct X {
     int i;
     float f;
     
-    union {
+    union { // expected-warning{{nested anonymous types are an extension}}
       float f2;
       mutable double d;
     };
@@ -101,7 +101,7 @@ void g() {
 struct BadMembers {
   union {
     struct X { }; // expected-error {{types cannot be declared in an anonymous union}}
-    struct { int x; int y; } y;
+    struct { int x; int y; } y; // expected-warning{{nested anonymous types are an extension}}
     
     void f(); // expected-error{{functions cannot be declared in an anonymous union}}
   private: int x1; // expected-error{{anonymous union cannot contain a private data member}}
@@ -128,7 +128,7 @@ namespace test4 {
     struct { // expected-warning{{anonymous structs are a GNU extension}}
       int s0; // expected-note {{declared private here}}
       double s1; // expected-note {{declared private here}}
-      union {
+      union { // expected-warning{{nested anonymous type}}
         int su0; // expected-note {{declared private here}}
         double su1; // expected-note {{declared private here}}
       };
@@ -136,7 +136,7 @@ namespace test4 {
     union {
       int u0; // expected-note {{declared private here}}
       double u1; // expected-note {{declared private here}}
-      struct { // expected-warning{{anonymous structs are a GNU extension}}
+      struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{nested anonymous type}}
         int us0; // expected-note {{declared private here}}
         double us1; // expected-note {{declared private here}}
       };
@@ -187,7 +187,7 @@ namespace PR8326 {
   
   private:
     const union { // expected-warning{{anonymous union cannot be 'const'}}
-      struct { // expected-warning{{anonymous structs are a GNU extension}}
+      struct { // expected-warning{{anonymous structs are a GNU extension}} expected-warning{{nested anonymous type}}
         T x;
         T y;
       };
index f504eb621f6c27e61e95961a3a5f485002e6dca4..8bb448cab030893e0cd3aade56596b8aa88a7df5 100644 (file)
@@ -1153,8 +1153,8 @@ namespace ConvertedConstantExpr {
 namespace IndirectField {
   struct S {
     struct { // expected-warning {{GNU extension}}
-      union {
-        struct { // expected-warning {{GNU extension}}
+      union { // expected-warning {{nested anonymous types are an extension}}
+        struct { // expected-warning {{GNU extension}} expected-warning {{nested anonymous types are an extension}}
           int a;
           int b;
         };