2021-07-28 17:11:04 -07:00
|
|
|
// -*- 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2022-11-25 10:25:10 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
#ifndef _LIBCPP___RANGES_REVERSE_VIEW_H
|
|
|
|
#define _LIBCPP___RANGES_REVERSE_VIEW_H
|
|
|
|
|
2021-08-10 22:01:19 -04:00
|
|
|
#include <__concepts/constructible.h>
|
2021-07-28 17:11:04 -07:00
|
|
|
#include <__config>
|
|
|
|
#include <__iterator/concepts.h>
|
|
|
|
#include <__iterator/next.h>
|
|
|
|
#include <__iterator/reverse_iterator.h>
|
|
|
|
#include <__ranges/access.h>
|
|
|
|
#include <__ranges/all.h>
|
|
|
|
#include <__ranges/concepts.h>
|
|
|
|
#include <__ranges/enable_borrowed_range.h>
|
|
|
|
#include <__ranges/non_propagating_cache.h>
|
2021-09-24 12:04:12 -04:00
|
|
|
#include <__ranges/range_adaptor.h>
|
2021-07-28 17:11:04 -07:00
|
|
|
#include <__ranges/size.h>
|
2021-09-24 12:04:12 -04:00
|
|
|
#include <__ranges/subrange.h>
|
2021-07-28 17:11:04 -07:00
|
|
|
#include <__ranges/view_interface.h>
|
2023-02-12 12:32:36 +01:00
|
|
|
#include <__type_traits/conditional.h>
|
|
|
|
#include <__type_traits/remove_cvref.h>
|
2021-09-24 12:04:12 -04:00
|
|
|
#include <__utility/forward.h>
|
2021-08-10 22:01:19 -04:00
|
|
|
#include <__utility/move.h>
|
2021-07-28 17:11:04 -07:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2021-07-28 17:11:04 -07:00
|
|
|
#endif
|
|
|
|
|
[libc++] Fix missing and incorrect push/pop macros (#79204)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
2024-01-25 15:48:46 -05:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-02-14 00:56:09 +01:00
|
|
|
#if _LIBCPP_STD_VER >= 20
|
2021-07-28 17:11:04 -07:00
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
template <view _View>
|
|
|
|
requires bidirectional_range<_View>
|
|
|
|
class reverse_view : public view_interface<reverse_view<_View>> {
|
|
|
|
// We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
|
|
|
|
// amortized O(1) begin() method.
|
|
|
|
static constexpr bool _UseCache = !random_access_range<_View> && !common_range<_View>;
|
|
|
|
using _Cache = _If<_UseCache, __non_propagating_cache<reverse_iterator<iterator_t<_View>>>, __empty_cache>;
|
2022-02-10 13:23:40 +02:00
|
|
|
_LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();
|
|
|
|
_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI reverse_view()
|
|
|
|
requires default_initializable<_View>
|
|
|
|
= default;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2022-02-11 01:01:17 +01:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr explicit reverse_view(_View __view) : __base_(std::move(__view)) {}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
|
|
|
|
requires copy_constructible<_View>
|
|
|
|
{
|
|
|
|
return __base_;
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2022-02-11 01:01:17 +01:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator<iterator_t<_View>> begin() {
|
|
|
|
if constexpr (_UseCache)
|
|
|
|
if (__cached_begin_.__has_value())
|
|
|
|
return *__cached_begin_;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2022-02-11 01:01:17 +01:00
|
|
|
auto __tmp = std::make_reverse_iterator(ranges::next(ranges::begin(__base_), ranges::end(__base_)));
|
2021-07-28 17:11:04 -07:00
|
|
|
if constexpr (_UseCache)
|
2021-08-11 14:26:33 -04:00
|
|
|
__cached_begin_.__emplace(__tmp);
|
2021-07-28 17:11:04 -07:00
|
|
|
return __tmp;
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator<iterator_t<_View>> begin()
|
|
|
|
requires common_range<_View>
|
|
|
|
{
|
2022-02-11 01:01:17 +01:00
|
|
|
return std::make_reverse_iterator(ranges::end(__base_));
|
2021-07-28 17:11:04 -07:00
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
|
|
|
|
requires common_range<const _View>
|
|
|
|
{
|
2022-02-11 01:01:17 +01:00
|
|
|
return std::make_reverse_iterator(ranges::end(__base_));
|
2021-07-28 17:11:04 -07:00
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr reverse_iterator<iterator_t<_View>> end() {
|
2022-02-11 01:01:17 +01:00
|
|
|
return std::make_reverse_iterator(ranges::begin(__base_));
|
2021-07-28 17:11:04 -07:00
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr auto end() const
|
|
|
|
requires common_range<const _View>
|
|
|
|
{
|
2022-02-11 01:01:17 +01:00
|
|
|
return std::make_reverse_iterator(ranges::begin(__base_));
|
2021-07-28 17:11:04 -07:00
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr auto size()
|
|
|
|
requires sized_range<_View>
|
|
|
|
{
|
|
|
|
return ranges::size(__base_);
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr auto size() const
|
|
|
|
requires sized_range<const _View>
|
|
|
|
{
|
|
|
|
return ranges::size(__base_);
|
|
|
|
}
|
|
|
|
};
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
template <class _Range>
|
|
|
|
reverse_view(_Range&&) -> reverse_view<views::all_t<_Range>>;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
template <class _Tp>
|
|
|
|
inline constexpr bool enable_borrowed_range<reverse_view<_Tp>> = enable_borrowed_range<_Tp>;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
namespace views {
|
|
|
|
namespace __reverse {
|
|
|
|
template <class _Tp>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool __is_reverse_view = false;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Tp>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool __is_reverse_view<reverse_view<_Tp>> = true;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Tp>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool __is_sized_reverse_subrange = false;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Iter>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool
|
|
|
|
__is_sized_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, subrange_kind::sized>> =
|
|
|
|
true;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Tp>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool __is_unsized_reverse_subrange = false;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Iter, subrange_kind _Kind>
|
2022-02-28 18:48:58 -05:00
|
|
|
inline constexpr bool __is_unsized_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, _Kind>> =
|
|
|
|
_Kind == subrange_kind::unsized;
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Tp>
|
|
|
|
struct __unwrapped_reverse_subrange {
|
|
|
|
using type =
|
|
|
|
void; // avoid SFINAE-ing out the overload below -- let the concept requirements do it for better diagnostics
|
|
|
|
};
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Iter, subrange_kind _Kind>
|
|
|
|
struct __unwrapped_reverse_subrange<subrange<reverse_iterator<_Iter>, reverse_iterator<_Iter>, _Kind>> {
|
|
|
|
using type = subrange<_Iter, _Iter, _Kind>;
|
|
|
|
};
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
struct __fn : __range_adaptor_closure<__fn> {
|
|
|
|
template <class _Range>
|
|
|
|
requires __is_reverse_view<remove_cvref_t<_Range>>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
|
2022-02-11 01:01:17 +01:00
|
|
|
noexcept(noexcept(std::forward<_Range>(__range).base())) -> decltype(std::forward<_Range>(__range).base()) {
|
|
|
|
return std::forward<_Range>(__range).base();
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Range,
|
|
|
|
class _UnwrappedSubrange = typename __unwrapped_reverse_subrange<remove_cvref_t<_Range>>::type>
|
|
|
|
requires __is_sized_reverse_subrange<remove_cvref_t<_Range>>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
|
|
|
|
noexcept(noexcept(_UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size())))
|
|
|
|
-> decltype(_UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size())) {
|
|
|
|
return _UnwrappedSubrange(__range.end().base(), __range.begin().base(), __range.size());
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Range,
|
|
|
|
class _UnwrappedSubrange = typename __unwrapped_reverse_subrange<remove_cvref_t<_Range>>::type>
|
|
|
|
requires __is_unsized_reverse_subrange<remove_cvref_t<_Range>>
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
|
|
|
|
noexcept(noexcept(_UnwrappedSubrange(__range.end().base(), __range.begin().base())))
|
|
|
|
-> decltype(_UnwrappedSubrange(__range.end().base(), __range.begin().base())) {
|
|
|
|
return _UnwrappedSubrange(__range.end().base(), __range.begin().base());
|
|
|
|
}
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
template <class _Range>
|
|
|
|
requires(!__is_reverse_view<remove_cvref_t<_Range>> && !__is_sized_reverse_subrange<remove_cvref_t<_Range>> &&
|
|
|
|
!__is_unsized_reverse_subrange<remove_cvref_t<_Range>>)
|
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const noexcept(noexcept(reverse_view{
|
2022-02-11 01:01:17 +01:00
|
|
|
std::forward<_Range>(__range)})) -> decltype(reverse_view{std::forward<_Range>(__range)}) {
|
|
|
|
return reverse_view{std::forward<_Range>(__range)};
|
|
|
|
}
|
2021-09-24 12:04:12 -04:00
|
|
|
};
|
2022-02-01 18:11:49 +01:00
|
|
|
} // namespace __reverse
|
2023-12-18 14:01:33 -05:00
|
|
|
|
2021-09-24 12:04:12 -04:00
|
|
|
inline namespace __cpo {
|
|
|
|
inline constexpr auto reverse = __reverse::__fn{};
|
2022-02-01 18:11:49 +01:00
|
|
|
} // namespace __cpo
|
2021-09-24 12:04:12 -04:00
|
|
|
} // namespace views
|
2021-07-28 17:11:04 -07:00
|
|
|
} // namespace ranges
|
|
|
|
|
2023-02-14 00:56:09 +01:00
|
|
|
#endif // _LIBCPP_STD_VER >= 20
|
2021-07-28 17:11:04 -07:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
[libc++] Fix missing and incorrect push/pop macros (#79204)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
2024-01-25 15:48:46 -05:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2021-07-28 17:11:04 -07:00
|
|
|
#endif // _LIBCPP___RANGES_REVERSE_VIEW_H
|