[clang-tools-extra] fa8f861 - [clang-tidy] hicpp-signed-bitwise - Return location of the operand (and not of the operator beginning)
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 17 11:26:09 PDT 2022
Author: Vladimir Plyashkun
Date: 2022-08-17T19:25:59+01:00
New Revision: fa8f8616028abbc10aa5a3ad4641e8a40ec20cc8
URL: https://github.com/llvm/llvm-project/commit/fa8f8616028abbc10aa5a3ad4641e8a40ec20cc8
DIFF: https://github.com/llvm/llvm-project/commit/fa8f8616028abbc10aa5a3ad4641e8a40ec20cc8.diff
LOG: [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
Added:
Modified:
clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp b/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
index 415babe9890d6..35631bdbf3614 100644
--- a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
+++ b/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
@@ -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
diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp b/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
index 56237d8b70cc4..c963157956589 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp
@@ -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;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp b/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp
index 58cc911d108ea..bea9366ea5a30 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp
@@ -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
};
More information about the cfe-commits
mailing list