|
9 | 9 | #include "std_stream.h" |
10 | 10 | #include <__locale> |
11 | 11 | #include <new> |
12 | | -#include <string> |
13 | 12 |
|
14 | 13 | #define _str(s) #s |
15 | 14 | #define str(s) _str(s) |
16 | 15 | #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE) |
17 | 16 |
|
18 | 17 | _LIBCPP_BEGIN_NAMESPACE_STD |
19 | 18 |
|
20 | | -alignas(istream) _LIBCPP_EXPORTED_FROM_ABI char cin[sizeof(istream)] |
21 | | -#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
22 | | - __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR |
23 | | - "@std@@@12@A") |
24 | | -#endif |
25 | | - ; |
26 | | -alignas(__stdinbuf<char>) static char __cin[sizeof(__stdinbuf<char>)]; |
27 | 19 | static mbstate_t mb_cin; |
28 | | - |
29 | | -#if _LIBCPP_HAS_WIDE_CHARACTERS |
30 | | -alignas(wistream) _LIBCPP_EXPORTED_FROM_ABI char wcin[sizeof(wistream)] |
31 | | -# if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
32 | | - __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR |
33 | | - "@std@@@12@A") |
34 | | -# endif |
35 | | - ; |
36 | | -alignas(__stdinbuf<wchar_t>) static char __wcin[sizeof(__stdinbuf<wchar_t>)]; |
37 | 20 | static mbstate_t mb_wcin; |
38 | | -#endif // _LIBCPP_HAS_WIDE_CHARACTERS |
39 | | - |
40 | | -alignas(ostream) _LIBCPP_EXPORTED_FROM_ABI char cout[sizeof(ostream)] |
41 | | -#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
42 | | - __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR |
43 | | - "@std@@@12@A") |
44 | | -#endif |
45 | | - ; |
46 | | -alignas(__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)]; |
47 | 21 | static mbstate_t mb_cout; |
48 | | - |
49 | | -#if _LIBCPP_HAS_WIDE_CHARACTERS |
50 | | -alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wcout[sizeof(wostream)] |
51 | | -# if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
52 | | - __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR |
53 | | - "@std@@@12@A") |
54 | | -# endif |
55 | | - ; |
56 | | -alignas(__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)]; |
57 | 22 | static mbstate_t mb_wcout; |
58 | | -#endif // _LIBCPP_HAS_WIDE_CHARACTERS |
59 | | - |
60 | | -alignas(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 |
63 | | - "@std@@@12@A") |
64 | | -#endif |
65 | | - ; |
66 | | -alignas(__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)]; |
67 | 23 | static mbstate_t mb_cerr; |
68 | | - |
69 | | -#if _LIBCPP_HAS_WIDE_CHARACTERS |
70 | | -alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wcerr[sizeof(wostream)] |
71 | | -# if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
72 | | - __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR |
73 | | - "@std@@@12@A") |
74 | | -# endif |
75 | | - ; |
76 | | -alignas(__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)]; |
77 | 24 | static mbstate_t mb_wcerr; |
78 | | -#endif // _LIBCPP_HAS_WIDE_CHARACTERS |
79 | 25 |
|
80 | | -alignas(ostream) _LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)] |
81 | | -#if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
82 | | - __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR |
83 | | - "@std@@@12@A") |
| 26 | +#if __has_cpp_attribute(clang::no_destroy) |
| 27 | +template <class T> |
| 28 | +using no_destroy_t = T; |
| 29 | + |
| 30 | +# define STREAM_ATTRS [[clang::no_destroy]] _LIBCPP_INIT_PRIORITY_MAX |
| 31 | +#else |
| 32 | +template <class T> |
| 33 | +using no_destroy_t = __no_destroy<T>; |
| 34 | + |
| 35 | +# define STREAM_ATTRS _LIBCPP_INIT_PRIORITY_MAX |
84 | 36 | #endif |
85 | | - ; |
| 37 | + |
| 38 | +# 38 __FILE__ 3 |
| 39 | + |
| 40 | +STREAM_ATTRS static no_destroy_t<__stdinbuf<char>> cin_stdbuf(stdin, &mb_cin); |
| 41 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<istream> cin(&cin_stdbuf); |
| 42 | + |
| 43 | +STREAM_ATTRS static no_destroy_t<__stdoutbuf<char>> cout_stdbuf(stdout, &mb_cout); |
| 44 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<ostream> cout(&cout_stdbuf); |
| 45 | + |
| 46 | +STREAM_ATTRS static no_destroy_t<__stdoutbuf<char>> cerr_stdbuf(stderr, &mb_cerr); |
| 47 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<ostream> cerr(&cerr_stdbuf); |
| 48 | + |
| 49 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<ostream> clog(&cerr_stdbuf); |
86 | 50 |
|
87 | 51 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
88 | | -alignas(wostream) _LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)] |
89 | | -# if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) |
90 | | - __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR |
91 | | - "@std@@@12@A") |
92 | | -# endif |
93 | | - ; |
| 52 | +STREAM_ATTRS static __stdinbuf<wchar_t> wcin_stdbuf(stdin, &mb_wcin); |
| 53 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI wistream wcin(&wcin_stdbuf); |
| 54 | + |
| 55 | +STREAM_ATTRS static no_destroy_t<__stdoutbuf<wchar_t>> wcout_stdbuf(stdout, &mb_wcout); |
| 56 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<wostream> wcout(&wcout_stdbuf); |
| 57 | + |
| 58 | +STREAM_ATTRS static no_destroy_t<__stdoutbuf<wchar_t>> wcerr_stdbuf(stderr, &mb_wcerr); |
| 59 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<wostream> wcerr(&wcerr_stdbuf); |
| 60 | + |
| 61 | +STREAM_ATTRS _LIBCPP_EXPORTED_FROM_ABI no_destroy_t<wostream> wclog(&wcerr_stdbuf); |
| 62 | + |
94 | 63 | #endif // _LIBCPP_HAS_WIDE_CHARACTERS |
95 | 64 |
|
| 65 | +# 64 __FILE__ 1 |
| 66 | + |
96 | 67 | // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority |
97 | 68 | // attribute with a value that's reserved for the implementation (we're the implementation). |
98 | 69 | #include "iostream_init.h" |
@@ -124,36 +95,47 @@ class DoIOSInit { |
124 | 95 | DoIOSInit::DoIOSInit() { |
125 | 96 | force_locale_initialization(); |
126 | 97 |
|
127 | | - istream* cin_ptr = ::new (cin) istream(::new (__cin) __stdinbuf<char>(stdin, &mb_cin)); |
128 | | - ostream* cout_ptr = ::new (cout) ostream(::new (__cout) __stdoutbuf<char>(stdout, &mb_cout)); |
129 | | - ostream* cerr_ptr = ::new (cerr) ostream(::new (__cerr) __stdoutbuf<char>(stderr, &mb_cerr)); |
130 | | - ::new (clog) ostream(cerr_ptr->rdbuf()); |
| 98 | +#if __has_cpp_attribute(clang::no_destroy) |
| 99 | + auto cin_ptr = &cin; |
| 100 | + auto cout_ptr = &cout; |
| 101 | + auto cerr_ptr = &cerr; |
| 102 | +# if _LIBCPP_HAS_WIDE_CHARACTERS |
| 103 | + auto wcin_ptr = &wcin; |
| 104 | + auto wcout_ptr = &wcout; |
| 105 | + auto wcerr_ptr = &wcerr; |
| 106 | +# endif |
| 107 | +#else |
| 108 | + auto cin_ptr = &cin.__get(); |
| 109 | + auto cout_ptr = &cout.__get(); |
| 110 | + auto cerr_ptr = &cerr.__get(); |
| 111 | +# if _LIBCPP_HAS_WIDE_CHARACTERS |
| 112 | + auto wcin_ptr = &wcin.__get(); |
| 113 | + auto wcout_ptr = &wcout.__get(); |
| 114 | + auto wcerr_ptr = &wcerr.__get(); |
| 115 | +# endif |
| 116 | +#endif |
| 117 | + |
131 | 118 | cin_ptr->tie(cout_ptr); |
132 | 119 | std::unitbuf(*cerr_ptr); |
133 | 120 | cerr_ptr->tie(cout_ptr); |
134 | 121 |
|
135 | 122 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
136 | | - wistream* wcin_ptr = ::new (wcin) wistream(::new (__wcin) __stdinbuf<wchar_t>(stdin, &mb_wcin)); |
137 | | - wostream* wcout_ptr = ::new (wcout) wostream(::new (__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout)); |
138 | | - wostream* wcerr_ptr = ::new (wcerr) wostream(::new (__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr)); |
139 | | - ::new (wclog) wostream(wcerr_ptr->rdbuf()); |
140 | | - |
141 | 123 | wcin_ptr->tie(wcout_ptr); |
142 | 124 | std::unitbuf(*wcerr_ptr); |
143 | 125 | wcerr_ptr->tie(wcout_ptr); |
144 | 126 | #endif |
145 | 127 | } |
146 | 128 |
|
147 | 129 | DoIOSInit::~DoIOSInit() { |
148 | | - ostream* cout_ptr = reinterpret_cast<ostream*>(cout); |
| 130 | + ostream* cout_ptr = reinterpret_cast<ostream*>(&cout); |
149 | 131 | cout_ptr->flush(); |
150 | | - ostream* clog_ptr = reinterpret_cast<ostream*>(clog); |
| 132 | + ostream* clog_ptr = reinterpret_cast<ostream*>(&clog); |
151 | 133 | clog_ptr->flush(); |
152 | 134 |
|
153 | 135 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
154 | | - wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout); |
| 136 | + wostream* wcout_ptr = reinterpret_cast<wostream*>(&wcout); |
155 | 137 | wcout_ptr->flush(); |
156 | | - wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog); |
| 138 | + wostream* wclog_ptr = reinterpret_cast<wostream*>(&wclog); |
157 | 139 | wclog_ptr->flush(); |
158 | 140 | #endif |
159 | 141 | } |
|
0 commit comments