From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 A547938618E8 for ; Fri, 15 Dec 2023 13:22:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A547938618E8 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 A547938618E8 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=1702646569; cv=none; b=hWfT4JjXp/fompPSAaJ8vkS3Nmlwur/C8K6nw04bhNWq5KTmzaaDFZVlremmCLi2Y9VFHeDTchkQoPqu4M5tJcotyjqPDb1O8A9m8jsJepbuin4Olp2ExP6eaIU/KaUg3s5o7XxQphVRfZFkSaj2IAqpHAAH3mQSSxFGjkUymuw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702646569; c=relaxed/simple; bh=3TP3o0ipm30E2VKlDakMi29b8G8TmPs9Rk1pUArdK9k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nhEchels6t/72sj8I0GwUOHS6EMTX3S3b5bFAyPZfYud81LDW5Qta8yv7e8Osx7/JZc9VsECdtjaB5t6eM+iN4eDJ8knsrKqbptGcPOt6Vl8h8uEq21rmvZpSWDnPnO6V/05IIfEvJRfG5uW8uxzQOaw0tbcgFU7KzydzIiTvow= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702646567; 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=+4UDileGuVrdH2vo6DoZkSE2EF+jR28EJe93VBEG0VE=; b=ME3oaOlqeb7kXzpcD3BnNUfFE5BRmQSoUNIBsZS4BdYrRcRx2PKnvZphkbcQ+bryOCDqS2 d3Qq9g5ZNGpkD1bnppYy6iiFLUzx0fjMqvEBKUAN3VQ5rmTkWH3GqMq6WKJVbTuS2bWCUk ljPygvslvj60Knt/tBtXhrtGuc9vBVY= 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-628-wqXyS27uPDOw-ZP84I2ppg-1; Fri, 15 Dec 2023 08:22:46 -0500 X-MC-Unique: wqXyS27uPDOw-ZP84I2ppg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 B6450185A780; Fri, 15 Dec 2023 13:22:45 +0000 (UTC) Received: from localhost (unknown [10.42.28.145]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E80351D5; Fri, 15 Dec 2023 13:22:45 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Simplify std::vprint_unicode for non-Windows targets Date: Fri, 15 Dec 2023 13:22:28 +0000 Message-ID: <20231215132244.2139474-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 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_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 List-Id: Tested x86_64-linux and x86_64-w64-mingw. Pushed to trunk. -- >8 -- Since we don't need to do anything special to print Unicode on non-Windows targets, we might as well just use std::vprint_nonunicode to implement std::vprint_unicode. Removing the duplicated code should reduce code size in cases where those calls aren't inlined. Also use an RAII type for the unused case where a non-Windows target calls __open_terminal(streambuf*) and needs to fclose the result. This makes the code futureproof in case we ever start using the __write_terminal function for non-Windows targets. libstdc++-v3/ChangeLog: * include/std/ostream (vprint_unicode) [_WIN32]: Use RAII guard. (vprint_unicode) [!_WIN32]: Just call vprint_nonunicode. * include/std/print (vprint_unicode) [!_WIN32]: Likewise. --- libstdc++-v3/include/std/ostream | 31 +++++++++++++++++++++++-------- libstdc++-v3/include/std/print | 10 +++++++--- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream index 0cac293e4d6..a9a7aefad71 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -906,6 +906,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) { +#ifndef _WIN32 + // For most targets we don't need to do anything special to write + // Unicode to a terminal. + std::vprint_nonunicode(__os, __fmt, __args); +#else ostream::sentry __cerb(__os); if (__cerb) { @@ -913,12 +918,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args); auto __out = __buf.view(); -#ifdef _WIN32 void* __open_terminal(streambuf*); error_code __write_to_terminal(void*, span); // If stream refers to a terminal, write a Unicode string to it. if (auto __term = __open_terminal(__os.rdbuf())) { +#ifndef _WIN32 + // For POSIX, __open_terminal(streambuf*) uses fdopen to open a + // new file, so we would need to close it here. This code is not + // actually compiled because it's inside an #ifdef _WIN32 group, + // but just in case that changes in future ... + struct _Guard + { + _Guard(void* __p) : _M_f((FILE*)__p) { } + ~_Guard() { std::fclose(_M_f); } + _Guard(_Guard&&) = delete; + _Guard& operator=(_Guard&&) = delete; + FILE* _M_f; + }; + _Guard __g(__term); +#endif + ios_base::iostate __err = ios_base::goodbit; __try { @@ -927,11 +947,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else if (auto __e = __write_to_terminal(__term, __out)) if (__e != std::make_error_code(errc::illegal_byte_sequence)) __err = ios::badbit; -#ifndef _WIN32 - // __open_terminal(streambuf*) opens a new FILE with fdopen, - // so we need to close it here. - std::fclose((FILE*)__term); -#endif } __catch(const __cxxabiv1::__forced_unwind&) { @@ -945,9 +960,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __os.setstate(__err); return; } -#endif - // Otherwise just insert the string as normal. + // Otherwise just insert the string as vprint_nonunicode does. __try { std::__ostream_write(__os, __out.data(), __out.size()); @@ -960,6 +974,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __catch(...) { __os._M_setstate(ios_base::badbit); } } +#endif // _WIN32 } template diff --git a/libstdc++-v3/include/std/print b/libstdc++-v3/include/std/print index e7099ab6fe3..bb029610d70 100644 --- a/libstdc++-v3/include/std/print +++ b/libstdc++-v3/include/std/print @@ -64,11 +64,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline void vprint_unicode(FILE* __stream, string_view __fmt, format_args __args) { +#ifndef _WIN32 + // For most targets we don't need to do anything special to write + // Unicode to a terminal. + std::vprint_nonunicode(__stream, __fmt, __args); +#else __format::_Str_sink __buf; std::vformat_to(__buf.out(), __fmt, __args); auto __out = __buf.view(); -#ifdef _WIN32 void* __open_terminal(FILE*); error_code __write_to_terminal(void*, span); // If stream refers to a terminal, write a native Unicode string to it. @@ -88,11 +92,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __e = error_code(errno, generic_category()); _GLIBCXX_THROW_OR_ABORT(system_error(__e, "std::vprint_unicode")); } -#endif - // Otherwise just write the string to the file. + // Otherwise just write the string to the file as vprint_nonunicode does. if (std::fwrite(__out.data(), 1, __out.size(), __stream) != __out.size()) __throw_system_error(EIO); +#endif } template -- 2.43.0