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