diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -279,6 +279,7 @@ __iterator/access.h __iterator/advance.h __iterator/back_insert_iterator.h + __iterator/bounded_iter.h __iterator/common_iterator.h __iterator/concepts.h __iterator/counted_iterator.h diff --git a/libcxx/include/__config b/libcxx/include/__config --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -94,8 +94,6 @@ # define _LIBCPP_ABI_OPTIMIZED_FUNCTION // All the regex constants must be distinct and nonzero. # define _LIBCPP_ABI_REGEX_CONSTANTS_NONZERO -// Use raw pointers, not wrapped ones, for std::span's iterator type. -# define _LIBCPP_ABI_SPAN_POINTER_ITERATORS // Re-worked external template instantiations for std::string with a focus on // performance and fast-path inlining. # define _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION diff --git a/libcxx/include/__iterator/bounded_iter.h b/libcxx/include/__iterator/bounded_iter.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__iterator/bounded_iter.h @@ -0,0 +1,215 @@ +// -*- 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___ITERATOR_BOUNDED_ITER_H +#define _LIBCPP___ITERATOR_BOUNDED_ITER_H + +#include <__assert> +#include <__config> +#include <__iterator/iterator_traits.h> +#include <__memory/pointer_traits.h> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +// Iterator wrapper that carries the valid range it is allowed to access. +// +// This is a simple iterator wrapper for contiguous iterators that points +// within a [begin, end) range and carries these bounds with it. The iterator +// ensures that it is pointing within that [begin, end) range when it is +// dereferenced. +// +// Arithmetic operations are allowed and the bounds of the resulting iterator +// are not checked. Hence, it is possible to create an iterator pointing outside +// its range, but it is not possible to dereference it. +template ::value +> > +struct __bounded_iter { + using value_type = typename iterator_traits<_Iterator>::value_type; + using difference_type = typename iterator_traits<_Iterator>::difference_type; + using pointer = typename iterator_traits<_Iterator>::pointer; + using reference = typename iterator_traits<_Iterator>::reference; + using iterator_category = typename iterator_traits<_Iterator>::iterator_category; +#if _LIBCPP_STD_VER > 17 + using iterator_concept = contiguous_iterator_tag; +#endif + + // Create a singular iterator. + // + // Such an iterator does not point to any object and is conceptually out of bounds, so it is + // not dereferenceable. Observing operations like comparison and assignment are valid. + _LIBCPP_HIDE_FROM_ABI __bounded_iter() = default; + + _LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default; + + template ::value + > > + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT + : __current_(__other.__current_), __begin_(__other.__begin_), __end_(__other.__end_) + { } + + // Assign a bounded iterator to another one, rebinding the bounds of the iterator as well. + _LIBCPP_HIDE_FROM_ABI __bounded_iter& operator=(__bounded_iter const&) = default; + +private: + // Create an iterator wrapping the given iterator, and whose bounds are described + // by the provided [begin, end) range. + // + // This constructor does not check whether the resulting iterator is within its bounds. + // However, it does check that the provided [begin, end) range is a valid range (that + // is, begin <= end). + // + // Since it is non-standard for iterators to have this constructor, __bounded_iter + // must be befriended by containers who wish to use it. + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + explicit __bounded_iter(_Iterator __current, _Iterator __begin, _Iterator __end) + : __current_(__current), __begin_(__begin), __end_(__end) + { + _LIBCPP_ASSERT(__begin <= __end, "__bounded_iter(current, begin, end): [begin, end) is not a valid range"); + } + + // Classes allowed to use this iterator: + template friend class _LIBCPP_TEMPLATE_VIS span; + +public: + // Dereference and indexing operations. + // + // These operations check that the iterator is dereferenceable, that is within [begin, end). + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + reference operator*() const _NOEXCEPT { + _LIBCPP_ASSERT(__in_bounds(__current_), "__bounded_iter::operator*: Attempt to dereference an out-of-range iterator"); + return *__current_; + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + pointer operator->() const _NOEXCEPT { + _LIBCPP_ASSERT(__in_bounds(__current_), "__bounded_iter::operator->: Attempt to dereference an out-of-range iterator"); + return std::__to_address(__current_); + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + reference operator[](difference_type __n) const _NOEXCEPT { + _LIBCPP_ASSERT(__in_bounds(__current_ + __n), "__bounded_iter::operator[]: Attempt to index an iterator out-of-range"); + return __current_[__n]; + } + + // Arithmetic operations. + // + // These operations do not check that the resulting iterator is within the bounds, since that + // would make it impossible to create a past-the-end iterator. + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter& operator++() _NOEXCEPT { + ++__current_; + return *this; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter operator++(int) _NOEXCEPT { + __bounded_iter __tmp(*this); + ++*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter& operator--() _NOEXCEPT { + --__current_; + return *this; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter operator--(int) _NOEXCEPT { + __bounded_iter __tmp(*this); + --*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter& operator+=(difference_type __n) _NOEXCEPT { + __current_ += __n; + return *this; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + friend __bounded_iter operator+(__bounded_iter const& __self, difference_type __n) _NOEXCEPT { + __bounded_iter __tmp(__self); + __tmp += __n; + return __tmp; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + friend __bounded_iter operator+(difference_type __n, __bounded_iter const& __self) _NOEXCEPT { + __bounded_iter __tmp(__self); + __tmp += __n; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + __bounded_iter& operator-=(difference_type __n) _NOEXCEPT { + __current_ -= __n; + return *this; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + friend __bounded_iter operator-(__bounded_iter const& __self, difference_type __n) _NOEXCEPT { + __bounded_iter __tmp(__self); + __tmp -= __n; + return __tmp; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 + friend difference_type operator-(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ - __y.__current_; + } + + // Comparison operations. + // + // These operations do not check whether the iterators are within their bounds. + // The valid range for each iterator is also not considered as part of the comparison, + // i.e. two iterators pointing to the same location will be considered equal even + // if they have different validity ranges. + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator==(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ == __y.__current_; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator!=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ != __y.__current_; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator<(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ < __y.__current_; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator>(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ == __y.__current_; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator<=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ == __y.__current_; + } + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + friend bool operator>=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT { + return __x.__current_ == __y.__current_; + } + +private: + // Return whether the given iterator is in the bounds of this __bounded_iter. + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR + bool __in_bounds(_Iterator const& __iter) const { + return __iter >= __begin_ && __iter < __end_; + } + + _Iterator __current_; // current iterator + _Iterator __begin_, __end_; // valid range represented as [begin, end) +}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -680,6 +680,7 @@ #include <__iterator/access.h> #include <__iterator/advance.h> #include <__iterator/back_insert_iterator.h> +#include <__iterator/bounded_iter.h> #include <__iterator/common_iterator.h> #include <__iterator/concepts.h> #include <__iterator/counted_iterator.h> diff --git a/libcxx/include/span b/libcxx/include/span --- a/libcxx/include/span +++ b/libcxx/include/span @@ -131,6 +131,7 @@ #include <__config> #include <__debug> #include <__fwd/span.h> +#include <__iterator/bounded_iter.h> #include <__iterator/concepts.h> #include <__iterator/wrap_iter.h> #include <__ranges/concepts.h> @@ -197,10 +198,6 @@ is_convertible_v>(*)[], _ElementType(*)[]>; #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) -#if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS) -# define _LIBCPP_SPAN_USE_POINTER_ITERATOR -#endif - template class _LIBCPP_TEMPLATE_VIS span { public: @@ -213,8 +210,8 @@ using const_pointer = const _Tp *; using reference = _Tp &; using const_reference = const _Tp &; -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - using iterator = pointer; +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + using iterator = __bounded_iter; #else using iterator = __wrap_iter; #endif @@ -309,7 +306,7 @@ _LIBCPP_INLINE_VISIBILITY constexpr span first() const noexcept { - static_assert(_Count <= _Extent, "Count out of range in span::first()"); + static_assert(_Count <= _Extent, "span::first(): Count out of range"); return span{data(), _Count}; } @@ -317,21 +314,21 @@ _LIBCPP_INLINE_VISIBILITY constexpr span last() const noexcept { - static_assert(_Count <= _Extent, "Count out of range in span::last()"); + static_assert(_Count <= _Extent, "span::last(): Count out of range"); return span{data() + size() - _Count, _Count}; } _LIBCPP_INLINE_VISIBILITY constexpr span first(size_type __count) const noexcept { - _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); + _LIBCPP_ASSERT(__count <= size(), "span::first(count): count out of range"); return {data(), __count}; } _LIBCPP_INLINE_VISIBILITY constexpr span last(size_type __count) const noexcept { - _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); + _LIBCPP_ASSERT(__count <= size(), "span::last(count): count out of range"); return {data() + size() - __count, __count}; } @@ -340,8 +337,8 @@ constexpr auto subspan() const noexcept -> span { - static_assert(_Offset <= _Extent, "Offset out of range in span::subspan()"); - static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "Offset + count out of range in span::subspan()"); + static_assert(_Offset <= _Extent, "span::subspan(): Offset out of range"); + static_assert(_Count == dynamic_extent || _Count <= _Extent - _Offset, "span::subspan(): Offset + Count out of range"); using _ReturnType = span; return _ReturnType{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; @@ -352,11 +349,11 @@ constexpr span subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { - _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); - _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "Count out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__offset <= size(), "span::subspan(offset, count): offset out of range"); + _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "span::subspan(offset, count): count out of range"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; - _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__count <= size() - __offset, "span::subspan(offset, count): offset + count out of range"); return {data() + __offset, __count}; } @@ -366,7 +363,7 @@ _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept { - _LIBCPP_ASSERT(__idx < size(), "span[] index out of bounds"); + _LIBCPP_ASSERT(__idx < size(), "span::operator[](index): index out of range"); return __data[__idx]; } @@ -386,15 +383,15 @@ // [span.iter], span iterator support _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - return iterator(data()); +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + return iterator(data(), data(), data() + size()); #else return iterator(this, data()); #endif } _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - return iterator(data() + size()); +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + return iterator(data() + size(), data(), data() + size()); #else return iterator(this, data() + size()); #endif @@ -428,8 +425,8 @@ using const_pointer = const _Tp *; using reference = _Tp &; using const_reference = const _Tp &; -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - using iterator = pointer; +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + using iterator = __bounded_iter; #else using iterator = __wrap_iter; #endif @@ -503,7 +500,7 @@ _LIBCPP_INLINE_VISIBILITY constexpr span first() const noexcept { - _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::first()"); + _LIBCPP_ASSERT(_Count <= size(), "span::first(): Count out of range"); return span{data(), _Count}; } @@ -511,21 +508,21 @@ _LIBCPP_INLINE_VISIBILITY constexpr span last() const noexcept { - _LIBCPP_ASSERT(_Count <= size(), "Count out of range in span::last()"); + _LIBCPP_ASSERT(_Count <= size(), "span::last(): Count out of range"); return span{data() + size() - _Count, _Count}; } _LIBCPP_INLINE_VISIBILITY constexpr span first(size_type __count) const noexcept { - _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::first(count)"); + _LIBCPP_ASSERT(__count <= size(), "span::first(count): count out of range"); return {data(), __count}; } _LIBCPP_INLINE_VISIBILITY constexpr span last (size_type __count) const noexcept { - _LIBCPP_ASSERT(__count <= size(), "Count out of range in span::last(count)"); + _LIBCPP_ASSERT(__count <= size(), "span::last(count): count out of range"); return {data() + size() - __count, __count}; } @@ -533,8 +530,8 @@ _LIBCPP_INLINE_VISIBILITY constexpr span subspan() const noexcept { - _LIBCPP_ASSERT(_Offset <= size(), "Offset out of range in span::subspan()"); - _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "Offset + count out of range in span::subspan()"); + _LIBCPP_ASSERT(_Offset <= size(), "span::subspan(): Offset out of range"); + _LIBCPP_ASSERT(_Count == dynamic_extent || _Count <= size() - _Offset, "span::subspan(): Offset + Count out of range"); return span{data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count}; } @@ -542,11 +539,11 @@ _LIBCPP_INLINE_VISIBILITY subspan(size_type __offset, size_type __count = dynamic_extent) const noexcept { - _LIBCPP_ASSERT(__offset <= size(), "Offset out of range in span::subspan(offset, count)"); - _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "count out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__offset <= size(), "span::subspan(offset, count): offset out of range"); + _LIBCPP_ASSERT(__count <= size() || __count == dynamic_extent, "span::subspan(offset, count): count out of range"); if (__count == dynamic_extent) return {data() + __offset, size() - __offset}; - _LIBCPP_ASSERT(__count <= size() - __offset, "Offset + count out of range in span::subspan(offset, count)"); + _LIBCPP_ASSERT(__count <= size() - __offset, "span::subspan(offset, count): offset + count out of range"); return {data() + __offset, __count}; } @@ -556,19 +553,19 @@ _LIBCPP_INLINE_VISIBILITY constexpr reference operator[](size_type __idx) const noexcept { - _LIBCPP_ASSERT(__idx < size(), "span[] index out of bounds"); + _LIBCPP_ASSERT(__idx < size(), "span::operator[](index): index out of range"); return __data[__idx]; } _LIBCPP_INLINE_VISIBILITY constexpr reference front() const noexcept { - _LIBCPP_ASSERT(!empty(), "span[].front() on empty span"); + _LIBCPP_ASSERT(!empty(), "span::front() on empty span"); return __data[0]; } _LIBCPP_INLINE_VISIBILITY constexpr reference back() const noexcept { - _LIBCPP_ASSERT(!empty(), "span[].back() on empty span"); + _LIBCPP_ASSERT(!empty(), "span::back() on empty span"); return __data[size()-1]; } @@ -577,15 +574,15 @@ // [span.iter], span iterator support _LIBCPP_INLINE_VISIBILITY constexpr iterator begin() const noexcept { -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - return iterator(data()); +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + return iterator(data(), data(), data() + size()); #else return iterator(this, data()); #endif } _LIBCPP_INLINE_VISIBILITY constexpr iterator end() const noexcept { -#ifdef _LIBCPP_SPAN_USE_POINTER_ITERATOR - return iterator(data() + size()); +#ifdef _LIBCPP_ENABLE_DEBUG_MODE + return iterator(data() + size(), data(), data() + size()); #else return iterator(this, data() + size()); #endif diff --git a/libcxx/test/libcxx/containers/views/views.span/debug.iterator-indexing.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/debug.iterator-indexing.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/debug.iterator-indexing.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// Make sure that std::span's iterators check for OOB accesses when the debug mode is enabled. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: !libcpp-has-debug-mode, c++03 + +#include + +#include "check_assertion.h" + +struct Foo { + int x; +}; + +int main(int, char**) { + { + Foo array[] = {{0}, {1}, {2}}; + std::span const span(array, 3); + { + auto it = span.end(); + TEST_LIBCPP_ASSERT_FAILURE(*it, "Attempt to dereference an out-of-range iterator"); + } + { + auto it = span.end(); + TEST_LIBCPP_ASSERT_FAILURE(it->x, "Attempt to dereference an out-of-range iterator"); + } + { + auto it = span.begin(); + TEST_LIBCPP_ASSERT_FAILURE(it[3], "Attempt to index an iterator out-of-range"); + } + } + { + Foo array[] = {{0}, {1}, {2}}; + std::span const span(array, 3); + { + auto it = span.end(); + TEST_LIBCPP_ASSERT_FAILURE(*it, "Attempt to dereference an out-of-range iterator"); + } + { + auto it = span.end(); + TEST_LIBCPP_ASSERT_FAILURE(it->x, "Attempt to dereference an out-of-range iterator"); + } + { + auto it = span.begin(); + TEST_LIBCPP_ASSERT_FAILURE(it[3], "Attempt to index an iterator out-of-range"); + } + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.back.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.back.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.back.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr reference back() const noexcept; + +// Make sure that accessing a span out-of-bounds triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 0); + TEST_LIBCPP_ASSERT_FAILURE(s.back(), "span::back() on empty span"); + } + + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 0); + TEST_LIBCPP_ASSERT_FAILURE(s.back(), "span::back() on empty span"); + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.front.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.front.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.front.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr reference front() const noexcept; + +// Make sure that accessing a span out-of-bounds triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 0); + TEST_LIBCPP_ASSERT_FAILURE(s.front(), "span::front() on empty span"); + } + + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 0); + TEST_LIBCPP_ASSERT_FAILURE(s.front(), "span::front() on empty span"); + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.elem/assert.op_idx.pass.cpp @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr reference operator[](size_type idx) const; + +// Make sure that accessing a span out-of-bounds triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(array, 3); + TEST_LIBCPP_ASSERT_FAILURE(s[3], "span::operator[](index): index out of range"); + } + + { + int array[] = {0, 1, 2}; + std::span const s(array, 3); + TEST_LIBCPP_ASSERT_FAILURE(s[3], "span::operator[](index): index out of range"); + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.first.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.first.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.first.pass.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr span first(size_type count) const; + +// Make sure that creating a sub-span with an incorrect number of elements triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.first(4), "span::first(count): count out of range"); + TEST_LIBCPP_ASSERT_FAILURE(s.first<4>(), "span::first(): Count out of range"); + } + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.first(4), "span::first(count): count out of range"); + // s.first<4>() caught at compile-time (tested elsewhere) + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.last.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.last.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.last.pass.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr span last(size_type count) const; + +// Make sure that creating a sub-span with an incorrect number of elements triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.last(4), "span::last(count): count out of range"); + TEST_LIBCPP_ASSERT_FAILURE(s.last<4>(), "span::last(): Count out of range"); + } + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.last(4), "span::last(count): count out of range"); + // s.last<4>() caught at compile-time (tested elsewhere) + } + + return 0; +} diff --git a/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.subspan.pass.cpp b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.subspan.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/views/views.span/span.sub/assert.subspan.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// +// +// constexpr span subspan( +// size_type offset, size_type count = dynamic_extent) const; +// +// Requires: (0 <= Offset && Offset <= size()) +// && (Count == dynamic_extent || Count >= 0 && Offset + Count <= size()) + +// Make sure that creating a sub-span with an incorrect number of elements triggers an assertion. + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03 +// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0|12.0}} +// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_ASSERTIONS=1 + +#include +#include + +#include "check_assertion.h" + +int main(int, char**) { + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(4), "span::subspan(offset, count): offset out of range"); + TEST_LIBCPP_ASSERT_FAILURE(s.subspan<4>(), "span::subspan(): Offset out of range"); + + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(0, 4), "span::subspan(offset, count): count out of range"); + TEST_LIBCPP_ASSERT_FAILURE((s.subspan<0, 4>()), "span::subspan(): Offset + Count out of range"); + + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(1, 3), "span::subspan(offset, count): offset + count out of range"); + TEST_LIBCPP_ASSERT_FAILURE((s.subspan<1, 3>()), "span::subspan(): Offset + Count out of range"); + } + { + int array[] = {0, 1, 2}; + std::span const s(&array[0], 3); + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(4), "span::subspan(offset, count): offset out of range"); + // s.subspan<4>() caught at compile-time (tested elsewhere) + + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(0, 4), "span::subspan(offset, count): count out of range"); + // s.subspan<0, 4>() caught at compile-time (tested elsewhere) + + TEST_LIBCPP_ASSERT_FAILURE(s.subspan(1, 3), "span::subspan(offset, count): offset + count out of range"); + // s.subspan<1, 3>() caught at compile-time (tested elsewhere) + } + + return 0; +} 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 @@ -310,6 +310,7 @@ #include <__iterator/access.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/access.h'}} #include <__iterator/advance.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/advance.h'}} #include <__iterator/back_insert_iterator.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/back_insert_iterator.h'}} +#include <__iterator/bounded_iter.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/bounded_iter.h'}} #include <__iterator/common_iterator.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/common_iterator.h'}} #include <__iterator/concepts.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/concepts.h'}} #include <__iterator/counted_iterator.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/counted_iterator.h'}} diff --git a/libcxx/test/std/containers/views/views.span/span.sub/first.fail.cpp b/libcxx/test/std/containers/views/views.span/span.sub/first.verify.cpp rename from libcxx/test/std/containers/views/views.span/span.sub/first.fail.cpp rename to libcxx/test/std/containers/views/views.span/span.sub/first.verify.cpp --- a/libcxx/test/std/containers/views/views.span/span.sub/first.fail.cpp +++ b/libcxx/test/std/containers/views/views.span/span.sub/first.verify.cpp @@ -12,30 +12,18 @@ // template // constexpr span first() const; // -// constexpr span first(size_type count) const; -// // Requires: Count <= size(). #include - #include -#include "test_macros.h" - -constexpr int carr[] = {1, 2, 3, 4}; - -int main(int, char**) { - std::span sp(carr); +void f() { + int array[] = {1, 2, 3, 4}; + std::span sp(array); // Count too large - { - [[maybe_unused]] auto s1 = sp.first<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::first()"}} - } + [[maybe_unused]] auto s1 = sp.first<5>(); // expected-error@span:* {{span::first(): Count out of range}} // Count numeric_limits - { - [[maybe_unused]] auto s1 = sp.first(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::first()"}} - } - - return 0; + [[maybe_unused]] auto s2 = sp.first(); // expected-error@span:* {{span::first(): Count out of range}} } diff --git a/libcxx/test/std/containers/views/views.span/span.sub/last.fail.cpp b/libcxx/test/std/containers/views/views.span/span.sub/last.verify.cpp rename from libcxx/test/std/containers/views/views.span/span.sub/last.fail.cpp rename to libcxx/test/std/containers/views/views.span/span.sub/last.verify.cpp --- a/libcxx/test/std/containers/views/views.span/span.sub/last.fail.cpp +++ b/libcxx/test/std/containers/views/views.span/span.sub/last.verify.cpp @@ -12,30 +12,18 @@ // template // constexpr span last() const; // -// constexpr span last(size_type count) const; -// // Requires: Count <= size(). #include - #include -#include "test_macros.h" - -constexpr int carr[] = {1, 2, 3, 4}; - -int main(int, char**) { - std::span sp(carr); +void f() { + int array[] = {1, 2, 3, 4}; + std::span sp(array); // Count too large - { - [[maybe_unused]] auto s1 = sp.last<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::last()"}} - } + [[maybe_unused]] auto s1 = sp.last<5>(); // expected-error@span:* {{span::last(): Count out of range}} // Count numeric_limits - { - [[maybe_unused]] auto s1 = sp.last(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Count out of range in span::last()"}} - } - - return 0; + [[maybe_unused]] auto s2 = sp.last(); // expected-error@span:* {{span::last(): Count out of range}} } diff --git a/libcxx/test/std/containers/views/views.span/span.sub/subspan.fail.cpp b/libcxx/test/std/containers/views/views.span/span.sub/subspan.fail.cpp deleted file mode 100644 --- a/libcxx/test/std/containers/views/views.span/span.sub/subspan.fail.cpp +++ /dev/null @@ -1,53 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// UNSUPPORTED: c++03, c++11, c++14, c++17 - -// - -// template -// constexpr span subspan() const; -// -// constexpr span subspan( -// size_type offset, size_type count = dynamic_extent) const; -// -// Requires: offset <= size() && -// (count == dynamic_extent || count <= size() - offset) - -#include - -#include - -#include "test_macros.h" - -constexpr int carr[] = {1, 2, 3, 4}; - -int main(int, char**) { - std::span sp(carr); - - // Offset too large templatized - { - [[maybe_unused]] auto s1 = sp.subspan<5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset out of range in span::subspan()"}} - } - - // Count too large templatized - { - [[maybe_unused]] auto s1 = sp.subspan<0, 5>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}} - } - - // Offset + Count too large templatized - { - [[maybe_unused]] auto s1 = sp.subspan<2, 3>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}} - } - - // Offset + Count overflow templatized - { - [[maybe_unused]] auto s1 = sp.subspan<3, std::size_t(-2)>(); // expected-error-re@span:* {{static_assert failed{{( due to requirement '.*')?}} "Offset + count out of range in span::subspan()"}}, expected-error-re@span:* {{array is too large{{(.* elements)}}}} - } - - return 0; -} diff --git a/libcxx/test/std/containers/views/views.span/span.sub/subspan.verify.cpp b/libcxx/test/std/containers/views/views.span/span.sub/subspan.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/containers/views/views.span/span.sub/subspan.verify.cpp @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// This test also generates spurious warnings when instantiating std::span +// with a very large extent (like size_t(-2)) -- silence those. +// ADDITIONAL_COMPILE_FLAGS: -Xclang -verify-ignore-unexpected=warning + +// + +// template +// constexpr span subspan() const; +// +// Requires: offset <= size() && +// (count == dynamic_extent || count <= size() - offset) + +#include +#include + +void f() { + int array[] = {1, 2, 3, 4}; + std::span sp(array); + + // Offset too large templatized + [[maybe_unused]] auto s1 = sp.subspan<5>(); // expected-error@span:* {{span::subspan(): Offset out of range"}} + + // Count too large templatized + [[maybe_unused]] auto s2 = sp.subspan<0, 5>(); // expected-error@span:* {{span::subspan(): Offset + Count out of range"}} + + // Offset + Count too large templatized + [[maybe_unused]] auto s3 = sp.subspan<2, 3>(); // expected-error@span:* {{span::subspan(): Offset + Count out of range"}} + + // Offset + Count overflow templatized + [[maybe_unused]] auto s4 = sp.subspan<3, std::size_t(-2)>(); // expected-error@span:* {{span::subspan(): Offset + Count out of range"}}, expected-error-re@span:* {{array is too large{{(.* elements)}}}} +}