10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
20 if (
const auto *Op = dyn_cast_or_null<BinaryOperator>(
E->IgnoreImplicit()))
21 return Op->isComparisonOp();
23 dyn_cast_or_null<CXXOperatorCallExpr>(
E->IgnoreImplicit()))
24 return Op->isComparisonOp();
30 hasBinaryOperatorAChildComparisonOperatorWithoutParen) {
36 hasCppOperatorAChildComparisonOperatorWithoutParen) {
37 return std::any_of(
Node.arg_begin(),
Node.arg_end(),
41struct ChainedComparisonData {
42 llvm::SmallString<256U>
Name;
45 explicit ChainedComparisonData(
const Expr *Op) { extract(Op); }
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);
55void ChainedComparisonData::add(
const Expr *Operand) {
63void ChainedComparisonData::add(llvm::StringRef Opcode) {
68void ChainedComparisonData::extract(
const BinaryOperator *Op) {
69 const Expr *LHS = Op->getLHS()->IgnoreImplicit();
75 add(Op->getOpcodeStr());
77 const Expr *RHS = Op->getRHS()->IgnoreImplicit();
84void ChainedComparisonData::extract(
const CXXOperatorCallExpr *Op) {
85 const Expr *FirstArg = Op->getArg(0U)->IgnoreImplicit();
91 add(getOperatorSpelling(Op->getOperator()));
93 const Expr *SecondArg = Op->getArg(1U)->IgnoreImplicit();
100void ChainedComparisonData::extract(
const Expr *Op) {
104 if (
const auto *BinaryOp = dyn_cast<BinaryOperator>(Op)) {
109 if (
const auto *OverloadedOp = dyn_cast<CXXOperatorCallExpr>(Op)) {
110 if (OverloadedOp->getNumArgs() == 2U)
111 extract(OverloadedOp);
118 const auto OperatorMatcher = expr(anyOf(
119 binaryOperator(isComparisonOperator(),
120 hasBinaryOperatorAChildComparisonOperatorWithoutParen()),
122 isComparisonOperator(),
123 hasCppOperatorAChildComparisonOperatorWithoutParen())));
126 expr(OperatorMatcher, unless(hasParent(OperatorMatcher))).bind(
"op"),
131 const auto *MatchedOperator = Result.Nodes.getNodeAs<Expr>(
"op");
133 ChainedComparisonData Data(MatchedOperator);
134 if (Data.Operands.empty())
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();
143 for (std::size_t Index = 0U; Index < Data.Operands.size(); ++Index) {
144 diag(Data.Operands[Index]->getBeginLoc(),
"operand 'v%0' is here",
146 << Index << Data.Operands[Index]->getSourceRange();
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)