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

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 02:12:19 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() &&
----------------
ilya-biryukov wrote:

I wanted to propose to litigate this in a separate PR. Would that work?

I think it's useful to land this and fix the UB ASAP and the less things are changed within the commits, the easier-to-read commit history.

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


More information about the cfe-commits mailing list