From 6fe2965ce722826ae7c3af85271f900286ef20c3 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 17 Nov 2011 06:01:57 +0000 Subject: [PATCH] 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 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 +- lib/Sema/SemaDeclCXX.cpp | 10 +++---- .../class.base/class.base.init/p5-0x.cpp | 26 +++++++++++++++++++ .../class.init/class.base.init/p8-0x.cpp | 13 ++++++---- test/SemaCXX/class-base-member-init.cpp | 6 ++--- test/SemaCXX/constructor-initializer.cpp | 2 +- 6 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 test/CXX/class/class.base/class.base.init/p5-0x.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 82a5ba29f8..6c5e79a901 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 8aeb8fb33f..52eb9b0d3f 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -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(Parent->getDeclContext()); - } while (Parent->isAnonymousStructOrUnion()); + } return false; } diff --git a/test/CXX/class/class.base/class.base.init/p5-0x.cpp b/test/CXX/class/class.base/class.base.init/p5-0x.cpp new file mode 100644 index 0000000000..e9aa6da7dc --- /dev/null +++ b/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/test/CXX/special/class.init/class.base.init/p8-0x.cpp b/test/CXX/special/class.init/class.base.init/p8-0x.cpp index 3e26e4992d..a108533bed 100644 --- a/test/CXX/special/class.init/class.base.init/p8-0x.cpp +++ b/test/CXX/special/class.init/class.base.init/p8-0x.cpp @@ -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. diff --git a/test/SemaCXX/class-base-member-init.cpp b/test/SemaCXX/class-base-member-init.cpp index ca9f045416..9d030c28de 100644 --- a/test/SemaCXX/class-base-member-init.cpp +++ b/test/SemaCXX/class-base-member-init.cpp @@ -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}} }; } diff --git a/test/SemaCXX/constructor-initializer.cpp b/test/SemaCXX/constructor-initializer.cpp index e439a76c17..c6bcad2c09 100644 --- a/test/SemaCXX/constructor-initializer.cpp +++ b/test/SemaCXX/constructor-initializer.cpp @@ -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) {} };