clang-tools 6.0.0

InaccurateEraseCheck.cpp
Go to the documentation of this file.
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 {
19 namespace bugprone {
20
21 namespace {
22 AST_MATCHER(Decl, isInStdNamespace) { return Node.isInStdNamespace(); }
23 }
24
25 void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
26 // Only register the matchers for C++; the functionality currently does not
27 // provide any benefit to other languages, despite being benign.
28 if (!getLangOpts().CPlusPlus)
29 return;
30
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");
41
42 const auto DeclInStd = type(hasUnqualifiedDesugaredType(
43 tagType(hasDeclaration(decl(isInStdNamespace())))));
44 Finder->addMatcher(
45 cxxMemberCallExpr(
46 on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
47 callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
48 hasArgument(0, has(ignoringImplicit(
49 anyOf(EndCall, has(ignoringImplicit(EndCall)))))),
50 unless(isInTemplateInstantiation()))
51 .bind("erase"),
52 this);
53 }
54
55 void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) {
56 const auto *MemberCall =
57 Result.Nodes.getNodeAs<CXXMemberCallExpr>("erase");
58 const auto *EndExpr =
59 Result.Nodes.getNodeAs<CXXMemberCallExpr>("end");
60 const SourceLocation Loc = MemberCall->getLocStart();
61
62 FixItHint Hint;
63
64 if (!Loc.isMacroID() && EndExpr) {
65 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("alg");
66 std::string ReplacementText = Lexer::getSourceText(
67 CharSourceRange::getTokenRange(EndExpr->getSourceRange()),
68 *Result.SourceManager, getLangOpts());
69 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
70 AlgCall->getLocEnd(), 0, *Result.SourceManager, getLangOpts());
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
79 } // namespace bugprone
80 } // namespace tidy
81 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:36
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
InaccurateEraseCheck.h
clang
Definition: AndroidTidyModule.cpp:28
clang::ast_matchers::AST_MATCHER
AST_MATCHER(VarDecl, isAsm)
Definition: NoAssemblerCheck.cpp:18