1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "std_stream.h"
14 #ifdef _LIBCPP_MSVCRT_LIKE
15 # include <__locale_dir/locale_base_api/locale_guard.h>
19 #define str(s) _str(s)
20 #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 _ALIGNAS_TYPE (istream
) _LIBCPP_EXPORTED_FROM_ABI
char cin
[sizeof(istream
)]
25 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
26 __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
29 _ALIGNAS_TYPE (__stdinbuf
<char> ) static char __cin
[sizeof(__stdinbuf
<char>)];
30 static mbstate_t mb_cin
;
32 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
33 _ALIGNAS_TYPE (wistream
) _LIBCPP_EXPORTED_FROM_ABI
char wcin
[sizeof(wistream
)]
34 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
35 __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
38 _ALIGNAS_TYPE (__stdinbuf
<wchar_t> ) static char __wcin
[sizeof(__stdinbuf
<wchar_t>)];
39 static mbstate_t mb_wcin
;
40 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
42 _ALIGNAS_TYPE (ostream
) _LIBCPP_EXPORTED_FROM_ABI
char cout
[sizeof(ostream
)]
43 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
44 __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
47 _ALIGNAS_TYPE (__stdoutbuf
<char>) static char __cout
[sizeof(__stdoutbuf
<char>)];
48 static mbstate_t mb_cout
;
50 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
51 _ALIGNAS_TYPE (wostream
) _LIBCPP_EXPORTED_FROM_ABI
char wcout
[sizeof(wostream
)]
52 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
53 __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
56 _ALIGNAS_TYPE (__stdoutbuf
<wchar_t>) static char __wcout
[sizeof(__stdoutbuf
<wchar_t>)];
57 static mbstate_t mb_wcout
;
58 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
60 _ALIGNAS_TYPE (ostream
) _LIBCPP_EXPORTED_FROM_ABI
char cerr
[sizeof(ostream
)]
61 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
62 __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
65 _ALIGNAS_TYPE (__stdoutbuf
<char>) static char __cerr
[sizeof(__stdoutbuf
<char>)];
66 static mbstate_t mb_cerr
;
68 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
69 _ALIGNAS_TYPE (wostream
) _LIBCPP_EXPORTED_FROM_ABI
char wcerr
[sizeof(wostream
)]
70 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
71 __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
74 _ALIGNAS_TYPE (__stdoutbuf
<wchar_t>) static char __wcerr
[sizeof(__stdoutbuf
<wchar_t>)];
75 static mbstate_t mb_wcerr
;
76 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
78 _ALIGNAS_TYPE (ostream
) _LIBCPP_EXPORTED_FROM_ABI
char clog
[sizeof(ostream
)]
79 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
80 __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
84 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
85 _ALIGNAS_TYPE (wostream
) _LIBCPP_EXPORTED_FROM_ABI
char wclog
[sizeof(wostream
)]
86 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
87 __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR
"@std@@@12@A")
90 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
92 // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
93 // attribute with a value that's reserved for the implementation (we're the implementation).
94 #include "iostream_init.h"
96 // On Windows the TLS storage for locales needs to be initialized before we create
97 // the standard streams, otherwise it may not be alive during program termination
98 // when we flush the streams.
99 static void force_locale_initialization() {
100 #if defined(_LIBCPP_MSVCRT_LIKE)
101 static bool once
= []() {
102 auto loc
= newlocale(LC_ALL_MASK
, "C", 0);
104 __libcpp_locale_guard
g(loc
); // forces initialization of locale TLS
120 DoIOSInit::DoIOSInit()
122 force_locale_initialization();
124 istream
* cin_ptr
= ::new(cin
) istream(::new(__cin
) __stdinbuf
<char>(stdin
, &mb_cin
));
125 ostream
* cout_ptr
= ::new(cout
) ostream(::new(__cout
) __stdoutbuf
<char>(stdout
, &mb_cout
));
126 ostream
* cerr_ptr
= ::new(cerr
) ostream(::new(__cerr
) __stdoutbuf
<char>(stderr
, &mb_cerr
));
127 ::new(clog
) ostream(cerr_ptr
->rdbuf());
128 cin_ptr
->tie(cout_ptr
);
129 _VSTD::unitbuf(*cerr_ptr
);
130 cerr_ptr
->tie(cout_ptr
);
132 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
133 wistream
* wcin_ptr
= ::new(wcin
) wistream(::new(__wcin
) __stdinbuf
<wchar_t>(stdin
, &mb_wcin
));
134 wostream
* wcout_ptr
= ::new(wcout
) wostream(::new(__wcout
) __stdoutbuf
<wchar_t>(stdout
, &mb_wcout
));
135 wostream
* wcerr_ptr
= ::new(wcerr
) wostream(::new(__wcerr
) __stdoutbuf
<wchar_t>(stderr
, &mb_wcerr
));
136 ::new(wclog
) wostream(wcerr_ptr
->rdbuf());
138 wcin_ptr
->tie(wcout_ptr
);
139 _VSTD::unitbuf(*wcerr_ptr
);
140 wcerr_ptr
->tie(wcout_ptr
);
144 DoIOSInit::~DoIOSInit()
146 ostream
* cout_ptr
= reinterpret_cast<ostream
*>(cout
);
148 ostream
* clog_ptr
= reinterpret_cast<ostream
*>(clog
);
151 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
152 wostream
* wcout_ptr
= reinterpret_cast<wostream
*>(wcout
);
154 wostream
* wclog_ptr
= reinterpret_cast<wostream
*>(wclog
);
159 ios_base::Init::Init()
161 static DoIOSInit init_the_streams
; // gets initialized once
164 ios_base::Init::~Init()
168 _LIBCPP_END_NAMESPACE_STD