[clang] [Clang][CodeGen] Fix clang codegen eh cleanup (PR #226743)

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 26 18:56:24 PDT 2026


https://github.com/rdong8 created https://github.com/llvm/llvm-project/pull/226743

Current code is incorrectly trying to `&` two enumerators from different enums together. This patch fixes it to call `needsEHCleanup`, matching the surrounding code.

>From cac024e1f2a164d49bdf65aff017fa9c23e49009 Mon Sep 17 00:00:00 2001
From: Richard Dong <66289396+rdong8 at users.noreply.github.com>
Date: Sat, 26 Sep 2026 20:53:54 +0000
Subject: [PATCH] [Clang][CodeGen] Fix EH cleanup check for compound literals
 with destructors

In EmitCompoundLiteralLValue and VisitCompoundLiteralExpr, the code checked
`DtorKind & EHCleanup` to determine whether an exception-handling cleanup
was needed. However, DtorKind is of type QualType::DestructionKind (an enum)
while EHCleanup is CleanupKind (1). Bitwise AND between them inadvertently
tested whether the enum value was odd, skipping EH cleanup for
DK_objc_strong_lifetime (value 2). Use needsEHCleanup(DtorKind) instead.
---
 clang/lib/CodeGen/CGExpr.cpp    | 2 +-
 clang/lib/CodeGen/CGExprAgg.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index eba802e187beb..6dee25dbf7eb7 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -6105,7 +6105,7 @@ LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
     if (QualType::DestructionKind DtorKind = E->getType().isDestructedType())
       pushLifetimeExtendedDestroy(getCleanupKind(DtorKind), DeclPtr,
                                   E->getType(), getDestroyer(DtorKind),
-                                  DtorKind & EHCleanup);
+                                  needsEHCleanup(DtorKind));
 
   return Result;
 }
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index f18978c7a936e..411c3b95bf6e1 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -816,7 +816,7 @@ void AggExprEmitter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
     if (QualType::DestructionKind DtorKind = E->getType().isDestructedType())
       CGF.pushLifetimeExtendedDestroy(
           CGF.getCleanupKind(DtorKind), Slot.getAddress(), E->getType(),
-          CGF.getDestroyer(DtorKind), DtorKind & EHCleanup);
+          CGF.getDestroyer(DtorKind), CGF.needsEHCleanup(DtorKind));
 }
 
 /// Attempt to look through various unimportant expressions to find a



More information about the cfe-commits mailing list