Skip to content

Commit 77d2283

Browse files
committed
[clang][Interp] Diagnose shift overflows
1 parent ed304b6 commit 77d2283

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

clang/lib/AST/Interp/Interp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
147147
const APSInt Val = RHS.toAPSInt();
148148
QualType Ty = E->getType();
149149
S.CCEDiag(E, diag::note_constexpr_large_shift) << Val << Ty << Bits;
150-
return true; // We will do the shift anyway but fix up the shift amount.
150+
return !(S.getEvalStatus().Diag && !S.getEvalStatus().Diag->empty() && S.getLangOpts().CPlusPlus11);
151151
}
152152

153153
if (LHS.isSigned() && !S.getLangOpts().CPlusPlus20) {

clang/test/AST/Interp/shifts.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,14 @@ namespace LongInt {
200200
};
201201

202202
enum shiftof {
203-
X = (1<<-29) // all-error {{expression is not an integral constant expression}} \
204-
// all-note {{negative shift count -29}}
205-
};
203+
X = (1<<-29), // all-error {{expression is not an integral constant expression}} \
204+
// all-note {{negative shift count -29}}
205+
206+
X2 = (-1<<29), // cxx17-error {{expression is not an integral constant expression}} \
207+
// cxx17-note {{left shift of negative value -1}} \
208+
// ref-cxx17-error {{expression is not an integral constant expression}} \
209+
// ref-cxx17-note {{left shift of negative value -1}}
206210

207-
enum shiftof2 {
208-
X2 = (-1<<29) // cxx17-error {{expression is not an integral constant expression}} \
209-
// cxx17-note {{left shift of negative value -1}} \
210-
// ref-cxx17-error {{expression is not an integral constant expression}} \
211-
// ref-cxx17-note {{left shift of negative value -1}}
211+
X3 = (1<<32) // all-error {{expression is not an integral constant expression}} \
212+
// all-note {{shift count 32 >= width of type 'int'}}
212213
};

0 commit comments

Comments
 (0)