From ba7a6b314fd14bb2c9ff5d3f4fe2b6525514cada Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Tue, 2 Nov 2021 04:52:48 +0000 Subject: [PATCH] Fix iterator_adaptor_base/enumerator_iter to allow composition of llvm::enumerate with llvm::make_filter_range * Properly specify reference type in enumerator_iter * Fix constness of iterator_adaptor_base::operator* Differential Revision: https://reviews.llvm.org/D112981 --- llvm/include/llvm/ADT/STLExtras.h | 3 +-- llvm/include/llvm/ADT/iterator.h | 3 ++- llvm/unittests/ADT/IteratorTest.cpp | 10 ++++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 68bc65604207..a00561760bc0 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1907,8 +1907,7 @@ class enumerator_iter : public iterator_facade_base< enumerator_iter, std::forward_iterator_tag, result_pair, typename std::iterator_traits>::difference_type, - typename std::iterator_traits>::pointer, - typename std::iterator_traits>::reference> { + typename std::iterator_traits>::pointer> { using result_type = result_pair; public: diff --git a/llvm/include/llvm/ADT/iterator.h b/llvm/include/llvm/ADT/iterator.h index b9bac9d385c0..b93aaf39c171 100644 --- a/llvm/include/llvm/ADT/iterator.h +++ b/llvm/include/llvm/ADT/iterator.h @@ -296,7 +296,8 @@ public: return LHS.I < RHS.I; } - ReferenceT operator*() const { return *I; } + const ReferenceT operator*() const { return *I; } + ReferenceT operator*() { return *I; } }; /// An iterator type that allows iterating over the pointees via some diff --git a/llvm/unittests/ADT/IteratorTest.cpp b/llvm/unittests/ADT/IteratorTest.cpp index de74811321a9..f5fa88e7493f 100644 --- a/llvm/unittests/ADT/IteratorTest.cpp +++ b/llvm/unittests/ADT/IteratorTest.cpp @@ -178,6 +178,16 @@ TEST(FilterIteratorTest, Lambda) { EXPECT_EQ((SmallVector{1, 3, 5}), Actual); } +TEST(FilterIteratorTest, Enumerate) { + auto IsOdd = [](auto N) { return N.value() % 2 == 1; }; + int A[] = {0, 1, 2, 3, 4, 5, 6}; + auto Enumerate = llvm::enumerate(A); + SmallVector Actual; + for (auto IndexedValue : make_filter_range(Enumerate, IsOdd)) + Actual.push_back(IndexedValue.value()); + EXPECT_EQ((SmallVector{1, 3, 5}), Actual); +} + TEST(FilterIteratorTest, CallableObject) { int Counter = 0; struct Callable {