[clang] [clang][CompundLiteralExpr] Don't defer evaluation for CLEs (PR #137163)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 25 12:06:41 PDT 2025


================
@@ -4522,6 +4523,38 @@ static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
 
         BaseVal = MTE->getOrCreateValue(false);
         assert(BaseVal && "got reference to unevaluated temporary");
+      } else if (const CompoundLiteralExpr *CLE =
+                     dyn_cast_or_null<CompoundLiteralExpr>(Base)) {
+        // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating
+        // the initializer until now for such expressions. Such an expression
+        // can't be an ICE in C, so this only matters for fold.
+        if (LValType.isVolatileQualified()) {
+          Info.FFDiag(E);
+          return CompleteObject();
+        }
+
+        // According to GCC info page:
+        //
+        // 6.28 Compound Literals
+        //
+        // As an optimization, G++ sometimes gives array compound literals
+        // longer lifetimes: when the array either appears outside a function or
+        // has a const-qualified type. If foo and its initializer had elements
+        // of type char *const rather than char *, or if foo were a global
+        // variable, the array would have static storage duration. But it is
+        // probably safest just to avoid the use of array compound literals in
+        // C++ code.
+        //
+        // Obey that rule by checking constness for converted array types.
+        if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
+                                             !LValType->isArrayType() &&
----------------
efriedma-quic wrote:

I'm not sure it's actually possible for this condition to be true?  Do tests pass without this check?  It's possible that the condition this was supposed to be checking for just isn't relevant with these changes.

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


More information about the cfe-commits mailing list