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 | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 19 | namespace misc { |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 20 | |
| 21 | void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) { |
Aaron Ballman | 327e97b | 2015-08-28 19:27:19 +0000 | [diff] [blame] | 22 | // Only register the matchers for C++; the functionality currently does not |
| 23 | // provide any benefit to other languages, despite being benign. |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 24 | if (!getLangOpts().CPlusPlus) |
| 25 | return; |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 26 | |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 27 | const auto CheckForEndCall = hasArgument( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 28 | 1, anyOf(cxxConstructExpr( |
| 29 | has(cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end")))) |
| 30 | .bind("InaccEndCall"))), |
| 31 | anything())); |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 32 | |
| 33 | Finder->addMatcher( |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 34 | cxxMemberCallExpr( |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 35 | on(hasType(namedDecl(matchesName("^::std::")))), |
Aaron Ballman | b9ea09c | 2015-09-17 13:31:25 +0000 | [diff] [blame^] | 36 | callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1), |
Aaron Ballman | bf89109 | 2015-08-31 15:28:57 +0000 | [diff] [blame] | 37 | hasArgument(0, has(callExpr(callee(functionDecl(matchesName( |
| 38 | "^::std::(remove(_if)?|unique)$"))), |
| 39 | CheckForEndCall) |
| 40 | .bind("InaccAlgCall"))), |
| 41 | unless(isInTemplateInstantiation())) |
| 42 | .bind("InaccErase"), |
| 43 | this); |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) { |
| 47 | const auto *MemberCall = |
| 48 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("InaccErase"); |
| 49 | const auto *EndExpr = |
| 50 | Result.Nodes.getNodeAs<CXXMemberCallExpr>("InaccEndCall"); |
| 51 | const SourceLocation Loc = MemberCall->getLocStart(); |
| 52 | |
| 53 | FixItHint Hint; |
| 54 | |
| 55 | if (!Loc.isMacroID() && EndExpr) { |
| 56 | const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("InaccAlgCall"); |
| 57 | std::string ReplacementText = Lexer::getSourceText( |
| 58 | CharSourceRange::getTokenRange(EndExpr->getSourceRange()), |
| 59 | *Result.SourceManager, Result.Context->getLangOpts()); |
| 60 | const SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
| 61 | AlgCall->getLocEnd(), 0, *Result.SourceManager, |
| 62 | Result.Context->getLangOpts()); |
| 63 | Hint = FixItHint::CreateInsertion(EndLoc, ", " + ReplacementText); |
| 64 | } |
| 65 | |
| 66 | diag(Loc, "this call will remove at most one item even when multiple items " |
| 67 | "should be removed") |
| 68 | << Hint; |
| 69 | } |
| 70 | |
Alexander Kornienko | 2b312420 | 2015-03-02 12:25:03 +0000 | [diff] [blame] | 71 | } // namespace misc |
Gabor Horvath | d4637fb | 2015-02-10 09:14:26 +0000 | [diff] [blame] | 72 | } // namespace tidy |
| 73 | } // namespace clang |