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