clang-tools 22.0.0git
llvm/UseRangesCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "UseRangesCheck.h"
10
11// FixItHint - Let the docs script know that this class does provide fixits
12
14
15namespace {
16
17class StdToLLVMReplacer : public utils::UseRangesCheck::Replacer {
18public:
19 explicit StdToLLVMReplacer(
20 ArrayRef<utils::UseRangesCheck::Signature> Signatures)
21 : Signatures(Signatures) {}
22
23 ArrayRef<utils::UseRangesCheck::Signature>
24 getReplacementSignatures() const override {
25 return Signatures;
26 }
27
28 std::optional<std::string>
29 getReplaceName(const NamedDecl &OriginalName) const override {
30 return ("llvm::" + OriginalName.getName()).str();
31 }
32
33 std::optional<std::string>
34 getHeaderInclusion(const NamedDecl &) const override {
35 return "llvm/ADT/STLExtras.h";
36 }
37
38private:
39 ArrayRef<utils::UseRangesCheck::Signature> Signatures;
40};
41
42} // namespace
43
45 ReplacerMap Results;
46
47 static const Signature SingleSig = {{0}};
48 static const Signature TwoSig = {{0}, {2}};
49
50 const auto AddStdToLLVM =
51 [&Results](llvm::IntrusiveRefCntPtr<Replacer> Replacer,
52 std::initializer_list<StringRef> Names) {
53 for (const auto &Name : Names) {
54 Results.try_emplace(("::std::" + Name).str(), Replacer);
55 }
56 };
57
58 // Single range algorithms
59 AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
60 {"all_of", "any_of",
61 "none_of", "for_each",
62 "find", "find_if",
63 "find_if_not", "fill",
64 "count", "count_if",
65 "copy", "copy_if",
66 "transform", "replace",
67 "remove_if", "stable_sort",
68 "partition", "partition_point",
69 "is_sorted", "min_element",
70 "max_element", "binary_search",
71 "lower_bound", "upper_bound",
72 "unique", "uninitialized_copy"});
73
74 // Two range algorithms
75 AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
76 {"equal", "mismatch", "includes"});
77
78 return Results;
79}
80
82 : utils::UseRangesCheck(Name, Context) {}
83
84DiagnosticBuilder UseRangesCheck::createDiag(const CallExpr &Call) {
85 return diag(Call.getBeginLoc(), "use an LLVM range-based algorithm");
86}
87
88ArrayRef<std::pair<StringRef, StringRef>>
90 static constexpr std::pair<StringRef, StringRef> Refs[] = {
91 {"::std::begin", "::std::end"},
92 {"::std::cbegin", "::std::cend"},
93 {"::std::rbegin", "::std::rend"},
94 {"::std::crbegin", "::std::crend"},
95 };
96 return Refs;
97}
98
99} // namespace clang::tidy::llvm_check
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
UseRangesCheck(StringRef Name, ClangTidyContext *Context)
ReplacerMap getReplacerMap() const override
Gets a map of function to replace and methods to create the replacements.
DiagnosticBuilder createDiag(const CallExpr &Call) override
Create a diagnostic for the CallExpr Override this to support custom diagnostic messages.
ArrayRef< std::pair< StringRef, StringRef > > getFreeBeginEndMethods() const override
Gets the fully qualified names of begin and end functions.
llvm::StringMap< llvm::IntrusiveRefCntPtr< Replacer > > ReplacerMap