31 const auto SignedIntegerOperand =
32 (IgnorePositiveIntegerLiterals
33 ? expr(ignoringImpCasts(
34 allOf(hasType(isSignedInteger()), unless(integerLiteral()))))
35 : expr(ignoringImpCasts(hasType(isSignedInteger()))))
36 .bind(
"signed-operand");
42 const auto BitmaskType = namedDecl(
43 hasAnyName(
"::std::locale::category",
"::std::ctype_base::mask",
44 "::std::ios_base::fmtflags",
"::std::ios_base::iostate",
45 "::std::ios_base::openmode"));
46 const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
50 binaryOperator(hasAnyOperatorName(
"^",
"|",
"&",
"^=",
"|=",
"&="),
52 unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
54 hasEitherOperand(SignedIntegerOperand),
55 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
56 .bind(
"binary-no-sign-interference"),
62 binaryOperator(hasAnyOperatorName(
"<<",
">>",
"<<=",
">>="),
63 hasEitherOperand(SignedIntegerOperand),
64 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
65 .bind(
"binary-sign-interference"),
70 unaryOperator(hasOperatorName(
"~"), hasUnaryOperand(SignedIntegerOperand))
71 .bind(
"unary-signed"),
76 const ast_matchers::BoundNodes &N = Result.Nodes;
77 const auto *SignedOperand = N.getNodeAs<Expr>(
"signed-operand");
78 assert(SignedOperand &&
79 "No signed operand found in problematic bitwise operations");
82 SourceLocation OperatorLoc;
84 if (
const auto *UnaryOp = N.getNodeAs<UnaryOperator>(
"unary-signed")) {
86 OperatorLoc = UnaryOp->getOperatorLoc();
88 if (
const auto *BinaryOp =
89 N.getNodeAs<BinaryOperator>(
"binary-no-sign-interference"))
90 OperatorLoc = BinaryOp->getOperatorLoc();
91 else if (
const auto *BinaryOp =
92 N.getNodeAs<BinaryOperator>(
"binary-sign-interference"))
93 OperatorLoc = BinaryOp->getOperatorLoc();
95 llvm_unreachable(
"unexpected matcher result");
97 diag(SignedOperand->getBeginLoc(),
"use of a signed integer operand with a "
98 "%select{binary|unary}0 bitwise operator")
99 << IsUnary << SignedOperand->getSourceRange() << OperatorLoc;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.