From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4AF0C3858D1E; Fri, 15 Dec 2023 13:21:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AF0C3858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1702646516; bh=J3w8r/vOxXzBi7WHT5EEo4mZZ9mGgbJvIeSQTRDIxx4=; h=From:To:Subject:Date:From; b=VaURMlLWLl82QTgDHGHYtivVTEVkXvaVF3zh+dh9DquhLUqpg3d8U9otddia5F5Ac VJgDyb4LwMnqRmhsH/q5Iw+JJkljrRukmA9Yi5/cZs5tXaEeAzIwjjhTCQA0Xs5CbC 5gg0JqqGlByo2WGCWWZW3RBRdmq4Et1bYMgJtKmo= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-6591] libstdc++: Simplify std::vprint_unicode for non-Windows targets X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: 8a5cac92e7c20e13f01ca0cfa5dc00d014caf0d3 X-Git-Newrev: 7d2e100058790e3407ae02b6db4a276957deaa4f Message-Id: <20231215132156.4AF0C3858D1E@sourceware.org> Date: Fri, 15 Dec 2023 13:21:56 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7d2e100058790e3407ae02b6db4a276957deaa4f commit r14-6591-g7d2e100058790e3407ae02b6db4a276957deaa4f Author: Jonathan Wakely Date: Fri Dec 15 12:24:26 2023 +0000 libstdc++: Simplify std::vprint_unicode for non-Windows targets 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. Diff: --- 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