[clang] [WinEH] Also check object unwinding for enabling CXXExceptions(/ehs*) (PR #180959)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 12 21:57:10 PST 2026


https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/180959

>From f77c9f28c55209bd589a7122de4dcc5bab113528 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Wed, 11 Feb 2026 23:38:58 +0800
Subject: [PATCH 1/2] [WinEH] Also check object unwinding for enabling
 CXXExceptions(/ehs*)

---
 clang/lib/CodeGen/CGDecl.cpp             | 6 ++++--
 clang/test/CodeGenCXX/exceptions-seh.cpp | 9 +++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index b470f48ccd911..81697e02e8f3f 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2228,8 +2228,10 @@ 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 EHa, prevent it
-    if (getLangOpts().EHAsynch && currentFunctionUsesSEHTry())
+    // 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);
     emitAutoVarTypeCleanup(emission, dtorKind);
diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index 0716ed7ee81d6..0afabd788146a 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -12,6 +12,8 @@
 // RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR3
 // RUN: %clang_cc1 -triple x86_64-windows -fcxx-exceptions -fexceptions \
 // RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR4
+// RUN: %clang_cc1 -triple x86_64-windows \
+// RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR5
 
 extern "C" unsigned long _exception_code();
 extern "C" void might_throw();
@@ -206,6 +208,13 @@ void seh_unwinding() {
   }
 }
 #elif defined(ERR4)
+void seh_unwinding() {
+  __try {
+    HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+  } __except (1) {
+  }
+}
+#elif defined(ERR5)
 void seh_unwinding() {
   __try {
     HasCleanup x; // expected-no-diagnostics

>From bc2f72cda3651e30d9442e8e04ef66cde3a20072 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Fri, 13 Feb 2026 13:57:02 +0800
Subject: [PATCH 2/2] Use NOERR

---
 clang/test/CodeGenCXX/exceptions-seh.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index 0afabd788146a..270b249f700fa 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -13,7 +13,7 @@
 // RUN: %clang_cc1 -triple x86_64-windows -fcxx-exceptions -fexceptions \
 // RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR4
 // RUN: %clang_cc1 -triple x86_64-windows \
-// RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DERR5
+// RUN:         -fms-extensions -x c++ -emit-llvm-only -verify %s -DNOERR
 
 extern "C" unsigned long _exception_code();
 extern "C" void might_throw();
@@ -214,7 +214,7 @@ void seh_unwinding() {
   } __except (1) {
   }
 }
-#elif defined(ERR5)
+#elif defined(NOERR)
 void seh_unwinding() {
   __try {
     HasCleanup x; // expected-no-diagnostics



More information about the cfe-commits mailing list