[PATCH] D75107: [clang-tidy] hicpp-signed-bitwise IgnorePositiveIntegerLiterals now partially recursive

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 25 01:28:28 PST 2020


njames93 created this revision.
njames93 added reviewers: aaron.ballman, gribozavr2, alexfh, hokein, JonasToth.
Herald added subscribers: cfe-commits, xazax.hun.
Herald added a project: clang.

Addresses hicpp-signed-bitwise.IgnorePositiveIntegerLiterals should be recursive. <https://bugs.llvm.org/show_bug.cgi?id=44977>
Only recursive for `|`, `^` and `&` operations and their assignment counterparts.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75107

Files:
  clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/hicpp-signed-bitwise-integer-literals.cpp
@@ -21,6 +21,11 @@
   IResult = Int << 1;
   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
   IResult = ~0; //Ok
+
+  IResult = 2 | 4 | 8;
+  IResult = 2 | 4 | 8 | 16;
+  IResult = 2 | 4 | 8 | 16 | -1;
+  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator
 }
 
 enum EnumConstruction {
Index: clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
+++ clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp
@@ -46,15 +46,24 @@
                  "::std::ios_base::openmode"));
   const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
 
+  const auto SignNoInterferenceOp =
+      hasAnyOperatorName("^", "^=", "|", "|=", "&", "&=");
+
+  const auto SignedIntegerOperandExtended =
+      (IgnorePositiveIntegerLiterals
+           ? expr(ignoringImpCasts(hasType(isSignedInteger())),
+                  unless(anyOf(integerLiteral(),
+                               binaryOperator(SignNoInterferenceOp))))
+           : expr(ignoringImpCasts(hasType(isSignedInteger()))))
+          .bind("signed-operand");
+
   // Match binary bitwise operations on signed integer arguments.
   Finder->addMatcher(
-      binaryOperator(anyOf(hasOperatorName("^"), hasOperatorName("|"),
-                           hasOperatorName("&"), hasOperatorName("^="),
-                           hasOperatorName("|="), hasOperatorName("&=")),
+      binaryOperator(SignNoInterferenceOp,
 
                      unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
 
-                     hasEitherOperand(SignedIntegerOperand),
+                     hasEitherOperand(SignedIntegerOperandExtended),
                      hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
           .bind("binary-no-sign-interference"),
       this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75107.246386.patch
Type: text/x-patch
Size: 2342 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200225/c741189b/attachment-0001.bin>


More information about the cfe-commits mailing list