[libc++] Add an option to disable wide character support in libc++

Some embedded platforms do not wish to support the C library functionality
for handling wchar_t because they have no use for it. It makes sense for
libc++ to work properly on those platforms, so this commit adds a carve-out
of functionality for wchar_t.

Unfortunately, unlike some other carve-outs (e.g. random device), this
patch touches several parts of the library. However, despite the wide
impact of this patch, I still think it is important to support this
configuration since it makes it much simpler to port libc++ to some
embedded platforms.

Differential Revision: https://reviews.llvm.org/D111265

NOKEYCHECK=True
GitOrigin-RevId: f4c1258d5633fcf06385ff3fd1f4bf57ab971964
diff --git a/src/iostream.cpp b/src/iostream.cpp
index 99bd528..61d5e54 100644
--- a/src/iostream.cpp
+++ b/src/iostream.cpp
@@ -24,6 +24,8 @@
 ;
 _ALIGNAS_TYPE (__stdinbuf<char> ) static char __cin[sizeof(__stdinbuf <char>)];
 static mbstate_t mb_cin;
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 _ALIGNAS_TYPE (wistream) _LIBCPP_FUNC_VIS char wcin[sizeof(wistream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
 __asm__("?wcin@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_istream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
@@ -31,6 +33,7 @@
 ;
 _ALIGNAS_TYPE (__stdinbuf<wchar_t> ) static char __wcin[sizeof(__stdinbuf <wchar_t>)];
 static mbstate_t mb_wcin;
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
 _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cout[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
@@ -39,6 +42,8 @@
 ;
 _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cout[sizeof(__stdoutbuf<char>)];
 static mbstate_t mb_cout;
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcout[sizeof(wostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
 __asm__("?wcout@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
@@ -46,6 +51,7 @@
 ;
 _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcout[sizeof(__stdoutbuf<wchar_t>)];
 static mbstate_t mb_wcout;
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
 _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char cerr[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
@@ -54,6 +60,8 @@
 ;
 _ALIGNAS_TYPE (__stdoutbuf<char>) static char __cerr[sizeof(__stdoutbuf<char>)];
 static mbstate_t mb_cerr;
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wcerr[sizeof(wostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
 __asm__("?wcerr@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
@@ -61,17 +69,21 @@
 ;
 _ALIGNAS_TYPE (__stdoutbuf<wchar_t>) static char __wcerr[sizeof(__stdoutbuf<wchar_t>)];
 static mbstate_t mb_wcerr;
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
 _ALIGNAS_TYPE (ostream) _LIBCPP_FUNC_VIS char clog[sizeof(ostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
 __asm__("?clog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@DU?$char_traits@D@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
 #endif
 ;
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 _ALIGNAS_TYPE (wostream) _LIBCPP_FUNC_VIS char wclog[sizeof(wostream)]
 #if defined(_LIBCPP_ABI_MICROSOFT) && defined(__clang__)
 __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream@_WU?$char_traits@_W@" _LIBCPP_ABI_NAMESPACE_STR "@std@@@12@A")
 #endif
 ;
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
 // Hacky way to make the compiler believe that we're inside a system header so
 // it doesn't flag the use of the init_priority attribute with a value that's
@@ -109,33 +121,38 @@
     force_locale_initialization();
 
     istream* cin_ptr  = ::new(cin)  istream(::new(__cin)  __stdinbuf <char>(stdin, &mb_cin));
-    wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
     ostream* cout_ptr = ::new(cout) ostream(::new(__cout) __stdoutbuf<char>(stdout, &mb_cout));
-    wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
     ostream* cerr_ptr = ::new(cerr) ostream(::new(__cerr) __stdoutbuf<char>(stderr, &mb_cerr));
                         ::new(clog) ostream(cerr_ptr->rdbuf());
+    cin_ptr->tie(cout_ptr);
+    _VSTD::unitbuf(*cerr_ptr);
+    cerr_ptr->tie(cout_ptr);
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+    wistream* wcin_ptr  = ::new(wcin)  wistream(::new(__wcin)  __stdinbuf <wchar_t>(stdin, &mb_wcin));
+    wostream* wcout_ptr = ::new(wcout) wostream(::new(__wcout) __stdoutbuf<wchar_t>(stdout, &mb_wcout));
     wostream* wcerr_ptr = ::new(wcerr) wostream(::new(__wcerr) __stdoutbuf<wchar_t>(stderr, &mb_wcerr));
                           ::new(wclog) wostream(wcerr_ptr->rdbuf());
 
-    cin_ptr->tie(cout_ptr);
     wcin_ptr->tie(wcout_ptr);
-    _VSTD::unitbuf(*cerr_ptr);
     _VSTD::unitbuf(*wcerr_ptr);
-    cerr_ptr->tie(cout_ptr);
     wcerr_ptr->tie(wcout_ptr);
+#endif
 }
 
 DoIOSInit::~DoIOSInit()
 {
     ostream* cout_ptr = reinterpret_cast<ostream*>(cout);
-    wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
     cout_ptr->flush();
-    wcout_ptr->flush();
-
     ostream* clog_ptr = reinterpret_cast<ostream*>(clog);
-    wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
     clog_ptr->flush();
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+    wostream* wcout_ptr = reinterpret_cast<wostream*>(wcout);
+    wcout_ptr->flush();
+    wostream* wclog_ptr = reinterpret_cast<wostream*>(wclog);
     wclog_ptr->flush();
+#endif
 }
 
 ios_base::Init::Init()