10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
14using namespace clang::ast_matchers::internal;
21 IgnorePositiveIntegerLiterals(
22 Options.get(
"IgnorePositiveIntegerLiterals", false)) {}
26 IgnorePositiveIntegerLiterals);
30 const auto SignedIntegerOperand =
31 (IgnorePositiveIntegerLiterals
32 ? expr(ignoringImpCasts(hasType(isSignedInteger())),
33 unless(integerLiteral()))
34 : expr(ignoringImpCasts(hasType(isSignedInteger()))))
35 .bind(
"signed-operand");
41 const auto BitmaskType = namedDecl(
42 hasAnyName(
"::std::locale::category",
"::std::ctype_base::mask",
43 "::std::ios_base::fmtflags",
"::std::ios_base::iostate",
44 "::std::ios_base::openmode"));
45 const auto IsStdBitmask = ignoringImpCasts(declRefExpr(hasType(BitmaskType)));
49 binaryOperator(hasAnyOperatorName(
"^",
"|",
"&",
"^=",
"|=",
"&="),
51 unless(allOf(hasLHS(IsStdBitmask), hasRHS(IsStdBitmask))),
53 hasEitherOperand(SignedIntegerOperand),
54 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
55 .bind(
"binary-no-sign-interference"),
61 binaryOperator(hasAnyOperatorName(
"<<",
">>",
"<<=",
">>="),
62 hasEitherOperand(SignedIntegerOperand),
63 hasLHS(hasType(isInteger())), hasRHS(hasType(isInteger())))
64 .bind(
"binary-sign-interference"),
69 unaryOperator(hasOperatorName(
"~"), hasUnaryOperand(SignedIntegerOperand))
70 .bind(
"unary-signed"),
75 const ast_matchers::BoundNodes &N = Result.Nodes;
76 const auto *SignedOperand = N.getNodeAs<Expr>(
"signed-operand");
77 assert(SignedOperand &&
78 "No signed operand found in problematic bitwise operations");
81 SourceLocation OperatorLoc;
83 if (
const auto *UnaryOp = N.getNodeAs<UnaryOperator>(
"unary-signed")) {
85 OperatorLoc = UnaryOp->getOperatorLoc();
87 if (
const auto *BinaryOp =
88 N.getNodeAs<BinaryOperator>(
"binary-no-sign-interference"))
89 OperatorLoc = BinaryOp->getOperatorLoc();
90 else if (
const auto *BinaryOp =
91 N.getNodeAs<BinaryOperator>(
"binary-sign-interference"))
92 OperatorLoc = BinaryOp->getOperatorLoc();
94 llvm_unreachable(
"unexpected matcher result");
96 diag(SignedOperand->getBeginLoc(),
"use of a signed integer operand with a "
97 "%select{binary|unary}0 bitwise operator")
98 << IsUnary << SignedOperand->getSourceRange() << OperatorLoc;
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
llvm::StringMap< ClangTidyValue > OptionMap