public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-6591] libstdc++: Simplify std::vprint_unicode for non-Windows targets
@ 2023-12-15 13:21 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-12-15 13:21 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:7d2e100058790e3407ae02b6db4a276957deaa4f
commit r14-6591-g7d2e100058790e3407ae02b6db4a276957deaa4f
Author: Jonathan Wakely <jwakely@redhat.com>
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<char>);
// 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<typename... _Args>
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<char> __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<char>);
// 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<typename... _Args>
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-12-15 13:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-15 13:21 [gcc r14-6591] libstdc++: Simplify std::vprint_unicode for non-Windows targets Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).