================
@@ -9125,9 +9126,25 @@ bool
 LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
   assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
          "lvalue compound literal in c++?");
-  // Defer visiting the literal until the lvalue-to-rvalue conversion. We can
-  // only see this when folding in C, so there's no standard to follow here.
-  return Success(E);
+  APValue *Lit;
+  // If CompountLiteral has static storage, its value can be used outside
+  // this expression. So evaluate it once and store it in ASTContext.
+  if (E->hasStaticStorage()) {
+    Lit = E->getOrCreateStaticValue(Info.Ctx);
+    Result.set(E);
+    // Reset any previously evaluated state, otherwise evaluation below might
+    // fail.
+    // FIXME: Should we just re-use the previously evaluated value instead?
+    *Lit = APValue();
----------------
kadircet wrote:

> Do we need the Info.EvalMode == EvalInfo::EM_ConstantFold check from 
> MaterializeTemporaryExpr?

Considering the previous comments in the code, I think we need to perform 
folding also for CLEs with static duration. There are also many test failures 
if we don't fold these in static initializers, 
https://github.com/llvm/llvm-project/blob/main/clang/test/AST/ByteCode/c.c#L155-L174
 is an example (but I am also surprised to see that hold for 
`MaterializeTemporaryExpr`s).

https://github.com/llvm/llvm-project/pull/137163
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to