aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/bugprone/IncDecInConditionsCheck.cpp
blob: 73bffe93146e6f2163c6239cb0449cd89ddf7d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===--- IncDecInConditionsCheck.cpp - clang-tidy -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "IncDecInConditionsCheck.h"
#include "../utils/Matchers.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang::tidy::bugprone {

namespace {

AST_MATCHER(BinaryOperator, isLogicalOperator) { return Node.isLogicalOp(); }

AST_MATCHER(UnaryOperator, isUnaryPrePostOperator) {
  return Node.isPrefix() || Node.isPostfix();
}

AST_MATCHER(CXXOperatorCallExpr, isPrePostOperator) {
  return Node.getOperator() == OO_PlusPlus ||
         Node.getOperator() == OO_MinusMinus;
}

} // namespace

void IncDecInConditionsCheck::registerMatchers(MatchFinder *Finder) {
  auto OperatorMatcher = expr(
      anyOf(binaryOperator(anyOf(isComparisonOperator(), isLogicalOperator())),
            cxxOperatorCallExpr(isComparisonOperator())));

  auto IsInUnevaluatedContext =
      expr(anyOf(hasAncestor(expr(matchers::hasUnevaluatedContext())),
                 hasAncestor(typeLoc())));

  Finder->addMatcher(
      expr(
          OperatorMatcher, unless(isExpansionInSystemHeader()),
          unless(hasAncestor(OperatorMatcher)), expr().bind("parent"),

          forEachDescendant(
              expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
                                       hasUnaryOperand(expr().bind("operand"))),
                         cxxOperatorCallExpr(
                             isPrePostOperator(),
                             hasUnaryOperand(expr().bind("operand")))),
                   unless(IsInUnevaluatedContext),
                   hasAncestor(
                       expr(equalsBoundNode("parent"),
                            hasDescendant(
                                expr(unless(equalsBoundNode("operand")),
                                     matchers::isStatementIdenticalToBoundNode(
                                         "operand"),
                                     unless(IsInUnevaluatedContext))
                                    .bind("second")))))
                  .bind("operator"))),
      this);
}

void IncDecInConditionsCheck::check(const MatchFinder::MatchResult &Result) {

  SourceLocation ExprLoc;
  bool IsIncrementOp = false;

  if (const auto *MatchedDecl =
          Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator")) {
    ExprLoc = MatchedDecl->getExprLoc();
    IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
  } else if (const auto *MatchedDecl =
                 Result.Nodes.getNodeAs<UnaryOperator>("operator")) {
    ExprLoc = MatchedDecl->getExprLoc();
    IsIncrementOp = MatchedDecl->isIncrementOp();
  } else
    return;

  diag(ExprLoc,
       "%select{decrementing|incrementing}0 and referencing a variable in a "
       "complex condition can cause unintended side-effects due to C++'s order "
       "of evaluation, consider moving the modification outside of the "
       "condition to avoid misunderstandings")
      << IsIncrementOp;
  diag(Result.Nodes.getNodeAs<Expr>("second")->getExprLoc(),
       "variable is referenced here", DiagnosticIDs::Note);
}

} // namespace clang::tidy::bugprone