Include named unions in union member init checking
llvm-svn: 144883
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 82a5ba2..6c5e79a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4346,7 +4346,7 @@
 def err_multiple_mem_initialization : Error <
   "multiple initializations given for non-static member %0">;
 def err_multiple_mem_union_initialization : Error <
-  "initializing multiple members of anonymous union">;
+  "initializing multiple members of union">;
 def err_multiple_base_initialization : Error <
   "multiple initializations given for base %0">;
 
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 8aeb8fb..52eb9b0 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3065,11 +3065,9 @@
                              RedundantUnionMap &Unions) {
   FieldDecl *Field = Init->getAnyMember();
   RecordDecl *Parent = Field->getParent();
-  if (!Parent->isAnonymousStructOrUnion())
-    return false;
-
   NamedDecl *Child = Field;
-  do {
+
+  while (Parent->isAnonymousStructOrUnion() || Parent->isUnion()) {
     if (Parent->isUnion()) {
       UnionEntry &En = Unions[Parent];
       if (En.first && En.first != Child) {
@@ -3085,11 +3083,13 @@
         En.first = Child;
         En.second = Init;
       }
+      if (!Parent->isAnonymousStructOrUnion())
+        return false;
     }
 
     Child = Parent;
     Parent = cast<RecordDecl>(Parent->getDeclContext());
-  } while (Parent->isAnonymousStructOrUnion());
+  }
 
   return false;
 }
diff --git a/clang/test/CXX/class/class.base/class.base.init/p5-0x.cpp b/clang/test/CXX/class/class.base/class.base.init/p5-0x.cpp
new file mode 100644
index 0000000..e9aa6da
--- /dev/null
+++ b/clang/test/CXX/class/class.base/class.base.init/p5-0x.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
+
+// [class.base.init]p5
+// A ctor-initializer may initialize a variant member of the constructor’s 
+// class. If a ctor-initializer specifies more than one mem-initializer for the
+// same member or for the same base class, the ctor-initializer is ill-formed.
+
+union E {
+  int a;
+  int b;
+  E() : a(1),  // expected-note{{previous initialization is here}}
+        b(2) { // expected-error{{initializing multiple members of union}}
+  }
+};
+
+union F {
+  struct {
+    int a;
+    int b;
+  };
+  int c;
+  F() : a(1),  // expected-note{{previous initialization is here}}
+        b(2),
+        c(3) { // expected-error{{initializing multiple members of union}}
+  }
+};
diff --git a/clang/test/CXX/special/class.init/class.base.init/p8-0x.cpp b/clang/test/CXX/special/class.init/class.base.init/p8-0x.cpp
index 3e26e49..a108533 100644
--- a/clang/test/CXX/special/class.init/class.base.init/p8-0x.cpp
+++ b/clang/test/CXX/special/class.init/class.base.init/p8-0x.cpp
@@ -16,14 +16,17 @@
 } s(0);
 
 union U {
-  int a = 0;
+  int a = 0; // desired-note 5 {{previous initialization is here}}
   char b = 'x';
 
   // FIXME: these should all be rejected
-  U() {} // desired-error {{at most one member of a union may be initialized}}
-  U(int) : a(1) {} // desired-error {{at most one member of a union may be initialized}}
-  U(char) : b('y') {} // desired-error {{at most one member of a union may be initialized}}
-  U(double) : a(1), b('y') {} // desired-error {{at most one member of a union may be initialized}}
+  U() {} // desired-error {{initializing multiple members of union}}
+  U(int) : a(1) {} // desired-error {{initializing multiple members of union}}
+  U(char) : b('y') {} // desired-error {{initializing multiple members of union}}
+  // this expected note should be removed & the note should appear on the 
+  // declaration of 'a' when this set of cases is handled correctly.
+  U(double) : a(1), // expected-note{{previous initialization is here}} desired-error {{initializing multiple members of union}}
+              b('y') {} // expected-error{{initializing multiple members of union}}
 };
 
 // PR10954: variant members do not acquire an implicit initializer.
diff --git a/clang/test/SemaCXX/class-base-member-init.cpp b/clang/test/SemaCXX/class-base-member-init.cpp
index ca9f045..9d030c2 100644
--- a/clang/test/SemaCXX/class-base-member-init.cpp
+++ b/clang/test/SemaCXX/class-base-member-init.cpp
@@ -69,8 +69,8 @@
     };
 
     A(char _) : a(0), b(0) {}
-    A(short _) : a(0), c(0) {} // expected-error {{initializing multiple members of anonymous union}} expected-note {{previous initialization is here}}
-    A(int _) : d(0), e(0) {} // expected-error {{initializing multiple members of anonymous union}} expected-note {{previous initialization is here}}
-    A(long _) : a(0), d(0) {} // expected-error {{initializing multiple members of anonymous union}} expected-note {{previous initialization is here}}
+    A(short _) : a(0), c(0) {} // expected-error {{initializing multiple members of union}} expected-note {{previous initialization is here}}
+    A(int _) : d(0), e(0) {} // expected-error {{initializing multiple members of union}} expected-note {{previous initialization is here}}
+    A(long _) : a(0), d(0) {} // expected-error {{initializing multiple members of union}} expected-note {{previous initialization is here}}
   };
 }
diff --git a/clang/test/SemaCXX/constructor-initializer.cpp b/clang/test/SemaCXX/constructor-initializer.cpp
index e439a76..c6bcad2 100644
--- a/clang/test/SemaCXX/constructor-initializer.cpp
+++ b/clang/test/SemaCXX/constructor-initializer.cpp
@@ -74,7 +74,7 @@
   union { int b; double d; };
 
   U() :  a(1), // expected-note {{previous initialization is here}}
-         p(0), // expected-error {{initializing multiple members of anonymous union}}
+         p(0), // expected-error {{initializing multiple members of union}}
          d(1.0)  {}
 };