Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "__std_stream" |
Eric Fiselier | 1be552d | 2019-05-22 03:45:49 +0000 | [diff] [blame] | 10 | #include "__locale" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | #include "string" |
Howard Hinnant | 877c679 | 2012-03-16 15:13:51 +0000 | [diff] [blame] | 12 | #include "new" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | |
Shoaib Meenai | bc5af9d | 2017-08-07 19:59:58 +0000 | [diff] [blame] | 14 | #define _str(s) #s |
| 15 | #define str(s) _str(s) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 16 | #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE) |
Shoaib Meenai | bc5af9d | 2017-08-07 19:59:58 +0000 | [diff] [blame] | 17 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 19 | |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 20 | _ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin[sizeof(istream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 21 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 22 | __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 23 | #endif |
| 24 | ; |
| 25 | _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 26 | static mbstate_t mb_cin; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 27 | |
| 28 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 29 | _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 30 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 31 | __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 32 | #endif |
| 33 | ; |
| 34 | _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 35 | static mbstate_t mb_wcin; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 36 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 37 | |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 38 | _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 39 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 40 | __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 41 | #endif |
| 42 | ; |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 43 | _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 44 | static mbstate_t mb_cout; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 45 | |
| 46 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 47 | _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 48 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 49 | __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 50 | #endif |
| 51 | ; |
Howard Hinnant | 62cad69 | 2012-05-31 19:31:14 +0000 | [diff] [blame] | 52 | _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 53 | static mbstate_t mb_wcout; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 54 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 55 | |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 56 | _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 57 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 58 | __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 59 | #endif |
| 60 | ; |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 61 | _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 62 | static mbstate_t mb_cerr; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 63 | |
| 64 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 65 | _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 66 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 67 | __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 68 | #endif |
| 69 | ; |
Howard Hinnant | 62cad69 | 2012-05-31 19:31:14 +0000 | [diff] [blame] | 70 | _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)]; |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 71 | static mbstate_t mb_wcerr; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 72 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 74 | _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 75 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 76 | __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 77 | #endif |
| 78 | ; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 79 | |
| 80 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 81 | _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)] |
Shoaib Meenai | 759d2c0 | 2017-04-19 20:11:04 +0000 | [diff] [blame] | 82 | #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
Eric Fiselier | b33778a | 2018-10-30 21:44:53 +0000 | [diff] [blame] | 83 | __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A") |
Eric Fiselier | a53c6ea | 2017-01-07 06:09:12 +0000 | [diff] [blame] | 84 | #endif |
| 85 | ; |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 86 | #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | |
Louis Dionne | 0443961 | 2021-12-08 08:26:27 -0500 | [diff] [blame] | 88 | // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority |
| 89 | // attribute with a value that's reserved for the implementation (we're the implementation). |
| 90 | #include "iostream_init.h" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | |
Eric Fiselier | 1be552d | 2019-05-22 03:45:49 +0000 | [diff] [blame] | 92 | // On Windows the TLS storage for locales needs to be initialized before we create |
| 93 | // the standard streams, otherwise it may not be alive during program termination |
| 94 | // when we flush the streams. |
| 95 | static void force_locale_initialization() { |
| 96 | #if defined(_LIBCPP_MSVCRT_LIKE) |
| 97 | static bool once = []() { |
| 98 | auto loc = newlocale(LC_ALL_MASK, "C", 0); |
| 99 | { |
| 100 | __libcpp_locale_guard g(loc); // forces initialization of locale TLS |
| 101 | ((void)g); |
| 102 | } |
| 103 | freelocale(loc); |
| 104 | return true; |
| 105 | }(); |
| 106 | ((void)once); |
| 107 | #endif |
| 108 | } |
| 109 | |
Marshall Clow | bc276f1 | 2019-09-13 15:28:06 +0000 | [diff] [blame] | 110 | class DoIOSInit { |
| 111 | public: |
| 112 | DoIOSInit(); |
| 113 | ~DoIOSInit(); |
| 114 | }; |
| 115 | |
| 116 | DoIOSInit::DoIOSInit() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | { |
Eric Fiselier | 1be552d | 2019-05-22 03:45:49 +0000 | [diff] [blame] | 118 | force_locale_initialization(); |
| 119 | |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 120 | istream* cin_ptr = ::new(cin) istream(::new(__cin) __stdinbuf <char>(stdin, &mb_cin)); |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 121 | ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout)); |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 122 | ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr)); |
Howard Hinnant | 877c679 | 2012-03-16 15:13:51 +0000 | [diff] [blame] | 123 | ::new(clog) ostream(cerr_ptr->rdbuf()); |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 124 | cin_ptr->tie(cout_ptr); |
| 125 | _VSTD::unitbuf(*cerr_ptr); |
| 126 | cerr_ptr->tie(cout_ptr); |
| 127 | |
| 128 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
| 129 | wistream* wcin_ptr = ::new(wcin) wistream(::new(__wcin) __stdinbuf <wchar_t>(stdin, &mb_wcin)); |
| 130 | wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout)); |
Ed Schouten | 0d354bd | 2015-03-26 14:33:46 +0000 | [diff] [blame] | 131 | wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr)); |
Howard Hinnant | 877c679 | 2012-03-16 15:13:51 +0000 | [diff] [blame] | 132 | ::new(wclog) wostream(wcerr_ptr->rdbuf()); |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 133 | |
Howard Hinnant | 877c679 | 2012-03-16 15:13:51 +0000 | [diff] [blame] | 134 | wcin_ptr->tie(wcout_ptr); |
| 135 | _VSTD::unitbuf(*wcerr_ptr); |
| 136 | wcerr_ptr->tie(wcout_ptr); |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 137 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Marshall Clow | bc276f1 | 2019-09-13 15:28:06 +0000 | [diff] [blame] | 140 | DoIOSInit::~DoIOSInit() |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 141 | { |
Joerg Sonnenberger | b5bfdec | 2013-04-27 19:12:36 +0000 | [diff] [blame] | 142 | ostream* cout_ptr = reinterpret_cast<ostream*>(cout); |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 143 | cout_ptr->flush(); |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 144 | ostream* clog_ptr = reinterpret_cast<ostream*>(clog); |
Ed Schouten | 34f9b54 | 2015-03-17 18:40:58 +0000 | [diff] [blame] | 145 | clog_ptr->flush(); |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 146 | |
| 147 | #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
| 148 | wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout); |
| 149 | wcout_ptr->flush(); |
| 150 | wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog); |
Howard Hinnant | 877c679 | 2012-03-16 15:13:51 +0000 | [diff] [blame] | 151 | wclog_ptr->flush(); |
Louis Dionne | 8925814 | 2021-08-23 15:32:36 -0400 | [diff] [blame] | 152 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Marshall Clow | bc276f1 | 2019-09-13 15:28:06 +0000 | [diff] [blame] | 155 | ios_base::Init::Init() |
| 156 | { |
| 157 | static DoIOSInit init_the_streams; // gets initialized once |
| 158 | } |
| 159 | |
| 160 | ios_base::Init::~Init() |
| 161 | { |
| 162 | } |
| 163 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | _LIBCPP_END_NAMESPACE_STD |