[clang] [clang][CodeGen] Fix crash on if/switch init-statement ending in noreturn (PR #201047)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 2 00:38:59 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: lijinpei-amd
<details>
<summary>Changes</summary>
EmitStmt may `ClearInsertionPoint()` to mark dead code, EmitDecl is not prepared to handle it. Fix by `EnsureInsertPoint()` in transition from EmitStmt to EmitDecl. If/Switch body may contain a label which makes them not dead.
Fixes #<!-- -->115514.
---
Full diff: https://github.com/llvm/llvm-project/pull/201047.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGStmt.cpp (+12-2)
- (added) clang/test/CodeGenCXX/noreturn-init-stmt.cpp (+32)
``````````diff
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;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/201047
More information about the cfe-commits
mailing list