Skip to content

Add support for std::rotate(_copy) and inplace_merge to modernize-use-ranges #99057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clang-tools-extra/clang-tidy/modernize/UseRangesCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ static constexpr const char *TwoRangeNames[] = {
"is_permutation",
};

static constexpr const char *SinglePivotRangeNames[] = {"rotate", "rotate_copy",
"inplace_merge"};

namespace {
class StdReplacer : public utils::UseRangesCheck::Replacer {
public:
Expand Down Expand Up @@ -141,13 +144,19 @@ utils::UseRangesCheck::ReplacerMap UseRangesCheck::getReplacerMap() const {
// 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}};
{TwoRangeFunc, TwoRangeNames},
{SinglePivotFunc, SinglePivotRangeNames}};
SmallString<64> Buff;
for (const auto &[Signatures, Values] : AlgorithmNames) {
auto Replacer = llvm::makeIntrusiveRefCnt<StdAlgorithmReplacer>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Calls to the following std library algorithms are checked:
``std::for_each``,
``std::generate``,
``std::includes``,
``std::inplace_merge``,
``std::iota``,
``std::is_heap_until``,
``std::is_heap``,
Expand Down Expand Up @@ -79,6 +80,8 @@ Calls to the following std library algorithms are checked:
``std::replace``,
``std::reverse_copy``,
``std::reverse``,
``std::rotate``,
``std::rotate_copy``,
``std::sample``,
``std::search``,
``std::set_difference``,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ bool equal(InputIt1 first1, InputIt1 last1,
template <class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value);

template <class ForwardIt>
ForwardIt rotate(ForwardIt first, ForwardIt middle, ForwardIt last);

} // namespace std

#endif // USE_RANGES_FAKE_STD_H
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ void Positives() {
// CHECK-MESSAGES-CPP23: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES-CPP23: std::ranges::iota(I, 0);

std::rotate(I.begin(), I.begin() + 2, I.end());
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use a ranges version of this algorithm
// CHECK-FIXES: std::ranges::rotate(I, I.begin() + 2);

using std::find;
namespace my_std = std;

Expand Down Expand Up @@ -100,4 +104,11 @@ void Negatives() {
std::equal(I.begin(), I.end(), J.end(), J.end());
std::equal(std::rbegin(I), std::rend(I), std::rend(J), std::rbegin(J));
std::equal(I.begin(), J.end(), I.begin(), I.end());

// std::rotate expects the full range in the 1st and 3rd argument.
// Anyone writing this code has probably written a bug, but this isn't the
// purpose of this check.
std::rotate(I.begin(), I.end(), I.begin() + 2);
// Pathological, but probably shouldn't diagnose this
std::rotate(I.begin(), I.end(), I.end() + 0);
}
Loading