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::tidy::bugprone { |
17 | |
18 | void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) { |
19 | const auto EndCall = |
20 | callExpr( |
21 | callee(InnerMatcher: functionDecl(hasAnyName("remove" , "remove_if" , "unique" ))), |
22 | hasArgument( |
23 | N: 1, InnerMatcher: optionally(cxxMemberCallExpr(callee(InnerMatcher: cxxMethodDecl(hasName(Name: "end" )))) |
24 | .bind(ID: "end" )))) |
25 | .bind(ID: "alg" ); |
26 | |
27 | const auto DeclInStd = type(hasUnqualifiedDesugaredType( |
28 | InnerMatcher: tagType(hasDeclaration(InnerMatcher: decl(isInStdNamespace()))))); |
29 | Finder->addMatcher( |
30 | NodeMatch: cxxMemberCallExpr( |
31 | on(InnerMatcher: anyOf(hasType(InnerMatcher: DeclInStd), hasType(InnerMatcher: pointsTo(InnerMatcher: DeclInStd)))), |
32 | callee(InnerMatcher: cxxMethodDecl(hasName(Name: "erase" ))), argumentCountIs(N: 1), |
33 | hasArgument(N: 0, InnerMatcher: EndCall)) |
34 | .bind(ID: "erase" ), |
35 | Action: this); |
36 | } |
37 | |
38 | void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) { |
39 | const auto *MemberCall = |
40 | Result.Nodes.getNodeAs<CXXMemberCallExpr>(ID: "erase" ); |
41 | const auto *EndExpr = |
42 | Result.Nodes.getNodeAs<CXXMemberCallExpr>(ID: "end" ); |
43 | const SourceLocation Loc = MemberCall->getBeginLoc(); |
44 | |
45 | FixItHint Hint; |
46 | |
47 | if (!Loc.isMacroID() && EndExpr) { |
48 | const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>(ID: "alg" ); |
49 | std::string ReplacementText = std::string(Lexer::getSourceText( |
50 | Range: CharSourceRange::getTokenRange(EndExpr->getSourceRange()), |
51 | SM: *Result.SourceManager, LangOpts: getLangOpts())); |
52 | const SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
53 | Loc: AlgCall->getEndLoc(), Offset: 0, SM: *Result.SourceManager, LangOpts: getLangOpts()); |
54 | Hint = FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: ", " + ReplacementText); |
55 | } |
56 | |
57 | diag(Loc, Description: "this call will remove at most one item even when multiple items " |
58 | "should be removed" ) |
59 | << Hint; |
60 | } |
61 | |
62 | } // namespace clang::tidy::bugprone |
63 | |