clang-tools 20.0.0git
ChainedComparisonCheck.cpp
Go to the documentation of this file.
1//===--- ChainedComparisonCheck.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 "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
14#include <algorithm>
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::bugprone {
19static bool isExprAComparisonOperator(const Expr *E) {
20 if (const auto *Op = dyn_cast_or_null<BinaryOperator>(E->IgnoreImplicit()))
21 return Op->isComparisonOp();
22 if (const auto *Op =
23 dyn_cast_or_null<CXXOperatorCallExpr>(E->IgnoreImplicit()))
24 return Op->isComparisonOp();
25 return false;
26}
27
28namespace {
29AST_MATCHER(BinaryOperator,
30 hasBinaryOperatorAChildComparisonOperatorWithoutParen) {
31 return isExprAComparisonOperator(Node.getLHS()) ||
33}
34
35AST_MATCHER(CXXOperatorCallExpr,
36 hasCppOperatorAChildComparisonOperatorWithoutParen) {
37 return std::any_of(Node.arg_begin(), Node.arg_end(),
39}
40
41struct ChainedComparisonData {
42 llvm::SmallString<256U> Name;
43 llvm::SmallVector<const Expr *, 32U> Operands;
44
45 explicit ChainedComparisonData(const Expr *Op) { extract(Op); }
46
47private:
48 void add(const Expr *Operand);
49 void add(llvm::StringRef Opcode);
50 void extract(const Expr *Op);
51 void extract(const BinaryOperator *Op);
52 void extract(const CXXOperatorCallExpr *Op);
53};
54
55void ChainedComparisonData::add(const Expr *Operand) {
56 if (!Name.empty())
57 Name += ' ';
58 Name += 'v';
59 Name += std::to_string(Operands.size());
60 Operands.push_back(Operand);
61}
62
63void ChainedComparisonData::add(llvm::StringRef Opcode) {
64 Name += ' ';
65 Name += Opcode;
66}
67
68void ChainedComparisonData::extract(const BinaryOperator *Op) {
69 const Expr *LHS = Op->getLHS()->IgnoreImplicit();
71 extract(LHS);
72 else
73 add(LHS);
74
75 add(Op->getOpcodeStr());
76
77 const Expr *RHS = Op->getRHS()->IgnoreImplicit();
79 extract(RHS);
80 else
81 add(RHS);
82}
83
84void ChainedComparisonData::extract(const CXXOperatorCallExpr *Op) {
85 const Expr *FirstArg = Op->getArg(0U)->IgnoreImplicit();
86 if (isExprAComparisonOperator(FirstArg))
87 extract(FirstArg);
88 else
89 add(FirstArg);
90
91 add(getOperatorSpelling(Op->getOperator()));
92
93 const Expr *SecondArg = Op->getArg(1U)->IgnoreImplicit();
94 if (isExprAComparisonOperator(SecondArg))
95 extract(SecondArg);
96 else
97 add(SecondArg);
98}
99
100void ChainedComparisonData::extract(const Expr *Op) {
101 if (!Op)
102 return;
103
104 if (const auto *BinaryOp = dyn_cast<BinaryOperator>(Op)) {
105 extract(BinaryOp);
106 return;
107 }
108
109 if (const auto *OverloadedOp = dyn_cast<CXXOperatorCallExpr>(Op)) {
110 if (OverloadedOp->getNumArgs() == 2U)
111 extract(OverloadedOp);
112 }
113}
114
115} // namespace
116
118 const auto OperatorMatcher = expr(anyOf(
119 binaryOperator(isComparisonOperator(),
120 hasBinaryOperatorAChildComparisonOperatorWithoutParen()),
121 cxxOperatorCallExpr(
122 isComparisonOperator(),
123 hasCppOperatorAChildComparisonOperatorWithoutParen())));
124
125 Finder->addMatcher(
126 expr(OperatorMatcher, unless(hasParent(OperatorMatcher))).bind("op"),
127 this);
128}
129
130void ChainedComparisonCheck::check(const MatchFinder::MatchResult &Result) {
131 const auto *MatchedOperator = Result.Nodes.getNodeAs<Expr>("op");
132
133 ChainedComparisonData Data(MatchedOperator);
134 if (Data.Operands.empty())
135 return;
136
137 diag(MatchedOperator->getBeginLoc(),
138 "chained comparison '%0' may generate unintended results, use "
139 "parentheses to specify order of evaluation or a logical operator to "
140 "separate comparison expressions")
141 << llvm::StringRef(Data.Name).trim() << MatchedOperator->getSourceRange();
142
143 for (std::size_t Index = 0U; Index < Data.Operands.size(); ++Index) {
144 diag(Data.Operands[Index]->getBeginLoc(), "operand 'v%0' is here",
145 DiagnosticIDs::Note)
146 << Index << Data.Operands[Index]->getSourceRange();
147 }
148}
149
150} // namespace clang::tidy::bugprone
const Expr * E
llvm::SmallVector< const Expr *, 32U > Operands
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static bool isExprAComparisonOperator(const Expr *E)
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)