[clang] [WinEH] Fix try scopes leaking to caller on inline (PR #167176)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 30 11:58:16 PDT 2026


================
@@ -666,8 +667,11 @@ void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
       CatchScope->setHandler(I, CGM.getCXXABI().getCatchAllTypeInfo(), Handler);
       // Under async exceptions, catch(...) need to catch HW exception too
       // Mark scope with SehTryBegin as a SEH __try scope
-      if (getLangOpts().EHAsynch)
+      if (getLangOpts().EHAsynch) {
         EmitSehTryScopeBegin();
+        // Push cleanup to emit the end of the scope
+        EHStack.pushCleanup<TerminateTryScope>(NormalCleanup);
----------------
GkvJwa wrote:

Add SEHTryEpilogueStack.push_back(EHStack.stable_begin()); better?
Then PopCleanupBlock(); in CodeGenFunction::ExitSEHTryStmt

like:
```
void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S) {             
    // ... existing code ...
    if (getLangOpts().EHAsynch) {                                          
      EHStack.pushCleanup<TerminateTryScope>(NormalCleanup);               
      SEHTryEpilogueStack.push_back(EHStack.stable_begin()); // new member 
    }                                                                      
  }                                                                        
                                                                           
  void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S) {              
    if (S.getFinallyHandler()) { PopCleanupBlock(); return; }
                                                                           
    if (getLangOpts().EHAsynch) {                                          
      assert(!SEHTryEpilogueStack.empty());                                
      auto top = SEHTryEpilogueStack.pop_back_val();                       
      assert(EHStack.stable_begin() == top &&                              
             "TerminateTryScope cleanup got reordered");
      PopCleanupBlock();   // guaranteed to pop the one we pushed          
    }                                                                      
    // ... existing code ...                                               
  }
```

or, similar to my approach, add a marker
```
EHStack.pushCleanup<TerminateTryScope>(
      static_cast<CleanupKind>(NormalCleanup | SEHTryScopeCleanup));

if (Scope.isSEHTryScopeCleanup() || Scope.isSEHFinallyCleanup())
    EmitSehTryScopeEnd();
  else
    EmitSehCppScopeEnd();
```

https://github.com/llvm/llvm-project/pull/167176


More information about the cfe-commits mailing list