Skip to content

Commit

Permalink
[clang][Interp] Fix lvalue CompoundLiteralExprs
Browse files Browse the repository at this point in the history
We need to leave a pointer on the stack for them, even if their
type is primitive.
  • Loading branch information
tbaederr committed Feb 26, 2024
1 parent 1253e53 commit b504870
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1679,13 +1679,24 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(

std::optional<PrimType> T = classify(E->getType());
if (E->isFileScope()) {
// Avoid creating a variable if this is a primitive RValue anyway.
if (T && !E->isLValue())
return this->delegate(Init);

if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
if (classify(E->getType()))
return this->visit(Init);
if (!this->emitGetPtrGlobal(*GlobalIndex, E))
return false;

if (T) {
if (!this->visit(Init))
return false;
return this->emitInitGlobal(*T, *GlobalIndex, E);
}

return this->visitInitializer(Init);
}

return false;
}

// Otherwise, use a local variable.
Expand Down
3 changes: 3 additions & 0 deletions clang/test/AST/Interp/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ _Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant exp
// pedantic-expected-warning {{not an integer constant expression}}
_Static_assert(!!1, "");

_Static_assert(!(_Bool){(void*)0}, ""); // pedantic-ref-warning {{not an integer constant expression}} \
// pedantic-expected-warning {{not an integer constant expression}}

int a = (1 == 1 ? 5 : 3);
_Static_assert(a == 5, ""); // all-error {{not an integral constant expression}}

Expand Down

0 comments on commit b504870

Please sign in to comment.