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 906c3fb27SDimitry Andric #include "std_stream.h" 10*cb14a3feSDimitry Andric #include <__locale> 1181ad6265SDimitry Andric #include <new> 1281ad6265SDimitry Andric #include <string> 130b57cec5SDimitry Andric 1406c3fb27SDimitry Andric #ifdef _LIBCPP_MSVCRT_LIKE 1506c3fb27SDimitry Andric # include <__locale_dir/locale_base_api/locale_guard.h> 1606c3fb27SDimitry Andric #endif 1706c3fb27SDimitry 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*cb14a3feSDimitry Andric _ALIGNAS_TYPE(istream) 25*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char cin[sizeof(istream)] 260b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 27*cb14a3feSDimitry Andric __asm__("?cin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR 28*cb14a3feSDimitry Andric "@std@@@12@A") 290b57cec5SDimitry Andric #endif 300b57cec5SDimitry Andric ; 310b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdinbuf<char>) static char __cin[sizeof(__stdinbuf<char>)]; 320b57cec5SDimitry Andric static mbstate_t mb_cin; 33349cc55cSDimitry Andric 34349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 35*cb14a3feSDimitry Andric _ALIGNAS_TYPE(wistream) 36*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char wcin[sizeof(wistream)] 370b57cec5SDimitry Andric # if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 38*cb14a3feSDimitry Andric __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR 39*cb14a3feSDimitry Andric "@std@@@12@A") 400b57cec5SDimitry Andric # endif 410b57cec5SDimitry Andric ; 420b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdinbuf<wchar_t>) static char __wcin[sizeof(__stdinbuf<wchar_t>)]; 430b57cec5SDimitry Andric static mbstate_t mb_wcin; 44349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 450b57cec5SDimitry Andric 46*cb14a3feSDimitry Andric _ALIGNAS_TYPE(ostream) 47*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char cout[sizeof(ostream)] 480b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 49*cb14a3feSDimitry Andric __asm__("?cout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR 50*cb14a3feSDimitry Andric "@std@@@12@A") 510b57cec5SDimitry Andric #endif 520b57cec5SDimitry Andric ; 530b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)]; 540b57cec5SDimitry Andric static mbstate_t mb_cout; 55349cc55cSDimitry Andric 56349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 57*cb14a3feSDimitry Andric _ALIGNAS_TYPE(wostream) 58*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char wcout[sizeof(wostream)] 590b57cec5SDimitry Andric # if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 60*cb14a3feSDimitry Andric __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR 61*cb14a3feSDimitry Andric "@std@@@12@A") 620b57cec5SDimitry Andric # endif 630b57cec5SDimitry Andric ; 640b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)]; 650b57cec5SDimitry Andric static mbstate_t mb_wcout; 66349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 670b57cec5SDimitry Andric 68*cb14a3feSDimitry Andric _ALIGNAS_TYPE(ostream) 69*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char cerr[sizeof(ostream)] 700b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 71*cb14a3feSDimitry Andric __asm__("?cerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR 72*cb14a3feSDimitry Andric "@std@@@12@A") 730b57cec5SDimitry Andric #endif 740b57cec5SDimitry Andric ; 750b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)]; 760b57cec5SDimitry Andric static mbstate_t mb_cerr; 77349cc55cSDimitry Andric 78349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 79*cb14a3feSDimitry Andric _ALIGNAS_TYPE(wostream) 80*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char wcerr[sizeof(wostream)] 810b57cec5SDimitry Andric # if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 82*cb14a3feSDimitry Andric __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR 83*cb14a3feSDimitry Andric "@std@@@12@A") 840b57cec5SDimitry Andric # endif 850b57cec5SDimitry Andric ; 860b57cec5SDimitry Andric _ALIGNAS_TYPE(__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)]; 870b57cec5SDimitry Andric static mbstate_t mb_wcerr; 88349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 890b57cec5SDimitry Andric 90*cb14a3feSDimitry Andric _ALIGNAS_TYPE(ostream) 91*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char clog[sizeof(ostream)] 920b57cec5SDimitry Andric #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 93*cb14a3feSDimitry Andric __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR 94*cb14a3feSDimitry Andric "@std@@@12@A") 950b57cec5SDimitry Andric #endif 960b57cec5SDimitry Andric ; 97349cc55cSDimitry Andric 98349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 99*cb14a3feSDimitry Andric _ALIGNAS_TYPE(wostream) 100*cb14a3feSDimitry Andric _LIBCPP_EXPORTED_FROM_ABI char wclog[sizeof(wostream)] 1010b57cec5SDimitry Andric # if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__) 102*cb14a3feSDimitry Andric __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR 103*cb14a3feSDimitry Andric "@std@@@12@A") 1040b57cec5SDimitry Andric # endif 1050b57cec5SDimitry Andric ; 106349cc55cSDimitry Andric #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 1070b57cec5SDimitry Andric 1080eae32dcSDimitry Andric // Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority 1090eae32dcSDimitry Andric // attribute with a value that's reserved for the implementation (we're the implementation). 1100eae32dcSDimitry Andric #include "iostream_init.h" 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // On Windows the TLS storage for locales needs to be initialized before we create 1130b57cec5SDimitry Andric // the standard streams, otherwise it may not be alive during program termination 1140b57cec5SDimitry Andric // when we flush the streams. 1150b57cec5SDimitry Andric static void force_locale_initialization() { 1160b57cec5SDimitry Andric #if defined(_LIBCPP_MSVCRT_LIKE) 1170b57cec5SDimitry Andric static bool once = []() { 1180b57cec5SDimitry Andric auto loc = newlocale(LC_ALL_MASK, "C", 0); 1190b57cec5SDimitry Andric { 1200b57cec5SDimitry Andric __libcpp_locale_guard g(loc); // forces initialization of locale TLS 1210b57cec5SDimitry Andric ((void)g); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric freelocale(loc); 1240b57cec5SDimitry Andric return true; 1250b57cec5SDimitry Andric }(); 1260b57cec5SDimitry Andric ((void)once); 1270b57cec5SDimitry Andric #endif 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 130e40139ffSDimitry Andric class DoIOSInit { 131e40139ffSDimitry Andric public: 132e40139ffSDimitry Andric DoIOSInit(); 133e40139ffSDimitry Andric ~DoIOSInit(); 134e40139ffSDimitry Andric }; 135e40139ffSDimitry Andric 136*cb14a3feSDimitry Andric DoIOSInit::DoIOSInit() { 1370b57cec5SDimitry Andric force_locale_initialization(); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric istream* cin_ptr = ::new (cin) istream(::new (__cin) __stdinbuf<char>(stdin, &mb_cin)); 1400b57cec5SDimitry Andric ostream* cout_ptr = ::new (cout) ostream(::new (__cout) __stdoutbuf<char>(stdout, &mb_cout)); 1410b57cec5SDimitry Andric ostream* cerr_ptr = ::new (cerr) ostream(::new (__cerr) __stdoutbuf<char>(stderr, &mb_cerr)); 1420b57cec5SDimitry Andric ::new (clog) ostream(cerr_ptr->rdbuf()); 143349cc55cSDimitry Andric cin_ptr->tie(cout_ptr); 1445f757f3fSDimitry Andric std::unitbuf(*cerr_ptr); 145349cc55cSDimitry Andric cerr_ptr->tie(cout_ptr); 146349cc55cSDimitry Andric 147349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 148349cc55cSDimitry Andric wistream* wcin_ptr = ::new (wcin) wistream(::new (__wcin) __stdinbuf<wchar_t>(stdin, &mb_wcin)); 149349cc55cSDimitry Andric wostream* wcout_ptr = ::new (wcout) wostream(::new (__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout)); 1500b57cec5SDimitry Andric wostream* wcerr_ptr = ::new (wcerr) wostream(::new (__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr)); 1510b57cec5SDimitry Andric ::new (wclog) wostream(wcerr_ptr->rdbuf()); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric wcin_ptr->tie(wcout_ptr); 1545f757f3fSDimitry Andric std::unitbuf(*wcerr_ptr); 1550b57cec5SDimitry Andric wcerr_ptr->tie(wcout_ptr); 1560b57cec5SDimitry Andric #endif 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 159*cb14a3feSDimitry Andric DoIOSInit::~DoIOSInit() { 1600b57cec5SDimitry Andric ostream* cout_ptr = reinterpret_cast<ostream*>(cout); 1610b57cec5SDimitry Andric cout_ptr->flush(); 1620b57cec5SDimitry Andric ostream* clog_ptr = reinterpret_cast<ostream*>(clog); 1630b57cec5SDimitry Andric clog_ptr->flush(); 164349cc55cSDimitry Andric 165349cc55cSDimitry Andric #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 166349cc55cSDimitry Andric wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout); 167349cc55cSDimitry Andric wcout_ptr->flush(); 168349cc55cSDimitry Andric wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog); 1690b57cec5SDimitry Andric wclog_ptr->flush(); 170349cc55cSDimitry Andric #endif 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 173*cb14a3feSDimitry Andric ios_base::Init::Init() { 174e40139ffSDimitry Andric static DoIOSInit init_the_streams; // gets initialized once 175e40139ffSDimitry Andric } 176e40139ffSDimitry Andric 177*cb14a3feSDimitry Andric ios_base::Init::~Init() {} 178e40139ffSDimitry Andric 1790b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD 180