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);
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);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits