[Windows] Convert from UTF-8 to UTF-16 when writing to a Windows console
authorReid Kleckner <rnk@google.com>
Wed, 5 Sep 2018 00:08:56 +0000 (00:08 +0000)
committerReid Kleckner <rnk@google.com>
Wed, 5 Sep 2018 00:08:56 +0000 (00:08 +0000)
commit2b8c69204b1d37d91cc775d51af50df7f478cecd
tree67a7bb12b6c40ec503027e5a4f735a53be435b7f
parent2768b52117cb6a1eb0d6a0c4bb01cf52d436adbb
[Windows] Convert from UTF-8 to UTF-16 when writing to a Windows console

Summary:
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.

Reviewers: zturner, efriedma

Subscribers: llvm-commits, hiraditya

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

llvm-svn: 341433
llvm/include/llvm/Support/raw_ostream.h
llvm/lib/Support/Locale.cpp
llvm/lib/Support/SourceMgr.cpp
llvm/lib/Support/raw_ostream.cpp