clang-tools 23.0.0git
IncDecInConditionsCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
18namespace {
19
20AST_MATCHER(BinaryOperator, isLogicalOperator) { return Node.isLogicalOp(); }
21
22AST_MATCHER(UnaryOperator, isUnaryPrePostOperator) {
23 return Node.isPrefix() || Node.isPostfix();
24}
25
26AST_MATCHER(CXXOperatorCallExpr, isPrePostOperator) {
27 return Node.getOperator() == OO_PlusPlus ||
28 Node.getOperator() == OO_MinusMinus;
29}
30
31} // namespace
32
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 auto IsInLambda =
43 hasAncestor(lambdaExpr(hasAncestor(expr(equalsBoundNode("parent")))));
44
45 Finder->addMatcher(
46 expr(
47 OperatorMatcher, unless(hasAncestor(OperatorMatcher)),
48 expr().bind("parent"),
49
50 forEachDescendant(
51 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
52 hasUnaryOperand(expr().bind("operand"))),
53 cxxOperatorCallExpr(
54 isPrePostOperator(),
55 hasUnaryOperand(expr().bind("operand")))),
56 unless(IsInUnevaluatedContext), unless(IsInLambda),
57 hasAncestor(
58 expr(equalsBoundNode("parent"),
59 hasDescendant(
60 expr(unless(equalsBoundNode("operand")),
61 matchers::isStatementIdenticalToBoundNode(
62 "operand"),
63 unless(IsInUnevaluatedContext))
64 .bind("second")))))
65 .bind("operator"))),
66 this);
67}
68
69void IncDecInConditionsCheck::check(const MatchFinder::MatchResult &Result) {
70 SourceLocation ExprLoc;
71 bool IsIncrementOp = false;
72
73 if (const auto *MatchedDecl =
74 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator")) {
75 ExprLoc = MatchedDecl->getExprLoc();
76 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
77 } else if (const auto *MatchedDecl =
78 Result.Nodes.getNodeAs<UnaryOperator>("operator")) {
79 ExprLoc = MatchedDecl->getExprLoc();
80 IsIncrementOp = MatchedDecl->isIncrementOp();
81 } else {
82 return;
83 }
84
85 diag(ExprLoc,
86 "%select{decrementing|incrementing}0 and referencing a variable in a "
87 "complex condition can cause unintended side-effects due to C++'s order "
88 "of evaluation, consider moving the modification outside of the "
89 "condition to avoid misunderstandings")
90 << IsIncrementOp;
91 diag(Result.Nodes.getNodeAs<Expr>("second")->getExprLoc(),
92 "variable is referenced here", DiagnosticIDs::Note);
93}
94
95} // namespace clang::tidy::bugprone
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)