diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -320,6 +320,7 @@ __iterator/sortable.h __iterator/unreachable_sentinel.h __iterator/wrap_iter.h + __libcxx/debug_randomize_range.h __locale __mbstate_t.h __memory/addressof.h diff --git a/libcxx/include/__algorithm/nth_element.h b/libcxx/include/__algorithm/nth_element.h --- a/libcxx/include/__algorithm/nth_element.h +++ b/libcxx/include/__algorithm/nth_element.h @@ -15,6 +15,7 @@ #include <__config> #include <__debug> #include <__iterator/iterator_traits.h> +#include <__libcxx/debug_randomize_range.h> #include <__utility/swap.h> #if defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY) @@ -227,12 +228,12 @@ void nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, _RandomAccessIterator __last, _Compare __comp) { - _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __last); + std::__debug_randomize_range(__first, __last); typedef typename __comp_ref_type<_Compare>::type _Comp_ref; _VSTD::__nth_element<_Comp_ref>(__first, __nth, __last, __comp); - _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __nth); + std::__debug_randomize_range(__first, __nth); if (__nth != __last) { - _LIBCPP_DEBUG_RANDOMIZE_RANGE(++__nth, __last); + std::__debug_randomize_range(++__nth, __last); } } diff --git a/libcxx/include/__algorithm/partial_sort.h b/libcxx/include/__algorithm/partial_sort.h --- a/libcxx/include/__algorithm/partial_sort.h +++ b/libcxx/include/__algorithm/partial_sort.h @@ -17,6 +17,7 @@ #include <__config> #include <__debug> #include <__iterator/iterator_traits.h> +#include <__libcxx/debug_randomize_range.h> #include <__utility/swap.h> #if defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY) @@ -55,10 +56,10 @@ partial_sort(_RandomAccessIterator __first, _RandomAccessIterator __middle, _RandomAccessIterator __last, _Compare __comp) { - _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __last); + std::__debug_randomize_range(__first, __last); typedef typename __comp_ref_type<_Compare>::type _Comp_ref; _VSTD::__partial_sort<_Comp_ref>(__first, __middle, __last, __comp); - _LIBCPP_DEBUG_RANDOMIZE_RANGE(__middle, __last); + std::__debug_randomize_range(__middle, __last); } template diff --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h --- a/libcxx/include/__algorithm/sort.h +++ b/libcxx/include/__algorithm/sort.h @@ -20,6 +20,7 @@ #include <__functional/operations.h> #include <__functional/ranges_operations.h> #include <__iterator/iterator_traits.h> +#include <__libcxx/debug_randomize_range.h> #include <__utility/swap.h> #include #include @@ -582,7 +583,7 @@ template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __sort_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, _Comp& __comp) { - _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __last); + std::__debug_randomize_range(__first, __last); using _Comp_ref = typename __comp_ref_type<_Comp>::type; if (__libcpp_is_constant_evaluated()) { std::__partial_sort<_Comp_ref>(__first, __last, __last, _Comp_ref(__comp)); diff --git a/libcxx/include/__debug b/libcxx/include/__debug --- a/libcxx/include/__debug +++ b/libcxx/include/__debug @@ -27,22 +27,6 @@ # define _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY #endif -// TODO: Define this as a function instead -#if defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY) -# if defined(_LIBCPP_CXX03_LANG) -# error Support for unspecified stability is only for C++11 and higher -# endif -# define _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __last) \ - do { \ - if (!__builtin_is_constant_evaluated()) \ - std::shuffle(__first, __last, __libcpp_debug_randomizer()); \ - } while (false) -#else -# define _LIBCPP_DEBUG_RANDOMIZE_RANGE(__first, __last) \ - do { \ - } while (false) -#endif - #ifdef _LIBCPP_ENABLE_DEBUG_MODE # define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(::std::__libcpp_is_constant_evaluated() || (x), m) #else diff --git a/libcxx/include/__libcxx/debug_randomize_range.h b/libcxx/include/__libcxx/debug_randomize_range.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__libcxx/debug_randomize_range.h @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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___LIBCXX_DEBUG_RANDOMIZE_RANGE_H +#define _LIBCPP___LIBCXX_DEBUG_RANDOMIZE_RANGE_H + +#include <__algorithm/shuffle.h> +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +template +_LIBCPP_CONSTEXPR void __debug_randomize_range(_Iterator __first, _Iterator __last) { +#ifdef _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY +# ifdef _LIBCPP_CXX03_LANG +# error Support for unspecified stability is only for C++11 and higher +# endif + + if (!__libcpp_is_constant_evaluated()) + std::shuffle(__first, __last, __libcpp_debug_randomizer()); +#else + (void) __first; + (void) __last; +#endif +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___LIBCXX_DEBUG_RANDOMIZE_RANGE_H diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -742,6 +742,11 @@ header "latch" export * } + + module __libcxx { + module debug_randomize_range { private header "__libcxx/debug_randomize_range.h" } + } + module limits { header "limits" export * diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -351,6 +351,7 @@ #include <__iterator/sortable.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/sortable.h'}} #include <__iterator/unreachable_sentinel.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/unreachable_sentinel.h'}} #include <__iterator/wrap_iter.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/wrap_iter.h'}} +#include <__libcxx/debug_randomize_range.h> // expected-error@*:* {{use of private header from outside its module: '__libcxx/debug_randomize_range.h'}} #include <__locale> // expected-error@*:* {{use of private header from outside its module: '__locale'}} #include <__mbstate_t.h> // expected-error@*:* {{use of private header from outside its module: '__mbstate_t.h'}} #include <__memory/addressof.h> // expected-error@*:* {{use of private header from outside its module: '__memory/addressof.h'}}