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
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
29void IncDecInConditionsCheck::registerMatchers(MatchFinder *Finder) {
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 NodeMatch: expr(
40 OperatorMatcher, unless(isExpansionInSystemHeader()),
41 unless(hasAncestor(OperatorMatcher)), expr().bind(ID: "parent"),
42
43 forEachDescendant(
44 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
45 hasUnaryOperand(InnerMatcher: expr().bind(ID: "operand"))),
46 cxxOperatorCallExpr(
47 isPrePostOperator(),
48 hasUnaryOperand(InnerMatcher: expr().bind(ID: "operand")))),
49 unless(IsInUnevaluatedContext),
50 hasAncestor(
51 expr(equalsBoundNode(ID: "parent"),
52 hasDescendant(
53 expr(unless(equalsBoundNode(ID: "operand")),
54 matchers::isStatementIdenticalToBoundNode(
55 ID: "operand"),
56 unless(IsInUnevaluatedContext))
57 .bind(ID: "second")))))
58 .bind(ID: "operator"))),
59 Action: 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>(ID: "operator")) {
69 ExprLoc = MatchedDecl->getExprLoc();
70 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
71 } else if (const auto *MatchedDecl =
72 Result.Nodes.getNodeAs<UnaryOperator>(ID: "operator")) {
73 ExprLoc = MatchedDecl->getExprLoc();
74 IsIncrementOp = MatchedDecl->isIncrementOp();
75 } else
76 return;
77
78 diag(Loc: ExprLoc,
79 Description: "%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(Loc: Result.Nodes.getNodeAs<Expr>(ID: "second")->getExprLoc(),
85 Description: "variable is referenced here", Level: DiagnosticIDs::Note);
86}
87
88} // namespace clang::tidy::bugprone
89

source code of clang-tools-extra/clang-tidy/bugprone/IncDecInConditionsCheck.cpp