================
@@ -630,9 +632,17 @@ llvm::Expected<Expr *> 
Interpreter::convertExprToValue(Expr *E) {
 
   // It could fail, like printing an array type in C. (not supported)
   if (SetValueE.isInvalid())
-    return E;
-
-  return SetValueE.get();
+    return FullExpr;
+
+  // The temporaries of E must be destroyed at the end of the statement.
+  // Without the cleanups, CodeGen destroys them at the end of the function
+  // running the top-level statements, which it finishes after emitting the
+  // deferred declarations: their destructors would not be emitted.
+  Expr *Result = SetValueE.get();
+  if (EWC && !isa<ExprWithCleanups>(Result))
+    Result = ExprWithCleanups::Create(
+        Ctx, Result, EWC->cleanupsHaveSideEffects(), EWC->getObjects());
----------------
guitargeek wrote:

Good question! I checked: the placement new does kick in. For a printed object 
of class type, e.g. `h(S())` with `R h(S)`, the `R` is constructed once 
directly into the storage of the `Value`, without any copy. But that only takes 
care of the printed object itself; the temporaries of its initializer (here the 
`S()` argument) still need a full-expression to be destroyed at. Without this 
patch that case hits the same `moveLazyEmissionStates()` assertion as `f(S())`. 
The `ActOnFinishFullExpr()` on the placement new path doesn't help, because 
`convertExprToValue()` runs in `HandleTopLevelDecl()` after the statement was 
already finished, so Sema has no pending cleanups left to attach.

And for the other case in the test, `f(S())` prints an `int`, so no placement 
new is involved at all.

This is also how cling did it: 
`ValueExtractionSynthesizer::SynthesizeSVRInit()` strips the `ExprWithCleanups` 
and puts the synthesized call (including the placement new) back into it: "In 
case of ExprWithCleanups we need to extend its 'scope' to the call." I've added 
the placement new case to the test.

https://github.com/llvm/llvm-project/pull/229675
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to