[all-commits] [llvm/llvm-project] fcbbd9: [libcxx] Fix using std::wcout/wcin on Windows with...

Martin Storsjö via All-commits all-commits at lists.llvm.org
Sat Jun 3 13:13:42 PDT 2023

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: fcbbd9649ac165aaf7fc7d60b8fef3b23755179a
  Author: Martin Storsjö <martin at martin.st>
  Date:   2023-06-03 (Sat, 03 Jun 2023)

  Changed paths:
    M libcxx/docs/UsingLibcxx.rst
    M libcxx/src/std_stream.h
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/check-stderr.sh
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/check-stdout.sh
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/send-stdin.sh
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/test.dat
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcerr-imbue.sh.cpp
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcerr-wide-mode.sh.cpp
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcin-imbue.sh.cpp
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcin-wide-mode.sh.cpp
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcout-imbue.sh.cpp
    A libcxx/test/std/input.output/iostream.objects/wide.stream.objects/wcout-wide-mode.sh.cpp

  Log Message:
  [libcxx] Fix using std::wcout/wcin on Windows with streams configured in wide mode

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.

Differential Revision: https://reviews.llvm.org/D146398

More information about the All-commits mailing list