clang-tools  9.0.0
InaccurateEraseCheck.cpp
Go to the documentation of this file.
1 //===--- InaccurateEraseCheck.cpp - clang-tidy-----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "InaccurateEraseCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace bugprone {
19 
20 void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
21  // Only register the matchers for C++; the functionality currently does not
22  // provide any benefit to other languages, despite being benign.
23  if (!getLangOpts().CPlusPlus)
24  return;
25 
26  const auto EndCall =
27  callExpr(
28  callee(functionDecl(hasAnyName("remove", "remove_if", "unique"))),
29  hasArgument(
30  1,
31  anyOf(cxxConstructExpr(has(ignoringImplicit(
32  cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end"))))
33  .bind("end")))),
34  anything())))
35  .bind("alg");
36 
37  const auto DeclInStd = type(hasUnqualifiedDesugaredType(
38  tagType(hasDeclaration(decl(isInStdNamespace())))));
39  Finder->addMatcher(
40  cxxMemberCallExpr(
41  on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
42  callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
43  hasArgument(0, has(ignoringImplicit(
44  anyOf(EndCall, has(ignoringImplicit(EndCall)))))),
45  unless(isInTemplateInstantiation()))
46  .bind("erase"),
47  this);
48 }
49 
50 void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) {
51  const auto *MemberCall =
52  Result.Nodes.getNodeAs<CXXMemberCallExpr>("erase");
53  const auto *EndExpr =
54  Result.Nodes.getNodeAs<CXXMemberCallExpr>("end");
55  const SourceLocation Loc = MemberCall->getBeginLoc();
56 
57  FixItHint Hint;
58 
59  if (!Loc.isMacroID() && EndExpr) {
60  const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("alg");
61  std::string ReplacementText = Lexer::getSourceText(
62  CharSourceRange::getTokenRange(EndExpr->getSourceRange()),
63  *Result.SourceManager, getLangOpts());
64  const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
65  AlgCall->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
66  Hint = FixItHint::CreateInsertion(EndLoc, ", " + ReplacementText);
67  }
68 
69  diag(Loc, "this call will remove at most one item even when multiple items "
70  "should be removed")
71  << Hint;
72 }
73 
74 } // namespace bugprone
75 } // namespace tidy
76 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:203
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36