Skip to content

Commit 793dc97

Browse files
committed
Implement compound operator case with unsigned flags
1 parent d7ae305 commit 793dc97

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

clang-tools-extra/clang-tidy/misc/BoolBitwiseOperationCheck.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <optional>
1414
#include <utility>
1515

16+
// TODO: change warning message
17+
1618
using namespace clang::ast_matchers;
1719

1820
namespace clang::tidy::misc {
@@ -99,6 +101,10 @@ static bool isBooleanBitwise(const BinaryOperator *BinOp, ASTContext *AC, std::o
99101
rootAssignsToBoolean = rootAssignsToBoolean.value_or(true);
100102
return true;
101103
}
104+
if (BinOp->isCompoundAssignmentOp() && BinOp->getLHS()->IgnoreImpCasts()->getType().getTypePtr()->isBooleanType()) {
105+
rootAssignsToBoolean = rootAssignsToBoolean.value_or(true);
106+
return true;
107+
}
102108
}
103109
}
104110
return false;
@@ -232,6 +238,7 @@ void BoolBitwiseOperationCheck::visitBinaryTreesNode(
232238
const BinaryOperator *BinOp, const BinaryOperator *ParentBinOp,
233239
const clang::SourceManager &SM, clang::ASTContext &Ctx,
234240
std::optional<bool>& rootAssignsToBoolean) {
241+
//llvm::outs() << "ENTER " << rootAssignsToBoolean << "\n";
235242
if (!BinOp)
236243
return;
237244

@@ -244,6 +251,8 @@ void BoolBitwiseOperationCheck::visitBinaryTreesNode(
244251
visitBinaryTreesNode(
245252
dyn_cast<BinaryOperator>(BinOp->getRHS()->IgnoreParenImpCasts()), BinOp,
246253
SM, Ctx, rootAssignsToBoolean);
254+
255+
//llvm::outs() << "LEAVE\n";
247256
}
248257

249258
void BoolBitwiseOperationCheck::check(const MatchFinder::MatchResult &Result) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %check_clang_tidy %s misc-bool-bitwise-operation %t
2+
3+
void general(unsigned flags, bool value) {
4+
flags |= value;
5+
}
6+
7+
void assign_to_boolean(unsigned flags, bool value) {
8+
value |= flags << 1;
9+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean variable 'value' instead of bitwise operator '|=' [misc-bool-bitwise-operation]
10+
// CHECK-FIXES: value = value || flags << 1;
11+
value |= (flags << 1) | (flags << 2);
12+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean variable 'value' instead of bitwise operator '|=' [misc-bool-bitwise-operation]
13+
// CHECK-MESSAGES: :[[@LINE-2]]:27: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
14+
// CHECK-FIXES: value = value || (flags << 1) || (flags << 2);
15+
}

clang-tools-extra/test/clang-tidy/checkers/misc/bool-bitwise-operation-not-only-bool-operand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %check_clang_tidy %s misc-bool-bitwise-operation %t
22

3-
// got from clang/lib/APINotes/APINotesWriter.cpp
3+
// The case is taken from the real code in clang/lib/APINotes/APINotesWriter.cpp
44

55
void general(unsigned flags, bool value) {
66
(flags << 1) | value;
@@ -9,8 +9,8 @@ void general(unsigned flags, bool value) {
99
flags = (flags << 1) | (flags << 2) | (flags << 4) | value;
1010
}
1111

12-
// TODO: compound operators
1312
// TODO: make sure parens dont spoil prior for compound operators
13+
// TODO: r = value |= flags << 1; (when r boolean, value unsigned)
1414

1515
void take(bool value) {}
1616

0 commit comments

Comments
 (0)