clang-tools 20.0.0git
IncDecInConditionsCheck.cpp
Go to the documentation of this file.
1//===--- IncDecInConditionsCheck.cpp - clang-tidy -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/Matchers.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::bugprone {
17
18AST_MATCHER(BinaryOperator, isLogicalOperator) { return Node.isLogicalOp(); }
19
20AST_MATCHER(UnaryOperator, isUnaryPrePostOperator) {
21 return Node.isPrefix() || Node.isPostfix();
22}
23
24AST_MATCHER(CXXOperatorCallExpr, isPrePostOperator) {
25 return Node.getOperator() == OO_PlusPlus ||
26 Node.getOperator() == OO_MinusMinus;
27}
28
30 auto OperatorMatcher = expr(
31 anyOf(binaryOperator(anyOf(isComparisonOperator(), isLogicalOperator())),
32 cxxOperatorCallExpr(isComparisonOperator())));
33
34 auto IsInUnevaluatedContext =
35 expr(anyOf(hasAncestor(expr(matchers::hasUnevaluatedContext())),
36 hasAncestor(typeLoc())));
37
38 Finder->addMatcher(
39 expr(
40 OperatorMatcher, unless(isExpansionInSystemHeader()),
41 unless(hasAncestor(OperatorMatcher)), expr().bind("parent"),
42
43 forEachDescendant(
44 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
45 hasUnaryOperand(expr().bind("operand"))),
46 cxxOperatorCallExpr(
47 isPrePostOperator(),
48 hasUnaryOperand(expr().bind("operand")))),
49 unless(IsInUnevaluatedContext),
50 hasAncestor(
51 expr(equalsBoundNode("parent"),
52 hasDescendant(
53 expr(unless(equalsBoundNode("operand")),
54 matchers::isStatementIdenticalToBoundNode(
55 "operand"),
56 unless(IsInUnevaluatedContext))
57 .bind("second")))))
58 .bind("operator"))),
59 this);
60}
61
62void IncDecInConditionsCheck::check(const MatchFinder::MatchResult &Result) {
63
64 SourceLocation ExprLoc;
65 bool IsIncrementOp = false;
66
67 if (const auto *MatchedDecl =
68 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator")) {
69 ExprLoc = MatchedDecl->getExprLoc();
70 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
71 } else if (const auto *MatchedDecl =
72 Result.Nodes.getNodeAs<UnaryOperator>("operator")) {
73 ExprLoc = MatchedDecl->getExprLoc();
74 IsIncrementOp = MatchedDecl->isIncrementOp();
75 } else
76 return;
77
79 "%select{decrementing|incrementing}0 and referencing a variable in a "
80 "complex condition can cause unintended side-effects due to C++'s order "
81 "of evaluation, consider moving the modification outside of the "
82 "condition to avoid misunderstandings")
83 << IsIncrementOp;
84 diag(Result.Nodes.getNodeAs<Expr>("second")->getExprLoc(),
85 "variable is referenced here", DiagnosticIDs::Note);
86}
87
88} // namespace clang::tidy::bugprone
SourceLocation ExprLoc
::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)