https://github.com/lijinpei-amd updated 
https://github.com/llvm/llvm-project/pull/201047

>From ebe8ff2fbc2ee27c2bd0abb8cfe37f7d22b8a6f3 Mon Sep 17 00:00:00 2001
From: Li Jinpei <[email protected]>
Date: Tue, 2 Jun 2026 15:15:08 +0800
Subject: [PATCH] [clang][CodeGen] Fix crash on if/switch init-statement ending
 in noreturn

EmitStmt may `ClearInsertionPoint()` to mark dead code; the condition emitted
next assumes a valid insertion point. Restore one with `EnsureInsertPoint()`
after the init statement. The if/switch body may still be reachable through a
label, so it must be emitted rather than skipped.

Fixes #115514.
---
 clang/lib/CodeGen/CGStmt.cpp                 | 14 +++++++--
 clang/test/CodeGenCXX/noreturn-init-stmt.cpp | 32 ++++++++++++++++++++
 2 files changed, 44 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/noreturn-init-stmt.cpp

diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 4f847a5879dc6..1b108d9ceea7b 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -885,9 +885,14 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
   LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
   ApplyDebugLocation DL(*this, S.getCond());
 
-  if (S.getInit())
+  if (S.getInit()) {
     EmitStmt(S.getInit());
 
+    // The init statement may have cleared the insertion point (e.g. it ended 
in
+    // a 'noreturn' call); the condition emitted below needs a valid one.
+    EnsureInsertPoint();
+  }
+
   if (S.getConditionVariable())
     EmitDecl(*S.getConditionVariable());
 
@@ -2374,9 +2379,14 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt 
&S) {
 
   RunCleanupsScope ConditionScope(*this);
 
-  if (S.getInit())
+  if (S.getInit()) {
     EmitStmt(S.getInit());
 
+    // The init statement may have cleared the insertion point (e.g. it ended 
in
+    // a 'noreturn' call); the condition emitted below needs a valid one.
+    EnsureInsertPoint();
+  }
+
   if (S.getConditionVariable())
     EmitDecl(*S.getConditionVariable());
   llvm::Value *CondV = EmitScalarExpr(S.getCond());
diff --git a/clang/test/CodeGenCXX/noreturn-init-stmt.cpp 
b/clang/test/CodeGenCXX/noreturn-init-stmt.cpp
new file mode 100644
index 0000000000000..8ea66f0a2344b
--- /dev/null
+++ b/clang/test/CodeGenCXX/noreturn-init-stmt.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s 
-o - | FileCheck %s
+
+struct T {
+  ~T();
+};
+[[noreturn]] void die();
+bool check(const T &);
+int pick(const T &);
+
+// CHECK-LABEL: define {{.*}} @_Z4testb(
+int test(bool cond) {
+  if (cond)
+    goto label;
+  if (die(); check(T())) {
+  label:
+    return 1;
+  }
+  return 0;
+}
+
+// CHECK-LABEL: define {{.*}} @_Z11test_switchb(
+int test_switch(bool cond) {
+  if (cond)
+    goto label;
+  switch (die(); pick(T())) {
+  case 1:
+    return 1;
+  label:
+    return 2;
+  }
+  return 0;
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to