Skip to content

Commit 352d118

Browse files
committed
Restore old unit-tests
1 parent 6e523d8 commit 352d118

File tree

5 files changed

+78
-68
lines changed

5 files changed

+78
-68
lines changed

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,37 @@ static llvm::StringRef translate(llvm::StringRef Value) {
8787
}
8888

8989
static bool isBooleanBitwise(const BinaryOperator *BinOp, ASTContext *AC, std::optional<bool>& rootAssignsToBoolean) {
90+
if (!BinOp)
91+
return false;
92+
9093
for (const auto &[Bitwise, _] : OperatorsTransformation) {
9194
if (BinOp->getOpcodeStr() == Bitwise) {
92-
const bool hasBooleanOperands = llvm::all_of(
93-
std::array{BinOp->getLHS(), BinOp->getRHS()}, [&](const Expr *E) {
94-
return E->IgnoreImpCasts()->getType().getDesugaredType(*AC)->isBooleanType();
95-
});
96-
if (hasBooleanOperands) {
95+
const bool lhsBoolean = BinOp->getLHS()
96+
->IgnoreImpCasts()
97+
->getType()
98+
.getDesugaredType(*AC)
99+
->isBooleanType();
100+
const bool rhsBoolean = BinOp->getRHS()
101+
->IgnoreImpCasts()
102+
->getType()
103+
.getDesugaredType(*AC)
104+
->isBooleanType();
105+
if (lhsBoolean && rhsBoolean) {
97106
rootAssignsToBoolean = rootAssignsToBoolean.value_or(false);
98107
return true;
99108
}
100109
if (assignsToBoolean(BinOp, AC) || rootAssignsToBoolean.value_or(false)) {
101110
rootAssignsToBoolean = rootAssignsToBoolean.value_or(true);
102111
return true;
103112
}
104-
if (BinOp->isCompoundAssignmentOp() && BinOp->getLHS()->IgnoreImpCasts()->getType().getDesugaredType(*AC)->isBooleanType()) {
113+
if (BinOp->isCompoundAssignmentOp() && lhsBoolean) {
105114
rootAssignsToBoolean = rootAssignsToBoolean.value_or(true);
106115
return true;
107116
}
117+
if (std::optional<bool> DummyFlag = false; (lhsBoolean || isBooleanBitwise(dyn_cast<BinaryOperator>(BinOp->getLHS()->IgnoreParenImpCasts()), AC, DummyFlag)) && (rhsBoolean || isBooleanBitwise(dyn_cast<BinaryOperator>(BinOp->getRHS()->IgnoreParenImpCasts()), AC, DummyFlag))) {
118+
rootAssignsToBoolean = rootAssignsToBoolean.value_or(false);
119+
return true;
120+
}
108121
}
109122
}
110123
return false;

clang-tools-extra/test/clang-tidy/checkers/misc/bool-bitwise-operation-nontraditional.cpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ void bad_side_effects_volatile() {
126126
bool a = true;
127127
volatile bool b = false;
128128
bool c = true;
129-
bool r;
130129

131130
a bitor b;
132131
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
@@ -142,15 +141,15 @@ void bad_side_effects_volatile() {
142141
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean variable 'a' instead of bitwise operator '&=' [misc-bool-bitwise-operation]
143142
// CHECK-MESSAGES-NOT: :[[@LINE-2]]:{{.*}}: note: FIX-IT applied suggested code changes
144143

145-
r = (a bitor c) bitor b;
146-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
147-
// CHECK-MESSAGES: :[[@LINE-2]]:21: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
148-
// CHECK-FIXES: r = (a or c) bitor b;
144+
(a bitor c) bitor b;
145+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
146+
// CHECK-MESSAGES: :[[@LINE-2]]:17: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
147+
// CHECK-FIXES: (a or c) bitor b;
149148

150-
r = a bitor c bitor b;
151-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
152-
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
153-
// CHECK-FIXES: r = a or c bitor b;
149+
a bitor c bitor b;
150+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
151+
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
152+
// CHECK-FIXES: a or c bitor b;
154153
}
155154

156155
void bad_with_priors() {
@@ -186,20 +185,20 @@ void bad_with_priors2() {
186185
// CHECK-FIXES: a xor (b and c);
187186

188187
// braces added in the first change
189-
r = a bitor b bitand c;
190-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
191-
// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
192-
// CHECK-FIXES: r = a or (b and c);
188+
a bitor b bitand c;
189+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
190+
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
191+
// CHECK-FIXES: a or (b and c);
193192

194-
r = b bitand c xor a;
195-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
196-
// CHECK-FIXES: r = (b and c) xor a;
193+
b bitand c xor a;
194+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
195+
// CHECK-FIXES: (b and c) xor a;
197196

198197
// braces added in the first change
199-
r = b bitand c bitor a;
200-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
201-
// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
202-
// CHECK-FIXES: r = (b and c) or a;
198+
b bitand c bitor a;
199+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
200+
// CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
201+
// CHECK-FIXES: (b and c) or a;
203202
}
204203

205204
template<typename T>
@@ -224,7 +223,6 @@ void bad_has_ancestor() {
224223

225224
void bad_with_priors_already_braced() {
226225
bool a = false, b = true, c = true;
227-
bool r;
228226
a and (b bitor c);
229227
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
230228
// CHECK-FIXES: a and (b or c);
@@ -242,19 +240,19 @@ void bad_with_priors_already_braced() {
242240
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
243241
// CHECK-FIXES: a xor (b and c);
244242

245-
r = a bitor (b bitand c);
246-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
247-
// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
248-
// CHECK-FIXES: r = a or (b and c);
243+
a bitor (b bitand c);
244+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
245+
// CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
246+
// CHECK-FIXES: a or (b and c);
249247

250248
(b bitand c) xor a;
251249
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
252250
// CHECK-FIXES: (b and c) xor a;
253251

254-
r = (b bitand c) bitor a;
255-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
256-
// CHECK-MESSAGES: :[[@LINE-2]]:22: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
257-
// CHECK-FIXES: r = (b and c) or a;
252+
(b bitand c) bitor a;
253+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
254+
// CHECK-MESSAGES: :[[@LINE-2]]:18: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
255+
// CHECK-FIXES: (b and c) or a;
258256
}
259257

260258
void bad_with_priors_compound() {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
void general(unsigned flags, bool value) {
44
flags |= value;
5+
6+
unsigned mask = 0b1100;
7+
bool result = flags &= mask;
8+
auto result2 = flags &= mask;
9+
result = flags |= flags << 1;
510
}
611

712
void assign_to_boolean(unsigned flags, bool value) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ void general(unsigned flags, bool value) {
99
flags = (flags << 1) | (flags << 2) | (flags << 4) | value;
1010
}
1111

12-
// TODO: make sure parens dont spoil prior for compound operators
13-
// TODO: r = value |= flags << 1; (when r boolean, value unsigned)
14-
1512
void take(bool value) {}
1613

1714
template<bool bb = true | 1>

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

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ void bad_side_effects_volatile() {
126126
bool a = true;
127127
volatile bool b = false;
128128
bool c = true;
129-
bool r;
130129

131130
a | b;
132131
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
@@ -142,15 +141,15 @@ void bad_side_effects_volatile() {
142141
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean variable 'a' instead of bitwise operator '&=' [misc-bool-bitwise-operation]
143142
// CHECK-MESSAGES-NOT: :[[@LINE-2]]:{{.*}}: note: FIX-IT applied suggested code changes
144143

145-
r = (a | c) | b;
146-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
147-
// CHECK-MESSAGES: :[[@LINE-2]]:17: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
148-
// CHECK-FIXES: r = (a || c) | b;
144+
(a | c) | b;
145+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
146+
// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
147+
// CHECK-FIXES: (a || c) | b;
149148

150-
r = a | c | b;
151-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
152-
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
153-
// CHECK-FIXES: r = a || c | b;
149+
a | c | b;
150+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
151+
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
152+
// CHECK-FIXES: a || c | b;
154153
}
155154

156155
void bad_with_priors() {
@@ -180,25 +179,24 @@ void bad_with_priors() {
180179

181180
void bad_with_priors2() {
182181
bool a = false, b = true, c = true;
183-
bool r;
184182
a ^ b & c;
185183
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
186184
// CHECK-FIXES: a ^ (b && c);
187185
// braces added in the first change
188-
r = a | b & c;
189-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
190-
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
191-
// CHECK-FIXES: r = a || (b && c);
186+
a | b & c;
187+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
188+
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
189+
// CHECK-FIXES: a || (b && c);
192190

193-
r = b & c ^ a;
194-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
195-
// CHECK-FIXES: r = (b && c) ^ a;
191+
b & c ^ a;
192+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
193+
// CHECK-FIXES: (b && c) ^ a;
196194

197195
// braces added in the first change
198-
r = b & c | a;
199-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
200-
// CHECK-MESSAGES: :[[@LINE-2]]:15: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
201-
// CHECK-FIXES: r = (b && c) || a;
196+
b & c | a;
197+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
198+
// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
199+
// CHECK-FIXES: (b && c) || a;
202200
}
203201

204202
template<typename T>
@@ -223,7 +221,6 @@ void bad_has_ancestor() {
223221

224222
void bad_with_priors_already_braced() {
225223
bool a = false, b = true, c = true;
226-
bool r;
227224
a && (b | c);
228225
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
229226
// CHECK-FIXES: a && (b || c);
@@ -241,19 +238,19 @@ void bad_with_priors_already_braced() {
241238
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
242239
// CHECK-FIXES: a ^ (b && c);
243240

244-
r = a | (b & c);
245-
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
246-
// CHECK-MESSAGES: :[[@LINE-2]]:16: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
247-
// CHECK-FIXES: r = a || (b && c);
241+
a | (b & c);
242+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
243+
// CHECK-MESSAGES: :[[@LINE-2]]:12: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
244+
// CHECK-FIXES: a || (b && c);
248245

249246
(b & c) ^ a;
250247
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
251248
// CHECK-FIXES: (b && c) ^ a;
252249

253-
r = (b & c) | a;
254-
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
255-
// CHECK-MESSAGES: :[[@LINE-2]]:17: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
256-
// CHECK-FIXES: r = (b && c) || a;
250+
(b & c) | a;
251+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use logical operator '&&' for boolean values instead of bitwise operator '&' [misc-bool-bitwise-operation]
252+
// CHECK-MESSAGES: :[[@LINE-2]]:13: warning: use logical operator '||' for boolean values instead of bitwise operator '|' [misc-bool-bitwise-operation]
253+
// CHECK-FIXES: (b && c) || a;
257254
}
258255

259256
void bad_with_priors_compound() {

0 commit comments

Comments
 (0)