[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 05211dac479bf25aaf6d8ecc3a53871a51f7ffdd Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH 1/3] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 13 +
2 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..21b3eb57ef914
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -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}}
+};
>From 53984191e3e45d3964f6ff71d70df7c030659369 Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Wed, 9 Jul 2025 00:10:51 +0200
Subject: [PATCH 2/3] ParseDeclCXX: reject unusable base classes via
isUsableType
In Parser::ParseBaseSpecifier, replace the previous `isInvalid` check
with `!BaseTy->isUsableType()` so that we early-reject any non-instantiable
class/struct/union as a base. Add a one-line entry to
clang/docs/ReleaseNotes.rst. Fixes #147213.
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Parse/ParseDeclCXX.cpp | 5 -
clang/lib/Sema/SemaDeclCXX.cpp | 17 +
3 files changed, 6 insertions(+), 17 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a919f3a71c9cf..141391a164814 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -920,6 +920,7 @@ Bug Fixes to C++ Support
- Correctly handle allocations in the condition of a ``if
constexpr``.(#GH120197) (#GH134820)
- Fixed a crash when handling invalid member using-declaration in C++20+ mode.
(#GH63254)
- Fix a crash when trying to instantiate an ambiguous specialization.
(#GH51866)
+- Switch `ParseBaseClause` to use `BaseResult::isUsable()` instead of
`isInvalid()`, fixing dropped or mis-parsed base specifiers in C++ classes.
(#GH147186)
Bug Fixes to AST Handling
^
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index eae8281964692..e7bb5b48058d0 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2252,7 +2252,10 @@ void Parser::ParseBaseClause(Decl *ClassDecl) {
while (true) {
// Parse a base-specifier.
BaseResult Result = ParseBaseSpecifier(ClassDecl);
-if (Result.isInvalid()) {
+// Skip any base-specifier we couldn’t actually build into a usable
+// CXXBaseSpecifier (covers both syntactic invalidity and
+// other un-usable cases).
+if (!Result.isUsable()) {
// Skip the rest of this base specifier, up until the comma or
// opening brace.
SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 231610241d8ee..f0247f865ba40 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,23 +3031,8 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
-
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
@@ -923,6 +923,7 @@ Bug Fixes to C++ Support - Improved handling of variables with ``consteval`` constructors, to consistently treat the initializer as manifestly constant-evaluated. (#GH135281) +- Switch `ParseBaseClause` to use `BaseResult::isUsable()` instead of `isInvalid()`, fixing dropped or mis-parsed base specifiers in C++ classes. (#GH147186) cor3ntin wrote: ```suggestion - Fix a crash in the presence of invalid base classes. (#GH147186) ``` Release notes are meant to be user-facing https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
@@ -2252,7 +2252,10 @@ void Parser::ParseBaseClause(Decl *ClassDecl) {
while (true) {
// Parse a base-specifier.
BaseResult Result = ParseBaseSpecifier(ClassDecl);
-if (Result.isInvalid()) {
+// Skip any base-specifier we couldn’t actually build into a usable
+// CXXBaseSpecifier (covers both syntactic invalidity and
+// other un-usable cases).
cor3ntin wrote:
```suggestion
```
This comment isn't very useful
https://github.com/llvm/llvm-project/pull/147213
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
@@ -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}}
+};
cor3ntin wrote:
```suggestion
namespace GH147186 {
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}}
};
}
```
https://github.com/llvm/llvm-project/pull/147213
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/cor3ntin approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 05211dac479bf25aaf6d8ecc3a53871a51f7ffdd Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 13 +
2 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..21b3eb57ef914
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -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}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/cor3ntin commented: Thanks for working on this Did you try to use `!isUsable` instead of IsInvalid in `ParseBaseSpecifier` ? I think that might be a simpler fix. --- This change needs a release note. Please add an entry to `clang/docs/ReleaseNotes.rst` in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks! https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 05211dac479bf25aaf6d8ecc3a53871a51f7ffdd Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 13 +
2 files changed, 29 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..21b3eb57ef914
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -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}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 8e0ec03d5d587df2d68775bc7bb7f3d8331db7b8 Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 17 +
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..e37b77bcd3fea
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -0,0 +1,17 @@
+// Tests that invalid base-specifiers no longer crash the compiler.
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class X; // forward declaration
+
+class A : X { // incomplete base → error, but no ICE
+// expected-error@-1 {{base class has incomplete type}}
+};
+
+class Y : int { // base not a class → error, but no ICE
+ // expected-error@-1 {{expected class name}}
+};
+
+class Z : X*, virtual int { // mixed invalid specifiers
+// expected-error@-2 {{base class has incomplete type}}
+// expected-error@-1 {{expected class name}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 6184a8edfbe7173f0b23ccebd4da80d8fb5e1485 Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 17 +
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..e37b77bcd3fea
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -0,0 +1,17 @@
+// Tests that invalid base-specifiers no longer crash the compiler.
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class X; // forward declaration
+
+class A : X { // incomplete base → error, but no ICE
+// expected-error@-1 {{base class has incomplete type}}
+};
+
+class Y : int { // base not a class → error, but no ICE
+ // expected-error@-1 {{expected class name}}
+};
+
+class Z : X*, virtual int { // mixed invalid specifiers
+// expected-error@-2 {{base class has incomplete type}}
+// expected-error@-1 {{expected class name}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 updated
https://github.com/llvm/llvm-project/pull/147213
>From 2b109b30c7db2fe48b562a38ac2543c56cb1beca Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Fixes #147186
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 17 +
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..e37b77bcd3fea
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -0,0 +1,17 @@
+// Tests that invalid base-specifiers no longer crash the compiler.
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class X; // forward declaration
+
+class A : X { // incomplete base → error, but no ICE
+// expected-error@-1 {{base class has incomplete type}}
+};
+
+class Y : int { // base not a class → error, but no ICE
+ // expected-error@-1 {{expected class name}}
+};
+
+class Z : X*, virtual int { // mixed invalid specifiers
+// expected-error@-2 {{base class has incomplete type}}
+// expected-error@-1 {{expected class name}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Shashi Shankar (shashi1687)
Changes
### Summary
Briefly what this PR does, in one or two sentences.
> Filters out invalid (null) base-specifiers in `ActOnBaseSpecifiers()`,
preventing ICEs on incomplete or non-class bases.
### Motivation
Why this change is needed.
> When you inherit from a forward‐declared class or non-class type, Sema
used to emit an error _and_ still hand the bad pointer to later code, causing
an internal compiler error. We now drop those invalid entries immediately.
### Root Cause Analysis
A very short bulleted “root cause.”
- `ActOnBaseSpecifier` returns `nullptr` for invalid bases.
- `ActOnBaseSpecifiers` passed **all** pointers (including null) to
`AttachBaseSpecifiers`.
- Later layout or special‐member lookup on null caused the ICE.
### Changes in this PR
List the key files and rough diff summary.
- **clang/lib/Sema/SemaDeclCXX.cpp**
- In `ActOnBaseSpecifiers()`, gather `Bases` into a `SmallVector`, skipping
null entries.
- **clang/test/SemaCXX/invalid-base-inheritance.cpp**
- New regression test checking for the proper diagnostics and that no ICE
occurs.
### Test Plan
How you verified the change.
1. **Local build/test**
```bash
mkdir build && cd build
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS="clang" \
-DCMAKE_BUILD_TYPE=Release
ninja clang
ninja check-clang
---
Full diff: https://github.com/llvm/llvm-project/pull/147213.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+16-1)
- (added) clang/test/SemaCXX/invalid-base-inheritance.cpp (+17)
``diff
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..e37b77bcd3fea
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -0,0 +1,17 @@
+// Tests that invalid base-specifiers no longer crash the compiler.
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class X; // forward declaration
+
+class A : X { // incomplete base → error, but no ICE
+// expected-error@-1 {{base class has incomplete type}}
+};
+
+class Y : int { // base not a class → error, but no ICE
+ // expected-error@-1 {{expected class name}}
+};
+
+class Z : X*, virtual int { // mixed invalid specifiers
+// expected-error@-2 {{base class has incomplete type}}
+// expected-error@-1 {{expected class name}}
+};
``
https://github.com/llvm/llvm-project/pull/147213
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
github-actions[bot] wrote: Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using `@` followed by their GitHub username. If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the [LLVM GitHub User Guide](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). https://github.com/llvm/llvm-project/pull/147213 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Sema: filter out invalid base-specifiers before attaching (PR #147213)
https://github.com/shashi1687 created
https://github.com/llvm/llvm-project/pull/147213
### Summary
Briefly what this PR does, in one or two sentences.
> Filters out invalid (null) base-specifiers in `ActOnBaseSpecifiers()`,
> preventing ICEs on incomplete or non-class bases.
### Motivation
Why this change is needed.
> When you inherit from a forward‐declared class or non-class type, Sema used
> to emit an error _and_ still hand the bad pointer to later code, causing an
> internal compiler error. We now drop those invalid entries immediately.
### Root Cause Analysis
A very short bulleted “root cause.”
- `ActOnBaseSpecifier` returns `nullptr` for invalid bases.
- `ActOnBaseSpecifiers` passed **all** pointers (including null) to
`AttachBaseSpecifiers`.
- Later layout or special‐member lookup on null caused the ICE.
### Changes in this PR
List the key files and rough diff summary.
- **clang/lib/Sema/SemaDeclCXX.cpp**
- In `ActOnBaseSpecifiers()`, gather `Bases` into a `SmallVector`, skipping
null entries.
- **clang/test/SemaCXX/invalid-base-inheritance.cpp**
- New regression test checking for the proper diagnostics and that no ICE
occurs.
### Test Plan
How you verified the change.
1. **Local build/test**
```bash
mkdir build && cd build
cmake -G Ninja ../llvm \
-DLLVM_ENABLE_PROJECTS="clang" \
-DCMAKE_BUILD_TYPE=Release
ninja clang
ninja check-clang
>From 36559cab1bce1bbd8b501521c6c252d6d3fa00ce Mon Sep 17 00:00:00 2001
From: Shashi Shankar
Date: Sun, 6 Jul 2025 22:25:48 +0200
Subject: [PATCH] Sema: filter out invalid base-specifiers before attaching
ActOnBaseSpecifiers now skips nullptr entries returned for invalid bases,
avoiding ICE on invalid inheritance. Added regression test in
SemaCXX/invalid-inheritance.cpp.
Signed-off-by: Shashi Shankar
---
clang/lib/Sema/SemaDeclCXX.cpp | 17 -
clang/test/SemaCXX/invalid-base-inheritance.cpp | 17 +
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/invalid-base-inheritance.cpp
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f0247f865ba40..231610241d8ee 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3031,8 +3031,23 @@ void Sema::ActOnBaseSpecifiers(Decl *ClassDecl,
if (!ClassDecl || Bases.empty())
return;
+ // --- drop any bases already diagnosed invalid ---
+ SmallVector 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(ClassDecl), Bases);
+ // Attach only the valid bases so downstream never ICEs
+ AttachBaseSpecifiers(cast(ClassDecl),
+ llvm::MutableArrayRef(
+ ValidBases.data(), ValidBases.size()));
}
bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0..e37b77bcd3fea
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -0,0 +1,17 @@
+// Tests that invalid base-specifiers no longer crash the compiler.
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+class X; // forward declaration
+
+class A : X { // incomplete base → error, but no ICE
+// expected-error@-1 {{base class has incomplete type}}
+};
+
+class Y : int { // base not a class → error, but no ICE
+ // expected-error@-1 {{expected class name}}
+};
+
+class Z : X*, virtual int { // mixed invalid specifiers
+// expected-error@-2 {{base class has incomplete type}}
+// expected-error@-1 {{expected class name}}
+};
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
