Skip to content

Sema: filter out invalid base-specifiers before attaching #147213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;

// --- drop any bases already diagnosed invalid ---
SmallVector<CXXBaseSpecifier *, 4> ValidBases;
ValidBases.reserve(Bases.size());
for (auto *BS : Bases)
if (BS)
ValidBases.push_back(BS);
if (ValidBases.empty())
return;

if (ValidBases.empty())
return; // nothing valid to attach

AdjustDeclIfTemplate(ClassDecl);
AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases);
// Attach only the valid bases so downstream never ICEs
AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl),
llvm::MutableArrayRef<CXXBaseSpecifier *>(
ValidBases.data(), ValidBases.size()));
}

bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaCXX/invalid-base-inheritance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Tests that invalid base-specifiers no longer crash the compiler.
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s

class X; // expected-note {{forward declaration of 'X'}} expected-note {{forward declaration of 'X'}}

class A : X { // expected-error {{base class has incomplete type}}
};

class Y : int { // expected-error {{expected class name}}
};

class Z : X*, virtual int { // expected-error {{base class has incomplete type}} expected-error {{expected class name}}
};