clean up use of _WIN32

Replace the use of _WIN32 in libc++. Replace most use with a C runtime
check _LIBCPP_MSVCRT or the new _LIBCPP_WIN32 to indicate that we are
using the Win32 API. Use a new _LIBCPP_WCHAR_IS_UCS2 to indicate that we
are on an environment that has a short wchar_t.

llvm-svn: 290910
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: b2826a1ddc761fb6abcf8d80793a20387e9dc5f6
diff --git a/src/chrono.cpp b/src/chrono.cpp
index 28cdb9d..8c8e6b5 100644
--- a/src/chrono.cpp
+++ b/src/chrono.cpp
@@ -12,7 +12,7 @@
 #include "system_error"  // __throw_system_error
 #include <time.h>        // clock_gettime, CLOCK_MONOTONIC and CLOCK_REALTIME
 
-#if defined(_WIN32)
+#if defined(_LIBCPP_WIN32API)
 #define WIN32_LEAN_AND_MEAN
 #define VC_EXTRA_LEAN
 #include <Windows.h>
@@ -22,13 +22,13 @@
 #else
 #if !defined(CLOCK_REALTIME)
 #include <sys/time.h>        // for gettimeofday and timeval
-#endif
-#endif
+#endif // !defined(CLOCK_REALTIME)
+#endif // defined(_LIBCPP_WIN32API)
 
 #if !defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK)
 #if __APPLE__
 #include <mach/mach_time.h>  // mach_absolute_time, mach_timebase_info_data_t
-#elif !defined(_WIN32) && !defined(CLOCK_MONOTONIC)
+#elif !defined(_LIBCPP_WIN32API) && !defined(CLOCK_MONOTONIC)
 #error "Monotonic clock not implemented"
 #endif
 #endif
@@ -45,7 +45,7 @@
 system_clock::time_point
 system_clock::now() _NOEXCEPT
 {
-#if defined(_WIN32)
+#if defined(_LIBCPP_WIN32API)
   // FILETIME is in 100ns units
   using filetime_duration =
       _VSTD::chrono::duration<__int64,
@@ -158,7 +158,7 @@
     return time_point(duration(fp()));
 }
 
-#elif defined(_WIN32)
+#elif defined(_LIBCPP_WIN32API)
 
 steady_clock::time_point
 steady_clock::now() _NOEXCEPT
diff --git a/src/include/config_elast.h b/src/include/config_elast.h
index 53b3184..304685e 100644
--- a/src/include/config_elast.h
+++ b/src/include/config_elast.h
@@ -10,7 +10,7 @@
 #ifndef _LIBCPP_CONFIG_ELAST
 #define _LIBCPP_CONFIG_ELAST
 
-#if defined(_WIN32)
+#if defined(_LIBCPP_MSCVRT)
 #include <stdlib.h>
 #else
 #include <errno.h>
@@ -28,7 +28,7 @@
 // No _LIBCPP_ELAST needed on Apple
 #elif defined(__sun__)
 #define _LIBCPP_ELAST ESTALE
-#elif defined(_WIN32)
+#elif defined(_LIBCPP_MSCVRT)
 #define _LIBCPP_ELAST _sys_nerr
 #else
 // Warn here so that the person doing the libcxx port has an easier time:
diff --git a/src/locale.cpp b/src/locale.cpp
index 5c7fa08..4b46266 100644
--- a/src/locale.cpp
+++ b/src/locale.cpp
@@ -3254,7 +3254,7 @@
     const intern_type* frm, const intern_type* frm_end, const intern_type*& frm_nxt,
     extern_type* to, extern_type* to_end, extern_type*& to_nxt) const
 {
-#if _WIN32
+#if defined(_LIBCPP_SHORT_WCHAR)
     const uint16_t* _frm = reinterpret_cast<const uint16_t*>(frm);
     const uint16_t* _frm_end = reinterpret_cast<const uint16_t*>(frm_end);
     const uint16_t* _frm_nxt = _frm;
@@ -3266,7 +3266,7 @@
     uint8_t* _to = reinterpret_cast<uint8_t*>(to);
     uint8_t* _to_end = reinterpret_cast<uint8_t*>(to_end);
     uint8_t* _to_nxt = _to;
-#if _WIN32
+#if defined(_LIBCPP_SHORT_WCHAR)
     result r = ucs2_to_utf8(_frm, _frm_end, _frm_nxt, _to, _to_end, _to_nxt,
                             _Maxcode_, _Mode_);
 #else
@@ -3286,7 +3286,7 @@
     const uint8_t* _frm = reinterpret_cast<const uint8_t*>(frm);
     const uint8_t* _frm_end = reinterpret_cast<const uint8_t*>(frm_end);
     const uint8_t* _frm_nxt = _frm;
-#if _WIN32
+#if defined(_LIBCPP_SHORT_WCHAR)
     uint16_t* _to = reinterpret_cast<uint16_t*>(to);
     uint16_t* _to_end = reinterpret_cast<uint16_t*>(to_end);
     uint16_t* _to_nxt = _to;
diff --git a/src/new.cpp b/src/new.cpp
index 734d931..782aac9 100644
--- a/src/new.cpp
+++ b/src/new.cpp
@@ -72,7 +72,7 @@
     if (static_cast<size_t>(alignment) < sizeof(void*))
       alignment = std::align_val_t(sizeof(void*));
     void* p;
-#if defined(_WIN32)
+#if defined(_LIBCPP_MSVCRT)
     while ((p = _aligned_malloc(size, static_cast<size_t>(alignment))) == nullptr)
 #else
     while (::posix_memalign(&p, static_cast<size_t>(alignment), size) != 0)
diff --git a/src/system_error.cpp b/src/system_error.cpp
index b00e461..c547124 100644
--- a/src/system_error.cpp
+++ b/src/system_error.cpp
@@ -65,7 +65,7 @@
 
 string do_strerror_r(int ev);
 
-#if defined(_WIN32)
+#if defined(_LIBCPP_MSVCRT)
 string do_strerror_r(int ev) {
   char buffer[strerror_buff_size];
   if (::strerror_s(buffer, strerror_buff_size, ev) == 0)
diff --git a/src/thread.cpp b/src/thread.cpp
index 4775e10..4fb1dd2 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -24,16 +24,17 @@
 # endif // defined(BSD)
 #endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
 
-#if !defined(_WIN32)
+#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
 # include <unistd.h>
-#endif // !_WIN32
+#endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
 
 #if defined(__NetBSD__)
 #pragma weak pthread_create // Do not create libpthread dependency
 #endif
-#if defined(_WIN32)
+
+#if defined(_LIBCPP_WIN32API)
 #include <windows.h>
-#endif
+#endif // defined(_LIBCPP_WIN32API)
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
@@ -91,7 +92,7 @@
     if (result < 0)
         return 0;
     return static_cast<unsigned>(result);
-#elif defined(_WIN32)
+#elif defined(_LIBCPP_WIN32API)
     SYSTEM_INFO info;
     GetSystemInfo(&info);
     return info.dwNumberOfProcessors;