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">;
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) {
En.first = Child;
En.second = Init;
}
+ if (!Parent->isAnonymousStructOrUnion())
+ return false;
}
Child = Parent;
Parent = cast<RecordDecl>(Parent->getDeclContext());
- } while (Parent->isAnonymousStructOrUnion());
+ }
return false;
}
--- /dev/null
+// 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}}
+ }
+};
} 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.
};
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}}
};
}
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) {}
};