Skip to content

Commit 3ea4784

Browse files
committed
[libc++] Refactor iostream.cpp
1 parent b842185 commit 3ea4784

File tree

1 file changed

+60
-78
lines changed

1 file changed

+60
-78
lines changed

libcxx/src/iostream.cpp

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,61 @@
99
#include "std_stream.h"
1010
#include <__locale>
1111
#include <new>
12-
#include <string>
1312

1413
#define _str(s) #s
1514
#define str(s) _str(s)
1615
#define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
1716

1817
_LIBCPP_BEGIN_NAMESPACE_STD
1918

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>)];
2719
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>)];
3720
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>)];
4721
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>)];
5722
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>)];
6723
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>)];
7724
static mbstate_t mb_wcerr;
78-
#endif // _LIBCPP_HAS_WIDE_CHARACTERS
7925

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
8436
#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);
8650

8751
#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+
9463
#endif // _LIBCPP_HAS_WIDE_CHARACTERS
9564

65+
# 64 __FILE__ 1
66+
9667
// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
9768
// attribute with a value that's reserved for the implementation (we're the implementation).
9869
#include "iostream_init.h"
@@ -124,36 +95,47 @@ class DoIOSInit {
12495
DoIOSInit::DoIOSInit() {
12596
force_locale_initialization();
12697

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+
131118
cin_ptr->tie(cout_ptr);
132119
std::unitbuf(*cerr_ptr);
133120
cerr_ptr->tie(cout_ptr);
134121

135122
#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-
141123
wcin_ptr->tie(wcout_ptr);
142124
std::unitbuf(*wcerr_ptr);
143125
wcerr_ptr->tie(wcout_ptr);
144126
#endif
145127
}
146128

147129
DoIOSInit::~DoIOSInit() {
148-
ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
130+
ostream* cout_ptr = reinterpret_cast<ostream*>(&cout);
149131
cout_ptr->flush();
150-
ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
132+
ostream* clog_ptr = reinterpret_cast<ostream*>(&clog);
151133
clog_ptr->flush();
152134

153135
#if _LIBCPP_HAS_WIDE_CHARACTERS
154-
wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
136+
wostream* wcout_ptr = reinterpret_cast<wostream*>(&wcout);
155137
wcout_ptr->flush();
156-
wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
138+
wostream* wclog_ptr = reinterpret_cast<wostream*>(&wclog);
157139
wclog_ptr->flush();
158140
#endif
159141
}

0 commit comments

Comments
 (0)