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 <shashishankar1...@gmail.com>
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 <shashishankar1...@gmail.com>
---
 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<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..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
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to