[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

@@ -8,7 +8,7 @@
// <string>
// size_type capacity() const;
// size_type capacity() const; // constexpr since C++20
#include <string>
#include <cassert>
@@ -18,11 +18,9 @@
#include "test_macros.h"
test_allocator_statistics alloc_stats;
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
test(S s, test_allocator_statistics& alloc_stats)
{
alloc_stats.throw_after = 0;
#ifndef TEST_HAS_NO_EXCEPTIONS
@@ -42,17 +40,18 @@ test(S s)
alloc_stats.throw_after = INT_MAX;
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
test_allocator_statistics alloc_stats;
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
S s((test_allocator<char>(&alloc_stats)));
test(s);
test(s, alloc_stats);
s.assign(10, 'a');
s.erase(5);
test(s);
test(s, alloc_stats);
s.assign(100, 'a');
s.erase(50);
test(s);
test(s, alloc_stats);
}
#if TEST_STD_VER >= 11
{
@@ -69,7 +68,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>
// void clear();
// void clear(); // constexpr since C++20
#include <string>
#include <cassert>
@@ -24,7 +26,7 @@ test(S s)
assert(s.size() == 0);
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s;
@@ -61,7 +63,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>
// bool empty() const noexcept;
// bool empty() const noexcept; // constexpr since C++20
#include <string>
#include <cassert>
@@ -24,7 +26,7 @@ test(const S& s)
assert(s.empty() == (s.size() == 0));
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
@@ -47,7 +49,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 @@
// class deque
// bool empty() const noexcept;
// bool empty() const noexcept; // constexpr since C++20
// UNSUPPORTED: c++03, c++11, c++14, c++17
@@ -18,7 +18,7 @@
#include "test_macros.h"
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
std::string c;
c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
@@ -29,7 +29,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>
// size_type length() const;
// size_type length() const; // constexpr since C++20
#include <string>
#include <cassert>
@@ -17,13 +19,13 @@
#include "min_allocator.h"
template <class S>
void
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
assert(s.length() == s.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
@@ -46,7 +48,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -9,7 +9,7 @@
// UNSUPPORTED: no-exceptions
// <string>
// size_type max_size() const;
// size_type max_size() const; // constexpr since C++20
// NOTE: asan and msan will fail for one of two reasons
// 1. If allocator_may_return_null=0 then they will fail because the allocation
@@ -25,7 +25,7 @@
#include "min_allocator.h"
template <class S>
void
TEST_CONSTEXPR_CXX20 void
test1(const S& s)
{
S s2(s);
@@ -36,7 +36,7 @@ test1(const S& s)
}
template <class S>
void
TEST_CONSTEXPR_CXX20 void
test2(const S& s)
{
S s2(s);
@@ -47,7 +47,7 @@ test2(const S& s)
}
template <class S>
void
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
assert(s.max_size() >= s.size());
@@ -55,7 +55,7 @@ test(const S& s)
test2(s);
}
bool test() {
void test() {
{
typedef std::string S;
test(S());
@@ -70,15 +70,25 @@ bool test() {
test(S("12345678901234567890123456789012345678901234567890"));
}
#endif
}
#if TEST_STD_VER > 17
constexpr bool test_constexpr() {
std::string str;
size_t size = str.max_size();
assert(size > 0);
return true;
}
#endif
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
test_constexpr();
static_assert(test_constexpr());
#endif
return 0;

View File

@@ -16,7 +16,7 @@
// <string>
// size_type max_size() const;
// size_type max_size() const; // constexpr since C++20
#include <string>
#include <cassert>
@@ -59,9 +59,6 @@ bool test() {
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
#endif
return 0;
}

View File

@@ -20,7 +20,7 @@
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
void
test(typename S::size_type min_cap, typename S::size_type erased_index)
{
S s(min_cap, 'a');
@@ -63,9 +63,6 @@ bool test() {
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
#endif
return 0;
}

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// void reserve(size_type res_arg);
// void reserve(size_type res_arg); // constexpr since C++20
// This test relies on https://llvm.org/PR45368 being fixed, which isn't in
// older Apple dylibs
@@ -62,7 +64,7 @@ test(typename S::size_type min_cap, typename S::size_type erased_index, typename
#endif
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
{
@@ -105,7 +107,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;

View File

@@ -90,18 +90,14 @@ int main(int, char**) {
test<char16_t>();
test<char32_t>();
#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
static_assert(test<char>());
static_assert(test<char8_t>());
static_assert(test<char16_t>());
static_assert(test<char32_t>());
#endif
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L
static_assert(test<wchar_t>());
#endif
#endif
return 0;
}

View File

@@ -6,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// void resize(size_type n);
// void resize(size_type n); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -43,7 +45,7 @@ test(S s, typename S::size_type n, S expected)
#endif
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, S());
@@ -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,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// void resize(size_type n, charT c);
// void resize(size_type n, charT c); // constexpr since C++20
#include <string>
#include <stdexcept>
@@ -43,7 +45,7 @@ test(S s, typename S::size_type n, typename S::value_type c, S expected)
#endif
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, 'a', S());
@@ -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,9 +6,11 @@
//
//===----------------------------------------------------------------------===//
// XFAIL: LIBCXX-AIX-FIXME
// <string>
// void shrink_to_fit();
// void shrink_to_fit(); // constexpr since C++20
#include <string>
#include <cassert>
@@ -29,7 +31,7 @@ test(S s)
assert(s.capacity() >= s.size());
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s;
@@ -66,7 +68,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>
// size_type size() const;
// size_type size() const; // constexpr since C++20
#include <string>
#include <cassert>
@@ -23,7 +25,7 @@ test(const S& s, typename S::size_type c)
assert(s.size() == c);
}
bool test() {
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0);
@@ -46,7 +48,7 @@ int main(int, char**)
{
test();
#if TEST_STD_VER > 17
// static_assert(test());
static_assert(test());
#endif
return 0;