Skip to content

Commit 18e7bcb

Browse files
committed
[clang][Interp] Reject inc/dec ops before C++ 14
1 parent a7521fd commit 18e7bcb

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
33953395

33963396
switch (E->getOpcode()) {
33973397
case UO_PostInc: { // x++
3398+
if (!Ctx.getLangOpts().CPlusPlus14)
3399+
return this->emitInvalid(E);
3400+
33983401
if (!this->visit(SubExpr))
33993402
return false;
34003403

@@ -3413,6 +3416,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
34133416
return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
34143417
}
34153418
case UO_PostDec: { // x--
3419+
if (!Ctx.getLangOpts().CPlusPlus14)
3420+
return this->emitInvalid(E);
3421+
34163422
if (!this->visit(SubExpr))
34173423
return false;
34183424

@@ -3431,6 +3437,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
34313437
return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
34323438
}
34333439
case UO_PreInc: { // ++x
3440+
if (!Ctx.getLangOpts().CPlusPlus14)
3441+
return this->emitInvalid(E);
3442+
34343443
if (!this->visit(SubExpr))
34353444
return false;
34363445

@@ -3475,6 +3484,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
34753484
return E->isGLValue() || this->emitLoadPop(*T, E);
34763485
}
34773486
case UO_PreDec: { // --x
3487+
if (!Ctx.getLangOpts().CPlusPlus14)
3488+
return this->emitInvalid(E);
3489+
34783490
if (!this->visit(SubExpr))
34793491
return false;
34803492

clang/test/AST/Interp/cxx11.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,19 @@ constexpr S s = { 5 };
3030
constexpr const int *p = &s.m + 1;
3131

3232
constexpr const int *np2 = &(*(int(*)[4])nullptr)[0]; // ok
33+
34+
constexpr int preDec(int x) { // both-error {{never produces a constant expression}}
35+
return --x; // both-note {{subexpression}}
36+
}
37+
38+
constexpr int postDec(int x) { // both-error {{never produces a constant expression}}
39+
return x--; // both-note {{subexpression}}
40+
}
41+
42+
constexpr int preInc(int x) { // both-error {{never produces a constant expression}}
43+
return ++x; // both-note {{subexpression}}
44+
}
45+
46+
constexpr int postInc(int x) { // both-error {{never produces a constant expression}}
47+
return x++; // both-note {{subexpression}}
48+
}

0 commit comments

Comments
 (0)