blob: 6e69f2a4e41596bac52f0993a180cf24cb3aa7ce [file] [log] [blame]
Gabor Horvathd4637fb2015-02-10 09:14:26 +00001//===--- 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
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
Alexander Kornienko2b3124202015-03-02 12:25:03 +000019namespace misc {
Gabor Horvathd4637fb2015-02-10 09:14:26 +000020
21void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballman327e97b2015-08-28 19:27:19 +000022 // Only register the matchers for C++; the functionality currently does not
23 // provide any benefit to other languages, despite being benign.
Aaron Ballmanbf891092015-08-31 15:28:57 +000024 if (!getLangOpts().CPlusPlus)
25 return;
Gabor Horvathd4637fb2015-02-10 09:14:26 +000026
Aaron Ballmanbf891092015-08-31 15:28:57 +000027 const auto CheckForEndCall = hasArgument(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000028 1, anyOf(cxxConstructExpr(
29 has(cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end"))))
30 .bind("InaccEndCall"))),
31 anything()));
Aaron Ballmanbf891092015-08-31 15:28:57 +000032
33 Finder->addMatcher(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000034 cxxMemberCallExpr(
Aaron Ballmanbf891092015-08-31 15:28:57 +000035 on(hasType(namedDecl(matchesName("^::std::")))),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +000036 callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
Aaron Ballmanbf891092015-08-31 15:28:57 +000037 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 Horvathd4637fb2015-02-10 09:14:26 +000044}
45
46void 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 Kornienko2b3124202015-03-02 12:25:03 +000071} // namespace misc
Gabor Horvathd4637fb2015-02-10 09:14:26 +000072} // namespace tidy
73} // namespace clang