clang-tools 22.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 Finder->addMatcher(
43 expr(
44 OperatorMatcher, unless(isExpansionInSystemHeader()),
45 unless(hasAncestor(OperatorMatcher)), expr().bind("parent"),
46
47 forEachDescendant(
48 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
49 hasUnaryOperand(expr().bind("operand"))),
50 cxxOperatorCallExpr(
51 isPrePostOperator(),
52 hasUnaryOperand(expr().bind("operand")))),
53 unless(IsInUnevaluatedContext),
54 hasAncestor(
55 expr(equalsBoundNode("parent"),
56 hasDescendant(
57 expr(unless(equalsBoundNode("operand")),
58 matchers::isStatementIdenticalToBoundNode(
59 "operand"),
60 unless(IsInUnevaluatedContext))
61 .bind("second")))))
62 .bind("operator"))),
63 this);
64}
65
66void 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>("operator")) {
73 ExprLoc = MatchedDecl->getExprLoc();
74 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
75 } else if (const auto *MatchedDecl =
76 Result.Nodes.getNodeAs<UnaryOperator>("operator")) {
77 ExprLoc = MatchedDecl->getExprLoc();
78 IsIncrementOp = MatchedDecl->isIncrementOp();
79 } else
80 return;
81
82 diag(ExprLoc,
83 "%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(Result.Nodes.getNodeAs<Expr>("second")->getExprLoc(),
89 "variable is referenced here", DiagnosticIDs::Note);
90}
91
92} // namespace clang::tidy::bugprone
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_MATCHER(BinaryOperator, isRelationalOperator)