[clang] [Clang] Fix crash on `void{}` (PR #147514)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 8 06:33:52 PDT 2025


https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/147514

>From 4b536efe500f3b9099d1cf1a1a8775633e193249 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 8 Jul 2025 14:34:16 +0200
Subject: [PATCH 1/2] [Clang] Fix crash on `void{}`

Caused by an incorrect assertion.

Fixes #116440
---
 clang/lib/CodeGen/CGExprScalar.cpp                  | 4 +++-
 clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp | 9 +++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index fc441dd92d1ee..44931d0481e26 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2114,8 +2114,10 @@ static int getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty) {
 Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
   bool Ignore = TestAndClearIgnoreResultAssign();
   (void)Ignore;
-  assert (Ignore == false && "init list ignored");
   unsigned NumInitElements = E->getNumInits();
+  assert(Ignore == false ||
+         (NumInitElements == 0 && E->getType()->isVoidType()) &&
+             "init list ignored");
 
   // HLSL initialization lists in the AST are an expansion which can contain
   // side-effecting expressions wrapped in opaque value expressions. To properly
diff --git a/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp b/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
index 2f6a6820a7589..478ad40359727 100644
--- a/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
+++ b/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
@@ -5,3 +5,12 @@ void f()
   // CHECK: store i32 0
   int i{};
 }
+
+
+namespace GH116440 {
+void f() {
+  void{};
+  void();
+}
+
+}

>From 6d15e60a669124899b29742d405c11779cd16ad3 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 8 Jul 2025 15:33:38 +0200
Subject: [PATCH 2/2] improve test

---
 clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp b/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
index 478ad40359727..4629e96724092 100644
--- a/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
+++ b/clang/test/CodeGenCXX/cxx0x-initializer-scalars.cpp
@@ -13,4 +13,7 @@ void f() {
   void();
 }
 
+// CHECK: define{{.*}} void @_ZN8GH1164401fEv()
+// CHECK-NEXT: entry
+// CHECK-NEXT: ret void
 }



More information about the cfe-commits mailing list