Include named unions in union member init checking

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@144883 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Blaikie 2011-11-17 06:01:57 +00:00
Родитель e6226ae490
Коммит 6fe2965ce7
6 изменённых файлов: 44 добавлений и 15 удалений

Просмотреть файл

@ -4346,7 +4346,7 @@ def err_only_constructors_take_base_inits : Error<
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">;

Просмотреть файл

@ -3065,11 +3065,9 @@ bool CheckRedundantUnionInit(Sema &S,
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 @@ bool CheckRedundantUnionInit(Sema &S,
En.first = Child;
En.second = Init;
}
if (!Parent->isAnonymousStructOrUnion())
return false;
}
Child = Parent;
Parent = cast<RecordDecl>(Parent->getDeclContext());
} while (Parent->isAnonymousStructOrUnion());
}
return false;
}

Просмотреть файл

@ -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 constructors
// 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}}
}
};

Просмотреть файл

@ -16,14 +16,17 @@ struct S {
} 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.

Просмотреть файл

@ -69,8 +69,8 @@ namespace test4 {
};
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}}
};
}

Просмотреть файл

@ -74,7 +74,7 @@ class U {
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) {}
};