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

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 07:08:05 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() &&
----------------
kadircet wrote:

oops, I was looking at the wrong terminal, we do have failures without this:
```
********************
FAIL: Clang :: SemaCXX/constant-expression-cxx11.cpp (3207 of 22469)
******************** TEST 'Clang :: SemaCXX/constant-expression-cxx11.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/usr/local/google/home/kadircet/repos/llvm/build/bin/clang -cc1 -internal-isystem /usr/local/google/home/kadircet/repos/llvm/build/lib/clang/21/include -nostdsysteminc -std=c++23 -isystem /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23              -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion # RUN: at line 1
+ /usr/local/google/home/kadircet/repos/llvm/build/bin/clang -cc1 -internal-isystem /usr/local/google/home/kadircet/repos/llvm/build/lib/clang/21/include -nostdsysteminc -std=c++23 -isystem /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/Inputs -fsyntax-only -verify=expected,cxx20_23,cxx23 -triple x86_64-linux -Wno-string-plus-int -Wno-pointer-arith -Wno-zero-length-array -Wno-c99-designator -fcxx-exceptions -pedantic /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp -Wno-comment -Wno-tautological-pointer-compare -Wno-bool-conversion
error: diagnostics with 'error' severity expected but not seen:
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1670 'expected-error': static assertion expression is not an integral constant expression
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1673 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp:1674) 'expected-error': static assertion expression is not an integral constant expression
error: diagnostics with 'note' severity expected but not seen:
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1670 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp:1671) 'expected-note': subexpression not valid
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1669 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp:1672) 'expected-note': declared here
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1673 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp:1675) 'expected-note': subexpression not valid
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 1673 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaCXX/constant-expression-cxx11.cpp:1676) 'expected-note': declared here
6 errors generated.

--

********************
FAIL: Clang :: SemaTemplate/constexpr-instantiate.cpp (13593 of 22469)
******************** TEST 'Clang :: SemaTemplate/constexpr-instantiate.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/usr/local/google/home/kadircet/repos/llvm/build/bin/clang -cc1 -internal-isystem /usr/local/google/home/kadircet/repos/llvm/build/lib/clang/21/include -nostdsysteminc -std=c++11 -verify /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp # RUN: at line 1
+ /usr/local/google/home/kadircet/repos/llvm/build/bin/clang -cc1 -internal-isystem /usr/local/google/home/kadircet/repos/llvm/build/lib/clang/21/include -nostdsysteminc -std=c++11 -verify /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp
error: 'expected-error' diagnostics expected but not seen:
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 222 (directive at /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp:223): must be initialized by a constant expression
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 236: narrow
error: 'expected-note' diagnostics expected but not seen:
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 222: not valid in a constant expression
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 222: declared here
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 236: instantiation of
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 236: cast
  File /usr/local/google/home/kadircet/repos/llvm/clang/test/SemaTemplate/constexpr-instantiate.cpp Line 237: instantiation of
7 errors generated.

--

********************
```

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


More information about the cfe-commits mailing list