xref: /freebsd/contrib/llvm-project/libcxx/src/iostream.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "__std_stream"
100b57cec5SDimitry Andric #include "__locale"
110b57cec5SDimitry Andric #include "string"
120b57cec5SDimitry Andric #include "new"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #define _str(s) #s
150b57cec5SDimitry Andric #define str(s) _str(s)
160b57cec5SDimitry Andric #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric _ALIGNAS_TYPE (istream) _LIBCPP_FUNC_VIS char cin[sizeof(istream)]
210b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
220b57cec5SDimitry Andric __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
230b57cec5SDimitry Andric #endif
240b57cec5SDimitry Andric ;
250b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)];
260b57cec5SDimitry Andric static mbstate_t mb_cin;
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
290b57cec5SDimitry Andric _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)]
300b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
310b57cec5SDimitry Andric __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
320b57cec5SDimitry Andric #endif
330b57cec5SDimitry Andric ;
340b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)];
350b57cec5SDimitry Andric static mbstate_t mb_wcin;
36349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)]
390b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
400b57cec5SDimitry Andric __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
410b57cec5SDimitry Andric #endif
420b57cec5SDimitry Andric ;
430b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
440b57cec5SDimitry Andric static mbstate_t mb_cout;
45349cc55cSDimitry Andric 
46349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
470b57cec5SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)]
480b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
490b57cec5SDimitry Andric __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
500b57cec5SDimitry Andric #endif
510b57cec5SDimitry Andric ;
520b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
530b57cec5SDimitry Andric static mbstate_t mb_wcout;
54349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)]
570b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
580b57cec5SDimitry Andric __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
590b57cec5SDimitry Andric #endif
600b57cec5SDimitry Andric ;
610b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
620b57cec5SDimitry Andric static mbstate_t mb_cerr;
63349cc55cSDimitry Andric 
64349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
650b57cec5SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)]
660b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
670b57cec5SDimitry Andric __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
680b57cec5SDimitry Andric #endif
690b57cec5SDimitry Andric ;
700b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
710b57cec5SDimitry Andric static mbstate_t mb_wcerr;
72349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)]
750b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
760b57cec5SDimitry Andric __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
770b57cec5SDimitry Andric #endif
780b57cec5SDimitry Andric ;
79349cc55cSDimitry Andric 
80349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
810b57cec5SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)]
820b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
830b57cec5SDimitry Andric __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
840b57cec5SDimitry Andric #endif
850b57cec5SDimitry Andric ;
86349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
870b57cec5SDimitry Andric 
88*0eae32dcSDimitry Andric // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
89*0eae32dcSDimitry Andric // attribute with a value that's reserved for the implementation (we're the implementation).
90*0eae32dcSDimitry Andric #include "iostream_init.h"
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric // On Windows the TLS storage for locales needs to be initialized before we create
930b57cec5SDimitry Andric // the standard streams, otherwise it may not be alive during program termination
940b57cec5SDimitry Andric // when we flush the streams.
950b57cec5SDimitry Andric static void force_locale_initialization() {
960b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT_LIKE)
970b57cec5SDimitry Andric   static bool once = []() {
980b57cec5SDimitry Andric     auto loc = newlocale(LC_ALL_MASK, "C", 0);
990b57cec5SDimitry Andric     {
1000b57cec5SDimitry Andric         __libcpp_locale_guard g(loc); // forces initialization of locale TLS
1010b57cec5SDimitry Andric         ((void)g);
1020b57cec5SDimitry Andric     }
1030b57cec5SDimitry Andric     freelocale(loc);
1040b57cec5SDimitry Andric     return true;
1050b57cec5SDimitry Andric   }();
1060b57cec5SDimitry Andric   ((void)once);
1070b57cec5SDimitry Andric #endif
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
110e40139ffSDimitry Andric class DoIOSInit {
111e40139ffSDimitry Andric public:
112e40139ffSDimitry Andric 	DoIOSInit();
113e40139ffSDimitry Andric 	~DoIOSInit();
114e40139ffSDimitry Andric };
115e40139ffSDimitry Andric 
116e40139ffSDimitry Andric DoIOSInit::DoIOSInit()
1170b57cec5SDimitry Andric {
1180b57cec5SDimitry Andric     force_locale_initialization();
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
1210b57cec5SDimitry Andric     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
1220b57cec5SDimitry Andric     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
1230b57cec5SDimitry Andric                         ::new(clog) ostream(cerr_ptr->rdbuf());
124349cc55cSDimitry Andric     cin_ptr->tie(cout_ptr);
125349cc55cSDimitry Andric     _VSTD::unitbuf(*cerr_ptr);
126349cc55cSDimitry Andric     cerr_ptr->tie(cout_ptr);
127349cc55cSDimitry Andric 
128349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
129349cc55cSDimitry Andric     wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
130349cc55cSDimitry Andric     wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
1310b57cec5SDimitry Andric     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
1320b57cec5SDimitry Andric                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric     wcin_ptr->tie(wcout_ptr);
1350b57cec5SDimitry Andric     _VSTD::unitbuf(*wcerr_ptr);
1360b57cec5SDimitry Andric     wcerr_ptr->tie(wcout_ptr);
1370b57cec5SDimitry Andric #endif
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric 
140e40139ffSDimitry Andric DoIOSInit::~DoIOSInit()
1410b57cec5SDimitry Andric {
1420b57cec5SDimitry Andric     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
1430b57cec5SDimitry Andric     cout_ptr->flush();
1440b57cec5SDimitry Andric     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
1450b57cec5SDimitry Andric     clog_ptr->flush();
146349cc55cSDimitry Andric 
147349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
148349cc55cSDimitry Andric     wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
149349cc55cSDimitry Andric     wcout_ptr->flush();
150349cc55cSDimitry Andric     wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
1510b57cec5SDimitry Andric     wclog_ptr->flush();
152349cc55cSDimitry Andric #endif
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
155e40139ffSDimitry Andric ios_base::Init::Init()
156e40139ffSDimitry Andric {
157e40139ffSDimitry Andric     static DoIOSInit init_the_streams; // gets initialized once
158e40139ffSDimitry Andric }
159e40139ffSDimitry Andric 
160e40139ffSDimitry Andric ios_base::Init::~Init()
161e40139ffSDimitry Andric {
162e40139ffSDimitry Andric }
163e40139ffSDimitry Andric 
1640b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
165