Skip to content

Commit 5729311

Browse files
committed
[clang] Fix a crash issue that caused by handling of of fields with initializers in nested anonymous unions
Signed-off-by: yronglin <[email protected]>
1 parent 92ad039 commit 5729311

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,19 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
868868

869869
if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
870870
const RecordDecl *RDecl = RType->getDecl();
871-
if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) {
871+
if (RDecl->isUnion() && ILE->getInitializedFieldInUnion())
872872
FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(), Entity, ILE,
873873
RequiresSecondPass, FillWithNoInit);
874+
else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) &&
875+
cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) {
876+
for (auto *Field : RDecl->fields()) {
877+
if (Field->hasInClassInitializer()) {
878+
FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass,
879+
FillWithNoInit);
880+
break;
881+
}
882+
}
874883
} else {
875-
assert((!RDecl->isUnion() || !isa<CXXRecordDecl>(RDecl) ||
876-
!cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) &&
877-
"We should have computed initialized fields already");
878884
// The fields beyond ILE->getNumInits() are default initialized, so in
879885
// order to leave them uninitialized, the ILE is expanded and the extra
880886
// fields are then filled with NoInitExpr.
@@ -2296,7 +2302,6 @@ void InitListChecker::CheckStructUnionTypes(
22962302
return;
22972303
}
22982304
}
2299-
llvm_unreachable("Couldn't find in-class initializer");
23002305
}
23012306

23022307
// Value-initialize the first member of the union that isn't an unnamed

clang/test/SemaCXX/cxx1y-initializer-aggregates.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,14 @@ namespace nested_union {
115115
// of Test3, or we should exclude f(Test3) as a candidate.
116116
static_assert(f({1}) == 2, ""); // expected-error {{call to 'f' is ambiguous}}
117117
}
118+
119+
// Fix crash issue https://github.com/llvm/llvm-project/issues/112560.
120+
// Make sure clang compiles the following code without crashing:
121+
namespace GH112560 {
122+
union U {
123+
int f = ; // expected-error {{expected expression}}
124+
};
125+
void foo() {
126+
U g{};
127+
}
128+
} // namespace GH112560

0 commit comments

Comments
 (0)