Skip to content

Commit fc2a81b

Browse files
committed
adjust supported algorithms and convert llvm -> LLVM
1 parent bb17fb6 commit fc2a81b

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

clang-tools-extra/clang-tidy/llvm/UseRangesCheck.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,19 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
5555

5656
// Single range algorithms
5757
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(SingleSig),
58-
{"all_of", "any_of", "none_of", "for_each",
59-
"find", "find_if", "find_if_not", "count",
60-
"count_if", "transform", "replace", "remove_if",
61-
"sort", "partition", "is_sorted", "min_element",
62-
"max_element", "binary_search", "lower_bound", "upper_bound",
63-
"unique", "copy", "copy_if", "fill"});
58+
{"all_of", "any_of", "none_of",
59+
"for_each", "find", "find_if",
60+
"find_if_not", "count", "count_if",
61+
"transform", "replace", "remove_if",
62+
"stable_sort", "partition", "partition_point",
63+
"is_sorted", "min_element", "max_element",
64+
"binary_search", "lower_bound", "upper_bound",
65+
"unique", "copy", "copy_if",
66+
"fill"});
6467

6568
// Two range algorithms
6669
AddStdToLLVM(llvm::makeIntrusiveRefCnt<StdToLLVMReplacer>(TwoSig),
67-
{"equal", "mismatch"});
70+
{"equal", "mismatch", "includes"});
6871

6972
return Results;
7073
}
@@ -73,7 +76,7 @@ UseRangesCheck::UseRangesCheck(StringRef Name, ClangTidyContext *Context)
7376
: utils::UseRangesCheck(Name, Context) {}
7477

7578
DiagnosticBuilder UseRangesCheck::createDiag(const CallExpr &Call) {
76-
return diag(Call.getBeginLoc(), "use a llvm range-based algorithm");
79+
return diag(Call.getBeginLoc(), "use a LLVM range-based algorithm");
7780
}
7881

7982
ArrayRef<std::pair<StringRef, StringRef>>

clang-tools-extra/docs/clang-tidy/checks/llvm/use-ranges.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ Calls to the following ``std`` library algorithms are checked:
4040
``std::find_if``,
4141
``std::find_if_not``,
4242
``std::for_each``,
43+
``std::includes``,
4344
``std::is_sorted``,
4445
``std::lower_bound``,
4546
``std::max_element``,
4647
``std::min_element``,
4748
``std::mismatch``,
4849
``std::none_of``,
4950
``std::partition``,
51+
``std::partition_point``,
5052
``std::remove_if``,
5153
``std::replace``,
52-
``std::sort``,
54+
``std::stable_sort``,
5355
``std::transform``,
5456
``std::unique``,
5557
``std::upper_bound``.

clang-tools-extra/test/clang-tidy/checkers/llvm/use-ranges.cpp

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ InputIt find(InputIt first, InputIt last, const T &value);
2727
template <class RandomIt>
2828
void sort(RandomIt first, RandomIt last);
2929

30+
template <class RandomIt>
31+
void stable_sort(RandomIt first, RandomIt last);
32+
3033
template <class InputIt, class UnaryPredicate>
3134
bool all_of(InputIt first, InputIt last, UnaryPredicate p);
3235

@@ -60,6 +63,9 @@ ForwardIt unique(ForwardIt first, ForwardIt last);
6063
template <class ForwardIt>
6164
bool is_sorted(ForwardIt first, ForwardIt last);
6265

66+
template <class InputIt1, class InputIt2>
67+
bool includes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2);
68+
6369
} // namespace std
6470

6571
bool is_even(int x);
@@ -70,54 +76,61 @@ void test_positive() {
7076
int arr[5] = {1, 2, 3, 4, 5};
7177

7278
auto it1 = std::find(vec.begin(), vec.end(), 3);
73-
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a llvm range-based algorithm
79+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a LLVM range-based algorithm
7480
// CHECK-FIXES: auto it1 = llvm::find(vec, 3);
7581

7682
auto it2 = std::find(std::begin(arr), std::end(arr), 3);
77-
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a llvm range-based algorithm
83+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a LLVM range-based algorithm
7884
// CHECK-FIXES: auto it2 = llvm::find(arr, 3);
7985

80-
std::sort(vec.begin(), vec.end());
81-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a llvm range-based algorithm
82-
// CHECK-FIXES: llvm::sort(vec);
86+
std::stable_sort(vec.begin(), vec.end());
87+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a LLVM range-based algorithm
88+
// CHECK-FIXES: llvm::stable_sort(vec);
8389

8490
bool all = std::all_of(vec.begin(), vec.end(), is_even);
85-
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a llvm range-based algorithm
91+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use a LLVM range-based algorithm
8692
// CHECK-FIXES: bool all = llvm::all_of(vec, is_even);
8793

8894
std::for_each(vec.begin(), vec.end(), double_ref);
89-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a llvm range-based algorithm
95+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a LLVM range-based algorithm
9096
// CHECK-FIXES: llvm::for_each(vec, double_ref);
9197

9298
auto min_it = std::min_element(vec.begin(), vec.end());
93-
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a llvm range-based algorithm
99+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a LLVM range-based algorithm
94100
// CHECK-FIXES: auto min_it = llvm::min_element(vec);
95101

96102
std::vector<int> vec2;
97103
bool eq = std::equal(vec.begin(), vec.end(), vec2.begin(), vec2.end());
98-
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a llvm range-based algorithm
104+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use a LLVM range-based algorithm
99105
// CHECK-FIXES: bool eq = llvm::equal(vec, vec2);
100106

101107
std::copy(vec.begin(), vec.end(), vec2.begin());
102-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a llvm range-based algorithm
108+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a LLVM range-based algorithm
103109
// CHECK-FIXES: llvm::copy(vec, vec2.begin());
104110

105111
std::fill(vec.begin(), vec.end(), 0);
106-
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a llvm range-based algorithm
112+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a LLVM range-based algorithm
107113
// CHECK-FIXES: llvm::fill(vec, 0);
108114

109115
auto last = std::unique(vec.begin(), vec.end());
110-
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use a llvm range-based algorithm
116+
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use a LLVM range-based algorithm
111117
// CHECK-FIXES: auto last = llvm::unique(vec);
112118

113119
bool sorted = std::is_sorted(vec.begin(), vec.end());
114-
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a llvm range-based algorithm
120+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use a LLVM range-based algorithm
115121
// CHECK-FIXES: bool sorted = llvm::is_sorted(vec);
122+
123+
std::includes(vec.begin(), vec.end(), std::begin(arr), std::end(arr));
124+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a LLVM range-based algorithm
125+
// CHECK-FIXES: llvm::includes(vec, arr);
116126
}
117127

118128
void test_negative() {
119129
std::vector<int> v;
120130

131+
// can not use `llvm::sort` because of potential different ordering from `std::sort`.
132+
std::sort(v.begin(), v.end());
133+
121134
//non-begin/end iterators
122135
auto it1 = std::find(v.begin() + 1, v.end(), 2);
123136
auto it2 = std::find(v.begin(), v.end() - 1, 2);

0 commit comments

Comments
 (0)