From patchwork Fri May 17 13:42:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1936507 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Hz+bsUKC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VgpBY1tZ3z1yfq for ; Fri, 17 May 2024 23:45:40 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 585DA384A880 for ; Fri, 17 May 2024 13:45:38 +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 14AAF3858D20 for ; Fri, 17 May 2024 13:45:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 14AAF3858D20 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 14AAF3858D20 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=1715953512; cv=none; b=O//rec1YsKoxykZPo0zBByPzhQxAUxEfCPX4MSVXdNXeSkjM3B79N56mT3eC4LYbye7z6wOU1CX91J8uuxM7I3v3nzWFs1+5EyOfQ2eTFYxTu/HWz7bZk01deQu+ZxsJd1xm4zIeulunsNsw370Ng40ZaqVMzqOOxm9e+6HZu64= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715953512; c=relaxed/simple; bh=SuvwWNvPuOpZ9+BVTWy3aMULme8DR/AnjqJgiqkO8xM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=p0w7LtWob1L4LL5wnaKNaWNysquLvt5gUZD5/Gf0NiO1I50DjSNOubEMVeNgOSh0Ajx/wkTIxiF3cmmgTTwiGknF9lEy8fHZPxCVPBf6DKc4de7yvJgx3Kpe4XvouCfM24zlhaoEqk4sX6DY5v6ZpN46pK0RaoCgDvb0ddnUPk4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715953508; 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; bh=WwWNr+QC+OqBIZCNczDGs3UTG6LgQLaRdPW8D2qu4vs=; b=Hz+bsUKCpcJTXqbfHD/BZds8/N4q3b0g832rwF53tWv++PVYcatvRZGsu5wF+8WOFXsk97 iJn5GRMEiEWYRmJDwSnXO0BqXwRS93v7sAXJYiB2bHgkN7KvzPyO6O596DltBn1wYMc3AB JfDdJnbfZs6mxWPpADYJ5oyhx3jmRgU= 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-60-qMjOHB9pNOm-lMgI4spsKw-1; Fri, 17 May 2024 09:45:07 -0400 X-MC-Unique: qMjOHB9pNOm-lMgI4spsKw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 1511C812296; Fri, 17 May 2024 13:45:07 +0000 (UTC) Received: from localhost (unknown [10.42.28.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF78340C6EB7; Fri, 17 May 2024 13:45:06 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Implement std::formatter without [PR115099] Date: Fri, 17 May 2024 14:42:00 +0100 Message-ID: <20240517134506.803430-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Does anybody see any issue with the drive-by fixes to constraint std::formatter to only work for pointers and integers (since we don't know how to format pthread_t if it's an arbitrary struct, for example), and to cast pointers to const void* for output (because if pthread_t is char* then writing it to a stream would be bad! and we don't want to allow users to overload operator<< for pointers to opaque structs, for example). I don't think this will change anything in practice for common targets, where pthread_t is either an integer or void*. Tested x86_64-linux. -- >8 -- The std::thread::id formatter uses std::basic_ostringstream without including , which went unnoticed because the test for it uses a stringstream to check the output is correct. The fix implemented here is to stop using basic_ostringstream for formatting thread::id and just use std::format instead. As a drive-by fix, the formatter specialization is constrained to require that the thread::id::native_handle_type can be formatted, to avoid making the formatter ill-formed if the pthread_t type is not a pointer or integer. Since non-void pointers can't be formatted, ensure that we convert pointers to const void* for formatting. Make a similar change to the existing operator<< overload so that in the unlikely case that pthread_t is a typedef for char* we don't treat it as a null-terminated string when inserting into a stream. libstdc++-v3/ChangeLog: PR libstdc++/115099 * include/bits/std_thread.h: Declare formatter as friend of thread::id. * include/std/thread (operator<<): Convert non-void pointers to void pointers for output. (formatter): Add constraint that thread::native_handle_type is a pointer or integer. (formatter::format): Reimplement without basic_ostringstream. * testsuite/30_threads/thread/id/output.cc: Check output compiles before has been included. --- libstdc++-v3/include/bits/std_thread.h | 11 ++++- libstdc++-v3/include/std/thread | 43 ++++++++++++++----- .../testsuite/30_threads/thread/id/output.cc | 21 ++++++++- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/libstdc++-v3/include/bits/std_thread.h b/libstdc++-v3/include/bits/std_thread.h index 2d7df12650d..5817bfb29dd 100644 --- a/libstdc++-v3/include/bits/std_thread.h +++ b/libstdc++-v3/include/bits/std_thread.h @@ -53,6 +53,10 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION +#if __glibcxx_formatters + template class formatter; +#endif + /** @addtogroup threads * @{ */ @@ -117,13 +121,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template friend basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, id __id); + +#if __glibcxx_formatters + friend formatter; + friend formatter; +#endif }; private: id _M_id; // _GLIBCXX_RESOLVE_LIB_DEFECTS - // 2097. packaged_task constructors should be constrained + // 2097. packaged_task constructors should be constrained // 3039. Unnecessary decay in thread and packaged_task template using __not_same = __not_, thread>>; diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index 09ca3116e7f..e994d683bff 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -42,10 +42,6 @@ # include // std::stop_source, std::stop_token, std::nostopstate #endif -#if __cplusplus > 202002L -# include -#endif - #include // std::thread, get_id, yield #include // std::this_thread::sleep_for, sleep_until @@ -53,6 +49,10 @@ #define __glibcxx_want_formatters #include +#if __cpp_lib_formatters +# include +#endif + namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -104,10 +104,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __out, thread::id __id) { + // Convert non-void pointers to const void* for formatted output. + using __output_type + = __conditional_t::value, + const void*, + thread::native_handle_type>; + if (__id == thread::id()) return __out << "thread::id of a non-executing thread"; else - return __out << __id._M_thread; + return __out << static_cast<__output_type>(__id._M_thread); } /// @} @@ -287,8 +293,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif // __cpp_lib_jthread #ifdef __cpp_lib_formatters // C++ >= 23 - template + requires is_pointer_v + || is_integral_v class formatter { public: @@ -331,10 +338,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename basic_format_context<_Out, _CharT>::iterator format(thread::id __id, basic_format_context<_Out, _CharT>& __fc) const { - std::basic_ostringstream<_CharT> __os; - __os << __id; - auto __str = __os.view(); - return __format::__write_padded_as_spec(__str, __str.size(), + basic_string_view<_CharT> __sv; + if constexpr (is_same_v<_CharT, char>) + __sv = "{}thread::id of a non-executing thread"; + else + __sv = L"{}thread::id of a non-executing thread"; + basic_string<_CharT> __str; + if (__id == thread::id()) + __sv.remove_prefix(2); + else + { + using _FmtStr = __format::_Runtime_format_string<_CharT>; + // Convert non-void pointers to const void* for formatted output. + using __output_type + = __conditional_t, + const void*, + thread::native_handle_type>; + auto __o = static_cast<__output_type>(__id._M_thread); + __sv = __str = std::format(_FmtStr(__sv.substr(0, 2)), __o); + } + return __format::__write_padded_as_spec(__sv, __sv.size(), __fc, _M_spec, __format::_Align_right); } diff --git a/libstdc++-v3/testsuite/30_threads/thread/id/output.cc b/libstdc++-v3/testsuite/30_threads/thread/id/output.cc index 3c167202b02..94a6ff0e2a1 100644 --- a/libstdc++-v3/testsuite/30_threads/thread/id/output.cc +++ b/libstdc++-v3/testsuite/30_threads/thread/id/output.cc @@ -3,8 +3,27 @@ // { dg-require-gthreads "" } #include -#include + +#include #include + +void +test_no_includes(std::ostream& out) +{ + std::thread::id i{}; + // Check stream insertion works without including : + out << i; +#if __cpp_lib_formatters >= 202302 + // PR libstdc++/115099 - compilation error: format thread::id + // Verify we can use std::thread::id with std::format without : + (void) std::format("{}", i); +#ifdef _GLIBCXX_USE_WCHAR_T + (void) std::format(L"{}", i); +#endif +#endif +} + +#include #include void