[PATCH] D51558: [Windows] Convert from UTF-8 to UTF-16 when writing to a Windows console

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 31 14:10:45 PDT 2018


rnk created this revision.
rnk added reviewers: zturner, efriedma.
Herald added a subscriber: hiraditya.

Calling WriteConsoleW is the most reliable way to print Unicode
characters to a Windows console.

If binary data gets printed to the console, attempting to re-encode it
shouldn't be a problem, since garbage in can produce garbage out.

This breaks printing strings in the local codepage, which WriteConsoleA
knows how to handle. For example, this can happen when user source code
is encoded with the local codepage, and an LLVM tool quotes it while
emitting a caret diagnostic. This is unfortunate, but well-behaved tools
should validate that their input is UTF-8 and escape non-UTF-8
characters before sending them to raw_fd_ostream. Clang already does
this, but not all LLVM tools do this.

One drawback to the current implementation is printing a string a byte
at a time doesn't work. Consider this LLVM code:

  for (char C : MyStr) outs() << C;

Because outs() is now unbuffered, we wil try to convert each byte to
UTF-16, which will fail. However, this already didn't work, so I think
we may as well update callers that do that as we find them to print
complete portions of strings. You can see a real example of this in my
patch to SourceMgr.cpp

Fixes PR38669 and PR36267.


https://reviews.llvm.org/D51558

Files:
  llvm/include/llvm/Support/raw_ostream.h
  llvm/lib/Support/Locale.cpp
  llvm/lib/Support/SourceMgr.cpp
  llvm/lib/Support/raw_ostream.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51558.163593.patch
Type: text/x-patch
Size: 7016 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180831/2fa53ee4/attachment.bin>


More information about the llvm-commits mailing list