10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
17 const auto HasContainsMatchingParamType = hasMethod(
18 cxxMethodDecl(isConst(), parameterCountIs(1), returns(booleanType()),
19 hasName(
"contains"), unless(isDeleted()), isPublic(),
20 hasParameter(0, hasType(hasUnqualifiedDesugaredType(
21 equalsBoundNode(
"parameterType"))))));
23 const auto CountCall =
28 hasParameter(0, hasType(hasUnqualifiedDesugaredType(
29 type().bind(
"parameterType")))),
30 ofClass(cxxRecordDecl(HasContainsMatchingParamType)))))
38 hasParameter(0, hasType(hasUnqualifiedDesugaredType(
39 type().bind(
"parameterType")))),
40 ofClass(cxxRecordDecl(HasContainsMatchingParamType)))))
43 const auto EndCall = cxxMemberCallExpr(
46 cxxMethodDecl(hasName(
"end"),
49 ofClass(cxxRecordDecl(HasContainsMatchingParamType)))));
51 const auto Literal0 = integerLiteral(equals(0));
52 const auto Literal1 = integerLiteral(equals(1));
54 auto AddSimpleMatcher = [&](
auto Matcher) {
56 traverse(TK_IgnoreUnlessSpelledInSource, std::move(Matcher)),
this);
60 Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()),
61 hasSourceExpression(CountCall))
62 .bind(
"positiveComparison"),
65 binaryOperation(hasOperatorName(
"!="), hasOperands(CountCall, Literal0))
66 .bind(
"positiveComparison"));
68 binaryOperation(hasLHS(CountCall), hasOperatorName(
">"), hasRHS(Literal0))
69 .bind(
"positiveComparison"));
71 binaryOperation(hasLHS(Literal0), hasOperatorName(
"<"), hasRHS(CountCall))
72 .bind(
"positiveComparison"));
73 AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName(
">="),
75 .bind(
"positiveComparison"));
76 AddSimpleMatcher(binaryOperation(hasLHS(Literal1), hasOperatorName(
"<="),
78 .bind(
"positiveComparison"));
82 binaryOperation(hasOperatorName(
"=="), hasOperands(CountCall, Literal0))
83 .bind(
"negativeComparison"));
84 AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName(
"<="),
86 .bind(
"negativeComparison"));
87 AddSimpleMatcher(binaryOperation(hasLHS(Literal0), hasOperatorName(
">="),
89 .bind(
"negativeComparison"));
91 binaryOperation(hasLHS(CountCall), hasOperatorName(
"<"), hasRHS(Literal1))
92 .bind(
"negativeComparison"));
94 binaryOperation(hasLHS(Literal1), hasOperatorName(
">"), hasRHS(CountCall))
95 .bind(
"negativeComparison"));
99 binaryOperation(hasOperatorName(
"!="), hasOperands(FindCall, EndCall))
100 .bind(
"positiveComparison"));
102 binaryOperation(hasOperatorName(
"=="), hasOperands(FindCall, EndCall))
103 .bind(
"negativeComparison"));
108 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"call");
109 const auto *PositiveComparison =
110 Result.Nodes.getNodeAs<Expr>(
"positiveComparison");
111 const auto *NegativeComparison =
112 Result.Nodes.getNodeAs<Expr>(
"negativeComparison");
113 assert((!PositiveComparison || !NegativeComparison) &&
114 "only one of PositiveComparison or NegativeComparison should be set");
115 bool Negated = NegativeComparison !=
nullptr;
116 const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
120 diag(Call->getExprLoc(),
"use 'contains' to check for membership");
123 SourceLocation FuncCallLoc = Comparison->getEndLoc();
124 if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
128 const auto *Member = cast<MemberExpr>(Call->getCallee());
129 Diag << FixItHint::CreateReplacement(
130 Member->getMemberNameInfo().getSourceRange(),
"contains");
131 SourceLocation ComparisonBegin = Comparison->getSourceRange().getBegin();
132 SourceLocation ComparisonEnd = Comparison->getSourceRange().getEnd();
133 SourceLocation CallBegin = Call->getSourceRange().getBegin();
134 SourceLocation CallEnd = Call->getSourceRange().getEnd();
135 Diag << FixItHint::CreateReplacement(
136 CharSourceRange::getCharRange(ComparisonBegin, CallBegin),
138 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
139 CallEnd.getLocWithOffset(1), ComparisonEnd));
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) final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.