xref: /freebsd/contrib/llvm-project/libcxx/src/iostream.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 
981ad6265SDimitry Andric #include <__locale>
10*06c3fb27SDimitry Andric #include "std_stream.h"
1181ad6265SDimitry Andric #include <new>
1281ad6265SDimitry Andric #include <string>
130b57cec5SDimitry Andric 
14*06c3fb27SDimitry Andric #ifdef _LIBCPP_MSVCRT_LIKE
15*06c3fb27SDimitry Andric #  include <__locale_dir/locale_base_api/locale_guard.h>
16*06c3fb27SDimitry Andric #endif
17*06c3fb27SDimitry Andric 
180b57cec5SDimitry Andric #define _str(s) #s
190b57cec5SDimitry Andric #define str(s) _str(s)
200b57cec5SDimitry Andric #define _LIBCPP_ABI_NAMESPACE_STR str(_LIBCPP_ABI_NAMESPACE)
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
230b57cec5SDimitry Andric 
24*06c3fb27SDimitry Andric _ALIGNAS_TYPE (istream) _LIBCPP_EXPORTED_FROM_ABI char cin[sizeof(istream)]
250b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
260b57cec5SDimitry Andric __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
270b57cec5SDimitry Andric #endif
280b57cec5SDimitry Andric ;
290b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)];
300b57cec5SDimitry Andric static mbstate_t mb_cin;
31349cc55cSDimitry Andric 
32349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
33*06c3fb27SDimitry Andric _ALIGNAS_TYPE (wistream) _LIBCPP_EXPORTED_FROM_ABI char wcin[sizeof(wistream)]
340b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
350b57cec5SDimitry Andric __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
360b57cec5SDimitry Andric #endif
370b57cec5SDimitry Andric ;
380b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)];
390b57cec5SDimitry Andric static mbstate_t mb_wcin;
40349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
410b57cec5SDimitry Andric 
42*06c3fb27SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_EXPORTED_FROM_ABI char cout[sizeof(ostream)]
430b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
440b57cec5SDimitry Andric __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
450b57cec5SDimitry Andric #endif
460b57cec5SDimitry Andric ;
470b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
480b57cec5SDimitry Andric static mbstate_t mb_cout;
49349cc55cSDimitry Andric 
50349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
51*06c3fb27SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_EXPORTED_FROM_ABI char wcout[sizeof(wostream)]
520b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
530b57cec5SDimitry Andric __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
540b57cec5SDimitry Andric #endif
550b57cec5SDimitry Andric ;
560b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
570b57cec5SDimitry Andric static mbstate_t mb_wcout;
58349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
590b57cec5SDimitry Andric 
60*06c3fb27SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_EXPORTED_FROM_ABI char cerr[sizeof(ostream)]
610b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
620b57cec5SDimitry Andric __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
630b57cec5SDimitry Andric #endif
640b57cec5SDimitry Andric ;
650b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
660b57cec5SDimitry Andric static mbstate_t mb_cerr;
67349cc55cSDimitry Andric 
68349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
69*06c3fb27SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_EXPORTED_FROM_ABI char wcerr[sizeof(wostream)]
700b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
710b57cec5SDimitry Andric __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
720b57cec5SDimitry Andric #endif
730b57cec5SDimitry Andric ;
740b57cec5SDimitry Andric _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
750b57cec5SDimitry Andric static mbstate_t mb_wcerr;
76349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
770b57cec5SDimitry Andric 
78*06c3fb27SDimitry Andric _ALIGNAS_TYPE (ostream) _LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)]
790b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
800b57cec5SDimitry Andric __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
810b57cec5SDimitry Andric #endif
820b57cec5SDimitry Andric ;
83349cc55cSDimitry Andric 
84349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
85*06c3fb27SDimitry Andric _ALIGNAS_TYPE (wostream) _LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)]
860b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
870b57cec5SDimitry Andric __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
880b57cec5SDimitry Andric #endif
890b57cec5SDimitry Andric ;
90349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
910b57cec5SDimitry Andric 
920eae32dcSDimitry Andric // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
930eae32dcSDimitry Andric // attribute with a value that's reserved for the implementation (we're the implementation).
940eae32dcSDimitry Andric #include "iostream_init.h"
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric // On Windows the TLS storage for locales needs to be initialized before we create
970b57cec5SDimitry Andric // the standard streams, otherwise it may not be alive during program termination
980b57cec5SDimitry Andric // when we flush the streams.
990b57cec5SDimitry Andric static void force_locale_initialization() {
1000b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT_LIKE)
1010b57cec5SDimitry Andric   static bool once = []() {
1020b57cec5SDimitry Andric     auto loc = newlocale(LC_ALL_MASK, "C", 0);
1030b57cec5SDimitry Andric     {
1040b57cec5SDimitry Andric         __libcpp_locale_guard g(loc); // forces initialization of locale TLS
1050b57cec5SDimitry Andric         ((void)g);
1060b57cec5SDimitry Andric     }
1070b57cec5SDimitry Andric     freelocale(loc);
1080b57cec5SDimitry Andric     return true;
1090b57cec5SDimitry Andric   }();
1100b57cec5SDimitry Andric   ((void)once);
1110b57cec5SDimitry Andric #endif
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
114e40139ffSDimitry Andric class DoIOSInit {
115e40139ffSDimitry Andric public:
116e40139ffSDimitry Andric     DoIOSInit();
117e40139ffSDimitry Andric     ~DoIOSInit();
118e40139ffSDimitry Andric };
119e40139ffSDimitry Andric 
120e40139ffSDimitry Andric DoIOSInit::DoIOSInit()
1210b57cec5SDimitry Andric {
1220b57cec5SDimitry Andric     force_locale_initialization();
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
1250b57cec5SDimitry Andric     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
1260b57cec5SDimitry Andric     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
1270b57cec5SDimitry Andric                         ::new(clog) ostream(cerr_ptr->rdbuf());
128349cc55cSDimitry Andric     cin_ptr->tie(cout_ptr);
129349cc55cSDimitry Andric     _VSTD::unitbuf(*cerr_ptr);
130349cc55cSDimitry Andric     cerr_ptr->tie(cout_ptr);
131349cc55cSDimitry Andric 
132349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
133349cc55cSDimitry Andric     wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
134349cc55cSDimitry Andric     wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
1350b57cec5SDimitry Andric     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
1360b57cec5SDimitry Andric                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric     wcin_ptr->tie(wcout_ptr);
1390b57cec5SDimitry Andric     _VSTD::unitbuf(*wcerr_ptr);
1400b57cec5SDimitry Andric     wcerr_ptr->tie(wcout_ptr);
1410b57cec5SDimitry Andric #endif
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
144e40139ffSDimitry Andric DoIOSInit::~DoIOSInit()
1450b57cec5SDimitry Andric {
1460b57cec5SDimitry Andric     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
1470b57cec5SDimitry Andric     cout_ptr->flush();
1480b57cec5SDimitry Andric     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
1490b57cec5SDimitry Andric     clog_ptr->flush();
150349cc55cSDimitry Andric 
151349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
152349cc55cSDimitry Andric     wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
153349cc55cSDimitry Andric     wcout_ptr->flush();
154349cc55cSDimitry Andric     wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
1550b57cec5SDimitry Andric     wclog_ptr->flush();
156349cc55cSDimitry Andric #endif
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric 
159e40139ffSDimitry Andric ios_base::Init::Init()
160e40139ffSDimitry Andric {
161e40139ffSDimitry Andric     static DoIOSInit init_the_streams; // gets initialized once
162e40139ffSDimitry Andric }
163e40139ffSDimitry Andric 
164e40139ffSDimitry Andric ios_base::Init::~Init()
165e40139ffSDimitry Andric {
166e40139ffSDimitry Andric }
167e40139ffSDimitry Andric 
1680b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
169