Skip to content

Commit

Permalink
[clang][Interp] Fix ignoring CompoundLiteralExprs
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D149837
  • Loading branch information
tbaederr committed Aug 1, 2023
1 parent e9e2983 commit c0a36a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 13 additions & 4 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,13 @@ bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr(
template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
for (const Expr *Init : E->inits()) {
if (!this->visit(Init))
return false;
if (DiscardResult) {
if (!this->discard(Init))
return false;
} else {
if (!this->visit(Init))
return false;
}
}
return true;
}
Expand Down Expand Up @@ -944,12 +949,16 @@ bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(
// Otherwise, use a local variable.
if (T) {
// For primitive types, we just visit the initializer.
return this->visit(Init);
return DiscardResult ? this->discard(Init) : this->visit(Init);
} else {
if (std::optional<unsigned> LocalIndex = allocateLocal(Init)) {
if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
return this->visitInitializer(Init);
if (!this->visitInitializer(Init))
return false;
if (DiscardResult)
return this->emitPopPtr(E);
return true;
}
}

Expand Down
17 changes: 17 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,26 @@ namespace DiscardExprs {
1 ? 0 : 1;
__is_trivial(int);

(int){1};
(int[]){1,2,3};

return 0;
}

constexpr int oh_my(int x) {
(int){ x++ };
return x;
}
static_assert(oh_my(0) == 1, "");

constexpr int oh_my2(int x) {
int y{x++};
return x;
}

static_assert(oh_my2(0) == 1, "");


/// Ignored comma expressions still have their
/// expressions evaluated.
constexpr int Comma(int start) {
Expand Down

0 comments on commit c0a36a1

Please sign in to comment.