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