clang-tools 18.0.0git
IncDecInConditionsCheck.cpp
Go to the documentation of this file.
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
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
30 auto OperatorMatcher = expr(
31 anyOf(binaryOperator(anyOf(isComparisonOperator(), isLogicalOperator())),
32 cxxOperatorCallExpr(isComparisonOperator())));
33
34 Finder->addMatcher(
35 expr(
36 OperatorMatcher, unless(isExpansionInSystemHeader()),
37 unless(hasAncestor(OperatorMatcher)), expr().bind("parent"),
38
39 forEachDescendant(
40 expr(anyOf(unaryOperator(isUnaryPrePostOperator(),
41 hasUnaryOperand(expr().bind("operand"))),
42 cxxOperatorCallExpr(
43 isPrePostOperator(),
44 hasUnaryOperand(expr().bind("operand")))),
45 hasAncestor(
46 expr(equalsBoundNode("parent"),
47 hasDescendant(
48 expr(unless(equalsBoundNode("operand")),
49 matchers::isStatementIdenticalToBoundNode(
50 "operand"))
51 .bind("second")))))
52 .bind("operator"))),
53 this);
54}
55
56void IncDecInConditionsCheck::check(const MatchFinder::MatchResult &Result) {
57
58 SourceLocation ExprLoc;
59 bool IsIncrementOp = false;
60
61 if (const auto *MatchedDecl =
62 Result.Nodes.getNodeAs<CXXOperatorCallExpr>("operator")) {
63 ExprLoc = MatchedDecl->getExprLoc();
64 IsIncrementOp = (MatchedDecl->getOperator() == OO_PlusPlus);
65 } else if (const auto *MatchedDecl =
66 Result.Nodes.getNodeAs<UnaryOperator>("operator")) {
67 ExprLoc = MatchedDecl->getExprLoc();
68 IsIncrementOp = MatchedDecl->isIncrementOp();
69 } else
70 return;
71
73 "%select{decrementing|incrementing}0 and referencing a variable in a "
74 "complex condition can cause unintended side-effects due to C++'s order "
75 "of evaluation, consider moving the modification outside of the "
76 "condition to avoid misunderstandings")
77 << IsIncrementOp;
78 diag(Result.Nodes.getNodeAs<Expr>("second")->getExprLoc(),
79 "variable is referenced here", DiagnosticIDs::Note);
80}
81
82} // namespace clang::tidy::bugprone
SourceLocation ExprLoc
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)