10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
21 return Node.isPrefix() ||
Node.isPostfix();
25 return Node.getOperator() == OO_PlusPlus ||
26 Node.getOperator() == OO_MinusMinus;
30 auto OperatorMatcher = expr(
31 anyOf(binaryOperator(anyOf(isComparisonOperator(), isLogicalOperator())),
32 cxxOperatorCallExpr(isComparisonOperator())));
36 OperatorMatcher, unless(isExpansionInSystemHeader()),
37 unless(hasAncestor(OperatorMatcher)), expr().bind(
"parent"),
40 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
41 hasUnaryOperand(expr().bind(
"operand"))),
44 hasUnaryOperand(expr().bind(
"operand")))),
46 expr(equalsBoundNode(
"parent"),
48 expr(unless(equalsBoundNode(
"operand")),
49 matchers::isStatementIdenticalToBoundNode(
59 bool IsIncrementOp =
false;
61 if (
const auto *MatchedDecl =
62 Result.Nodes.getNodeAs<CXXOperatorCallExpr>(
"operator")) {
63 ExprLoc = MatchedDecl->getExprLoc();
64 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
65 }
else if (
const auto *MatchedDecl =
66 Result.Nodes.getNodeAs<UnaryOperator>(
"operator")) {
67 ExprLoc = MatchedDecl->getExprLoc();
68 IsIncrementOp = MatchedDecl->isIncrementOp();
73 "%select{decrementing|incrementing}0 and referencing a variable in a "
74 "complex condition can cause unintended side-effects due to C++'s order "
75 "of evaluation, consider moving the modification outside of the "
76 "condition to avoid misunderstandings")
78 diag(Result.Nodes.getNodeAs<Expr>(
"second")->getExprLoc(),
79 "variable is referenced here", DiagnosticIDs::Note);
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)