mirror of
https://github.com/RPCS3/llvm.git
synced 2026-07-18 21:24:32 -04:00
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341433 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -7,24 +7,11 @@ namespace sys {
|
||||
namespace locale {
|
||||
|
||||
int columnWidth(StringRef Text) {
|
||||
#ifdef _WIN32
|
||||
return Text.size();
|
||||
#else
|
||||
return llvm::sys::unicode::columnWidthUTF8(Text);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool isPrint(int UCS) {
|
||||
#ifdef _WIN32
|
||||
// Restrict characters that we'll try to print to the lower part of ASCII
|
||||
// except for the control characters (0x20 - 0x7E). In general one can not
|
||||
// reliably output code points U+0080 and higher using narrow character C/C++
|
||||
// output functions in Windows, because the meaning of the upper 128 codes is
|
||||
// determined by the active code page in the console.
|
||||
return ' ' <= UCS && UCS <= '~';
|
||||
#else
|
||||
return llvm::sys::unicode::isPrintable(UCS);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace locale
|
||||
|
||||
Reference in New Issue
Block a user