[libc++] Implement P0980R1 (constexpr std::string)

Reviewed By: #libc, ldionne

Spies: daltenty, sdasgup3, ldionne, arichardson, MTC, ChuanqiXu, mehdi_amini, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes, tatianashp, rdzhabarov, teijeong, cota, dcaballe, Chia-hungDuan, wrengr, wenzhicui, arphaman, Mordante, miscco, Quuxplusone, smeenai, libcxx-commits

Differential Revision: https://reviews.llvm.org/D110598
This commit is contained in:
Nikolas Klauser
2022-04-27 10:11:44 +02:00
parent 6e078f9804
commit 425620ccdd
211 changed files with 2291 additions and 2074 deletions

View File

@@ -6,11 +6,13 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// template<class _Tp>
// basic_string(const _Tp& __t, size_type __pos, size_type __n,
// const allocator_type& __a = allocator_type());
// const allocator_type& __a = allocator_type()); // constexpr since C++20
//
// Mostly we're testing string_view here
@@ -91,7 +93,7 @@ test(SV sv, std::size_t pos, std::size_t n, const typename S::allocator_type& a)
#endif
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
@@ -189,7 +191,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,7 +8,7 @@
// <string>
// explicit basic_string(const Allocator& a = Allocator());
// explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <cassert>
@@ -85,7 +85,7 @@ test2()
#endif
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
#if TEST_STD_VER >= 11
test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
@@ -99,7 +99,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -11,14 +11,14 @@
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(basic_string<charT,traits,Allocator>&& str);
// operator=(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
// Test that assignment from {} and {ptr, len} are allowed and are not
// ambiguous.
{
@@ -39,7 +39,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string<charT,traits,Allocator>& operator=(charT c);
// basic_string<charT,traits,Allocator>& operator=(charT c); // constexpr since C++20
#include <string>
#include <cassert>
@@ -28,7 +30,7 @@ test(S s1, typename S::value_type s2)
assert(s1.capacity() >= s1.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 'a');
@@ -53,7 +55,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(const basic_string<charT,traits,Allocator>& str);
// basic_string(const basic_string<charT,traits,Allocator>& str); // constexpr since C++20
#include <string>
#include <cassert>
@@ -28,7 +30,7 @@ test(S s1)
assert(s2.get_allocator() == s1.get_allocator());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -53,7 +55,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(const basic_string& str, const Allocator& alloc);
// basic_string(const basic_string& str, const Allocator& alloc); // constexpr since C++20
#include <string>
#include <cassert>
@@ -88,7 +90,7 @@ test(S s1, const typename S::allocator_type& a)
assert(s2.get_allocator() == a);
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -135,7 +137,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,10 +6,12 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(const basic_string<charT,traits,Allocator>& str);
// operator=(const basic_string<charT,traits,Allocator>& str); // constexpr since C++20
#include <string>
#include <cassert>
@@ -27,7 +29,7 @@ test(S s1, const S& s2)
assert(s1.capacity() >= s1.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S());
@@ -83,7 +85,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,7 +8,7 @@
// <string>
// basic_string() noexcept(is_nothrow_default_constructible<allocator_type>::value);
// basic_string() noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
#include <cassert>
#include <string>
@@ -25,7 +25,7 @@ LIBCPP_STATIC_ASSERT(!std::is_nothrow_default_constructible<
std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>>>::value, "");
#endif
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
std::string str;
assert(str.empty());
@@ -36,7 +36,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -10,7 +10,7 @@
// <string>
// ~basic_string() // implied noexcept;
// ~basic_string() // implied noexcept; // constexpr since C++20
#include <string>
#include <cassert>
@@ -40,7 +40,7 @@ static_assert(std::is_nothrow_destructible<
LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<
std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
test_allocator_statistics alloc_stats;
{
std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
@@ -56,7 +56,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -45,7 +45,7 @@ using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;
// (13) basic_string(initializer_list<CharT>, A const& = A())
// (14) basic_string(BSV, A const& = A())
// (15) basic_string(const T&, size_type, size_type, A const& = A())
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
using TestSizeT = test_allocator<char>::size_type;
{ // Testing (1)
@@ -372,7 +372,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,9 +8,11 @@
// UNSUPPORTED: c++03
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(initializer_list<charT> il, const Allocator& a = Allocator());
// basic_string(initializer_list<charT> il, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <cassert>
@@ -19,7 +21,7 @@
#include "test_allocator.h"
#include "min_allocator.h"
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s = {'a', 'b', 'c'};
assert(s == "abc");
@@ -52,7 +54,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -10,7 +10,7 @@
// <string>
// basic_string& operator=(initializer_list<charT> il);
// basic_string& operator=(initializer_list<charT> il); // constexpr since C++20
#include <string>
#include <cassert>
@@ -18,7 +18,7 @@
#include "test_macros.h"
#include "min_allocator.h"
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s;
s = {'a', 'b', 'c'};
@@ -38,7 +38,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,11 +6,13 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// template<class InputIterator>
// basic_string(InputIterator begin, InputIterator end,
// const Allocator& a = Allocator());
// const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
@@ -62,7 +64,7 @@ test(It first, It last, const A& a)
assert(s2.capacity() >= s2.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
const char* s = "12345678901234567890123456789012345678901234567890";
@@ -138,7 +140,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -9,12 +9,14 @@
// <string>
// UNSUPPORTED: c++03, c++11, c++14
// XFAIL: LIBCXX-AIX-FIXME
// template<class InputIterator,
// class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
// basic_string(InputIterator, InputIterator, Allocator = Allocator())
// -> basic_string<typename iterator_traits<InputIterator>::value_type,
// char_traits<typename iterator_traits<InputIterator>::value_type>,
// Allocator>;
// Allocator>; // constexpr since C++20
//
// The deduction guide shall not participate in overload resolution if InputIterator
// is a type that does not qualify as an input iterator, or if Allocator is a type
@@ -49,7 +51,7 @@ static_assert(!CanDeduce<NotAnIterator, std::allocator<char>>::value);
static_assert(!CanDeduce<NotAnInputIterator, std::allocator<char16_t>>::value);
static_assert(!CanDeduce<wchar_t const*, NotAnAllocator<wchar_t>>::value);
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
const char* s = "12345678901234";
std::basic_string s1(s, s+10); // Can't use {} here
@@ -108,7 +110,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,9 +8,11 @@
// UNSUPPORTED: c++03
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(basic_string<charT,traits,Allocator>&& str);
// basic_string(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
@@ -32,7 +34,7 @@ test(S s0)
assert(s2.get_allocator() == s1.get_allocator());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -55,7 +57,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,9 +8,11 @@
// UNSUPPORTED: c++03
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(basic_string&& str, const Allocator& alloc);
// basic_string(basic_string&& str, const Allocator& alloc); // constexpr since C++20
#include <string>
#include <cassert>
@@ -32,7 +34,7 @@ test(S s0, const typename S::allocator_type& a)
assert(s2.get_allocator() == a);
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
test_allocator_statistics alloc_stats;
{
typedef test_allocator<char> A;
@@ -80,7 +82,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -13,12 +13,12 @@
// basic_string& operator=(basic_string&& c)
// noexcept(
// allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
// allocator_traits<allocator_type>::is_always_equal::value); // C++17
// allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
//
// before C++17, we use the conforming extension
// noexcept(
// allocator_type::propagate_on_container_move_assignment::value &&
// is_nothrow_move_assignable<allocator_type>::value);
// is_nothrow_move_assignable<allocator_type>::value); // constexpr since C++20
#include <string>
#include <cassert>
@@ -62,7 +62,7 @@ struct some_alloc3
typedef std::false_type is_always_equal;
};
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string C;
static_assert(std::is_nothrow_move_assignable<C>::value, "");
@@ -100,7 +100,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -8,10 +8,12 @@
// UNSUPPORTED: c++03
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(basic_string<charT,traits,Allocator>&& str);
// operator=(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
@@ -32,7 +34,7 @@ test(S s1, S s2)
assert(s1.capacity() >= s1.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S());
@@ -77,7 +79,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -11,7 +11,7 @@
// <string>
// basic_string(basic_string&&)
// noexcept(is_nothrow_move_constructible<allocator_type>::value);
// noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
// This tests a conforming extension

View File

@@ -8,7 +8,9 @@
// <string>
// basic_string(const charT* s, const Allocator& a = Allocator());
// XFAIL: LIBCXX-AIX-FIXME
// basic_string(const charT* s, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -51,7 +53,7 @@ test(const charT* s, const A& a)
assert(s2.capacity() >= s2.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
@@ -92,7 +94,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,10 +6,12 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(const charT* s);
// operator=(const charT* s); // constexpr since C++20
#include <string>
#include <cassert>
@@ -29,7 +31,7 @@ test(S s1, const typename S::value_type* s2)
assert(s1.capacity() >= s1.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "");
@@ -76,7 +78,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(const charT* s, size_type n, const Allocator& a = Allocator());
// basic_string(const charT* s, size_type n, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -48,7 +50,7 @@ test(const charT* s, unsigned n, const A& a)
assert(s2.capacity() >= s2.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
@@ -97,7 +99,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(size_type n, charT c, const Allocator& a = Allocator());
// basic_string(size_type n, charT c, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -50,7 +52,7 @@ test(unsigned n, charT c, const A& a)
}
template <class Tp>
void
TEST_CONSTEXPR_CXX20 void
test(Tp n, Tp c)
{
typedef char charT;
@@ -80,7 +82,7 @@ test(Tp n, Tp c, const A& a)
assert(s2.capacity() >= s2.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
@@ -127,7 +129,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator());
// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
#include <algorithm>
#include <cassert>
@@ -75,7 +77,7 @@ test(std::basic_string_view<charT> sv, const A& a)
}
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
@@ -118,7 +120,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string<charT,traits,Allocator>& operator=(basic_string_view<charT, traits> sv);
// basic_string<charT,traits,Allocator>& operator=(basic_string_view<charT, traits> sv); // constexpr since C++20
#include <string>
#include <cassert>
@@ -28,7 +30,7 @@ test(S s1, SV sv)
assert(s1.capacity() >= s1.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
@@ -77,7 +79,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// UNSUPPORTED: c++03, c++11, c++14
@@ -43,7 +45,7 @@ struct NotAnAllocator { };
static_assert( CanDeduce<std::string_view, std::allocator<char>>::value);
static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value);
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string_view sv = "12345678901234";
std::basic_string s1(sv);
@@ -77,7 +79,7 @@ bool test() {
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
#ifndef TEST_HAS_NO_CHAR8_T
{
std::u8string_view sv = u8"12345678901234";
std::basic_string s1{sv, min_allocator<char8_t>{}};
@@ -117,7 +119,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// UNSUPPORTED: c++03, c++11, c++14
@@ -47,7 +49,7 @@ struct NotAnAllocator { };
static_assert( CanDeduce<std::string_view, std::size_t, std::allocator<char>>::value);
static_assert(!CanDeduce<std::string_view, std::size_t, NotAnAllocator>::value);
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string_view sv = "12345678901234";
std::basic_string s1{sv, 0, 4};
@@ -81,7 +83,7 @@ bool test() {
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
#if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
#ifndef TEST_HAS_NO_CHAR8_T
{
std::u8string_view sv = u8"12345678901234";
std::basic_string s1{sv, 0, 4, min_allocator<char8_t>{}};
@@ -121,7 +123,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -6,15 +6,17 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos, size_type n,
// const Allocator& a = Allocator());
// const Allocator& a = Allocator()); // constexpr since C++20
//
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos,
// const Allocator& a = Allocator());
// const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -138,7 +140,7 @@ void test_lwg2583()
#endif
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
@@ -225,7 +227,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
test_lwg2583();