[clang] [WinEH] Fix crash, object unwinding in the except block (PR #172287)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 20 06:50:42 PST 2026
https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/172287
>From f16a7e15a2e54d2bb0090a06d99181176abb65e5 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Tue, 20 Jan 2026 22:50:18 +0800
Subject: [PATCH] Test
---
clang/lib/CodeGen/CGDecl.cpp | 8 ++++++-
clang/test/CodeGenCXX/exceptions-seh.cpp | 29 ++++++++++++++++++++++++
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 8b1cd83af2396..182570d9586a7 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2212,8 +2212,14 @@ void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
const VarDecl &D = *emission.Variable;
// Check the type for a cleanup.
- if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext()))
+ if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext())) {
+ // Check if we're in a SEH block and this is a C++ destructor
+ if (dtorKind == QualType::DK_cxx_destructor)
+ if (currentFunctionUsesSEHTry())
+ getContext().getDiagnostics().Report(D.getLocation(),
+ diag::err_seh_expected_handler);
emitAutoVarTypeCleanup(emission, dtorKind);
+ }
// In GC mode, honor objc_precise_lifetime.
if (getLangOpts().getGC() != LangOptions::NonGC &&
diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index bb374dd1f5bd5..6a48e8384bb34 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -4,6 +4,12 @@
// RUN: %clang_cc1 -std=c++11 -fblocks -fms-extensions %s -triple=x86_64-windows-msvc -emit-llvm \
// 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 -verify %s -DERR1
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm -verify %s -DERR2
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm -verify %s -DERR3
extern "C" unsigned long _exception_code();
extern "C" void might_throw();
@@ -175,3 +181,26 @@ void use_inline() {
// CHECK: attributes #[[NOINLINE]] = { {{.*noinline.*}} }
void seh_in_noexcept() noexcept { __try {} __finally {} }
+
+#if defined(ERR1)
+void seh_unwinding() {
+ __try {
+ HasCleanup x; // expected-error{{expected '__except' or '__finally' block}}
+ } __except (1) {
+ }
+}
+#elif defined(ERR2)
+void seh_unwinding() {
+ __try {
+ } __except (1) {
+ HasCleanup x; // expected-error{{expected '__except' or '__finally' block}}
+ }
+}
+#elif defined(ERR3)
+void seh_unwinding() {
+ HasCleanup x; // expected-error{{expected '__except' or '__finally' block}}
+ __try {
+ } __except (1) {
+ }
+}
+#endif
More information about the cfe-commits
mailing list