[Sema] Fix crash on invalid code with parenthesized aggregate initialization (#76232)

Fixes #76228.
Use the same logic as braced init lists, also adds a test that puts
incomplete types in various positions to check for regressions in the
future.
This commit is contained in:
Ilya Biryukov
2023-12-22 13:11:27 +01:00
committed by GitHub
parent 95b423e44f
commit 86dc6e15f2
2 changed files with 36 additions and 0 deletions

View File

@@ -5512,6 +5512,14 @@ static void TryOrBuildParenListInitialization(
} else if (auto *RT = Entity.getType()->getAs<RecordType>()) {
bool IsUnion = RT->isUnionType();
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
if (RD->isInvalidDecl()) {
// Exit early to avoid confusion when processing members.
// We do the same for braced list initialization in
// `CheckStructUnionTypes`.
Sequence.SetFailed(
clang::InitializationSequence::FK_ParenthesizedListInitFailed);
return;
}
if (!IsUnion) {
for (const CXXBaseSpecifier &Base : RD->bases()) {

View File

@@ -0,0 +1,28 @@
// RUN: %clang_cc1 -std=c++20 -verify %s
// Check we don't crash on incomplete members and bases when handling parenthesized initialization.
class incomplete; // expected-note@-0 3 {{forward declaration of 'incomplete'}}
struct foo {
int a;
incomplete b;
// expected-error@-1 {{incomplete type}}
};
foo a1(0);
struct one_int {
int a;
};
struct bar : one_int, incomplete {};
// expected-error@-1 {{incomplete type}}
bar a2(0);
incomplete a3[3](1,2,3);
// expected-error@-1 {{incomplete type}}
struct qux : foo {
};
qux a4(0);
struct fred {
foo a[3];
};
fred a5(0);