Skip to content

Commit 69c6fc0

Browse files
[clang] Emit bad shift warnings
1 parent cb5d1b5 commit 69c6fc0

File tree

13 files changed

+73
-20
lines changed

13 files changed

+73
-20
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,9 @@ static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
28562856
else if (LHS.countl_zero() < SA)
28572857
Info.CCEDiag(E, diag::note_constexpr_lshift_discards);
28582858
}
2859+
if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
2860+
Info.getLangOpts().CPlusPlus)
2861+
return false;
28592862
Result = LHS << SA;
28602863
return true;
28612864
}
@@ -2879,6 +2882,10 @@ static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
28792882
if (SA != RHS)
28802883
Info.CCEDiag(E, diag::note_constexpr_large_shift)
28812884
<< RHS << E->getType() << LHS.getBitWidth();
2885+
2886+
if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
2887+
Info.getLangOpts().CPlusPlus)
2888+
return false;
28822889
Result = LHS >> SA;
28832890
return true;
28842891
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11246,7 +11246,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
1124611246
if (Right.isNegative()) {
1124711247
S.DiagRuntimeBehavior(Loc, RHS.get(),
1124811248
S.PDiag(diag::warn_shift_negative)
11249-
<< RHS.get()->getSourceRange());
11249+
<< RHS.get()->getSourceRange());
1125011250
return;
1125111251
}
1125211252

@@ -11261,7 +11261,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
1126111261
if (Right.uge(LeftSize)) {
1126211262
S.DiagRuntimeBehavior(Loc, RHS.get(),
1126311263
S.PDiag(diag::warn_shift_gt_typewidth)
11264-
<< RHS.get()->getSourceRange());
11264+
<< RHS.get()->getSourceRange());
1126511265
return;
1126611266
}
1126711267

@@ -11294,7 +11294,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
1129411294
if (Left.isNegative()) {
1129511295
S.DiagRuntimeBehavior(Loc, LHS.get(),
1129611296
S.PDiag(diag::warn_shift_lhs_negative)
11297-
<< LHS.get()->getSourceRange());
11297+
<< LHS.get()->getSourceRange());
1129811298
return;
1129911299
}
1130011300

@@ -17130,11 +17130,20 @@ Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result,
1713017130
// Circumvent ICE checking in C++11 to avoid evaluating the expression twice
1713117131
// in the non-ICE case.
1713217132
if (!getLangOpts().CPlusPlus11 && E->isIntegerConstantExpr(Context)) {
17133+
SmallVector<PartialDiagnosticAt, 8> Notes;
1713317134
if (Result)
17134-
*Result = E->EvaluateKnownConstIntCheckOverflow(Context);
17135+
*Result = E->EvaluateKnownConstIntCheckOverflow(Context, &Notes);
1713517136
if (!isa<ConstantExpr>(E))
1713617137
E = Result ? ConstantExpr::Create(Context, E, APValue(*Result))
1713717138
: ConstantExpr::Create(Context, E);
17139+
17140+
if (Notes.size() && !Diagnoser.Suppress) {
17141+
Diagnoser.diagnoseNotICE(*this, DiagLoc) << E->getSourceRange();
17142+
for (const PartialDiagnosticAt &Note : Notes)
17143+
Diag(Note.first, Note.second);
17144+
return ExprError();
17145+
}
17146+
1713817147
return E;
1713917148
}
1714017149

clang/test/C/drs/dr0xx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ void dr081(void) {
430430
/* Demonstrate that we don't crash when left shifting a signed value; that's
431431
* implementation defined behavior.
432432
*/
433-
_Static_assert(-1 << 1 == -2, "fail"); /* Didn't shift a zero into the "sign bit". */
433+
_Static_assert(-1 << 1 == -2, "fail"); /* c89only-error {{static assertion expression is not an integral constant expression}}
434+
c89only-note {{left shift of negative value -1}} */
434435
_Static_assert(1 << 3 == 1u << 3u, "fail"); /* Shift of a positive signed value does sensible things. */
435436
}
436437

