[clang] [clang] Don't fold reads of modifiable file-scope compound literals (PR #241126)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 20:55:38 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Prajwal KP (Prajwal-kp-18)
<details>
<summary>Changes</summary>
A compound literal at file scope is an unnamed object with static storage duration. Unless its type is const-qualified it can be modified at runtime, but the constant evaluator returned its initial value for any read, for example through a const pointer:
static struct S *const p = &(struct S){255};
int f(void) { p->x = 252; return p->x; } // returned 255
Only use the stored value if the compound literal is const-qualified or was evaluated as part of the current evaluation, as in 'int x = (int){1};'. This matches the rule for non-const global variables and the behavior of the bytecode interpreter.
Fixes #<!-- -->54014
---
Full diff: https://github.com/llvm/llvm-project/pull/241126.diff
3 Files Affected:
- (modified) clang/lib/AST/ExprConstant.cpp (+21)
- (modified) clang/test/CodeGen/compound-literal.c (+25)
- (modified) clang/test/Sema/compound-literal.c (+8)
``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 57d5bb475d0f9..961a7c716aa10 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -823,6 +823,12 @@ namespace {
/// The number of heap allocations performed so far in this evaluation.
uintptr_t NumHeapAllocs = 0;
+ /// Compound literals with static storage duration whose initializers were
+ /// evaluated as part of this evaluation. Any other such compound literal
+ /// that is not const-qualified may have been modified at runtime, so its
+ /// value is not known.
+ llvm::SmallPtrSet<const CompoundLiteralExpr *, 2> EvaluatedCompoundLiterals;
+
struct EvaluatingConstructorRAII {
EvalInfo &EI;
ObjectUnderConstruction Object;
@@ -4995,6 +5001,19 @@ static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
return CompleteObject();
}
+ // A compound literal with static storage duration is an unnamed object
+ // (C11 6.5.2.5) that can be modified unless it is const-qualified. Its
+ // stored initial value can only be used if it was initialized as part
+ // of this evaluation, as in 'int x = (int){1};'.
+ if (!CLE->getType().isConstant(Info.Ctx) &&
+ !Info.EvaluatedCompoundLiterals.contains(CLE)) {
+ if (!IsAccess)
+ return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
+ Info.FFDiag(E);
+ Info.Note(CLE->getExprLoc(), diag::note_declared_at);
+ return CompleteObject();
+ }
+
BaseVal = &CLE->getStaticValue();
} else {
if (!IsAccess)
@@ -9923,6 +9942,8 @@ LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
*Lit = APValue();
return false;
}
+ if (E->hasStaticStorage())
+ Info.EvaluatedCompoundLiterals.insert(E);
return true;
}
diff --git a/clang/test/CodeGen/compound-literal.c b/clang/test/CodeGen/compound-literal.c
index 5fe9594c0f954..14e68ff73a538 100644
--- a/clang/test/CodeGen/compound-literal.c
+++ b/clang/test/CodeGen/compound-literal.c
@@ -94,3 +94,28 @@ int compound_array_fn(void) {
int compound_array[] = (int[]){1,2,3,4,5,6,7,8};
return compound_array[0];
}
+
+// GH54014: a file-scope compound literal is a modifiable object even when it
+// is only reachable through a const pointer, so reads of it must load from
+// memory instead of being folded to its initial value.
+struct GH54014 { int counter; };
+static struct GH54014 *const GH54014_ptr = &(struct GH54014){255};
+void GH54014_g(void);
+
+// CHECK-LABEL: define{{.*}} i32 @GH54014_write_then_read()
+// CHECK: store i32 252, ptr [[CL:@.compoundliteral[.0-9]*]]
+// CHECK-NEXT: [[V:%.*]] = load i32, ptr [[CL]]
+// CHECK-NEXT: ret i32 [[V]]
+int GH54014_write_then_read(void) {
+ GH54014_ptr->counter = 252;
+ return GH54014_ptr->counter;
+}
+
+// CHECK-LABEL: define{{.*}} i32 @GH54014_read_after_call()
+// CHECK: call void @GH54014_g()
+// CHECK-NEXT: [[V:%.*]] = load i32, ptr @.compoundliteral{{[.0-9]*}}
+// CHECK-NEXT: ret i32 [[V]]
+int GH54014_read_after_call(void) {
+ GH54014_g();
+ return GH54014_ptr->counter;
+}
diff --git a/clang/test/Sema/compound-literal.c b/clang/test/Sema/compound-literal.c
index 1026e5ece11b4..f638e175cfc0b 100644
--- a/clang/test/Sema/compound-literal.c
+++ b/clang/test/Sema/compound-literal.c
@@ -54,3 +54,11 @@ void vla(int n) {
(void)(int[size]){1,2,3}; // expected-error {{compound literal cannot be of variable-length array type}}
(void)(int[size]){1,2,3,4,5}; // expected-error {{compound literal cannot be of variable-length array type}}
}
+
+// GH54014: a non-const file-scope compound literal may be modified at runtime,
+// so its value can't be read through a pointer in a constant initializer.
+static int *const GH54014_p = &(int){255};
+static int GH54014_x = *GH54014_p; // expected-error {{initializer element is not a compile-time constant}}
+// A const-qualified compound literal can't change, so this is still folded.
+static const int *const GH54014_q = &(const int){3};
+static int GH54014_y = *GH54014_q;
``````````
</details>
https://github.com/llvm/llvm-project/pull/241126
More information about the cfe-commits
mailing list