Skip to content

Commit 9234ac6

Browse files
committed
Instantiate destructors from initialized anonymous union fields
1 parent 3f64899 commit 9234ac6

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

clang/include/clang/Sema/Sema.h

+2
Original file line numberDiff line numberDiff line change
@@ -5432,6 +5432,8 @@ class Sema final : public SemaBase {
54325432
void MarkBaseAndMemberDestructorsReferenced(SourceLocation Loc,
54335433
CXXRecordDecl *Record);
54345434

5435+
void MarkFieldDestructorReferenced(SourceLocation Loc, FieldDecl *Field);
5436+
54355437
/// Mark destructors of virtual bases of this class referenced. In the Itanium
54365438
/// C++ ABI, this is done when emitting a destructor for any non-abstract
54375439
/// class. In the Microsoft C++ ABI, this is done any time a class's

clang/lib/Sema/SemaDeclCXX.cpp

+52-35
Original file line numberDiff line numberDiff line change
@@ -5451,10 +5451,23 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
54515451
NumInitializers * sizeof(CXXCtorInitializer*));
54525452
Constructor->setCtorInitializers(baseOrMemberInitializers);
54535453

5454+
SourceLocation Location = Constructor->getLocation();
5455+
5456+
for (CXXCtorInitializer *Initializer : Info.AllToInit) {
5457+
FieldDecl *Field = Initializer->getAnyMember();
5458+
if (!Field)
5459+
continue;
5460+
5461+
RecordDecl *FieldRecordDecl = Field->getParent();
5462+
if (!FieldRecordDecl->isUnion() ||
5463+
!FieldRecordDecl->isAnonymousStructOrUnion())
5464+
continue;
5465+
5466+
MarkFieldDestructorReferenced(Location, Field);
5467+
}
54545468
// Constructors implicitly reference the base and member
54555469
// destructors.
5456-
MarkBaseAndMemberDestructorsReferenced(Constructor->getLocation(),
5457-
Constructor->getParent());
5470+
MarkBaseAndMemberDestructorsReferenced(Location, Constructor->getParent());
54585471
}
54595472

54605473
return HadError;
@@ -5759,6 +5772,42 @@ void Sema::ActOnMemInitializers(Decl *ConstructorDecl,
57595772
DiagnoseUninitializedFields(*this, Constructor);
57605773
}
57615774

5775+
void Sema::MarkFieldDestructorReferenced(SourceLocation Location,
5776+
FieldDecl *Field) {
5777+
if (Field->isInvalidDecl())
5778+
return;
5779+
5780+
// Don't destroy incomplete or zero-length arrays.
5781+
if (isIncompleteOrZeroLengthArrayType(Context, Field->getType()))
5782+
return;
5783+
5784+
QualType FieldType = Context.getBaseElementType(Field->getType());
5785+
5786+
const RecordType *RT = FieldType->getAs<RecordType>();
5787+
if (!RT)
5788+
return;
5789+
5790+
CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
5791+
if (FieldClassDecl->isInvalidDecl())
5792+
return;
5793+
if (FieldClassDecl->hasIrrelevantDestructor())
5794+
return;
5795+
// The destructor for an implicit anonymous union member is never invoked.
5796+
if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
5797+
return;
5798+
5799+
CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl);
5800+
// Dtor might still be missing, e.g because it's invalid.
5801+
if (!Dtor)
5802+
return;
5803+
CheckDestructorAccess(Field->getLocation(), Dtor,
5804+
PDiag(diag::err_access_dtor_field)
5805+
<< Field->getDeclName() << FieldType);
5806+
5807+
MarkFunctionReferenced(Location, Dtor);
5808+
DiagnoseUseOfDecl(Dtor, Location);
5809+
}
5810+
57625811
void
57635812
Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location,
57645813
CXXRecordDecl *ClassDecl) {
@@ -5774,39 +5823,7 @@ Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location,
57745823

57755824
// Non-static data members.
57765825
for (auto *Field : ClassDecl->fields()) {
5777-
if (Field->isInvalidDecl())
5778-
continue;
5779-
5780-
// Don't destroy incomplete or zero-length arrays.
5781-
if (isIncompleteOrZeroLengthArrayType(Context, Field->getType()))
5782-
continue;
5783-
5784-
QualType FieldType = Context.getBaseElementType(Field->getType());
5785-
5786-
const RecordType* RT = FieldType->getAs<RecordType>();
5787-
if (!RT)
5788-
continue;
5789-
5790-
CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
5791-
if (FieldClassDecl->isInvalidDecl())
5792-
continue;
5793-
if (FieldClassDecl->hasIrrelevantDestructor())
5794-
continue;
5795-
// The destructor for an implicit anonymous union member is never invoked.
5796-
if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
5797-
continue;
5798-
5799-
CXXDestructorDecl *Dtor = LookupDestructor(FieldClassDecl);
5800-
// Dtor might still be missing, e.g because it's invalid.
5801-
if (!Dtor)
5802-
continue;
5803-
CheckDestructorAccess(Field->getLocation(), Dtor,
5804-
PDiag(diag::err_access_dtor_field)
5805-
<< Field->getDeclName()
5806-
<< FieldType);
5807-
5808-
MarkFunctionReferenced(Location, Dtor);
5809-
DiagnoseUseOfDecl(Dtor, Location);
5826+
MarkFieldDestructorReferenced(Location, Field);
58105827
}
58115828

58125829
// We only potentially invoke the destructors of potentially constructed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2+
3+
template <class T> struct VSX {
4+
~VSX() { static_assert(sizeof(T) != 4, ""); } // expected-error {{static assertion failed due to requirement 'sizeof(int) != 4':}} \
5+
// expected-note {{expression evaluates to '4 != 4'}}
6+
};
7+
struct VS {
8+
union {
9+
VSX<int> _Tail;
10+
};
11+
~VS() { }
12+
VS(short);
13+
};
14+
VS::VS(short) : _Tail() { } // expected-note {{in instantiation of member function 'VSX<int>::~VSX' requested here}}

0 commit comments

Comments
 (0)