clang/test/C/drs/dr2xx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ void dr258(void) {
277277
void dr261(void) {
278278
/* This is still an integer constant expression despite the overflow. */
279279
enum e1 {
280-
ex1 = __INT_MAX__ + 1 /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}} */
280+
ex1 = __INT_MAX__ + 1 /* expected-warning {{overflow in expression; result is -2'147'483'648 with type 'int'}}
281+
c89only-error {{expression is not an integer constant expression}}
282+
c89only-note {{value 2147483648 is outside the range of representable values of type 'int'}} */
281283
};
282284

283285
/* This is not an integer constant expression, because of the comma operator,

clang/test/Sema/builtins.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ void test17(void) {
171171
#define OPT(...) (__builtin_constant_p(__VA_ARGS__) && strlen(__VA_ARGS__) < 4)
172172
// FIXME: These are incorrectly treated as ICEs because strlen is treated as
173173
// a builtin.
174-
ASSERT(OPT("abc"));
175-
ASSERT(!OPT("abcd"));
174+
ASSERT(OPT("abc")); /* expected-error {{expression is not an integer constant expression}}
175+
expected-note {{subexpression not valid in a constant expression}} */
176+
ASSERT(!OPT("abcd")); /* expected-error {{expression is not an integer constant expression}}
177+
expected-note {{subexpression not valid in a constant expression}} */
176178
// In these cases, the strlen is non-constant, but the __builtin_constant_p
177179
// is 0: the array size is not an ICE but is foldable.
178180
ASSERT(!OPT(test17_c));

clang/test/Sema/code_align.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ void foo1(int A)
9797
for(int I=0; I<256; ++I) { bar(I); }
9898

9999
#ifdef __SIZEOF_INT128__
100-
// cpp-local-error@+3{{expression is not an integral constant expression}}
101-
// cpp-local-note@+2{{left shift of negative value -1311768467294899680}}
102-
// c-local-error@+1{{'code_align' attribute requires an integer argument which is a constant power of two between 1 and 4096 inclusive; provided argument was -(__int128_t)1311768467294899680ULL << 64}}
100+
// cpp-local-error@+4{{expression is not an integral constant expression}}
101+
// cpp-local-note@+3{{left shift of negative value -1311768467294899680}}
102+
// c-local-error+2{{expression is not an integer constant expression}}
103+
// c-local-note+1{{left shift of negative value -1311768467294899680}}
103104
[[clang::code_align(-(__int128_t)0x1234567890abcde0ULL << 64)]]
104105
for(int I=0; I<256; ++I) { bar(I); }
105106
#endif

clang/test/Sema/constant-builtins-2.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,17 @@ char clz52[__builtin_clzg((unsigned __int128)0x1) == BITSIZE(__int128) - 1 ? 1 :
265265
char clz53[__builtin_clzg((unsigned __int128)0x1, 42) == BITSIZE(__int128) - 1 ? 1 : -1];
266266
char clz54[__builtin_clzg((unsigned __int128)0xf) == BITSIZE(__int128) - 4 ? 1 : -1];
267267
char clz55[__builtin_clzg((unsigned __int128)0xf, 42) == BITSIZE(__int128) - 4 ? 1 : -1];
268-
char clz56[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1))) == 0 ? 1 : -1];
269-
char clz57[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1)), 42) == 0 ? 1 : -1];
268+
char clz56[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1))) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to constant array as an extension}}
269+
char clz57[__builtin_clzg((unsigned __int128)(1 << (BITSIZE(__int128) - 1)), 42) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to constant array as an extension}}
270270
#endif
271271
int clz58 = __builtin_clzg((unsigned _BitInt(128))0); // expected-error {{not a compile-time constant}}
272272
char clz59[__builtin_clzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1];
273273
char clz60[__builtin_clzg((unsigned _BitInt(128))0x1) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
274274
char clz61[__builtin_clzg((unsigned _BitInt(128))0x1, 42) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1];
275275
char clz62[__builtin_clzg((unsigned _BitInt(128))0xf) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1];
276276
char clz63[__builtin_clzg((unsigned _BitInt(128))0xf, 42) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1];
277-
char clz64[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) - 1))) == 0 ? 1 : -1];
278-
char clz65[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) - 1)), 42) == 0 ? 1 : -1];
277+
char clz64[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) - 1))) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to constant array as an extension}}
278+
char clz65[__builtin_clzg((unsigned _BitInt(128))(1 << (BITSIZE(_BitInt(128)) - 1)), 42) == 0 ? 1 : -1]; // expected-warning {{variable length array folded to constant array as an extension}}
279279

