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
36 lines
989 B
C++
36 lines
989 B
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// <iostream>
|
|
|
|
// istream wcin;
|
|
|
|
// UNSUPPORTED: no-wide-characters
|
|
// REQUIRES: target={{.+}}-windows-{{.+}}
|
|
|
|
// UNSUPPORTED: executor-has-no-bash
|
|
// FILE_DEPENDENCIES: send-stdin.sh, test.dat
|
|
// RUN: %{build}
|
|
// RUN: %{exec} bash send-stdin.sh "%t.exe" "test.dat"
|
|
|
|
// Check that wcin works, preserving the unicode characters, after switching
|
|
// stdin to wide mode.
|
|
|
|
#include <iostream>
|
|
#include <cassert>
|
|
#include <io.h>
|
|
#include <fcntl.h>
|
|
|
|
int main(int, char**) {
|
|
_setmode(_fileno(stdin), _O_WTEXT);
|
|
std::wstring str;
|
|
std::wcin >> str;
|
|
assert(str == L"1234\u20ac\u00e5\u00e4\u00f6");
|
|
return 0;
|
|
}
|