[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / clang-tools-extra / clang-tidy / bugprone / InaccurateEraseCheck.cpp
blob164de76d4fa467f9bd8189df2790cd825eaf3ae2
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 //===----------------------------------------------------------------------===//
9 #include "InaccurateEraseCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
14 using namespace clang::ast_matchers;
16 namespace clang::tidy::bugprone {
18 void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
19 const auto EndCall =
20 callExpr(
21 callee(functionDecl(hasAnyName("remove", "remove_if", "unique"))),
22 hasArgument(
23 1, optionally(cxxMemberCallExpr(callee(cxxMethodDecl(hasName("end"))))
24 .bind("end"))))
25 .bind("alg");
27 const auto DeclInStd = type(hasUnqualifiedDesugaredType(
28 tagType(hasDeclaration(decl(isInStdNamespace())))));
29 Finder->addMatcher(
30 cxxMemberCallExpr(
31 on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
32 callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
33 hasArgument(0, EndCall))
34 .bind("erase"),
35 this);
38 void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) {
39 const auto *MemberCall =
40 Result.Nodes.getNodeAs<CXXMemberCallExpr>("erase");
41 const auto *EndExpr =
42 Result.Nodes.getNodeAs<CXXMemberCallExpr>("end");
43 const SourceLocation Loc = MemberCall->getBeginLoc();
45 FixItHint Hint;
47 if (!Loc.isMacroID() && EndExpr) {
48 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("alg");
49 std::string ReplacementText = std::string(Lexer::getSourceText(
50 CharSourceRange::getTokenRange(EndExpr->getSourceRange()),
51 *Result.SourceManager, getLangOpts()));
52 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
53 AlgCall->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
54 Hint = FixItHint::CreateInsertion(EndLoc, ", " + ReplacementText);
57 diag(Loc, "this call will remove at most one item even when multiple items "
58 "should be removed")
59 << Hint;
62 } // namespace clang::tidy::bugprone