[clang-tidy] hicpp-signed-bitwise - Return location of the operand (and not of the operator beginning)

Currently, the "hicpp/signed-bitwise" check returns the beginning of the binary/unary operator as location, which sometimes confuses users in the IDE due to incorrect [[ https://youtrack.jetbrains.com/issue/CPP-12445/Clang-Tidy-highlighting-for-binary-operators-applied-to-wrong-operand | highlighting ]].
Yes, the offset from Ranges can be used for this particular check, but i suppose better solution is to return begin location of the problematic operand instead of operator.

Reviewed By: njames93

Differential Revision: https://reviews.llvm.org/D131678
This commit is contained in:
Vladimir Plyashkun 2022-08-17 19:25:48 +01:00 committed by Nathan James
parent a5b11f40f8
commit fa8f861602
No known key found for this signature in database
GPG Key ID: CC007AFCDA90AA5F
3 changed files with 29 additions and 29 deletions

View File

@ -80,24 +80,24 @@ void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) {
"No signed operand found in problematic bitwise operations");
bool IsUnary = false;
SourceLocation Location;
SourceLocation OperatorLoc;
if (const auto *UnaryOp = N.getNodeAs<UnaryOperator>("unary-signed")) {
IsUnary = true;
Location = UnaryOp->getBeginLoc();
OperatorLoc = UnaryOp->getOperatorLoc();
} else {
if (const auto *BinaryOp =
N.getNodeAs<BinaryOperator>("binary-no-sign-interference"))
Location = BinaryOp->getBeginLoc();
OperatorLoc = BinaryOp->getOperatorLoc();
else if (const auto *BinaryOp =
N.getNodeAs<BinaryOperator>("binary-sign-interference"))
Location = BinaryOp->getBeginLoc();
OperatorLoc = BinaryOp->getOperatorLoc();
else
llvm_unreachable("unexpected matcher result");
}
diag(Location, "use of a signed integer operand with a "
"%select{binary|unary}0 bitwise operator")
<< IsUnary << SignedOperand->getSourceRange();
diag(SignedOperand->getBeginLoc(), "use of a signed integer operand with a "
"%select{binary|unary}0 bitwise operator")
<< IsUnary << SignedOperand->getSourceRange() << OperatorLoc;
}
} // namespace hicpp

View File

@ -8,14 +8,14 @@ void examples() {
URes = UValue & 1u; //Ok
URes = UValue & -1;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use of a signed integer operand with a binary bitwise operator
unsigned URes2 = URes << 1; //Ok
int IResult;
IResult = 10 & 2; //Ok
IResult = 3 << -1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use of a signed integer operand with a binary bitwise operator
int Int = 30;
IResult = Int << 1;

View File

@ -42,7 +42,7 @@ void binary_bitwise() {
UResult = SValue & -1;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UResult&= 1;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UResult = UValue & 1u; // Ok
UResult = UValue & UValue; // Ok
@ -63,43 +63,43 @@ void binary_bitwise() {
// More complex expressions.
UResult = UValue & (SByte1 + (SByte1 | SByte2));
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: use of a signed integer operand with a binary bitwise operator
// The rest is to demonstrate functionality but all operators are matched equally.
// Therefore functionality is the same for all binary operations.
UByte1 = UByte1 | UByte2; // Ok
UByte1 = UByte1 | SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use of a signed integer operand with a binary bitwise operator
UByte1|= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1|= UByte2; // Ok
UByte1 = UByte1 ^ UByte2; // Ok
UByte1 = UByte1 ^ SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use of a signed integer operand with a binary bitwise operator
UByte1^= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
UByte1^= UByte2; // Ok
UByte1 = UByte1 >> UByte2; // Ok
UByte1 = UByte1 >> SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator
UByte1>>= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UByte1>>= UByte2; // Ok
UByte1 = UByte1 << UByte2; // Ok
UByte1 = UByte1 << SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use of a signed integer operand with a binary bitwise operator
UByte1<<= SByte2;
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
UByte1<<= UByte2; // Ok
int SignedInt1 = 1 << 12;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator
int SignedInt2 = 1u << 12;
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use of a signed integer operand with a binary bitwise operator
}
void f1(unsigned char c) {}
@ -113,28 +113,28 @@ void unary_bitwise() {
UByte1 = ~UByte1; // Ok
SByte1 = ~UByte1;
SByte1 = ~SByte1;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a unary bitwise operator
UByte1 = ~SByte1;
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a unary bitwise operator
unsigned int UInt = 0u;
int SInt = 0;
f1(~UByte1); // Ok
f1(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
f1(~UInt);
f1(~SInt);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
f2(~UByte1);
f2(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
f2(~UInt);
f2(~SInt);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
f3(~UByte1); // Ok
f3(~SByte1);
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
}
/// HICPP uses these examples to demonstrate the rule.
@ -158,7 +158,7 @@ void standard_examples() {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a binary bitwise operator
r = ~0;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use of a signed integer operand with a unary bitwise operator
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use of a signed integer operand with a unary bitwise operator
r = ~0u; // Ok
k = ~k; // Ok
@ -236,5 +236,5 @@ enum EnumConstruction {
test2 = one << two,
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
test3 = 1u << 12,
// CHECK-MESSAGES: [[@LINE-1]]:11: warning: use of a signed integer operand with a binary bitwise operator
// CHECK-MESSAGES: [[@LINE-1]]:17: warning: use of a signed integer operand with a binary bitwise operator
};