Skip to content

Commit b504870

Browse files
committed
[clang][Interp] Fix lvalue CompoundLiteralExprs
We need to leave a pointer on the stack for them, even if their type is primitive.
1 parent 1253e53 commit b504870

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,13 +1679,24 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(
16791679

16801680
std::optional<PrimType> T = classify(E->getType());
16811681
if (E->isFileScope()) {
1682+
// Avoid creating a variable if this is a primitive RValue anyway.
1683+
if (T && !E->isLValue())
1684+
return this->delegate(Init);
1685+
16821686
if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
1683-
if (classify(E->getType()))
1684-
return this->visit(Init);
16851687
if (!this->emitGetPtrGlobal(*GlobalIndex, E))
16861688
return false;
1689+
1690+
if (T) {
1691+
if (!this->visit(Init))
1692+
return false;
1693+
return this->emitInitGlobal(*T, *GlobalIndex, E);
1694+
}
1695+
16871696
return this->visitInitializer(Init);
16881697
}
1698+
1699+
return false;
16891700
}
16901701

16911702
// Otherwise, use a local variable.

clang/test/AST/Interp/c.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ _Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant exp
2121
// pedantic-expected-warning {{not an integer constant expression}}
2222
_Static_assert(!!1, "");
2323

24+
_Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \
25+
// pedantic-expected-warning {{not an integer constant expression}}
26+
2427
int a = (1 == 1 ? 5 : 3);
2528
_Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}
2629

0 commit comments

Comments
 (0)