11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
15 using namespace clang::ast_matchers;
22 if (
const auto *LeftRefType = Left->getAs<ReferenceType>())
23 Left = LeftRefType->getPointeeType();
24 if (
const auto *RightRefType = Right->getAs<ReferenceType>())
25 Right = RightRefType->getPointeeType();
26 return Left->getCanonicalTypeUnqualified() ==
27 Right->getCanonicalTypeUnqualified();
30 void InefficientAlgorithmCheck::registerMatchers(MatchFinder *
Finder) {
33 if (!getLangOpts().CPlusPlus)
36 const auto Algorithms =
37 hasAnyName(
"::std::find",
"::std::count",
"::std::equal_range",
38 "::std::lower_bound",
"::std::upper_bound");
39 const auto ContainerMatcher = classTemplateSpecializationDecl(hasAnyName(
40 "::std::set",
"::std::map",
"::std::multiset",
"::std::multimap",
41 "::std::unordered_set",
"::std::unordered_map",
42 "::std::unordered_multiset",
"::std::unordered_multimap"));
46 callee(functionDecl(Algorithms)),
48 0, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
49 callee(cxxMethodDecl(hasName(
"begin"))),
51 hasDeclaration(decl().bind(
"IneffContObj")),
52 anyOf(hasType(ContainerMatcher.bind(
"IneffCont")),
54 ContainerMatcher.bind(
"IneffContPtr")))))
55 .bind(
"IneffContExpr"))))))),
57 1, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
58 callee(cxxMethodDecl(hasName(
"end"))),
60 hasDeclaration(equalsBoundNode(
"IneffContObj"))))))))),
61 hasArgument(2, expr().bind(
"AlgParam")),
62 unless(isInTemplateInstantiation()))
65 Finder->addMatcher(Matcher,
this);
68 void InefficientAlgorithmCheck::check(
const MatchFinder::MatchResult &
Result) {
69 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>(
"IneffAlg");
70 const auto *IneffCont =
71 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffCont");
72 bool PtrToContainer =
false;
75 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffContPtr");
76 PtrToContainer =
true;
78 const llvm::StringRef IneffContName = IneffCont->getName();
79 const bool Unordered =
80 IneffContName.find(
"unordered") != llvm::StringRef::npos;
81 const bool Maplike = IneffContName.find(
"map") != llvm::StringRef::npos;
85 QualType ValueType = AlgCall->getArg(2)->getType();
87 IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType();
91 if (AlgCall->getNumArgs() == 4 && !Unordered) {
92 const Expr *Arg = AlgCall->getArg(3);
93 const QualType AlgCmp =
94 Arg->getType().getUnqualifiedType().getCanonicalType();
95 const unsigned CmpPosition =
96 (IneffContName.find(
"map") == llvm::StringRef::npos) ? 1 : 2;
97 const QualType ContainerCmp = IneffCont->getTemplateArgs()[CmpPosition]
101 if (AlgCmp != ContainerCmp) {
102 diag(Arg->getLocStart(),
103 "different comparers used in the algorithm and the container");
108 const auto *AlgDecl = AlgCall->getDirectCallee();
112 if (Unordered && AlgDecl->getName().find(
"bound") != llvm::StringRef::npos)
115 const auto *AlgParam = Result.Nodes.getNodeAs<Expr>(
"AlgParam");
116 const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>(
"IneffContExpr");
119 SourceManager &
SM = *Result.SourceManager;
120 LangOptions
LangOpts = getLangOpts();
122 CharSourceRange CallRange =
123 CharSourceRange::getTokenRange(AlgCall->getSourceRange());
136 if (SM.isMacroArgExpansion(CallRange.getBegin()) &&
137 SM.isMacroArgExpansion(CallRange.getEnd())) {
138 CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin()));
139 CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd()));
142 if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
143 StringRef ContainerText = Lexer::getSourceText(
144 CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
146 StringRef ParamText = Lexer::getSourceText(
147 CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM,
149 std::string ReplacementText =
150 (llvm::Twine(ContainerText) + (PtrToContainer ?
"->" :
".") +
151 AlgDecl->getName() +
"(" + ParamText +
")")
153 Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
156 diag(AlgCall->getLocStart(),
157 "this STL algorithm call should be replaced with a container method")
static bool areTypesCompatible(QualType Left, QualType Right)
std::unique_ptr< ast_matchers::MatchFinder > Finder