280280
char ctz1[__builtin_ctz(1) == 0 ? 1 : -1];
281281
char ctz2[__builtin_ctz(8) == 3 ? 1 : -1];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -Wshift-count-negative %s
2+
// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -Wall %s
3+
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp -Wshift-count-negative %s
4+
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp -Wall %s
5+
6+
enum shiftof {
7+
X = (1<<-29) // c-error {{expression is not an integer constant expression}}
8+
// cpp-error@-1 {{expression is not an integral constant expression}}
9+
// expected-note@-2 {{negative shift count -29}}
10+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wshift-count-overflow %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -Wall %s
3+
4+
enum shiftof {
5+
X = (1<<32) // expected-error {{expression is not an integer constant expression}}
6+
// expected-note@-1 {{shift count 32 >= width of type 'int'}}
7+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -Wshift-negative-value %s
2+
// RUN: %clang_cc1 -x c -fsyntax-only -verify=expected,c -Wall %s
3+
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp -Wshift-negative-value %s
4+
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cpp -Wall %s
5+
6+
enum shiftof {
7+
X = (-1<<29) // c-error {{expression is not an integer constant expression}}
8+
// cpp-error@-1 {{expression is not an integral constant expression}}
9+
// expected-note@-2 {{left shift of negative value -1}}
10+
};

clang/test/Sema/vla-2.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
// a different codepath when we have already emitted an error.)
55

66
int PotentiallyEvaluatedSizeofWarn(int n) {
7-
return (int)sizeof *(0 << 32,(int(*)[n])0); // expected-warning {{left operand of comma operator has no effect}} expected-warning {{shift count >= width of type}}
7+
return (int)sizeof *(0 << 32,(int(*)[n])0); /* expected-warning {{shift count >= width of type}}
8+
expected-warning {{left operand of comma operator has no effect}} */
89
}
910

1011
void PotentiallyEvaluatedTypeofWarn(int n) {
11-
__typeof(*(0 << 32,(int(*)[n])0)) x; // expected-warning {{left operand of comma operator has no effect}} expected-warning {{shift count >= width of type}}
12+
__typeof(*(0 << 32,(int(*)[n])0)) x; /* expected-warning {{shift count >= width of type}}
13+
expected-warning {{left operand of comma operator has no effect}} */
1214
(void)x;
1315
}
1416

clang/test/SemaCXX/enum.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ void PR8089() {
9898

9999
// This is accepted as a GNU extension. In C++98, there was no provision for
100100
// expressions with UB to be non-constant.
101-
enum { overflow = 123456 * 234567 };
101+
enum { overflow = 123456 * 234567 }; /* expected-error {{expression is not an integral constant expression}}
102+
expected-note {{value 28958703552 is outside the range of representable values of type 'int}} */
102103
#if __cplusplus >= 201103L
103104
// expected-warning@-2 {{not an integral constant expression}}
104105
// expected-note@-3 {{value 28958703552 is outside the range of representable values}}
@@ -107,7 +108,8 @@ enum { overflow = 123456 * 234567 };
107108
#endif
108109

109110
// FIXME: This is not consistent with the above case.
110-
enum NoFold : int { overflow2 = 123456 * 234567 };
111+
enum NoFold : int { overflow2 = 123456 * 234567 }; /* expected-error {{expression is not an integral constant expression}}
112+
expected-note {{value 28958703552 is outside the range of representable values of type 'int}} */
111113
#if __cplusplus >= 201103L
112114
// expected-error@-2 {{enumerator value is not a constant expression}}
113115
// expected-note@-3 {{value 28958703552 is outside the range of representable values}}

clang/test/SemaCXX/shift.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void test() {
2222
c = 1 << -1; // expected-warning {{shift count is negative}}
2323
c = 1 >> -1; // expected-warning {{shift count is negative}}
2424
c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}
25-
// expected-warning@-1 {{implicit conversion}}
25+
// cxx17-warning {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}}
2626
c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
2727
c = 1 << c;
2828
c <<= 0;

0 commit comments

Comments
 (0)