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