[clang] [clang] Compound Literal Statement Constant Expression Assertion Fix (PR #139479)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu May 15 09:23:11 PDT 2025
================
@@ -7241,10 +7241,20 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
? VK_PRValue
: VK_LValue;
+ // C99 6.5.2.5
+ // "If the compound literal occurs outside the body of a function, the
+ // initializer list shall consist of constant expressions."
if (IsFileScope)
if (auto ILE = dyn_cast<InitListExpr>(LiteralExpr))
for (unsigned i = 0, j = ILE->getNumInits(); i != j; i++) {
Expr *Init = ILE->getInit(i);
+ if (!Init->isTypeDependent() && !Init->isValueDependent())
+ if (!Init->isConstantInitializer(Context, /*IsForRef=*/false)) {
+ Diag(Init->getExprLoc(), diag::err_init_element_not_constant)
+ << Init->getSourceBitField();
+ return ExprError();
+ }
----------------
AaronBallman wrote:
```suggestion
if (!Init->isTypeDependent() && !Init->isValueDependent() &&
!Init->isConstantInitializer(Context, /*IsForRef=*/false)) {
Diag(Init->getExprLoc(), diag::err_init_element_not_constant)
<< Init->getSourceBitField();
return ExprError();
}
```
Can combine these into a single `if`
https://github.com/llvm/llvm-project/pull/139479
More information about the cfe-commits
mailing list