Skip to content

Commit 86b21ee

Browse files
committed
Add a modernize-use-ranges check
1 parent f190343 commit 86b21ee

File tree

11 files changed

+783
-0
lines changed

11 files changed

+783
-0
lines changed

clang-tools-extra/clang-tidy/modernize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_clang_library(clangTidyModernizeModule
4040
UseNoexceptCheck.cpp
4141
UseNullptrCheck.cpp
4242
UseOverrideCheck.cpp
43+
UseRangesCheck.cpp
4344
UseStartsEndsWithCheck.cpp
4445
UseStdFormatCheck.cpp
4546
UseStdNumbersCheck.cpp

clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "UseNoexceptCheck.h"
4242
#include "UseNullptrCheck.h"
4343
#include "UseOverrideCheck.h"
44+
#include "UseRangesCheck.h"
4445
#include "UseStartsEndsWithCheck.h"
4546
#include "UseStdFormatCheck.h"
4647
#include "UseStdNumbersCheck.h"
@@ -75,6 +76,7 @@ class ModernizeModule : public ClangTidyModule {
7576
CheckFactories.registerCheck<PassByValueCheck>("modernize-pass-by-value");
7677
CheckFactories.registerCheck<UseDesignatedInitializersCheck>(
7778
"modernize-use-designated-initializers");
79+
CheckFactories.registerCheck<UseRangesCheck>("modernize-use-ranges");
7880
CheckFactories.registerCheck<UseStartsEndsWithCheck>(
7981
"modernize-use-starts-ends-with");
8082
CheckFactories.registerCheck<UseStdFormatCheck>("modernize-use-std-format");
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//===--- UseRangesCheck.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 "UseRangesCheck.h"
10+
#include "clang/AST/Decl.h"
11+
#include "llvm/ADT/ArrayRef.h"
12+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
13+
#include "llvm/ADT/SmallVector.h"
14+
#include "llvm/ADT/StringRef.h"
15+
#include <initializer_list>
16+
17+
// FixItHint - Let the docs script know that this class does provide fixits
18+
19+
namespace clang::tidy::modernize {
20+
21+
static constexpr const char *SingleRangeNames[] = {
22+
"all_of",
23+
"any_of",
24+
"none_of",
25+
"for_each",
26+
"find",
27+
"find_if",
28+
"find_if_not",
29+
"adjacent_find",
30+
"copy",
31+
"copy_if",
32+
"copy_backward",
33+
"move",
34+
"move_backward",
35+
"fill",
36+
"transform",
37+
"replace",
38+
"replace_if",
39+
"generate",
40+
"remove",
41+
"remove_if",
42+
"remove_copy",
43+
"remove_copy_if",
44+
"unique",
45+
"unique_copy",
46+
"sample",
47+
"partition_point",
48+
"lower_bound",
49+
"upper_bound",
50+
"equal_range",
51+
"binary_search",
52+
"push_heap",
53+
"pop_heap",
54+
"make_heap",
55+
"sort_heap",
56+
"next_permutation",
57+
"prev_permutation",
58+
"iota",
59+
};
60+
61+
static constexpr const char *SingleRangeWithExecNames[] = {
62+
"reverse",
63+
"reverse_copy",
64+
"shift_left",
65+
"shift_right",
66+
"is_partitioned",
67+
"partition",
68+
"partition_copy",
69+
"stable_partition",
70+
"sort",
71+
"stable_sort",
72+
"is_sorted",
73+
"is_sorted_until",
74+
"is_heap",
75+
"is_heap_until",
76+
"max_element",
77+
"min_element",
78+
"minmax_element",
79+
"uninitialized_copy",
80+
"uninitialized_fill",
81+
"uninitialized_move",
82+
"uninitialized_default_construct",
83+
"uninitialized_value_construct",
84+
"destroy",
85+
};
86+
87+
static constexpr const char *TwoRangeWithExecNames[] = {
88+
"partial_sort_copy",
89+
"includes",
90+
"set_union",
91+
"set_intersection",
92+
"set_difference",
93+
"set_symmetric_difference",
94+
"merge",
95+
"lexicographical_compare",
96+
"find_end",
97+
"search",
98+
};
99+
100+
static constexpr const char *OneOrTwoRangeNames[] = {
101+
"is_permutation",
102+
};
103+
104+
static constexpr const char *OneOrTwoRangeWithExecNames[] = {
105+
"equal",
106+
"mismatch",
107+
};
108+
109+
namespace {
110+
class StdReplacer : public utils::UseRangesCheck::Replacer {
111+
public:
112+
explicit StdReplacer(SmallVector<UseRangesCheck::Signature> Indexes)
113+
: Indexes(std::move(Indexes)) {}
114+
std::string getReplaceName(const NamedDecl &OriginalName) const override {
115+
return ("std::ranges::" + OriginalName.getName()).str();
116+
}
117+
ArrayRef<UseRangesCheck::Signature>
118+
getReplacementSignatures() const override {
119+
return Indexes;
120+
}
121+
std::optional<std::string>
122+
getHeaderInclusion(const NamedDecl & /*OriginalName*/) const override {
123+
return "<algorithm>";
124+
}
125+
126+
private:
127+
SmallVector<UseRangesCheck::Signature> Indexes;
128+
};
129+
} // namespace
130+
131+
utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
132+
133+
utils::UseRangesCheck::ReplacerMap Result;
134+
135+
// template<typename Iter> Func(Iter first, Iter last,...).
136+
static const Signature SingleRangeArgs = {{0}};
137+
// template<typename Policy, typename Iter>
138+
// Func(Policy policy, Iter first, // Iter last,...).
139+
static const Signature SingleRangeExecPolicy = {{1}};
140+
// template<typename Iter1, typename Iter2>
141+
// Func(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2,...).
142+
static const Signature TwoRangeArgs = {{0}, {2}};
143+
// template<typename Policy, typename Iter1, typename Iter2>
144+
// Func(Policy policy, Iter1 first1, Iter1 last1, Iter2 first2, Iter2
145+
// last2,...).
146+
static const Signature TwoRangeExecPolicy = {{1}, {3}};
147+
148+
static const Signature SingleRangeFunc[] = {SingleRangeArgs};
149+
150+
static const Signature SingleRangeExecFunc[] = {SingleRangeArgs,
151+
SingleRangeExecPolicy};
152+
static const Signature TwoRangeExecFunc[] = {TwoRangeArgs,
153+
TwoRangeExecPolicy};
154+
static const Signature OneOrTwoFunc[] = {SingleRangeArgs, TwoRangeArgs};
155+
static const Signature OneOrTwoExecFunc[] = {
156+
SingleRangeArgs, SingleRangeExecPolicy, TwoRangeArgs, TwoRangeExecPolicy};
157+
158+
static const std::pair<ArrayRef<Signature>, ArrayRef<const char *>> Names[] =
159+
{{SingleRangeFunc, SingleRangeNames},
160+
{SingleRangeExecFunc, SingleRangeWithExecNames},
161+
{TwoRangeExecFunc, TwoRangeWithExecNames},
162+
{OneOrTwoFunc, OneOrTwoRangeNames},
163+
{OneOrTwoExecFunc, OneOrTwoRangeWithExecNames}};
164+
SmallString<64> Buff;
165+
for (const auto &[Signature, Values] : Names) {
166+
auto Replacer = llvm::makeIntrusiveRefCnt<StdReplacer>(
167+
SmallVector<UseRangesCheck::Signature>{Signature.begin(),
168+
Signature.end()});
169+
for (const auto &Name : Values) {
170+
Buff.assign({"::std::", Name});
171+
Result.try_emplace(Buff, Replacer);
172+
}
173+
}
174+
return Result;
175+
}
176+
177+
bool UseRangesCheck::isLanguageVersionSupported(
178+
const LangOptions &LangOpts) const {
179+
return LangOpts.CPlusPlus20;
180+
}
181+
} // namespace clang::tidy::modernize
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- UseRangesCheck.h - clang-tidy --------------------------*- C++ -*-===//
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+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USERANGESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USERANGESCHECK_H
11+
12+
#include "../utils/UseRangesCheck.h"
13+
14+
namespace clang::tidy::modernize {
15+
16+
/// Detects calls to standard library iterator algorithms that could be
17+
/// replaced with a ranges version instead
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-ranges.html
21+
class UseRangesCheck : public utils::UseRangesCheck {
22+
public:
23+
using utils::UseRangesCheck::UseRangesCheck;
24+
25+
ReplacerMap getReplacerMap() const override;
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
27+
};
28+
29+
} // namespace clang::tidy::modernize
30+
31+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USERANGESCHECK_H

clang-tools-extra/clang-tidy/utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_clang_library(clangTidyUtils
2626
TransformerClangTidyCheck.cpp
2727
TypeTraits.cpp
2828
UsingInserter.cpp
29+
UseRangesCheck.cpp
2930

3031
LINK_LIBS
3132
clangTidy

0 commit comments

Comments
 (0)