[llvm] 7770d8f - [ADT] Use `adl_begin`/`adl_end` in `make_filter_range` (#130512)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 10 09:13:38 PDT 2025
Author: Jakub Kuderski
Date: 2025-03-10T12:13:35-04:00
New Revision: 7770d8f2256ba051296fe659b62145ef53027c5a
URL: https://github.com/llvm/llvm-project/commit/7770d8f2256ba051296fe659b62145ef53027c5a
DIFF: https://github.com/llvm/llvm-project/commit/7770d8f2256ba051296fe659b62145ef53027c5a.diff
LOG: [ADT] Use `adl_begin`/`adl_end` in `make_filter_range` (#130512)
This is to make sure that ADT helpers consistently use argument
dependent lookup when dealing with input ranges.
This was a part of https://github.com/llvm/llvm-project/pull/87936 but
reverted due to buildbot failures.
Also fix potential issue with double-move on the input range.
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/IteratorTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index fe1d010574e31..774df5742053c 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -574,11 +574,9 @@ iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
make_filter_range(RangeT &&Range, PredicateT Pred) {
using FilterIteratorT =
filter_iterator<detail::IterOfRange<RangeT>, PredicateT>;
- return make_range(
- FilterIteratorT(std::begin(std::forward<RangeT>(Range)),
- std::end(std::forward<RangeT>(Range)), Pred),
- FilterIteratorT(std::end(std::forward<RangeT>(Range)),
- std::end(std::forward<RangeT>(Range)), Pred));
+ auto B = adl_begin(Range);
+ auto E = adl_end(Range);
+ return make_range(FilterIteratorT(B, E, Pred), FilterIteratorT(E, E, Pred));
}
/// A pseudo-iterator adaptor that is designed to implement "early increment"
diff --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp
index a0d3c9b564d85..691fbce5080ff 100644
--- a/llvm/unittests/ADT/IteratorTest.cpp
+++ b/llvm/unittests/ADT/IteratorTest.cpp
@@ -22,6 +22,22 @@ using testing::ElementsAre;
namespace {
+namespace adl_test {
+struct WithFreeBeginEnd {
+ int data[3] = {21, 22, 23};
+};
+
+auto begin(const WithFreeBeginEnd &X) { return std::begin(X.data); }
+auto end(const WithFreeBeginEnd &X) { return std::end(X.data); }
+
+struct WithFreeRBeginREnd {
+ int data[3] = {42, 43, 44};
+};
+
+auto rbegin(const WithFreeRBeginREnd &X) { return std::rbegin(X.data); }
+auto rend(const WithFreeRBeginREnd &X) { return std::rend(X.data); }
+} // namespace adl_test
+
template <int> struct Shadow;
struct WeirdIter
@@ -364,6 +380,14 @@ TEST(FilterIteratorTest, ReverseFilterRange) {
EXPECT_EQ((SmallVector<int, 4>{6, 4, 2, 0}), Actual4);
}
+TEST(FilterIteratorTest, ADL) {
+ // Make sure that we use the `begin`/`end` functions
+ // from `adl_test`, using ADL.
+ adl_test::WithFreeBeginEnd R;
+ auto IsOdd = [](int N) { return N % 2 != 0; };
+ EXPECT_THAT(make_filter_range(R, IsOdd), ElementsAre(21, 23));
+}
+
TEST(PointerIterator, Basic) {
int A[] = {1, 2, 3, 4};
pointer_iterator<int *> Begin(std::begin(A)), End(std::end(A));
@@ -395,18 +419,9 @@ TEST(PointerIterator, Range) {
EXPECT_EQ(A + I++, P);
}
-namespace rbegin_detail {
-struct WithFreeRBegin {
- int data[3] = {42, 43, 44};
-};
-
-auto rbegin(const WithFreeRBegin &X) { return std::rbegin(X.data); }
-auto rend(const WithFreeRBegin &X) { return std::rend(X.data); }
-} // namespace rbegin_detail
-
TEST(ReverseTest, ADL) {
// Check that we can find the rbegin/rend functions via ADL.
- rbegin_detail::WithFreeRBegin Foo;
+ adl_test::WithFreeRBeginREnd Foo;
EXPECT_THAT(reverse(Foo), ElementsAre(44, 43, 42));
}
More information about the llvm-commits
mailing list