Skip to content

Commit 8f6b8cb

Browse files
a-tarasyukjph-13
authored andcommitted
[Clang] add -Wshift-bool warning to handle shifting of bool (llvm#127336)
Fixes llvm#28334 --- This PR introduces the `-Wshift-bool` warning to detect and warn against shifting `bool` values using the `>>` operator. Shifting a `bool` implicitly converts it to an `int`, which can lead to unintended behavior.
1 parent a04588e commit 8f6b8cb

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ Improvements to Clang's diagnostics
222222
under the subgroup ``-Wunsafe-buffer-usage-in-libc-call``.
223223
- Diagnostics on chained comparisons (``a < b < c``) are now an error by default. This can be disabled with
224224
``-Wno-error=parentheses``.
225+
- The ``-Wshift-bool`` warning has been added to warn about shifting a boolean. (#GH28334)
225226

226227
- The :doc:`ThreadSafetyAnalysis` now supports ``-Wthread-safety-pointer``,
227228
which enables warning on passing or returning pointers to guarded variables

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7132,6 +7132,9 @@ def warn_shift_result_sets_sign_bit : Warning<
71327132
"signed shift result (%0) sets the sign bit of the shift expression's "
71337133
"type (%1) and becomes negative">,
71347134
InGroup<DiagGroup<"shift-sign-overflow">>, DefaultIgnore;
7135+
def warn_shift_bool : Warning<
7136+
"right shifting a 'bool' implicitly converts it to 'int'">,
7137+
InGroup<DiagGroup<"shift-bool">>, DefaultIgnore;
71357138

71367139
def warn_precedence_bitwise_rel : Warning<
71377140
"%0 has lower precedence than %1; %1 will be evaluated first">,

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11247,6 +11247,10 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
1124711247
if (S.getLangOpts().OpenCL)
1124811248
return;
1124911249

11250+
if (Opc == BO_Shr &&
11251+
LHS.get()->IgnoreParenImpCasts()->getType()->isBooleanType())
11252+
S.Diag(Loc, diag::warn_shift_bool) << LHS.get()->getSourceRange();
11253+
1125011254
// Check right/shifter operand
1125111255
Expr::EvalResult RHSResult;
1125211256
if (RHS.get()->isValueDependent() ||

clang/test/Sema/shift-bool.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -fsyntax-only -Wshift-bool -verify %s
2+
3+
void t() {
4+
int x = 10;
5+
bool y = true;
6+
7+
bool a = y << x;
8+
bool b = y >> x; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
9+
10+
bool c = false << x;
11+
bool d = false >> x; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
12+
13+
bool e = y << 1;
14+
bool f = y >> 1; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
15+
16+
bool g = y << -1; // expected-warning {{shift count is negative}}
17+
bool h = y >> -1; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}} \
18+
// expected-warning {{shift count is negative}}
19+
20+
bool i = y << 0;
21+
bool j = y >> 0; // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
22+
23+
if ((y << 1) != 0) { }
24+
if ((y >> 1) != 0) { } // expected-warning {{right shifting a 'bool' implicitly converts it to 'int'}}
25+
}

0 commit comments

Comments
 (0)