[libcxx-commits] [PATCH] D146398: [libcxx] Fix using std::wcout/wcin on Windows with streams configured in wide mode

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 19 15:48:48 PDT 2023


mstorsjo created this revision.
mstorsjo added a reviewer: fsb4000.
Herald added a subscriber: mikhail.ramalho.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: libc++.
Herald added a reviewer: libc++.

On Windows, the underlying file descriptors for stdout/stdin/stderr
can be reconfigured to wide mode. In the default (narrow) mode, the
charset usually isn't utf8 (as libcxx assumes), but normally a locale
specific codepage (where each codepage only can represent a small
subset of unicode characters).

By configuring the stdout file descriptor to wide mode, the user can
output wchar_t based strings without convesion to the narrow charset.
Within libcxx, don't try to use codecvt to convert this to a narrow
character encoding, but output these strings as such with fputwc.

In wide mode, such strings could be output directly with fwrite too,
but if the file descriptor hasn't been configured in wide mode, that
breaks the output (which currently works reasonably). By always
outputting one character at a time with fputwc, it works regardless
of mode of the stdout file descriptor.

For the narrow output stream, std::cout, outputting (via fwrite)
does fail when the file descriptor is set to wide mode. This matches
how it behaves with both MS STL and GNU libstdc++ too, so this is
probably acceptable.

This fixes https://github.com/llvm/llvm-project/issues/46646, and
the downstream bugs https://github.com/mstorsjo/llvm-mingw/issues/145
and https://github.com/mstorsjo/llvm-mingw/issues/222.

The patch itself isn't very pretty, I would have preferred to get
by with much much fewer ifdefs. Initially, I considered adding
another template type for the char type to use for the stream
(which would be char on other platforms, and equal to char_type on
Windows), but codecvt doesn't support the other type being anything
other than char.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146398

Files:
  libcxx/include/__std_stream

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146398.506443.patch
Type: text/x-patch
Size: 4569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230319/0ca6180c/attachment-0001.bin>


More information about the libcxx-commits mailing list