Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 1 | //===--- InaccurateEraseCheck.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 "InaccurateEraseCheck.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 | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 19 | namespace bugprone { |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 20 | |
Alexander Kornienko | 0bdd8a1 | 2017-06-06 17:49:45 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | AST_MATCHER(Decl, isInStdNamespace) { return Node.isInStdNamespace(); } |
| 23 | } |
| 24 | |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 25 | void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 26 | // Only register the matchers for C++; the functionality currently does not |
| 27 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 28 | if (!getLangOpts().CPlusPlus) |
| 29 | return; |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 30 | |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 31 | const auto EndCall = |
| 32 | callExpr( |
| 33 | callee(functionDecl(hasAnyName("remove", "remove_if", "unique"))), |
| 34 | hasArgument( |
| 35 | 1, |
| 36 | anyOf(cxxConstructExpr(has(ignoringImplicit( |
| 37 | cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end")))) |
| 38 | .bind("end")))), |
| 39 | anything()))) |
| 40 | .bind("alg"); |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 41 | |
Manuel Klimek | 7b9c117 | 2017-08-02 13:13:11 +0000 | [diff] [blame] | 42 | const auto DeclInStd = type(hasUnqualifiedDesugaredType( |
| 43 | tagType(hasDeclaration(decl(isInStdNamespace()))))); |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 44 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 45 | cxxMemberCallExpr( |
Alexander Kornienko | 0bdd8a1 | 2017-06-06 17:49:45 +0000 | [diff] [blame] | 46 | on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))), |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame] | 47 | callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1), |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 48 | hasArgument(0, has(ignoringImplicit( |
| 49 | anyOf(EndCall, has(ignoringImplicit(EndCall)))))), |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 50 | unless(isInTemplateInstantiation())) |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 51 | .bind("erase"), |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 52 | this); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) { |
| 56 | const auto *MemberCall = |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 57 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("erase"); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 58 | const auto *EndExpr = |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 59 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("end"); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 60 | const SourceLocation Loc = MemberCall->getLocStart(); |
| 61 | |
| 62 | FixItHint Hint; |
| 63 | |
| 64 | if (!Loc.isMacroID() && EndExpr) { |
Alexander Kornienko | f1dc12e | 2017-06-07 08:25:51 +0000 | [diff] [blame] | 65 | const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("alg"); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 66 | std::string ReplacementText = Lexer::getSourceText( |
| 67 | CharSourceRange::getTokenRange(EndExpr->getSourceRange()), |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 68 | *Result.SourceManager, getLangOpts()); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 69 | const SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 70 | AlgCall->getLocEnd(), 0, *Result.SourceManager, getLangOpts()); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 71 | Hint = FixItHint::CreateInsertion(EndLoc, ", " + ReplacementText); |
| 72 | } |
| 73 | |
| 74 | diag(Loc, "this call will remove at most one item even when multiple items " |
| 75 | "should be removed") |
| 76 | << Hint; |
| 77 | } |
| 78 | |
Alexander Kornienko | d4ac4af | 2017-11-24 14:16:29 +0000 | [diff] [blame] | 79 | } // namespace bugprone |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 80 | } // namespace tidy |
| 81 | } // namespace clang |