diff options
author | 2023-09-01 02:07:15 -0700 | |
---|---|---|
committer | 2023-09-08 23:54:07 +0000 | |
commit | 743ce223c81f33f2955c64f39f85ca6c64099cb7 (patch) | |
tree | 599a9888434fcf757fd64af72187adb5bf6efbbb | |
parent | 5899d7919d5ad8e0c437e72c4aa04d4024e32431 (diff) |
Stop inheriting from std::iterator
std::iterator was deprecated in C++17 and after upgrading libc++, the
compiler warns about the many uses of it in ART.
p0174r1 gives a couple of reasons for its deprecation:
"The long sequence of void arguments is much less clear to the reader
than simply providing the expected typedefs in the class definition
itself, which is the approach taken by the current working draft, ...
"In addition to the reduced clarity, the iterator template also lays a
trap for the unwary, as in typical usage it will be a dependent base
class, which means it will not be looking into during name lookup from
within the class or its member functions."
The first reason is illustrated by the comments in
BitTable::const_iterator. The second reason is illustrated by the
various "using foo = typename std::iterator<...>::foo" declarations.
Follow p0174r1's advice and simply provide the 5 typedefs in each
iterator class.
Bug: b/175635923
Test: treehugger
Change-Id: I2fba5af68eb05fd0a8ba5e2add0c8b8ed1ebee1a
-rw-r--r-- | compiler/optimizing/nodes.h | 18 | ||||
-rw-r--r-- | dexlayout/dex_ir.h | 13 | ||||
-rw-r--r-- | libartbase/base/bit_table.h | 11 | ||||
-rw-r--r-- | libartbase/base/bit_utils_iterator.h | 9 | ||||
-rw-r--r-- | libartbase/base/bit_vector.h | 9 | ||||
-rw-r--r-- | libartbase/base/intrusive_forward_list.h | 8 | ||||
-rw-r--r-- | libartbase/base/stl_util.h | 35 | ||||
-rw-r--r-- | libartbase/base/stride_iterator.h | 11 | ||||
-rw-r--r-- | libdexfile/dex/class_accessor.h | 10 | ||||
-rw-r--r-- | libdexfile/dex/class_iterator.h | 9 | ||||
-rw-r--r-- | libdexfile/dex/dex_instruction_iterator.h | 10 | ||||
-rw-r--r-- | runtime/mirror/object_array.h | 8 |
12 files changed, 108 insertions, 43 deletions
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h index 1ad11d80a5..a44e349b4d 100644 --- a/compiler/optimizing/nodes.h +++ b/compiler/optimizing/nodes.h @@ -2194,9 +2194,14 @@ class HEnvironment : public ArenaObject<kArenaAllocEnvironment> { std::ostream& operator<<(std::ostream& os, const HInstruction& rhs); // Iterates over the Environments -class HEnvironmentIterator : public ValueObject, - public std::iterator<std::forward_iterator_tag, HEnvironment*> { +class HEnvironmentIterator : public ValueObject { public: + using iterator_category = std::forward_iterator_tag; + using value_type = HEnvironment*; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + explicit HEnvironmentIterator(HEnvironment* cur) : cur_(cur) {} HEnvironment* operator*() const { @@ -2915,9 +2920,14 @@ class HBackwardInstructionIterator : public ValueObject { }; template <typename InnerIter> -struct HSTLInstructionIterator : public ValueObject, - public std::iterator<std::forward_iterator_tag, HInstruction*> { +struct HSTLInstructionIterator : public ValueObject { public: + using iterator_category = std::forward_iterator_tag; + using value_type = HInstruction*; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + static_assert(std::is_same_v<InnerIter, HBackwardInstructionIterator> || std::is_same_v<InnerIter, HInstructionIterator> || std::is_same_v<InnerIter, HInstructionIteratorHandleChanges>, diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h index a4b43b6055..229e948c12 100644 --- a/dexlayout/dex_ir.h +++ b/dexlayout/dex_ir.h @@ -110,13 +110,14 @@ class AbstractDispatcher { DISALLOW_COPY_AND_ASSIGN(AbstractDispatcher); }; -template<class T> class Iterator : public std::iterator<std::random_access_iterator_tag, T> { +template <class T> +class Iterator { public: - using value_type = typename std::iterator<std::random_access_iterator_tag, T>::value_type; - using difference_type = - typename std::iterator<std::random_access_iterator_tag, value_type>::difference_type; - using pointer = typename std::iterator<std::random_access_iterator_tag, value_type>::pointer; - using reference = typename std::iterator<std::random_access_iterator_tag, value_type>::reference; + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; Iterator(const Iterator&) = default; Iterator(Iterator&&) noexcept = default; diff --git a/libartbase/base/bit_table.h b/libartbase/base/bit_table.h index 227f5eb082..eb97b54e9d 100644 --- a/libartbase/base/bit_table.h +++ b/libartbase/base/bit_table.h @@ -153,13 +153,13 @@ static const char* const* GetBitTableColumnNamesImpl(std::index_sequence<Columns template<typename Accessor> class BitTable : public BitTableBase<Accessor::kNumColumns> { public: - class const_iterator : public std::iterator<std::random_access_iterator_tag, - /* value_type */ Accessor, - /* difference_type */ int32_t, - /* pointer */ void, - /* reference */ void> { + class const_iterator { public: + using iterator_category = std::random_access_iterator_tag; + using value_type = Accessor; using difference_type = int32_t; + using pointer = void; + using reference = void; const_iterator() {} const_iterator(const BitTable* table, uint32_t row) : table_(table), row_(row) {} const_iterator operator+(difference_type n) { return const_iterator(table_, row_ + n); } @@ -189,6 +189,7 @@ class BitTable : public BitTableBase<Accessor::kNumColumns> { DCHECK_LT(row_ + index, table_->NumRows()); return Accessor(table_, row_ + index); } + private: const BitTable* table_ = nullptr; uint32_t row_ = 0; diff --git a/libartbase/base/bit_utils_iterator.h b/libartbase/base/bit_utils_iterator.h index bfcff86a48..296280ee95 100644 --- a/libartbase/base/bit_utils_iterator.h +++ b/libartbase/base/bit_utils_iterator.h @@ -32,14 +32,19 @@ namespace art { // Using the Curiously Recurring Template Pattern to implement everything shared // by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*(). template <typename T, typename Iter> -class BitIteratorBase - : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> { +class BitIteratorBase { static_assert(std::is_integral_v<T>, "T must be integral"); static_assert(std::is_unsigned_v<T>, "T must be unsigned"); static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size"); public: + using iterator_category = std::forward_iterator_tag; + using value_type = uint32_t; + using difference_type = ptrdiff_t; + using pointer = void; + using reference = void; + BitIteratorBase() : bits_(0u) { } explicit BitIteratorBase(T bits) : bits_(bits) { } diff --git a/libartbase/base/bit_vector.h b/libartbase/base/bit_vector.h index 071577bac1..a710c5d6ea 100644 --- a/libartbase/base/bit_vector.h +++ b/libartbase/base/bit_vector.h @@ -51,9 +51,14 @@ class BitVector { * // Use idx. * } */ - class IndexIterator : - public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, uint32_t> { + class IndexIterator { public: + using iterator_category = std::forward_iterator_tag; + using value_type = uint32_t; + using difference_type = ptrdiff_t; + using pointer = void; + using reference = uint32_t; + bool operator==(const IndexIterator& other) const; bool operator!=(const IndexIterator& other) const { diff --git a/libartbase/base/intrusive_forward_list.h b/libartbase/base/intrusive_forward_list.h index 06dd4079ef..f7f7395e74 100644 --- a/libartbase/base/intrusive_forward_list.h +++ b/libartbase/base/intrusive_forward_list.h @@ -59,8 +59,14 @@ template <typename T, class IntrusiveForwardList; template <typename T, typename HookTraits> -class IntrusiveForwardListIterator : public std::iterator<std::forward_iterator_tag, T> { +class IntrusiveForwardListIterator { public: + using iterator_category = std::forward_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + // Construct/copy/destroy (except the private constructor used by IntrusiveForwardList<>). IntrusiveForwardListIterator() : hook_(nullptr) { } IntrusiveForwardListIterator(const IntrusiveForwardListIterator& src) = default; diff --git a/libartbase/base/stl_util.h b/libartbase/base/stl_util.h index 2c9547f020..cb3e4cb43e 100644 --- a/libartbase/base/stl_util.h +++ b/libartbase/base/stl_util.h @@ -156,10 +156,14 @@ static inline std::vector<T*> MakeNonOwningPointerVector(const std::vector<std:: } template <typename IterLeft, typename IterRight> -class ZipLeftIter : public std::iterator< - std::forward_iterator_tag, - std::pair<typename IterLeft::value_type, typename IterRight::value_type>> { +class ZipLeftIter { public: + using iterator_category = std::forward_iterator_tag; + using value_type = std::pair<typename IterLeft::value_type, typename IterRight::value_type>; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + ZipLeftIter(IterLeft left, IterRight right) : left_iter_(left), right_iter_(right) {} ZipLeftIter<IterLeft, IterRight>& operator++() { ++left_iter_; @@ -186,8 +190,14 @@ class ZipLeftIter : public std::iterator< IterRight right_iter_; }; -class CountIter : public std::iterator<std::forward_iterator_tag, size_t, size_t, size_t, size_t> { +class CountIter { public: + using iterator_category = std::forward_iterator_tag; + using value_type = size_t; + using difference_type = size_t; + using pointer = size_t; + using reference = size_t; + CountIter() : count_(0) {} explicit CountIter(size_t count) : count_(count) {} CountIter& operator++() { @@ -238,9 +248,14 @@ static inline IterationRange<CountIter> Range(size_t end) { } template <typename RealIter, typename Filter> -struct FilterIterator - : public std::iterator<std::forward_iterator_tag, typename RealIter::value_type> { +struct FilterIterator { public: + using iterator_category = std::forward_iterator_tag; + using value_type = typename RealIter::value_type; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + FilterIterator(RealIter rl, Filter cond, std::optional<RealIter> end = std::nullopt) @@ -323,8 +338,14 @@ SafePrinter<Val> SafePrint(const Val* v) { } // Helper struct for iterating a split-string without allocation. -struct SplitStringIter : public std::iterator<std::forward_iterator_tag, std::string_view> { +struct SplitStringIter { public: + using iterator_category = std::forward_iterator_tag; + using value_type = std::string_view; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + // Direct iterator constructor. The iteration state is only the current index. // We use that with the split char and the full string to get the current and // next segment. diff --git a/libartbase/base/stride_iterator.h b/libartbase/base/stride_iterator.h index 6a7e4bef67..7d1b9fcf61 100644 --- a/libartbase/base/stride_iterator.h +++ b/libartbase/base/stride_iterator.h @@ -23,11 +23,14 @@ namespace art { -template<typename T> -class StrideIterator : public std::iterator<std::random_access_iterator_tag, T> { +template <typename T> +class StrideIterator { public: - using difference_type = - typename std::iterator<std::random_access_iterator_tag, T>::difference_type; + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; StrideIterator(const StrideIterator&) = default; StrideIterator(StrideIterator&&) noexcept = default; diff --git a/libdexfile/dex/class_accessor.h b/libdexfile/dex/class_accessor.h index 1061ff69ec..401195c6fe 100644 --- a/libdexfile/dex/class_accessor.h +++ b/libdexfile/dex/class_accessor.h @@ -178,11 +178,13 @@ class ClassAccessor { }; template <typename DataType> - class DataIterator : public std::iterator<std::forward_iterator_tag, DataType> { + class DataIterator { public: - using value_type = typename std::iterator<std::forward_iterator_tag, DataType>::value_type; - using difference_type = - typename std::iterator<std::forward_iterator_tag, value_type>::difference_type; + using iterator_category = std::forward_iterator_tag; + using value_type = DataType; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; DataIterator(const DexFile& dex_file, uint32_t position, diff --git a/libdexfile/dex/class_iterator.h b/libdexfile/dex/class_iterator.h index 8ed585b0b1..56913a927a 100644 --- a/libdexfile/dex/class_iterator.h +++ b/libdexfile/dex/class_iterator.h @@ -41,10 +41,13 @@ class ClassIteratorData { }; // Iterator for visiting classes in a Dex file. -class ClassIterator : public std::iterator<std::forward_iterator_tag, ClassIteratorData> { +class ClassIterator { public: - using value_type = std::iterator<std::forward_iterator_tag, ClassIteratorData>::value_type; - using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type; + using iterator_category = std::forward_iterator_tag; + using value_type = ClassIteratorData; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; ClassIterator(const DexFile& dex_file, uint32_t class_def_idx) : data_(dex_file, class_def_idx) {} diff --git a/libdexfile/dex/dex_instruction_iterator.h b/libdexfile/dex/dex_instruction_iterator.h index 6c7f42a925..6f661d922f 100644 --- a/libdexfile/dex/dex_instruction_iterator.h +++ b/libdexfile/dex/dex_instruction_iterator.h @@ -57,11 +57,13 @@ class DexInstructionPcPair { }; // Base helper class to prevent duplicated comparators. -class DexInstructionIteratorBase : public - std::iterator<std::forward_iterator_tag, DexInstructionPcPair> { +class DexInstructionIteratorBase { public: - using value_type = std::iterator<std::forward_iterator_tag, DexInstructionPcPair>::value_type; - using difference_type = std::iterator<std::forward_iterator_tag, value_type>::difference_type; + using iterator_category = std::forward_iterator_tag; + using value_type = DexInstructionPcPair; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; explicit DexInstructionIteratorBase(const Instruction* inst, uint32_t dex_pc) : data_(reinterpret_cast<const uint16_t*>(inst), dex_pc) {} diff --git a/runtime/mirror/object_array.h b/runtime/mirror/object_array.h index 9a53708018..c6b1e01793 100644 --- a/runtime/mirror/object_array.h +++ b/runtime/mirror/object_array.h @@ -162,11 +162,17 @@ class MANAGED ObjectArray: public Array { // Everything is NO_THREAD_SAFETY_ANALYSIS to work-around STL incompat with thread-annotations. // Everything should have REQUIRES_SHARED(Locks::mutator_lock_). template <typename T, typename Container> -class ArrayIter : public std::iterator<std::forward_iterator_tag, ObjPtr<T>> { +class ArrayIter { private: using Iter = ArrayIter<T, Container>; public: + using iterator_category = std::forward_iterator_tag; + using value_type = ObjPtr<T>; + using difference_type = ptrdiff_t; + using pointer = value_type*; + using reference = value_type&; + ArrayIter(Container array, int32_t idx) NO_THREAD_SAFETY_ANALYSIS : array_(array), idx_(idx) { CheckIdx(); } |