Skip to content

Commit a2da7d0

Browse files
committed
[clang][Interp] Ignore LValueToRValue casts before doing the load
If the SubExpr results in an invalid pointer, we will otherwise reject the constant expression.
1 parent 58ceefe commit a2da7d0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
7878
switch (CE->getCastKind()) {
7979

8080
case CK_LValueToRValue: {
81+
if (DiscardResult)
82+
return this->discard(SubExpr);
83+
8184
return dereference(
8285
SubExpr, DerefKind::Read,
8386
[](PrimType) {
@@ -86,9 +89,7 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
8689
},
8790
[this, CE](PrimType T) {
8891
// Pointer on stack - dereference it.
89-
if (!this->emitLoadPop(T, CE))
90-
return false;
91-
return DiscardResult ? this->emitPop(T, CE) : true;
92+
return this->emitLoadPop(T, CE);
9293
});
9394
}
9495

clang/test/AST/Interp/c.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ void f (int z) {
9898

9999
int expr;
100100
int chooseexpr[__builtin_choose_expr(1, 1, expr)];
101+
102+
int somefunc(int i) {
103+
return (i, 65537) * 65537; // expected-warning {{left operand of comma operator has no effect}} \
104+
// expected-warning {{overflow in expression; result is 131073}} \
105+
// pedantic-expected-warning {{left operand of comma operator has no effect}} \
106+
// pedantic-expected-warning {{overflow in expression; result is 131073}} \
107+
// ref-warning {{left operand of comma operator has no effect}} \
108+
// ref-warning {{overflow in expression; result is 131073}} \
109+
// pedantic-ref-warning {{left operand of comma operator has no effect}} \
110+
// pedantic-ref-warning {{overflow in expression; result is 131073}}
111+
112+
}

0 commit comments

Comments
 (0)