https://github.com/momo5502 created 
https://github.com/llvm/llvm-project/pull/128839

This fixes #93251

>From 36931d9d2a4d50ac363a1b2de123909eacfb72a6 Mon Sep 17 00:00:00 2001
From: Maurice Heumann <maurice.heum...@wibu.com>
Date: Wed, 26 Feb 2025 08:37:04 +0100
Subject: [PATCH] Don't generate SEH scopes for noexcept functions

---
 clang/lib/CodeGen/CGCleanup.cpp | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGCleanup.cpp b/clang/lib/CodeGen/CGCleanup.cpp
index 7e1c5b7da9552..69d605944b88c 100644
--- a/clang/lib/CodeGen/CGCleanup.cpp
+++ b/clang/lib/CodeGen/CGCleanup.cpp
@@ -152,6 +152,19 @@ EHScopeStack::getInnermostActiveNormalCleanup() const {
   return stable_end();
 }
 
+static bool FunctionCanThrow(const FunctionDecl *D) {
+  if (!D) {
+    return true;
+  }
+
+  const auto *Proto = D->getType()->getAs<FunctionProtoType>();
+  if (!Proto) {
+    // Function proto is not found, we conservatively assume throwing.
+    return true;
+  }
+  return !isNoexceptExceptionSpec(Proto->getExceptionSpecType()) ||
+         Proto->canThrow() != CT_Cannot;
+}
 
 void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t Size) {
   char *Buffer = allocate(EHCleanupScope::getSizeForCleanupSize(Size));
@@ -191,7 +204,9 @@ void *EHScopeStack::pushCleanup(CleanupKind Kind, size_t 
Size) {
   // consistent with MSVC's behavior, except in the presence of -EHa.
   // Check getInvokeDest() to generate llvm.seh.scope.begin() as needed.
   if (CGF->getLangOpts().EHAsynch && IsEHCleanup && !IsLifetimeMarker &&
-      CGF->getTarget().getCXXABI().isMicrosoft() && CGF->getInvokeDest())
+      CGF->getTarget().getCXXABI().isMicrosoft() &&
+      FunctionCanThrow(dyn_cast<FunctionDecl>(CGF->CurFuncDecl)) &&
+      CGF->getInvokeDest())
     CGF->EmitSehCppScopeBegin();
 
   return Scope->getCleanupBuffer();
@@ -784,7 +799,8 @@ void CodeGenFunction::PopCleanupBlock(bool 
FallthroughIsBranchThrough,
     cleanupFlags.setIsEHCleanupKind();
 
   // Under -EHa, invoke seh.scope.end() to mark scope end before dtor
-  bool IsEHa = getLangOpts().EHAsynch && !Scope.isLifetimeMarker();
+  bool IsEHa = getLangOpts().EHAsynch && !Scope.isLifetimeMarker() &&
+               FunctionCanThrow(dyn_cast<FunctionDecl>(CurFuncDecl));
   const EHPersonality &Personality = EHPersonality::get(*this);
   if (!RequiresNormalCleanup) {
     // Mark CPP scope end for passed-by-value Arg temp

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to