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 <shashishankar1...@gmail.com>
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 <shashishankar1...@gmail.com>
---
 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<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,
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp 
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
new file mode 100644
index 0000000000000..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 <shashishankar1...@gmail.com>
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;
 
-  // --- 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);
-  // Attach only the valid bases so downstream never ICEs
-  AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl),
-                       llvm::MutableArrayRef<CXXBaseSpecifier *>(
-                       ValidBases.data(), ValidBases.size()));
+  AttachBaseSpecifiers(cast<CXXRecordDecl>(ClassDecl), Bases);
 }
 
 bool Sema::IsDerivedFrom(SourceLocation Loc, CXXRecordDecl *Derived,

>From 2ba0f19d4c15b5de0e000762276801bca985ef4b Mon Sep 17 00:00:00 2001
From: Shashi Shankar <shashishankar1...@gmail.com>
Date: Wed, 9 Jul 2025 08:56:43 +0200
Subject: [PATCH 3/3] Apply suggestions from code review

Adding review comment

Co-authored-by: Corentin Jabot <corentinja...@gmail.com>
---
 clang/docs/ReleaseNotes.rst                     | 2 +-
 clang/lib/Parse/ParseDeclCXX.cpp                | 3 ---
 clang/test/SemaCXX/invalid-base-inheritance.cpp | 3 +++
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6913f298ce5e..09ab62df9d09f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -923,7 +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)
+- Fix a crash in the presence of invalid base classes. (#GH147186)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index e7bb5b48058d0..59e6e0af4b5b0 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2252,9 +2252,6 @@ void Parser::ParseBaseClause(Decl *ClassDecl) {
   while (true) {
     // Parse a base-specifier.
     BaseResult Result = ParseBaseSpecifier(ClassDecl);
-    // 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.
diff --git a/clang/test/SemaCXX/invalid-base-inheritance.cpp 
b/clang/test/SemaCXX/invalid-base-inheritance.cpp
index 21b3eb57ef914..1cfb8fe6f0246 100644
--- a/clang/test/SemaCXX/invalid-base-inheritance.cpp
+++ b/clang/test/SemaCXX/invalid-base-inheritance.cpp
@@ -1,6 +1,8 @@
 // Tests that invalid base-specifiers no longer crash the compiler.
 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
 
+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}}
@@ -11,3 +13,4 @@ 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
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to