clang-tools 23.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 StringRef Name : Names)
54 Results.try_emplace(("::std::" + Name).str(), Replacer);
55 };
56
57 // Single range algorithms. One per line, keep sorted.
58 // clang-format off
59 AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
60 {"accumulate",
61 "adjacent_find",
62 "all_of",
63 "any_of",
64 "binary_search",
65 "copy",
66 "copy_if",
67 "count",
68 "count_if",
69 "fill",
70 "find",
71 "find_if",
72 "find_if_not",
73 "for_each",
74 "is_sorted",
75 "lower_bound",
76 "max_element",
77 "min_element",
78 "none_of",
79 "partition",
80 "partition_point",
81 "remove_if",
82 "replace",
83 "replace_copy",
84 "replace_copy_if",
85 "stable_sort",
86 "transform",
87 "uninitialized_copy",
88 "unique",
89 "upper_bound"});
90 // clang-format on
91
92 // Two range algorithms.
93 AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
94 {"equal", "mismatch", "includes", "search"});
95
96 return Results;
97}
98
100 : utils::UseRangesCheck(Name, Context) {}
101
102DiagnosticBuilder UseRangesCheck::createDiag(const CallExpr &Call) {
103 return diag(Call.getBeginLoc(), "use an LLVM range-based algorithm");
104}
105
106ArrayRef<std::pair<StringRef, StringRef>>
108 static constexpr std::pair<StringRef, StringRef> Refs[] = {
109 {"::std::begin", "::std::end"},
110 {"::std::cbegin", "::std::cend"},
111 {"::std::rbegin", "::std::rend"},
112 {"::std::crbegin", "::std::crend"},
113 };
114 return Refs;
115}
116
117} // 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