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 std::string Algorithms =
37 "^::std::(find|count|equal_range|lower_bound|upper_bound)$";
38 const auto ContainerMatcher = classTemplateSpecializationDecl(
39 matchesName(
"^::std::(unordered_)?(multi)?(set|map)$"));
42 callee(functionDecl(matchesName(Algorithms))),
44 0, cxxConstructExpr(has(cxxMemberCallExpr(
45 callee(cxxMethodDecl(hasName(
"begin"))),
47 hasDeclaration(decl().bind(
"IneffContObj")),
48 anyOf(hasType(ContainerMatcher.bind(
"IneffCont")),
50 ContainerMatcher.bind(
"IneffContPtr")))))
51 .bind(
"IneffContExpr")))))),
52 hasArgument(1, cxxConstructExpr(has(cxxMemberCallExpr(
53 callee(cxxMethodDecl(hasName(
"end"))),
54 on(declRefExpr(hasDeclaration(
55 equalsBoundNode(
"IneffContObj")))))))),
56 hasArgument(2, expr().bind(
"AlgParam")),
57 unless(isInTemplateInstantiation()))
60 Finder->addMatcher(Matcher,
this);
63 void InefficientAlgorithmCheck::check(
const MatchFinder::MatchResult &
Result) {
64 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>(
"IneffAlg");
65 const auto *IneffCont =
66 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffCont");
67 bool PtrToContainer =
false;
70 Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(
"IneffContPtr");
71 PtrToContainer =
true;
73 const llvm::StringRef IneffContName = IneffCont->getName();
74 const bool Unordered =
75 IneffContName.find(
"unordered") != llvm::StringRef::npos;
76 const bool Maplike = IneffContName.find(
"map") != llvm::StringRef::npos;
80 QualType ValueType = AlgCall->getArg(2)->getType();
82 IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType();
86 if (AlgCall->getNumArgs() == 4 && !Unordered) {
87 const Expr *Arg = AlgCall->getArg(3);
88 const QualType AlgCmp =
89 Arg->getType().getUnqualifiedType().getCanonicalType();
90 const unsigned CmpPosition =
91 (IneffContName.find(
"map") == llvm::StringRef::npos) ? 1 : 2;
92 const QualType ContainerCmp = IneffCont->getTemplateArgs()[CmpPosition]
96 if (AlgCmp != ContainerCmp) {
97 diag(Arg->getLocStart(),
98 "different comparers used in the algorithm and the container");
103 const auto *AlgDecl = AlgCall->getDirectCallee();
107 if (Unordered && AlgDecl->getName().find(
"bound") != llvm::StringRef::npos)
110 const auto *AlgParam = Result.Nodes.getNodeAs<Expr>(
"AlgParam");
111 const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>(
"IneffContExpr");
114 SourceManager &
SM = *Result.SourceManager;
115 LangOptions
LangOpts = Result.Context->getLangOpts();
117 CharSourceRange CallRange =
118 CharSourceRange::getTokenRange(AlgCall->getSourceRange());
131 if (SM.isMacroArgExpansion(CallRange.getBegin()) &&
132 SM.isMacroArgExpansion(CallRange.getEnd())) {
133 CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin()));
134 CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd()));
137 if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
138 StringRef ContainerText = Lexer::getSourceText(
139 CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
141 StringRef ParamText = Lexer::getSourceText(
142 CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM,
144 std::string ReplacementText =
145 (llvm::Twine(ContainerText) + (PtrToContainer ?
"->" :
".") +
146 AlgDecl->getName() +
"(" + ParamText +
")")
148 Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
151 diag(AlgCall->getLocStart(),
152 "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