1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
//===--- UseRangesCheck.cpp - clang-tidy ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UseRangesCheck.h"
#include "clang/AST/Decl.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include <initializer_list>
// FixItHint - Let the docs script know that this class does provide fixits
namespace clang::tidy::modernize {
static constexpr const char *SingleRangeNames[] = {
"all_of",
"any_of",
"none_of",
"for_each",
"find",
"find_if",
"find_if_not",
"adjacent_find",
"copy",
"copy_if",
"copy_backward",
"move",
"move_backward",
"fill",
"transform",
"replace",
"replace_if",
"generate",
"remove",
"remove_if",
"remove_copy",
"remove_copy_if",
"unique",
"unique_copy",
"sample",
"partition_point",
"lower_bound",
"upper_bound",
"equal_range",
"binary_search",
"push_heap",
"pop_heap",
"make_heap",
"sort_heap",
"next_permutation",
"prev_permutation",
"reverse",
"reverse_copy",
"shift_left",
"shift_right",
"is_partitioned",
"partition",
"partition_copy",
"stable_partition",
"sort",
"stable_sort",
"is_sorted",
"is_sorted_until",
"is_heap",
"is_heap_until",
"max_element",
"min_element",
"minmax_element",
"uninitialized_copy",
"uninitialized_fill",
"uninitialized_move",
"uninitialized_default_construct",
"uninitialized_value_construct",
"destroy",
};
static constexpr const char *TwoRangeNames[] = {
"equal",
"mismatch",
"partial_sort_copy",
"includes",
"set_union",
"set_intersection",
"set_difference",
"set_symmetric_difference",
"merge",
"lexicographical_compare",
"find_end",
"search",
"is_permutation",
};
static constexpr const char *SinglePivotRangeNames[] = {"rotate", "rotate_copy",
"inplace_merge"};
namespace {
class StdReplacer : public utils::UseRangesCheck::Replacer {
public:
explicit StdReplacer(SmallVector<UseRangesCheck::Signature> Signatures)
: Signatures(std::move(Signatures)) {}
std::optional<std::string>
getReplaceName(const NamedDecl &OriginalName) const override {
return ("std::ranges::" + OriginalName.getName()).str();
}
ArrayRef<UseRangesCheck::Signature>
getReplacementSignatures() const override {
return Signatures;
}
private:
SmallVector<UseRangesCheck::Signature> Signatures;
};
class StdAlgorithmReplacer : public StdReplacer {
using StdReplacer::StdReplacer;
std::optional<std::string>
getHeaderInclusion(const NamedDecl & /*OriginalName*/) const override {
return "<algorithm>";
}
};
class StdNumericReplacer : public StdReplacer {
using StdReplacer::StdReplacer;
std::optional<std::string>
getHeaderInclusion(const NamedDecl & /*OriginalName*/) const override {
return "<numeric>";
}
};
} // namespace
utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
utils::UseRangesCheck::ReplacerMap Result;
// template<typename Iter> Func(Iter first, Iter last,...).
static const Signature SingleRangeArgs = {{0}};
// template<typename Iter1, typename Iter2>
// Func(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2,...).
static const Signature TwoRangeArgs = {{0}, {2}};
// template<typename Iter> Func(Iter first, Iter pivot, Iter last,...).
static const Signature SinglePivotRange = {{0, 2}};
static const Signature SingleRangeFunc[] = {SingleRangeArgs};
static const Signature TwoRangeFunc[] = {TwoRangeArgs};
static const Signature SinglePivotFunc[] = {SinglePivotRange};
static const std::pair<ArrayRef<Signature>, ArrayRef<const char *>>
AlgorithmNames[] = {{SingleRangeFunc, SingleRangeNames},
{TwoRangeFunc, TwoRangeNames},
{SinglePivotFunc, SinglePivotRangeNames}};
SmallString<64> Buff;
for (const auto &[Signatures, Values] : AlgorithmNames) {
auto Replacer = llvm::makeIntrusiveRefCnt<StdAlgorithmReplacer>(
SmallVector<UseRangesCheck::Signature>{Signatures});
for (const auto &Name : Values) {
Buff.assign({"::std::", Name});
Result.try_emplace(Buff, Replacer);
}
}
if (getLangOpts().CPlusPlus23)
Result.try_emplace(
"::std::iota",
llvm::makeIntrusiveRefCnt<StdNumericReplacer>(
SmallVector<UseRangesCheck::Signature>{std::begin(SingleRangeFunc),
std::end(SingleRangeFunc)}));
return Result;
}
UseRangesCheck::UseRangesCheck(StringRef Name, ClangTidyContext *Context)
: utils::UseRangesCheck(Name, Context),
UseReversePipe(Options.get("UseReversePipe", false)) {}
void UseRangesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
utils::UseRangesCheck::storeOptions(Opts);
Options.store(Opts, "UseReversePipe", UseReversePipe);
}
bool UseRangesCheck::isLanguageVersionSupported(
const LangOptions &LangOpts) const {
return LangOpts.CPlusPlus20;
}
ArrayRef<std::pair<StringRef, StringRef>>
UseRangesCheck::getFreeBeginEndMethods() const {
static const std::pair<StringRef, StringRef> Refs[] = {
{"::std::begin", "::std::end"}, {"::std::cbegin", "::std::cend"}};
return Refs;
}
std::optional<UseRangesCheck::ReverseIteratorDescriptor>
UseRangesCheck::getReverseDescriptor() const {
static const std::pair<StringRef, StringRef> Refs[] = {
{"::std::rbegin", "::std::rend"}, {"::std::crbegin", "::std::crend"}};
return ReverseIteratorDescriptor{UseReversePipe ? "std::views::reverse"
: "std::ranges::reverse_view",
"<ranges>", Refs, UseReversePipe};
}
} // namespace clang::tidy::modernize
|