Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 1 | //===--- InefficientAlgorithmCheck.cpp - clang-tidy------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "InefficientAlgorithmCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Lex/Lexer.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 19 | namespace misc { |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 20 | |
Gabor Horvath | 21b76ba | 2015-02-17 21:45:38 +0000 | [diff] [blame] | 21 | static bool areTypesCompatible(QualType Left, QualType Right) { |
| 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(); |
| 28 | } |
| 29 | |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 30 | void InefficientAlgorithmCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 31 | // Only register the matchers for C++; the functionality currently does not |
| 32 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 33 | if (!getLangOpts().CPlusPlus) |
| 34 | return; |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 35 | |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 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)$")); |
| 40 | const auto Matcher = |
| 41 | callExpr( |
| 42 | callee(functionDecl(matchesName(Algorithms))), |
| 43 | hasArgument( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 44 | 0, cxxConstructExpr(has(cxxMemberCallExpr( |
| 45 | callee(cxxMethodDecl(hasName("begin"))), |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 46 | on(declRefExpr( |
| 47 | hasDeclaration(decl().bind("IneffContObj")), |
| 48 | anyOf(hasType(ContainerMatcher.bind("IneffCont")), |
| 49 | hasType(pointsTo( |
| 50 | ContainerMatcher.bind("IneffContPtr"))))) |
| 51 | .bind("IneffContExpr")))))), |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 52 | hasArgument(1, cxxConstructExpr(has(cxxMemberCallExpr( |
| 53 | callee(cxxMethodDecl(hasName("end"))), |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 54 | on(declRefExpr(hasDeclaration( |
| 55 | equalsBoundNode("IneffContObj")))))))), |
| 56 | hasArgument(2, expr().bind("AlgParam")), |
| 57 | unless(isInTemplateInstantiation())) |
| 58 | .bind("IneffAlg"); |
| 59 | |
| 60 | Finder->addMatcher(Matcher, this); |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 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; |
Gabor Horvath | 21b76ba | 2015-02-17 21:45:38 +0000 | [diff] [blame] | 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); |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 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->getLocStart(), |
| 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 | |
Alexander Kornienko | b4fbb17 | 2015-07-31 13:34:58 +0000 | [diff] [blame] | 114 | SourceManager &SM = *Result.SourceManager; |
| 115 | LangOptions LangOpts = Result.Context->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); |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 144 | std::string ReplacementText = |
Alexander Kornienko | b4fbb17 | 2015-07-31 13:34:58 +0000 | [diff] [blame] | 145 | (llvm::Twine(ContainerText) + (PtrToContainer ? "->" : ".") + |
| 146 | AlgDecl->getName() + "(" + ParamText + ")") |
| 147 | .str(); |
| 148 | Hint = FixItHint::CreateReplacement(CallRange, ReplacementText); |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | diag(AlgCall->getLocStart(), |
| 152 | "this STL algorithm call should be replaced with a container method") |
| 153 | << Hint; |
| 154 | } |
| 155 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 156 | } // namespace misc |
Gabor Horvath | 3880bee | 2015-02-07 19:54:19 +0000 | [diff] [blame] | 157 | } // namespace tidy |
| 158 | } // namespace clang |