Skip to content

Commit 2954b3e

Browse files
committed
[libc++] Refactor iostream.cpp
1 parent b842185 commit 2954b3e

File tree

2 files changed

+52
-78
lines changed

2 files changed

+52
-78
lines changed

libcxx/include/__utility/no_destroy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ struct __no_destroy {
4545
_LIBCPP_HIDE_FROM_ABI _Tp& __get() { return *reinterpret_cast<_Tp*>(__obj_); }
4646
_LIBCPP_HIDE_FROM_ABI _Tp const& __get() const { return *reinterpret_cast<const _Tp*>(__obj_); }
4747

48+
_LIBCPP_HIDE_FROM_ABI _Tp* operator&() { return &__get(); }
49+
_LIBCPP_HIDE_FROM_ABI const _Tp* operator&() const { return &__get(); }
50+
4851
private:
4952
_ALIGNAS_TYPE(_Tp) char __obj_[sizeof(_Tp)];
5053
};

libcxx/src/iostream.cpp

Lines changed: 49 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+
# 65 __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,36 @@ 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+
auto cin_ptr = &cin;
99+
auto cout_ptr = &cout;
100+
auto cerr_ptr = &cerr;
101+
#if _LIBCPP_HAS_WIDE_CHARACTERS
102+
auto wcin_ptr = &wcin;
103+
auto wcout_ptr = &wcout;
104+
auto wcerr_ptr = &wcerr;
105+
#endif
106+
131107
cin_ptr->tie(cout_ptr);
132108
std::unitbuf(*cerr_ptr);
133109
cerr_ptr->tie(cout_ptr);
134110

135111
#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-
141112
wcin_ptr->tie(wcout_ptr);
142113
std::unitbuf(*wcerr_ptr);
143114
wcerr_ptr->tie(wcout_ptr);
144115
#endif
145116
}
146117

147118
DoIOSInit::~DoIOSInit() {
148-
ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
119+
ostream* cout_ptr = &cout;
149120
cout_ptr->flush();
150-
ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
121+
ostream* clog_ptr = &clog;
151122
clog_ptr->flush();
152123

153124
#if _LIBCPP_HAS_WIDE_CHARACTERS
154-
wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
125+
wostream* wcout_ptr = &wcout;
155126
wcout_ptr->flush();
156-
wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
127+
wostream* wclog_ptr = &wclog;
157128
wclog_ptr->flush();
158129
#endif
159130
}

0 commit comments

Comments
 (0)