From patchwork Mon Jan 8 01:19:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 185778 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp778715dyq; Sun, 7 Jan 2024 17:20:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IG3GlTkMvoBBtIvR4jsxYaN9/NkKXwzj7r88yGjAMZ3tVUrby+X7BQ6HLauWVgnVi86yuhN X-Received: by 2002:ad4:5764:0:b0:67a:a721:cb16 with SMTP id r4-20020ad45764000000b0067aa721cb16mr4610429qvx.119.1704676845522; Sun, 07 Jan 2024 17:20:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704676845; cv=pass; d=google.com; s=arc-20160816; b=SgRS0TSd9xNQsXMVPkQlefkVRrQqayrqrZjP2l66WstXu7Ag05vhAOZ+DUYSYR1i4V S/nMZ044wpMnKf4OzRhsUGlChlY6zevIr7CUQy6weEDgDKvYJ1Vi4nkzYvjMZiaq8PQh 35cdY6SibsfWz0EuGtXByX/FE/2nuLA76sWFSD+aMr10gMrJnCt81gsmXfh4RkINlpX8 GcNyYTnbquqDfDMrdLAueF/F3k5f0qOSNklvQUmXTwqOR4A7LoL3gLGu02LmGkC9Cu2j lJB6XccuFfZXKZedLRk8e3LQii95HdSFt19wTJqV00+oiTLBWy6V7iBJtaEGAtuIYUEq RAYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=KGHIXQYPGMXDxTv/Uybr2vkMZxLCnPQa5K5JszZ76/k=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=dQZdlW3Gg6GUbiAd+TfXfJLManxmc8NHL7oxRRCnm7beyfhSG9KCWaT7MiJ161m/Lv MGXWVCxb9z//XIoQEBAlV0hhbUf+bVM2IA9kMOjRSJc5oUThQc/Bb5fuVej3dFx5/4nX zz6N0IX2RsSxcOBTzaQ0UQwr5vSiJmFD3vohJiN5LudX3mmghoHgj3LTzuPA3AolQUU4 6IrRrWsBeVxhtZ0nAPTUNdZhmQ+oMYvzUaScwkKbaD813TN4/lFl0jWjY2wgNvfWHJbf 8/ZA4c9027Ufcyl6i1GXDn/k67RLhEWZPEjY3o1ooq4DFedJqO10cSZ26a0PhD3JBrZY D/7w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=di671run; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b1-20020a0cb3c1000000b0068063dc1d66si6870805qvf.588.2024.01.07.17.20.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Jan 2024 17:20:45 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=di671run; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 32C9F386C596 for ; Mon, 8 Jan 2024 01:20:45 +0000 (GMT) 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id AE299384DED6 for ; Mon, 8 Jan 2024 01:19:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AE299384DED6 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AE299384DED6 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704676777; cv=none; b=kFMENc1IrjFphGJV2F3pwJRgSVZCgHc+FhaKeRer7pq9Jm1ri1fF4Nr+XwFMYV6qHmuAT6b8hoHKy0cMNNtvDs1+HQ6jdh2/GbxR9TLhe+S4LWtUAqlFC6tKMjYvpntipNOK1+KdfdZBXXM1QG7pgMY9PGqAa5E1BDPgKAsbj/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704676777; c=relaxed/simple; bh=8CLkMpn1ClKZ5gR5r53C8c2rFX9D0Q6LrzwZvBLsFuo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=m9KUo+dakc+noRDKrcv9DtGruZVqsvigj1DknBJl+IgBHeTShacFMmUU27ml9qVRrzI62sno0vhw/xHGc8OqUvg2Zwm9KV+9OayAP+BHetiDiGu6rEadjkR+FOkeAoJKDLktVPUWnQxKnB5RoERpWszS3WDYxNFOdqfDctvOX+Q= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704676774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KGHIXQYPGMXDxTv/Uybr2vkMZxLCnPQa5K5JszZ76/k=; b=di671runRc9K+5kMVg4poJ5/rlrrMa+w9K8kPsl/RLfh8Kgtw9jp80c9IgYPYN6L2UtU8s qcXi9WX7QajVn6TahOei4O4SyDYEH/YLWXWdyWJHqzl93mqeUljxXpW4Z/G9S+gIpKnGms XXJMPLTiMR3JRyKDM85KRVkNS6TatbQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-322-d0LZP_I3MNO2DUyseYe2yw-1; Sun, 07 Jan 2024 20:19:32 -0500 X-MC-Unique: d0LZP_I3MNO2DUyseYe2yw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9EB66862DE2; Mon, 8 Jan 2024 01:19:32 +0000 (UTC) Received: from localhost (unknown [10.42.28.185]) by smtp.corp.redhat.com (Postfix) with ESMTP id 692201C060AF; Mon, 8 Jan 2024 01:19:32 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/2] libstdc++: Implement P2918R0 "Runtime format strings II" for C++26 Date: Mon, 8 Jan 2024 01:19:02 +0000 Message-ID: <20240108011930.3670651-2-jwakely@redhat.com> In-Reply-To: <20240108011930.3670651-1-jwakely@redhat.com> References: <20240108011930.3670651-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787483227569733579 X-GMAIL-MSGID: 1787483227569733579 Tested x86_64-linux and aarch64-linux. Pushed to trunk. -- >8 -- This adds std::runtime_format for C++26. These new overloaded functions enhance the std::format API so that it isn't necessary to use the less ergonomic std::vformat and std::make_format_args (which are meant to be implementation details). This was approved in Kona 2023 for C++26. libstdc++-v3/ChangeLog: * include/std/format (__format::_Runtime_format_string): Define new class template. (basic_format_string): Add non-consteval constructor for runtime format strings. (runtime_format): Define new function for C++26. * testsuite/std/format/runtime_format.cc: New test. --- libstdc++-v3/include/std/format | 22 +++++++++++ .../testsuite/std/format/runtime_format.cc | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 libstdc++-v3/testsuite/std/format/runtime_format.cc diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 160efa5155c..b3b5a0bbdbc 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -81,6 +81,9 @@ namespace __format template using __format_context = basic_format_context<_Sink_iter<_CharT>, _CharT>; + + template + struct _Runtime_format_string { basic_string_view<_CharT> _M_str; }; } // namespace __format /// @endcond @@ -115,6 +118,11 @@ namespace __format consteval basic_format_string(const _Tp& __s); + [[__gnu__::__always_inline__]] + basic_format_string(__format::_Runtime_format_string<_CharT>&& __s) + : _M_str(__s._M_str) + { } + [[__gnu__::__always_inline__]] constexpr basic_string_view<_CharT> get() const noexcept @@ -133,6 +141,20 @@ namespace __format = basic_format_string...>; #endif +#if __cplusplus > 202302L + [[__gnu__::__always_inline__]] + inline __format::_Runtime_format_string + runtime_format(string_view __fmt) + { return {__fmt}; } + +#ifdef _GLIBCXX_USE_WCHAR_T + [[__gnu__::__always_inline__]] + inline __format::_Runtime_format_string + runtime_format(wstring_view __fmt) + { return {__fmt}; } +#endif +#endif // C++26 + // [format.formatter], formatter /// The primary template of std::formatter is disabled. diff --git a/libstdc++-v3/testsuite/std/format/runtime_format.cc b/libstdc++-v3/testsuite/std/format/runtime_format.cc new file mode 100644 index 00000000000..174334c7676 --- /dev/null +++ b/libstdc++-v3/testsuite/std/format/runtime_format.cc @@ -0,0 +1,37 @@ +// { dg-do run { target c++26 } } + +#include +#include + +void +test_char() +{ + std::string fmt = "{}"; + auto s = std::format(std::runtime_format(fmt), 123); + VERIFY( s == "123" ); +} + +void +test_wchar() +{ + std::wstring fmt = L"{:#o}"; + auto s = std::format(std::runtime_format(fmt), 456); + VERIFY( s == L"0710" ); +} + +void +test_internal_api() +{ + // Using _Runtime_format_string directly works even in C++20 mode. + // This can be used internally by libstdc++. + std::string fmt = "{:#x}"; + auto s = std::format(std::__format::_Runtime_format_string(fmt), 789); + VERIFY( s == "0x315" ); +} + +int main() +{ + test_char(); + test_wchar(); + test_internal_api(); +}