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