• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- InefficientAlgorithmCheck.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 
9 #include "InefficientAlgorithmCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace performance {
19 
areTypesCompatible(QualType Left,QualType Right)20 static bool areTypesCompatible(QualType Left, QualType Right) {
21   if (const auto *LeftRefType = Left->getAs<ReferenceType>())
22     Left = LeftRefType->getPointeeType();
23   if (const auto *RightRefType = Right->getAs<ReferenceType>())
24     Right = RightRefType->getPointeeType();
25   return Left->getCanonicalTypeUnqualified() ==
26          Right->getCanonicalTypeUnqualified();
27 }
28 
registerMatchers(MatchFinder * Finder)29 void InefficientAlgorithmCheck::registerMatchers(MatchFinder *Finder) {
30   const auto Algorithms =
31       hasAnyName("::std::find", "::std::count", "::std::equal_range",
32                  "::std::lower_bound", "::std::upper_bound");
33   const auto ContainerMatcher = classTemplateSpecializationDecl(hasAnyName(
34       "::std::set", "::std::map", "::std::multiset", "::std::multimap",
35       "::std::unordered_set", "::std::unordered_map",
36       "::std::unordered_multiset", "::std::unordered_multimap"));
37 
38   const auto Matcher = traverse(
39       ast_type_traits::TK_AsIs,
40       callExpr(
41           callee(functionDecl(Algorithms)),
42           hasArgument(
43               0, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
44                      callee(cxxMethodDecl(hasName("begin"))),
45                      on(declRefExpr(
46                             hasDeclaration(decl().bind("IneffContObj")),
47                             anyOf(hasType(ContainerMatcher.bind("IneffCont")),
48                                   hasType(pointsTo(
49                                       ContainerMatcher.bind("IneffContPtr")))))
50                             .bind("IneffContExpr"))))))),
51           hasArgument(
52               1, cxxConstructExpr(has(ignoringParenImpCasts(cxxMemberCallExpr(
53                      callee(cxxMethodDecl(hasName("end"))),
54                      on(declRefExpr(
55                          hasDeclaration(equalsBoundNode("IneffContObj"))))))))),
56           hasArgument(2, expr().bind("AlgParam")),
57           unless(isInTemplateInstantiation()))
58           .bind("IneffAlg"));
59 
60   Finder->addMatcher(Matcher, this);
61 }
62 
check(const MatchFinder::MatchResult & Result)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;
68   if (!IneffCont) {
69     IneffCont =
70         Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("IneffContPtr");
71     PtrToContainer = true;
72   }
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;
77 
78   // Store if the key type of the container is compatible with the value
79   // that is searched for.
80   QualType ValueType = AlgCall->getArg(2)->getType();
81   QualType KeyType =
82       IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType();
83   const bool CompatibleTypes = areTypesCompatible(KeyType, ValueType);
84 
85   // Check if the comparison type for the algorithm and the container matches.
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]
93                                       .getAsType()
94                                       .getUnqualifiedType()
95                                       .getCanonicalType();
96     if (AlgCmp != ContainerCmp) {
97       diag(Arg->getBeginLoc(),
98            "different comparers used in the algorithm and the container");
99       return;
100     }
101   }
102 
103   const auto *AlgDecl = AlgCall->getDirectCallee();
104   if (!AlgDecl)
105     return;
106 
107   if (Unordered && AlgDecl->getName().find("bound") != llvm::StringRef::npos)
108     return;
109 
110   const auto *AlgParam = Result.Nodes.getNodeAs<Expr>("AlgParam");
111   const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>("IneffContExpr");
112   FixItHint Hint;
113 
114   SourceManager &SM = *Result.SourceManager;
115   LangOptions LangOpts = getLangOpts();
116 
117   CharSourceRange CallRange =
118       CharSourceRange::getTokenRange(AlgCall->getSourceRange());
119 
120   // FIXME: Create a common utility to extract a file range that the given token
121   // sequence is exactly spelled at (without macro argument expansions etc.).
122   // We can't use Lexer::makeFileCharRange here, because for
123   //
124   //   #define F(x) x
125   //   x(a b c);
126   //
127   // it will return "x(a b c)", when given the range "a"-"c". It makes sense for
128   // removals, but not for replacements.
129   //
130   // This code is over-simplified, but works for many real cases.
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()));
135   }
136 
137   if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
138     StringRef ContainerText = Lexer::getSourceText(
139         CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
140         LangOpts);
141     StringRef ParamText = Lexer::getSourceText(
142         CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM,
143         LangOpts);
144     std::string ReplacementText =
145         (llvm::Twine(ContainerText) + (PtrToContainer ? "->" : ".") +
146          AlgDecl->getName() + "(" + ParamText + ")")
147             .str();
148     Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
149   }
150 
151   diag(AlgCall->getBeginLoc(),
152        "this STL algorithm call should be replaced with a container method")
153       << Hint;
154 }
155 
156 } // namespace performance
157 } // namespace tidy
158 } // namespace clang
159