[clang] [CIR] Keep conditional cleanup scopes open during deactivation (PR #234303)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 11:58:48 PST 2026


================
@@ -766,3 +766,338 @@ void test_flag_cleared_before_cond_cleanup() {
 // OGCG:   call void @_ZN7PayloadD1Ev(ptr {{.*}} %[[AGG_TMP]])
 // OGCG:   call void @_ZN5GuardD1Ev(ptr {{.*}} %[[REF_TMP]])
 // OGCG:   resume { ptr, i32 }
+
+// The members that an aggregate initialization has constructed so far are
+// destroyed when a later member initializer is left early, here by a return
+// in a statement expression in an operand of a conditional operator. The
+// member cleanups are deactivated at the end of the initialization, while the
+// cleanup scope that the conditional operator opened for its operands is still
+// open: the flag that guards the cleanup of the first member is cleared inside
+// that scope.
+
+struct Obj {
+  char c;
+  Obj(char);
+  ~Obj();
+};
+
+struct Pair {
+  Obj a;
+  Obj b;
+};
+
+Obj copy(const Obj &);
+Obj make(bool);
+
+void struct_ternary(bool b, bool c) {
+  Pair p{Obj{'a'}, b ? ({
+                     if (c)
+                       return;
+                     Obj{'b'};
+                   })
+                     : Obj{'c'}};
+}
+
+// The cleanup scope of the conditional operator is empty and canonicalized
+// away.
+//
+// CIR-LABEL: cir.func {{.*}} @_Z14struct_ternarybb(
+// CIR:         %[[P:.*]] = cir.alloca "p"
+// CIR:         %[[ACTIVE:.*]] = cir.alloca "cleanup.isactive"
+// CIR:         %[[A:.*]] = cir.get_member %[[P]][0] {name = "a"}
+// CIR:         cir.call @_ZN3ObjC1Ec(%[[A]],
+// CIR-NEXT:    %[[TRUE:.*]] = cir.const #true
+// CIR-NEXT:    cir.store %[[TRUE]], %[[ACTIVE]]
+// CIR-NEXT:    cir.cleanup.scope {
+// CIR:           cir.if
+// CIR:             cir.if
+// CIR-NEXT:          cir.return
+// CIR:           } else {
+// CIR:             cir.call @_ZN3ObjC1Ec(
+// CIR-NEXT:      } loc(
+// CIR-NEXT:      %[[FALSE:.*]] = cir.const #false
+// CIR-NEXT:      cir.store %[[FALSE]], %[[ACTIVE]]
+// CIR-NEXT:      cir.yield
+// CIR-NEXT:    } cleanup all {
+// CIR-NEXT:      %[[IS_ACTIVE:.*]] = cir.load {{.*}} %[[ACTIVE]]
+// CIR-NEXT:      cir.if %[[IS_ACTIVE]] {
+// CIR-NEXT:        cir.call @_ZN3ObjD1Ev(%[[A]])
+// CIR:         cir.call @_ZN4PairD1Ev(%[[P]])
----------------
andykaylor wrote:

I think this would have passed before the change. All this is checking is that the Pair dtor is called sometime after the cleanup scope, which it was without this change, though it was in a cleanup region for an empty cleanup scope. I don't see anything else in the checks here that changed.

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


More information about the cfe-commits mailing list