diff options
author | Gabor Horvath <xazax.hun@gmail.com> | 2015-02-17 21:45:38 +0000 |
---|---|---|
committer | Gabor Horvath <xazax.hun@gmail.com> | 2015-02-17 21:45:38 +0000 |
commit | 21b76badebbdb7fcc871f60b0566e2f92d65b05a (patch) | |
tree | 75b1c1f1c11d130c494dc4f098afcbcf31224cf4 /clang-tools-extra/clang-tidy | |
parent | 8dfa40b84bd622b3054754b7458b84a8a90e2c5d (diff) | |
download | bcm5719-llvm-21b76badebbdb7fcc871f60b0566e2f92d65b05a.tar.gz bcm5719-llvm-21b76badebbdb7fcc871f60b0566e2f92d65b05a.zip |
[clang-tidy] Fixed two wrong fix-it cases in misc-inefficient-algorithm checker.
llvm-svn: 229552
Diffstat (limited to 'clang-tools-extra/clang-tidy')
-rw-r--r-- | clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp index 906140db54e..16401b7d475 100644 --- a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp @@ -17,9 +17,18 @@ using namespace clang::ast_matchers; namespace clang { namespace tidy { +static bool areTypesCompatible(QualType Left, QualType Right) { + if (const auto *LeftRefType = Left->getAs<ReferenceType>()) + Left = LeftRefType->getPointeeType(); + if (const auto *RightRefType = Right->getAs<ReferenceType>()) + Right = RightRefType->getPointeeType(); + return Left->getCanonicalTypeUnqualified() == + Right->getCanonicalTypeUnqualified(); +} + void InefficientAlgorithmCheck::registerMatchers(MatchFinder *Finder) { const std::string Algorithms = - "^::std::(find|count|equal_range|lower_blound|upper_bound)$"; + "^::std::(find|count|equal_range|lower_bound|upper_bound)$"; const auto ContainerMatcher = classTemplateSpecializationDecl( matchesName("^::std::(unordered_)?(multi)?(set|map)$")); const auto Matcher = @@ -57,6 +66,14 @@ void InefficientAlgorithmCheck::check(const MatchFinder::MatchResult &Result) { const llvm::StringRef IneffContName = IneffCont->getName(); const bool Unordered = IneffContName.find("unordered") != llvm::StringRef::npos; + const bool Maplike = IneffContName.find("map") != llvm::StringRef::npos; + + // Store if the key type of the container is compatible with the value + // that is searched for. + QualType ValueType = AlgCall->getArg(2)->getType(); + QualType KeyType = + IneffCont->getTemplateArgs()[0].getAsType().getCanonicalType(); + const bool CompatibleTypes = areTypesCompatible(KeyType, ValueType); // Check if the comparison type for the algorithm and the container matches. if (AlgCall->getNumArgs() == 4 && !Unordered) { @@ -87,7 +104,7 @@ void InefficientAlgorithmCheck::check(const MatchFinder::MatchResult &Result) { const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>("IneffContExpr"); FixItHint Hint; - if (!AlgCall->getLocStart().isMacroID()) { + if (!AlgCall->getLocStart().isMacroID() && !Maplike && CompatibleTypes) { std::string ReplacementText = (llvm::Twine(Lexer::getSourceText( CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), |