From patchwork Fri Nov 19 18:22:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1557340 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=e6w+oy4P; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4HwlR26fBCz9s1l for ; Sat, 20 Nov 2021 05:23:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 121B23858024 for ; Fri, 19 Nov 2021 18:23:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 121B23858024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1637346203; bh=iyoUApGwu787/Xd41I6dodXEsuvyda/2TFxu41kxik8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=e6w+oy4PCYVqvhGMmauiwkfRW4Lok1UV1eqqOhHxIiyCBTLge6n6P2u0Jlw8V6p4X NgHHyhUtoDNl/owkfA/uqFZuOZJ3mGrW3ij5p6c/7+PN5BVabc6OFuWVeLJS7W6hW8 eCf6HYX1kzlijEkJzuuBiZeDtyyhVoS/pPrX1iYE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 5B253385840D for ; Fri, 19 Nov 2021 18:23:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5B253385840D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-511-Dw9dhTQqOQWMg24_SEWRng-1; Fri, 19 Nov 2021 13:22:59 -0500 X-MC-Unique: Dw9dhTQqOQWMg24_SEWRng-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9618C1006AAA; Fri, 19 Nov 2021 18:22:58 +0000 (UTC) Received: from localhost (unknown [10.33.36.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23D9E60862; Fri, 19 Nov 2021 18:22:57 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Begin lifetime of chars in constexpr std::string [PR103295] Date: Fri, 19 Nov 2021 18:22:57 +0000 Message-Id: <20211119182257.464344-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux (and clang on x86_64-linux), pushed to trunk. Clang gives errors for constexpr std::string because the memory returned by std::allocator::allocate does not contain any objects yet, and attempting to set them using char_traits::assign or char_traits::copy fails with: assignment to object outside its lifetime is not allowed in a constant expression *__result = *__first; ^ This adds code to std::char_traits to use std::construct_at to begin lifetimes when called during constant evaluation. To support specializations of std::basic_string that don't use std::char_traits there is now another layer of wrapper around the allocator_traits, so that the lifetime of characters is begun as soon as the memory is allocated. By doing it in the char traits and allocator traits, the rest of basic_string can ignore the problem. While modifying char_traits::copy and char_traits::assign to begin lifetimes for the constexpr cases, I also replaced their uses of std::copy and std::fill_n respectively. That means we don't need for char_traits. libstdc++-v3/ChangeLog: PR libstdc++/103295 * include/bits/basic_string.h (_Alloc_traits): Replace typedef with struct for C++20 mode. * include/bits/basic_string.tcc (_M_replace): Use _Alloc_traits for allocation. * include/bits/char_traits.h (__gnu_cxx::char_traits::assign): Use std::_Construct during constant evaluation. (__gnu_cxx::char_traits::assign(CharT*, const CharT*, size_t)): Likewise. Replace std::fill_n with memset or manual loop. (__gnu_cxx::char_traits::copy): Likewise, replacing std::copy with memcpy. * include/ext/vstring.h: Include for std::min. * include/std/string_view: Likewise. * testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc: Add constexpr test. --- libstdc++-v3/include/bits/basic_string.h | 40 +++++++- libstdc++-v3/include/bits/basic_string.tcc | 7 +- libstdc++-v3/include/bits/char_traits.h | 92 ++++++++++++++++--- libstdc++-v3/include/ext/vstring.h | 1 + libstdc++-v3/include/std/string_view | 2 + .../capacity/char/resize_and_overwrite.cc | 14 +++ 6 files changed, 141 insertions(+), 15 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index d29c9cdc410..6e7de738308 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -87,7 +87,37 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template rebind<_CharT>::other _Char_alloc_type; + +#if __cpp_lib_constexpr_string < 201907L typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits; +#else + template + struct _Alloc_traits_impl : __gnu_cxx::__alloc_traits<_Char_alloc_type> + { + typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Base; + + [[__gnu__::__always_inline__]] + static constexpr typename _Base::pointer + allocate(_Char_alloc_type& __a, typename _Base::size_type __n) + { + pointer __p = _Base::allocate(__a, __n); + if (__builtin_is_constant_evaluated()) + // Begin the lifetime of characters in allocated storage. + for (size_type __i = 0; __i < __n; ++__i) + std::construct_at(__builtin_addressof(__p[__i])); + return __p; + } + }; + + template + struct _Alloc_traits_impl, _Dummy_for_PR85282> + : __gnu_cxx::__alloc_traits<_Char_alloc_type> + { + // std::char_traits begins the lifetime of characters. + }; + + using _Alloc_traits = _Alloc_traits_impl<_Traits, void>; +#endif // Types: public: @@ -485,7 +515,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 basic_string() _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value) : _M_dataplus(_M_local_data()) - { _M_set_length(0); } + { + _M_use_local_data(); + _M_set_length(0); + } /** * @brief Construct an empty string using allocator @a a. @@ -494,7 +527,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 explicit basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT : _M_dataplus(_M_local_data(), __a) - { _M_set_length(0); } + { + _M_use_local_data(); + _M_set_length(0); + } /** * @brief Construct string with copy of value of @a __str. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 9a54b63b933..374406c0e13 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -490,7 +490,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #if __cpp_lib_is_constant_evaluated if (__builtin_is_constant_evaluated()) { - auto __newp = this->_M_get_allocator().allocate(__new_size); + auto __newp = _Alloc_traits::allocate(_M_get_allocator(), + __new_size); _S_copy(__newp, this->_M_data(), __pos); _S_copy(__newp + __pos, __s, __len2); _S_copy(__newp + __pos + __len2, __p + __len1, __how_much); @@ -569,6 +570,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { __p = _M_create(__n, __capacity); this->_S_copy(__p, _M_data(), length()); // exclude trailing null +#if __cpp_lib_is_constant_evaluated + if (__builtin_is_constant_evaluated()) + traits_type::assign(__p + length(), __n - length(), _CharT()); +#endif _M_dispose(); _M_data(__p); _M_capacity(__n); diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h index 3c9f4ad9420..b1cdc55ea61 100644 --- a/libstdc++-v3/include/bits/char_traits.h +++ b/libstdc++-v3/include/bits/char_traits.h @@ -36,11 +36,11 @@ #pragma GCC system_header -#include // std::copy, std::fill_n #include // For streampos #include // For WEOF, wmemmove, wmemset, etc. -#if __cplusplus > 201703L +#if __cplusplus >= 202002L # include +# include #endif #ifndef _GLIBCXX_ALWAYS_INLINE @@ -100,7 +100,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX14_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static _GLIBCXX_CONSTEXPR bool eq(const char_type& __c1, const char_type& __c2) @@ -240,8 +247,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: copy(char_type* __s1, const char_type* __s2, std::size_t __n) { - // NB: Inline std::copy so no recursive dependencies. - std::copy(__s2, __s2 + __n, __s1); +#if __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + { + for (std::size_t __i = 0; __i < __n; ++__i) + std::construct_at(__s1 + __i, __s2[__i]); + return __s1; + } +#endif + + __builtin_memcpy(__s1, __s2, __n * sizeof(char_type)); return __s1; } @@ -251,8 +266,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: assign(char_type* __s, std::size_t __n, char_type __a) { - // NB: Inline std::fill_n so no recursive dependencies. - std::fill_n(__s, __n, __a); +#if __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + { + for (std::size_t __i = 0; __i < __n; ++__i) + std::construct_at(__s + __i, __a); + return __s; + } +#endif + + if _GLIBCXX17_CONSTEXPR (sizeof(_CharT) == 1 && __is_trivial(_CharT)) + { + unsigned char __c; + __builtin_memcpy(&__c, __builtin_addressof(__a), 1); + __builtin_memset(__s, __c, __n); + } + else + { + for (std::size_t __i = 0; __i < __n; ++__i) + __s[__i] = __a; + } return __s; } @@ -304,7 +337,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static _GLIBCXX_CONSTEXPR bool eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT @@ -435,7 +475,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static _GLIBCXX_CONSTEXPR bool eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT @@ -556,7 +603,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static _GLIBCXX_CONSTEXPR bool eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT @@ -692,7 +746,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) noexcept - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static constexpr bool eq(const char_type& __c1, const char_type& __c2) noexcept @@ -806,7 +867,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static _GLIBCXX17_CONSTEXPR void assign(char_type& __c1, const char_type& __c2) noexcept - { __c1 = __c2; } + { +#if __cpp_constexpr_dynamic_alloc && __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + std::construct_at(__builtin_addressof(__c1), __c2); + else +#endif + __c1 = __c2; + } static constexpr bool eq(const char_type& __c1, const char_type& __c2) noexcept diff --git a/libstdc++-v3/include/ext/vstring.h b/libstdc++-v3/include/ext/vstring.h index cb5872a7030..331282c0104 100644 --- a/libstdc++-v3/include/ext/vstring.h +++ b/libstdc++-v3/include/ext/vstring.h @@ -38,6 +38,7 @@ #include #include #include +#include // std::min namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) { diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index fd92df6e425..f2863c1beb4 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -39,9 +39,11 @@ #include #include +#include #include #include #include +#include #include #if __cplusplus >= 202002L diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc index f0e81126a41..c9087b55190 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc @@ -105,10 +105,24 @@ test04() } } +constexpr bool +test05() +{ + std::string s; + s.resize_and_overwrite(20, [](char* p, auto n) { + *p = '!'; // direct assignment should be OK + std::char_traits::copy(p, "constexpr", 9); + return 9; + }); + VERIFY( s == "constexpr" ); + return true; +} + int main() { test01(); test02(); test03(); test04(); + static_assert( test05() ); }