[clang] [WinEH] Diagnosis seh unwinding when skip emitting except block (PR #187718)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 20 08:13:45 PDT 2026
https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/187718
>From 6f1f514083d774895c959653ee9a98b903606dc6 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Fri, 20 Mar 2026 23:08:40 +0800
Subject: [PATCH 1/2] [WinEH] Diagnosis seh unwinding when skip emitting except
block
---
clang/lib/CodeGen/CGDecl.cpp | 2 --
clang/lib/CodeGen/CGException.cpp | 11 ++++++++
clang/test/CodeGenCXX/exceptions-seh.cpp | 34 ++++++++++++++++++------
3 files changed, 37 insertions(+), 10 deletions(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 8b5ffde1b73f3..cbfbc751ddf4f 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2229,8 +2229,6 @@ void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
// Check the type for a cleanup.
if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext())) {
// Check if we're in a SEH block with /EH, prevent it
- // TODO: /EHs* differs from /EHa, the former may not be executed to this
- // point.
if (getLangOpts().CXXExceptions && currentFunctionUsesSEHTry())
getContext().getDiagnostics().Report(D.getLocation(),
diag::err_seh_object_unwinding);
diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp
index 7559727721496..aaecf23b0be35 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -2246,6 +2246,17 @@ void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {
// TODO: Model unwind edges from instructions, either with iload / istore or
// a try body function.
if (!CatchScope.hasEHBranches()) {
+ // Even though we skip emitting the __except body, diagnose variables
+ // with non-trivial destructors that would normally be caught by
+ // EmitAutoVarCleanups.
+ if (getLangOpts().CXXExceptions && currentFunctionUsesSEHTry())
+ for (const Stmt *S : Except->getBlock()->body())
+ if (const auto *DS = dyn_cast<DeclStmt>(S))
+ for (const Decl *D : DS->decls())
+ if (const auto *VD = dyn_cast<VarDecl>(D))
+ if (VD->needsDestruction(getContext()))
+ getContext().getDiagnostics().Report(
+ VD->getLocation(), diag::err_seh_object_unwinding);
CatchScope.clearHandlerBlocks();
EHStack.popCatch();
SEHCodeSlotStack.pop_back();
diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index 270b249f700fa..730f2df58cc7c 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -5,13 +5,17 @@
// RUN: -o - -mconstructor-aliases -O1 -disable-llvm-passes | \
// RUN: FileCheck %s --check-prefix=CHECK --check-prefix=NOCXX
// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
-// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR1
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_ASYNC1
// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
-// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR2
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_ASYNC2
// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
-// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR3
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_ASYNC3
// RUN: %clang_cc1 -triple x86_64-windows -fcxx-exceptions -fexceptions \
-// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR4
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_SYNC1
+// RUN: %clang_cc1 -triple x86_64-windows -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_SYNC2
+// RUN: %clang_cc1 -triple x86_64-windows -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR_SYNC3
// RUN: %clang_cc1 -triple x86_64-windows \
// RUN: -fms-extensions -x c++ -emit-llvm-only -verify %s -DNOERR
@@ -186,31 +190,45 @@ void use_inline() {
void seh_in_noexcept() noexcept { __try {} __finally {} }
-#if defined(ERR1)
+#if defined(ERR_ASYNC1)
void seh_unwinding() {
__try {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
} __except (1) {
}
}
-#elif defined(ERR2)
+#elif defined(ERR_ASYNC2)
void seh_unwinding() {
__try {
} __except (1) {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
}
}
-#elif defined(ERR3)
+#elif defined(ERR_ASYNC3)
void seh_unwinding() {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
__try {
} __except (1) {
}
}
-#elif defined(ERR4)
+#elif defined(ERR_SYNC1)
+void seh_unwinding() {
+ __try {
+ HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ } __except (1) {
+ }
+}
+#elif defined(ERR_SYNC2)
void seh_unwinding() {
__try {
+ } __except (1) {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ }
+}
+#elif defined(ERR_SYNC3)
+void seh_unwinding() {
+ HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ __try {
} __except (1) {
}
}
>From 251814180f833a45628def2de48018a3767f9eb1 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Fri, 20 Mar 2026 23:13:30 +0800
Subject: [PATCH 2/2] Tidy
---
clang/test/CodeGenCXX/exceptions-seh.cpp | 27 +++---------------------
1 file changed, 3 insertions(+), 24 deletions(-)
diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index 730f2df58cc7c..c4b191377b844 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -190,42 +190,21 @@ void use_inline() {
void seh_in_noexcept() noexcept { __try {} __finally {} }
-#if defined(ERR_ASYNC1)
+#if defined(ERR_ASYNC1) || defined(ERR_SYNC1)
void seh_unwinding() {
__try {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
} __except (1) {
}
}
-#elif defined(ERR_ASYNC2)
+#elif defined(ERR_ASYNC2) || defined(ERR_SYNC2)
void seh_unwinding() {
__try {
} __except (1) {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
}
}
-#elif defined(ERR_ASYNC3)
-void seh_unwinding() {
- HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
- __try {
- } __except (1) {
- }
-}
-#elif defined(ERR_SYNC1)
-void seh_unwinding() {
- __try {
- HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
- } __except (1) {
- }
-}
-#elif defined(ERR_SYNC2)
-void seh_unwinding() {
- __try {
- } __except (1) {
- HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
- }
-}
-#elif defined(ERR_SYNC3)
+#elif defined(ERR_ASYNC3) || defined(ERR_SYNC3)
void seh_unwinding() {
HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
__try {
More information about the cfe-commits
mailing list