From 628c67f9a6e0fa13c43d2214fb53e98d29ef0a73 Mon Sep 17 00:00:00 2001 From: Michael Buch Date: Tue, 1 Oct 2024 09:08:09 +0100 Subject: [PATCH] Revert "[libc++] LWG3870: Remove `voidify` (#110355)" This reverts commit 78f9a8b82d772ff04a12ef95f2c9d31ee8f3e409. --- libcxx/docs/Status/Cxx23Issues.csv | 2 +- libcxx/include/CMakeLists.txt | 1 + libcxx/include/__memory/construct_at.h | 5 +-- libcxx/include/__memory/shared_ptr.h | 14 ++++---- .../__memory/uninitialized_algorithms.h | 21 ++++++------ libcxx/include/__memory/voidify.h | 30 +++++++++++++++++ libcxx/include/module.modulemap | 2 ++ libcxx/include/optional | 6 ++-- .../construct_at.pass.cpp | 15 +++++++++ .../ranges_construct_at.pass.cpp | 10 ++++++ ...s_uninitialized_default_construct.pass.cpp | 25 ++++++++++++++ ...uninitialized_default_construct_n.pass.cpp | 12 +++++++ ...ges_uninitialized_value_construct.pass.cpp | 25 ++++++++++++++ ...s_uninitialized_value_construct_n.pass.cpp | 12 +++++++ .../ranges_uninitialized_copy.pass.cpp | 33 +++++++++++++++++++ .../ranges_uninitialized_copy_n.pass.cpp | 16 +++++++++ .../ranges_uninitialized_fill_n.pass.cpp | 14 ++++++++ .../ranges_uninitialized_fill.pass.cpp | 29 ++++++++++++++++ .../ranges_uninitialized_move.pass.cpp | 33 +++++++++++++++++++ .../ranges_uninitialized_move_n.pass.cpp | 16 +++++++++ .../gn/secondary/libcxx/include/BUILD.gn | 1 + 21 files changed, 298 insertions(+), 24 deletions(-) create mode 100644 libcxx/include/__memory/voidify.h diff --git a/libcxx/docs/Status/Cxx23Issues.csv b/libcxx/docs/Status/Cxx23Issues.csv index 63e4176ecba1d7..1c8bb057b09660 100644 --- a/libcxx/docs/Status/Cxx23Issues.csv +++ b/libcxx/docs/Status/Cxx23Issues.csv @@ -296,7 +296,7 @@ "`LWG3862 `__","``basic_const_iterator``'s ``common_type`` specialization is underconstrained","2023-02 (Issaquah)","","","" "`LWG3865 `__","Sorting a range of ``pairs``","2023-02 (Issaquah)","|Complete|","17.0","" "`LWG3869 `__","Deprecate ``std::errc`` constants related to UNIX STREAMS","2023-02 (Issaquah)","|Complete|","19.0","" -"`LWG3870 `__","Remove ``voidify``","2023-02 (Issaquah)","|Complete|","20.0","" +"`LWG3870 `__","Remove ``voidify``","2023-02 (Issaquah)","","","" "`LWG3871 `__","Adjust note about ``terminate``","2023-02 (Issaquah)","","","" "`LWG3872 `__","``basic_const_iterator`` should have custom ``iter_move``","2023-02 (Issaquah)","","","" "`LWG3875 `__","``std::ranges::repeat_view::iterator`` may be ill-formed","2023-02 (Issaquah)","|Complete|","17.0","" diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 9bd1b41b8bfac4..8a63280053340f 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -560,6 +560,7 @@ set(files __memory/unique_temporary_buffer.h __memory/uses_allocator.h __memory/uses_allocator_construction.h + __memory/voidify.h __memory_resource/memory_resource.h __memory_resource/monotonic_buffer_resource.h __memory_resource/polymorphic_allocator.h diff --git a/libcxx/include/__memory/construct_at.h b/libcxx/include/__memory/construct_at.h index d8c97467f54b9f..eb021324800644 100644 --- a/libcxx/include/__memory/construct_at.h +++ b/libcxx/include/__memory/construct_at.h @@ -14,6 +14,7 @@ #include <__config> #include <__iterator/access.h> #include <__memory/addressof.h> +#include <__memory/voidify.h> #include <__type_traits/enable_if.h> #include <__type_traits/is_array.h> #include <__utility/declval.h> @@ -37,7 +38,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD template ()) _Tp(std::declval<_Args>()...))> _LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) { _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"); - return ::new (static_cast(__location)) _Tp(std::forward<_Args>(__args)...); + return ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...); } #endif @@ -48,7 +49,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __l return std::construct_at(__location, std::forward<_Args>(__args)...); #else return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"), - ::new (static_cast(__location)) _Tp(std::forward<_Args>(__args)...); + ::new (std::__voidify(*__location)) _Tp(std::forward<_Args>(__args)...); #endif } diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h index 20c1b69f45ae66..70964e6122d5a6 100644 --- a/libcxx/include/__memory/shared_ptr.h +++ b/libcxx/include/__memory/shared_ptr.h @@ -248,35 +248,33 @@ struct __for_overwrite_tag {}; template struct __shared_ptr_emplace : __shared_weak_count { - using __value_type = __remove_cv_t<_Tp>; - template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&...) : __storage_(std::move(__a)) { static_assert( sizeof...(_Args) == 0, "No argument should be provided to the control block when using _for_overwrite"); - ::new (static_cast(__get_elem())) __value_type; + ::new ((void*)__get_elem()) _Tp; } template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI explicit __shared_ptr_emplace(_Alloc __a, _Args&&... __args) : __storage_(std::move(__a)) { - using _TpAlloc = typename __allocator_traits_rebind<_Alloc, __value_type>::type; + using _TpAlloc = typename __allocator_traits_rebind<_Alloc, __remove_cv_t<_Tp> >::type; _TpAlloc __tmp(*__get_alloc()); allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), std::forward<_Args>(__args)...); } _LIBCPP_HIDE_FROM_ABI _Alloc* __get_alloc() _NOEXCEPT { return __storage_.__get_alloc(); } - _LIBCPP_HIDE_FROM_ABI __value_type* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } + _LIBCPP_HIDE_FROM_ABI _Tp* __get_elem() _NOEXCEPT { return __storage_.__get_elem(); } private: template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI void __on_zero_shared_impl() _NOEXCEPT { - __get_elem()->~__value_type(); + __get_elem()->~_Tp(); } template (__buffer_)->__alloc_); } - _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __value_type* __get_elem() _NOEXCEPT { + _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp* __get_elem() _NOEXCEPT { return std::addressof(reinterpret_cast<_Data*>(__buffer_)->__elem_); } }; diff --git a/libcxx/include/__memory/uninitialized_algorithms.h b/libcxx/include/__memory/uninitialized_algorithms.h index dd72f3c10cf15a..8ff87e28b3bb51 100644 --- a/libcxx/include/__memory/uninitialized_algorithms.h +++ b/libcxx/include/__memory/uninitialized_algorithms.h @@ -21,6 +21,7 @@ #include <__memory/allocator_traits.h> #include <__memory/construct_at.h> #include <__memory/pointer_traits.h> +#include <__memory/voidify.h> #include <__type_traits/enable_if.h> #include <__type_traits/extent.h> #include <__type_traits/is_array.h> @@ -63,7 +64,7 @@ inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitiali try { #endif for (; __ifirst != __ilast && !__stop_copying(__idx); ++__ifirst, (void)++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); + ::new (std::__voidify(*__idx)) _ValueType(*__ifirst); #ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__ofirst, __idx); @@ -93,7 +94,7 @@ __uninitialized_copy_n(_InputIterator __ifirst, _Size __n, _ForwardIterator __of try { #endif for (; __n > 0 && !__stop_copying(__idx); ++__ifirst, (void)++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(*__ifirst); + ::new (std::__voidify(*__idx)) _ValueType(*__ifirst); #ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__ofirst, __idx); @@ -123,7 +124,7 @@ __uninitialized_fill(_ForwardIterator __first, _Sentinel __last, const _Tp& __x) try { #endif for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); + ::new (std::__voidify(*__idx)) _ValueType(__x); #ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -151,7 +152,7 @@ __uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x) { try { #endif for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__x); + ::new (std::__voidify(*__idx)) _ValueType(__x); #ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -181,7 +182,7 @@ __uninitialized_default_construct(_ForwardIterator __first, _Sentinel __last) { try { # endif for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType; + ::new (std::__voidify(*__idx)) _ValueType; # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -207,7 +208,7 @@ inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_default_construct_ try { # endif for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType; + ::new (std::__voidify(*__idx)) _ValueType; # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -234,7 +235,7 @@ __uninitialized_value_construct(_ForwardIterator __first, _Sentinel __last) { try { # endif for (; __idx != __last; ++__idx) - ::new (static_cast(std::addressof(*__idx))) _ValueType(); + ::new (std::__voidify(*__idx)) _ValueType(); # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -260,7 +261,7 @@ inline _LIBCPP_HIDE_FROM_ABI _ForwardIterator __uninitialized_value_construct_n( try { # endif for (; __n > 0; ++__idx, (void)--__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(); + ::new (std::__voidify(*__idx)) _ValueType(); # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__first, __idx); @@ -296,7 +297,7 @@ inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitiali try { # endif for (; __ifirst != __ilast && !__stop_moving(__idx); ++__idx, (void)++__ifirst) { - ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); + ::new (std::__voidify(*__idx)) _ValueType(__iter_move(__ifirst)); } # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { @@ -334,7 +335,7 @@ inline _LIBCPP_HIDE_FROM_ABI pair<_InputIterator, _ForwardIterator> __uninitiali try { # endif for (; __n > 0 && !__stop_moving(__idx); ++__idx, (void)++__ifirst, --__n) - ::new (static_cast(std::addressof(*__idx))) _ValueType(__iter_move(__ifirst)); + ::new (std::__voidify(*__idx)) _ValueType(__iter_move(__ifirst)); # ifndef _LIBCPP_HAS_NO_EXCEPTIONS } catch (...) { std::__destroy(__ofirst, __idx); diff --git a/libcxx/include/__memory/voidify.h b/libcxx/include/__memory/voidify.h new file mode 100644 index 00000000000000..dbd083bd8c1e9a --- /dev/null +++ b/libcxx/include/__memory/voidify.h @@ -0,0 +1,30 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___MEMORY_VOIDIFY_H +#define _LIBCPP___MEMORY_VOIDIFY_H + +#include <__config> +#include <__memory/addressof.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void* __voidify(_Tp& __from) { + // Cast away cv-qualifiers to allow modifying elements of a range through const iterators. + return const_cast(static_cast(std::addressof(__from))); +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___MEMORY_VOIDIFY_H diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index dee9b0b88b7948..e4c4452afe97ff 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -2237,3 +2237,5 @@ module std_private_mbstate_t [system] { header "__mbstate_t.h" export * } + +module std_private_memory_voidify [system] { header "__memory/voidify.h" } diff --git a/libcxx/include/optional b/libcxx/include/optional index 4e44ef990f5d29..7578833685ec1f 100644 --- a/libcxx/include/optional +++ b/libcxx/include/optional @@ -287,7 +287,7 @@ struct __optional_destruct_base<_Tp, false> { static_assert(is_object_v, "instantiation of optional with a non-object type is undefined behavior"); union { char __null_state_; - remove_cv_t __val_; + value_type __val_; }; bool __engaged_; @@ -323,7 +323,7 @@ struct __optional_destruct_base<_Tp, true> { static_assert(is_object_v, "instantiation of optional with a non-object type is undefined behavior"); union { char __null_state_; - remove_cv_t __val_; + value_type __val_; }; bool __engaged_; @@ -377,7 +377,7 @@ struct __optional_storage_base : __optional_destruct_base<_Tp> { _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) { if (this->__engaged_ == __opt.has_value()) { if (this->__engaged_) - static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).__get(); + this->__val_ = std::forward<_That>(__opt).__get(); } else { if (this->__engaged_) this->reset(); diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/construct_at.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/construct_at.pass.cpp index 272441ebedc2f2..13442df9db3ae5 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/construct_at.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/construct_at.pass.cpp @@ -80,6 +80,21 @@ constexpr bool test() a.deallocate(p, 2); } + { + std::allocator a; + Counted const* p = a.allocate(2); + int count = 0; + std::construct_at(p, count); + assert(count == 1); + std::construct_at(p+1, count); + assert(count == 2); + (p+1)->~Counted(); + assert(count == 1); + p->~Counted(); + assert(count == 0); + a.deallocate(const_cast(p), 2); + } + return true; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/ranges_construct_at.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/ranges_construct_at.pass.cpp index f66bf0fd647778..396fed7cc3e49d 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/ranges_construct_at.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/ranges_construct_at.pass.cpp @@ -99,6 +99,16 @@ constexpr bool test() { alloc.deallocate(out, 2); } + // Works with const pointers. + { + int x = 1; + const int* ptr = &x; + + const int* result = std::ranges::construct_at(ptr, 42); + assert(result == ptr); + assert(x == 42); + } + return true; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct.pass.cpp index ef969190c63148..4581f1c909e381 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct.pass.cpp @@ -163,5 +163,30 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators, (iter, sentinel) overload. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_default_construct(buf.cbegin(), buf.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + + // Works with const iterators, (range) overload. + { + constexpr int N = 5; + Buffer buf; + auto range = std::ranges::subrange(buf.cbegin(), buf.cend()); + + std::ranges::uninitialized_default_construct(range); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct_n.pass.cpp index 40fbf226959098..9bebe4b52a8cc9 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.default/ranges_uninitialized_default_construct_n.pass.cpp @@ -75,5 +75,17 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_default_construct_n(buf.cbegin(), N); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct.pass.cpp index 6bab25ca38475b..ad74b82dce1f2b 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct.pass.cpp @@ -183,5 +183,30 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators, (iter, sentinel) overload. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_value_construct(buf.cbegin(), buf.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + + // Works with const iterators, (range) overload. + { + constexpr int N = 5; + Buffer buf; + + auto range = std::ranges::subrange(buf.cbegin(), buf.cend()); + std::ranges::uninitialized_value_construct(range); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct_n.pass.cpp index 4742aefcdb5ada..8f315ce0076d41 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.construct.value/ranges_uninitialized_value_construct_n.pass.cpp @@ -94,5 +94,17 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_value_construct_n(buf.cbegin(), N); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy.pass.cpp index 52ba70b009baba..92dc380728e242 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy.pass.cpp @@ -278,6 +278,39 @@ int main(int, char**) { Counted::reset(); #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators, (iter, sentinel) overload. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::uninitialized_copy(in, in + N, out.cbegin(), out.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + + // Works with const iterators, (range) overload. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::subrange out_range(out.cbegin(), out.cend()); + std::ranges::uninitialized_copy(in, out_range); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + // Conversions, (iter, sentinel) overload. { constexpr int N = 3; diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy_n.pass.cpp index 84fba1aa792932..80082eb3b98e6f 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.copy/ranges_uninitialized_copy_n.pass.cpp @@ -104,6 +104,22 @@ int main(int, char**) { #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::uninitialized_copy_n(in, N, out.cbegin(), out.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + // Conversions. { constexpr int N = 3; diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill.n/ranges_uninitialized_fill_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill.n/ranges_uninitialized_fill_n.pass.cpp index 0b35c1114d87c5..0e8846e8c7c8ff 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill.n/ranges_uninitialized_fill_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill.n/ranges_uninitialized_fill_n.pass.cpp @@ -101,5 +101,19 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_fill_n(buf.cbegin(), N, x); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::all_of(buf.begin(), buf.end(), pred)); + + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill/ranges_uninitialized_fill.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill/ranges_uninitialized_fill.pass.cpp index 6f75fc13a1c397..482515ec9483dd 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill/ranges_uninitialized_fill.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.fill/ranges_uninitialized_fill.pass.cpp @@ -198,5 +198,34 @@ int main(int, char**) { } #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators, (iter, sentinel) overload. + { + constexpr int N = 5; + Buffer buf; + + std::ranges::uninitialized_fill(buf.cbegin(), buf.cend(), x); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::all_of(buf.begin(), buf.end(), pred)); + + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + + // Works with const iterators, (range) overload. + { + constexpr int N = 5; + Buffer buf; + + auto range = std::ranges::subrange(buf.cbegin(), buf.cend()); + std::ranges::uninitialized_fill(range, x); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::all_of(buf.begin(), buf.end(), pred)); + + std::destroy(buf.begin(), buf.end()); + Counted::reset(); + } + return 0; } diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move.pass.cpp index c6b38b4fea8644..56dd25c66e1994 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move.pass.cpp @@ -282,6 +282,39 @@ int main(int, char**) { Counted::reset(); #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators, (iter, sentinel) overload. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::uninitialized_move(in, in + N, out.cbegin(), out.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + + // Works with const iterators, (range) overload. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::subrange out_range (out.cbegin(), out.cend()); + std::ranges::uninitialized_move(in, out_range); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + // Conversions, (iter, sentinel) overload. { constexpr int N = 3; diff --git a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move_n.pass.cpp b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move_n.pass.cpp index cac2acc5932b33..162b4a48537ff3 100644 --- a/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move_n.pass.cpp +++ b/libcxx/test/std/utilities/memory/specialized.algorithms/uninitialized.move/ranges_uninitialized_move_n.pass.cpp @@ -105,6 +105,22 @@ int main(int, char**) { #endif // TEST_HAS_NO_EXCEPTIONS + // Works with const iterators. + { + constexpr int N = 5; + Counted in[N] = {Counted(1), Counted(2), Counted(3), Counted(4), Counted(5)}; + Buffer out; + Counted::reset(); + + std::ranges::uninitialized_move_n(in, N, out.cbegin(), out.cend()); + assert(Counted::current_objects == N); + assert(Counted::total_objects == N); + assert(std::equal(in, in + N, out.begin(), out.end())); + + std::destroy(out.begin(), out.end()); + } + Counted::reset(); + // Conversions. { constexpr int N = 3; diff --git a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn index d850a7f20952d9..3e165c3d6f634e 100644 --- a/llvm/utils/gn/secondary/libcxx/include/BUILD.gn +++ b/llvm/utils/gn/secondary/libcxx/include/BUILD.gn @@ -632,6 +632,7 @@ if (current_toolchain == default_toolchain) { "__memory/unique_temporary_buffer.h", "__memory/uses_allocator.h", "__memory/uses_allocator_construction.h", + "__memory/voidify.h", "__memory_resource/memory_resource.h", "__memory_resource/monotonic_buffer_resource.h", "__memory_resource/polymorphic_allocator.h",