From cbf166a2b928e3e037fba2b1f1bb64e5b6e4c26c Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 3 Jun 2015 19:56:43 +0000 Subject: [PATCH] More of N4258 implementation. Mark all of our test_allocators as noexcept constructible. Make the constructors for basic_string noexcept all the time (under C++14). Update tests to reflect the new world order. More to come. llvm-svn: 238957 --- libcxx/include/string | 29 ++++++++++++++++++++-- .../basic.string/string.cons/alloc.pass.cpp | 25 +++++++++++++++++-- .../string.cons/default_noexcept.pass.cpp | 6 +++++ .../basic.string/string.cons/move_alloc.pass.cpp | 23 ++++++++++++++++- .../string.cons/move_noexcept.pass.cpp | 5 ++++ libcxx/test/support/allocators.h | 24 ++++++++++-------- libcxx/test/support/min_allocator.h | 28 +++++++++++---------- libcxx/test/support/test_allocator.h | 2 ++ 8 files changed, 113 insertions(+), 29 deletions(-) diff --git a/libcxx/include/string b/libcxx/include/string index 840932a..b8cbea3 100644 --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1321,14 +1321,31 @@ public: static const size_type npos = -1; _LIBCPP_INLINE_VISIBILITY basic_string() +#if _LIBCPP_STD_VER <= 14 _NOEXCEPT_(is_nothrow_default_constructible::value); - _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a); +#else + _NOEXCEPT; +#endif + + _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a) +#if _LIBCPP_STD_VER <= 14 + _NOEXCEPT_(is_nothrow_copy_constructible::value); +#else + _NOEXCEPT; +#endif + basic_string(const basic_string& __str); basic_string(const basic_string& __str, const allocator_type& __a); + #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES _LIBCPP_INLINE_VISIBILITY basic_string(basic_string&& __str) +#if _LIBCPP_STD_VER <= 14 _NOEXCEPT_(is_nothrow_move_constructible::value); +#else + _NOEXCEPT; +#endif + _LIBCPP_INLINE_VISIBILITY basic_string(basic_string&& __str, const allocator_type& __a); #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES @@ -1926,7 +1943,11 @@ basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type template inline _LIBCPP_INLINE_VISIBILITY basic_string<_CharT, _Traits, _Allocator>::basic_string() - _NOEXCEPT_(is_nothrow_default_constructible::value) +#if _LIBCPP_STD_VER <= 14 + _NOEXCEPT_(is_nothrow_default_constructible::value) +#else + _NOEXCEPT +#endif { #if _LIBCPP_DEBUG_LEVEL >= 2 __get_db()->__insert_c(this); @@ -2070,7 +2091,11 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st template inline _LIBCPP_INLINE_VISIBILITY basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str) +#if _LIBCPP_STD_VER <= 14 _NOEXCEPT_(is_nothrow_move_constructible::value) +#else + _NOEXCEPT +#endif : __r_(_VSTD::move(__str.__r_)) { __str.__zero(); diff --git a/libcxx/test/std/strings/basic.string/string.cons/alloc.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/alloc.pass.cpp index 512d118..1c4f204 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/alloc.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/alloc.pass.cpp @@ -14,6 +14,7 @@ #include #include +#include "test_macros.h" #include "test_allocator.h" #include "min_allocator.h" @@ -22,6 +23,11 @@ void test() { { +#if TEST_STD_VER > 14 + static_assert((noexcept(S{})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" ); +#endif S s; assert(s.__invariants()); assert(s.data()); @@ -30,6 +36,11 @@ test() assert(s.get_allocator() == typename S::allocator_type()); } { +#if TEST_STD_VER > 14 + static_assert((noexcept(S{typename S::allocator_type{}})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible::value), "" ); +#endif S s(typename S::allocator_type(5)); assert(s.__invariants()); assert(s.data()); @@ -39,13 +50,18 @@ test() } } -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 template void test2() { { +#if TEST_STD_VER > 14 + static_assert((noexcept(S{})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" ); +#endif S s; assert(s.__invariants()); assert(s.data()); @@ -54,6 +70,11 @@ test2() assert(s.get_allocator() == typename S::allocator_type()); } { +#if TEST_STD_VER > 14 + static_assert((noexcept(S{typename S::allocator_type{}})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible::value), "" ); +#endif S s(typename S::allocator_type{}); assert(s.__invariants()); assert(s.data()); @@ -68,7 +89,7 @@ test2() int main() { test, test_allocator > >(); -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 test2, min_allocator > >(); #endif } diff --git a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp index f935db8..c6137d6 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/default_noexcept.pass.cpp @@ -17,6 +17,7 @@ #include #include +#include "test_macros.h" #include "test_allocator.h" template @@ -39,7 +40,12 @@ int main() } { typedef std::basic_string, some_alloc> C; +// See N4258 - basic_string::basic_string() noexcept; +#if TEST_STD_VER <= 14 static_assert(!std::is_nothrow_default_constructible::value, ""); +#else + static_assert( std::is_nothrow_default_constructible::value, ""); +#endif } #endif } diff --git a/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp index 1f96314..a232a46 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/move_alloc.pass.cpp @@ -16,6 +16,7 @@ #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES +#include "test_macros.h" #include "test_allocator.h" #include "min_allocator.h" @@ -34,6 +35,11 @@ test(S s0, const typename S::allocator_type& a) } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +// #if _LIBCPP_STD_VER <= 14 +// _NOEXCEPT_(is_nothrow_move_constructible::value); +// #else +// _NOEXCEPT; +// #endif int main() { @@ -41,6 +47,11 @@ int main() { typedef test_allocator A; typedef std::basic_string, A> S; +#if TEST_STD_VER > 14 + static_assert((noexcept(S{})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S()) == std::is_nothrow_move_constructible::value), "" ); +#endif test(S(), A(3)); test(S("1"), A(5)); test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7)); @@ -50,15 +61,25 @@ int main() { typedef test_allocator A; typedef std::basic_string, A> S; +#if TEST_STD_VER > 14 + static_assert((noexcept(S{})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S()) == std::is_nothrow_move_constructible::value), "" ); +#endif S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe" ); S s2 (std::move(s1), A(1)); } assert ( test_alloc_base::alloc_count == alloc_count ); -#if __cplusplus >= 201103L +#if TEST_STD_VER >= 11 { typedef min_allocator A; typedef std::basic_string, A> S; +#if TEST_STD_VER > 14 + static_assert((noexcept(S{})), "" ); +#elif TEST_STD_VER >= 11 + static_assert((noexcept(S()) == std::is_nothrow_move_constructible::value), "" ); +#endif test(S(), A()); test(S("1"), A()); test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A()); diff --git a/libcxx/test/std/strings/basic.string/string.cons/move_noexcept.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/move_noexcept.pass.cpp index 556aabd..b287a94 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/move_noexcept.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/move_noexcept.pass.cpp @@ -17,6 +17,7 @@ #include #include +#include "test_macros.h" #include "test_allocator.h" template @@ -39,7 +40,11 @@ int main() } { typedef std::basic_string, some_alloc> C; +#if TEST_STD_VER <= 14 static_assert(!std::is_nothrow_move_constructible::value, ""); +#else + static_assert( std::is_nothrow_move_constructible::value, ""); +#endif } #endif } diff --git a/libcxx/test/support/allocators.h b/libcxx/test/support/allocators.h index 5372c07..b7aba12 100644 --- a/libcxx/test/support/allocators.h +++ b/libcxx/test/support/allocators.h @@ -13,6 +13,8 @@ #include #include +#include "test_macros.h" + #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES template @@ -20,7 +22,7 @@ class A1 { int id_; public: - explicit A1(int id = 0) : id_(id) {} + explicit A1(int id = 0) TEST_NOEXCEPT : id_(id) {} typedef T value_type; @@ -31,13 +33,13 @@ public: static bool allocate_called; static std::pair deallocate_called; - A1(const A1& a) : id_(a.id()) {copy_called = true;} - A1(A1&& a) : id_(a.id()) {move_called = true;} + A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} + A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;} template - A1(const A1& a) : id_(a.id()) {copy_called = true;} + A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} template - A1(A1&& a) : id_(a.id()) {move_called = true;} + A1(A1&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;} T* allocate(std::size_t n) { @@ -77,7 +79,7 @@ class A2 { int id_; public: - explicit A2(int id = 0) : id_(id) {} + explicit A2(int id = 0) TEST_NOEXCEPT : id_(id) {} typedef T value_type; @@ -92,8 +94,8 @@ public: static bool move_called; static bool allocate_called; - A2(const A2& a) : id_(a.id()) {copy_called = true;} - A2(A2&& a) : id_(a.id()) {move_called = true;} + A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} + A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;} T* allocate(std::size_t n, const void* hint) { @@ -125,7 +127,7 @@ class A3 { int id_; public: - explicit A3(int id = 0) : id_(id) {} + explicit A3(int id = 0) TEST_NOEXCEPT : id_(id) {} typedef T value_type; @@ -139,8 +141,8 @@ public: static bool constructed; static bool destroy_called; - A3(const A3& a) : id_(a.id()) {copy_called = true;} - A3(A3&& a) : id_(a.id()) {move_called = true;} + A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} + A3(A3&& a) TEST_NOEXCEPT: id_(a.id()) {move_called = true;} template void construct(U* p, Args&& ...args) diff --git a/libcxx/test/support/min_allocator.h b/libcxx/test/support/min_allocator.h index b643636..5e3ae5d 100644 --- a/libcxx/test/support/min_allocator.h +++ b/libcxx/test/support/min_allocator.h @@ -12,16 +12,18 @@ #include +#include "test_macros.h" + template class bare_allocator { public: typedef T value_type; - bare_allocator() {} + bare_allocator() TEST_NOEXCEPT {} template - bare_allocator(bare_allocator) {} + bare_allocator(bare_allocator) TEST_NOEXCEPT {} T* allocate(std::size_t n) { @@ -53,10 +55,10 @@ class min_pointer { const void* ptr_; public: - min_pointer() noexcept = default; - min_pointer(std::nullptr_t) : ptr_(nullptr) {} + min_pointer() TEST_NOEXCEPT = default; + min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {} template - min_pointer(min_pointer p) : ptr_(p.ptr_) {} + min_pointer(min_pointer p) TEST_NOEXCEPT : ptr_(p.ptr_) {} explicit operator bool() const {return ptr_ != nullptr;} @@ -70,15 +72,15 @@ class min_pointer { void* ptr_; public: - min_pointer() noexcept = default; - min_pointer(std::nullptr_t) : ptr_(nullptr) {} + min_pointer() TEST_NOEXCEPT = default; + min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {} template ::value >::type > - min_pointer(min_pointer p) : ptr_(p.ptr_) {} + min_pointer(min_pointer p) TEST_NOEXCEPT : ptr_(p.ptr_) {} explicit operator bool() const {return ptr_ != nullptr;} @@ -92,11 +94,11 @@ class min_pointer { T* ptr_; - explicit min_pointer(T* p) : ptr_(p) {} + explicit min_pointer(T* p) TEST_NOEXCEPT : ptr_(p) {} public: - min_pointer() noexcept = default; - min_pointer(std::nullptr_t) : ptr_(nullptr) {} - explicit min_pointer(min_pointer p) : ptr_(static_cast(p.ptr_)) {} + min_pointer() TEST_NOEXCEPT = default; + min_pointer(std::nullptr_t) TEST_NOEXCEPT : ptr_(nullptr) {} + explicit min_pointer(min_pointer p) TEST_NOEXCEPT : ptr_(static_cast(p.ptr_)) {} explicit operator bool() const {return ptr_ != nullptr;} @@ -164,7 +166,7 @@ class min_pointer explicit min_pointer(const T* p) : ptr_(p) {} public: - min_pointer() noexcept = default; + min_pointer() TEST_NOEXCEPT = default; min_pointer(std::nullptr_t) : ptr_(nullptr) {} min_pointer(min_pointer p) : ptr_(p.ptr_) {} explicit min_pointer(min_pointer p) : ptr_(static_cast(p.ptr_)) {} diff --git a/libcxx/test/support/test_allocator.h b/libcxx/test/support/test_allocator.h index 683fac2..03bd390 100644 --- a/libcxx/test/support/test_allocator.h +++ b/libcxx/test/support/test_allocator.h @@ -17,6 +17,8 @@ #include #include +#include "test_macros.h" + class test_alloc_base { protected: -- 2.7.4