11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 21 void SignedBitwiseCheck::registerMatchers(MatchFinder *Finder) {
22 const auto SignedIntegerOperand =
23 expr(ignoringImpCasts(hasType(isSignedInteger()))).bind(
"signed-operand");
29 const auto BitmaskType = namedDecl(
30 hasAnyName(
"::std::locale::category",
"::std::ctype_base::mask",
31 "::std::ios_base::fmtflags",
"::std::ios_base::iostate",
32 "::std::ios_base::openmode"));
33 const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
37 binaryOperator(anyOf(hasOperatorName(
"^"), hasOperatorName(
"|"),
38 hasOperatorName(
"&"), hasOperatorName(
"^="),
39 hasOperatorName(
"|="), hasOperatorName(
"&=")),
41 unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
43 hasEitherOperand(SignedIntegerOperand),
44 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
45 .bind(
"binary-no-sign-interference"),
51 binaryOperator(anyOf(hasOperatorName(
"<<"), hasOperatorName(
">>"),
52 hasOperatorName(
"<<="), hasOperatorName(
">>=")),
53 hasEitherOperand(SignedIntegerOperand),
54 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
55 .bind(
"binary-sign-interference"),
60 unaryOperator(hasOperatorName(
"~"), hasUnaryOperand(SignedIntegerOperand))
61 .bind(
"unary-signed"),
65 void SignedBitwiseCheck::check(
const MatchFinder::MatchResult &
Result) {
66 const ast_matchers::BoundNodes &N = Result.Nodes;
67 const auto *SignedOperand = N.getNodeAs<Expr>(
"signed-operand");
68 assert(SignedOperand &&
69 "No signed operand found in problematic bitwise operations");
74 if (
const auto *UnaryOp = N.getNodeAs<UnaryOperator>(
"unary-signed")) {
76 Location = UnaryOp->getBeginLoc();
78 if (
const auto *BinaryOp =
79 N.getNodeAs<BinaryOperator>(
"binary-no-sign-interference"))
80 Location = BinaryOp->getBeginLoc();
81 else if (
const auto *BinaryOp =
82 N.getNodeAs<BinaryOperator>(
"binary-sign-interference"))
83 Location = BinaryOp->getBeginLoc();
85 llvm_unreachable(
"unexpected matcher result");
87 diag(Location,
"use of a signed integer operand with a " 88 "%select{binary|unary}0 bitwise operator")
89 << IsUnary << SignedOperand->getSourceRange();
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//