[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-10 Thread Eric Fiselier via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL302727: [libc++] Refactor Windows support headers. (authored 
by EricWF).

Changed prior to commit:
  https://reviews.llvm.org/D32988?vs=98528=98530#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D32988

Files:
  libcxx/trunk/include/__mutex_base
  libcxx/trunk/include/__threading_support
  libcxx/trunk/include/algorithm
  libcxx/trunk/include/ctype.h
  libcxx/trunk/include/limits
  libcxx/trunk/include/stdio.h
  libcxx/trunk/include/stdlib.h
  libcxx/trunk/include/support/win32/limits_msvc_win32.h
  libcxx/trunk/include/support/win32/limits_win32.h
  libcxx/trunk/include/support/win32/locale_mgmt_win32.h
  libcxx/trunk/include/support/win32/locale_win32.h
  libcxx/trunk/include/support/win32/msvc_builtin_support.h
  libcxx/trunk/include/support/win32/support.h
  libcxx/trunk/include/wchar.h
  libcxx/trunk/src/string.cpp
  libcxx/trunk/src/support/runtime/exception_pointer_msvc.ipp

Index: libcxx/trunk/src/support/runtime/exception_pointer_msvc.ipp
===
--- libcxx/trunk/src/support/runtime/exception_pointer_msvc.ipp
+++ libcxx/trunk/src/support/runtime/exception_pointer_msvc.ipp
@@ -10,6 +10,7 @@
 
 #include 
 #include 
+#include  // for _CRTIMP2_PURE
 
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*);
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*);
Index: libcxx/trunk/src/string.cpp
===
--- libcxx/trunk/src/string.cpp
+++ libcxx/trunk/src/string.cpp
@@ -13,9 +13,6 @@
 #include "cerrno"
 #include "limits"
 #include "stdexcept"
-#ifdef _LIBCPP_MSVCRT
-#include "support/win32/support.h"
-#endif // _LIBCPP_MSVCRT
 #include 
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -430,7 +427,7 @@
 #ifndef _LIBCPP_MSVCRT
 return swprintf;
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif
 }
 
Index: libcxx/trunk/include/stdio.h
===
--- libcxx/trunk/include/stdio.h
+++ libcxx/trunk/include/stdio.h
@@ -111,8 +111,9 @@
 
 // snprintf
 #if defined(_LIBCPP_MSVCRT)
-extern "C++" {
-#include "support/win32/support.h"
+extern "C" {
+int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
+int asprintf(char **sptr, const char *__restrict fmt, ...);
 }
 #endif
 
Index: libcxx/trunk/include/__threading_support
===
--- libcxx/trunk/include/__threading_support
+++ libcxx/trunk/include/__threading_support
@@ -30,6 +30,7 @@
 #include 
 #include 
 #include 
+#include <__undef_min_max>
 #endif
 
 #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
Index: libcxx/trunk/include/limits
===
--- libcxx/trunk/include/limits
+++ libcxx/trunk/include/limits
@@ -111,8 +111,8 @@
 
 #include <__undef_min_max>
 
-#if defined(_LIBCPP_MSVCRT)
-#include "support/win32/limits_win32.h"
+#if defined(_LIBCPP_COMPILER_MSVC)
+#include "support/win32/limits_msvc_win32.h"
 #endif // _LIBCPP_MSVCRT
 
 #if defined(__IBMCPP__)
Index: libcxx/trunk/include/ctype.h
===
--- libcxx/trunk/include/ctype.h
+++ libcxx/trunk/include/ctype.h
@@ -40,15 +40,6 @@
 
 #ifdef __cplusplus
 
-#if defined(_LIBCPP_MSVCRT)
-// We support including .h headers inside 'extern "C"' contexts, so switch
-// back to C++ linkage before including these C++ headers.
-extern "C++" {
-  #include "support/win32/support.h"
-  #include "support/win32/locale_win32.h"
-}
-#endif // _LIBCPP_MSVCRT
-
 #undef isalnum
 #undef isalpha
 #undef isblank
Index: libcxx/trunk/include/stdlib.h
===
--- libcxx/trunk/include/stdlib.h
+++ libcxx/trunk/include/stdlib.h
@@ -97,10 +97,6 @@
 
 extern "C++" {
 
-#ifdef _LIBCPP_MSVCRT
-#include "support/win32/locale_win32.h"
-#endif // _LIBCPP_MSVCRT
-
 #undef abs
 #undef div
 #undef labs
Index: libcxx/trunk/include/__mutex_base
===
--- libcxx/trunk/include/__mutex_base
+++ libcxx/trunk/include/__mutex_base
@@ -15,6 +15,7 @@
 #include 
 #include 
 #include <__threading_support>
+#include <__undef_min_max>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #pragma GCC system_header
Index: libcxx/trunk/include/algorithm
===
--- libcxx/trunk/include/algorithm
+++ libcxx/trunk/include/algorithm
@@ -644,8 +644,8 @@
 #if defined(__IBMCPP__)
 #include "support/ibm/support.h"
 #endif
-#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
-#include "support/win32/support.h"
+#if defined(_LIBCPP_COMPILER_MSVC)
+#include "support/win32/msvc_builtin_support.h"
 #endif
 
 #include <__undef_min_max>
Index: 

[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-10 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF accepted this revision.
EricWF added a comment.

@compnerd I'm going to go ahead and land this and then submit a separate review 
for the changes you've requested. I hope you don't mind :-)


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-10 Thread Ben Craig via Phabricator via cfe-commits
bcraig added a comment.

LGTM


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-10 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF updated this revision to Diff 98528.
EricWF added a comment.

- General cleanup changes.

Libc++ trunk currently doesn't build under MinGW, so I'm not concerned about 
breaking that worse with this patch. I've set up a Appveyor builder to track 
MinGW so we can get it green later.

Also I'm planning on making all of the the changes suggested in inline 
comments, but in a follow up patch. I would like to keep this to simply 
renaming and restructuring include files.
With that in mind does anybody object to landing this?


https://reviews.llvm.org/D32988

Files:
  include/__mutex_base
  include/__threading_support
  include/algorithm
  include/ctype.h
  include/limits
  include/stdio.h
  include/stdlib.h
  include/support/win32/limits_msvc_win32.h
  include/support/win32/limits_win32.h
  include/support/win32/locale_mgmt_win32.h
  include/support/win32/locale_win32.h
  include/support/win32/msvc_builtin_support.h
  include/support/win32/support.h
  include/wchar.h
  src/string.cpp
  src/support/runtime/exception_pointer_msvc.ipp

Index: src/support/runtime/exception_pointer_msvc.ipp
===
--- src/support/runtime/exception_pointer_msvc.ipp
+++ src/support/runtime/exception_pointer_msvc.ipp
@@ -10,6 +10,7 @@
 
 #include 
 #include 
+#include  // for _CRTIMP2_PURE
 
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*);
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*);
Index: src/string.cpp
===
--- src/string.cpp
+++ src/string.cpp
@@ -13,9 +13,6 @@
 #include "cerrno"
 #include "limits"
 #include "stdexcept"
-#ifdef _LIBCPP_MSVCRT
-#include "support/win32/support.h"
-#endif // _LIBCPP_MSVCRT
 #include 
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -430,7 +427,7 @@
 #ifndef _LIBCPP_MSVCRT
 return swprintf;
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif
 }
 
Index: include/wchar.h
===
--- include/wchar.h
+++ include/wchar.h
@@ -166,9 +166,12 @@
 }
 #endif
 
-#if defined(__cplusplus) && (defined(_LIBCPP_MSVCRT) || defined(__MINGW32__))
-extern "C++" {
-#include  // pull in *swprintf defines
+#if defined(__cplusplus) && defined(_LIBCPP_MSVCRT)
+extern "C" {
+size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
+  size_t nmc, size_t len, mbstate_t *__restrict ps);
+size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
+  size_t nwc, size_t len, mbstate_t *__restrict ps);
 }  // extern "C++"
 #endif  // __cplusplus && _LIBCPP_MSVCRT
 
Index: include/support/win32/support.h
===
--- include/support/win32/support.h
+++ /dev/null
@@ -1,177 +0,0 @@
-// -*- C++ -*-
-//===--- support/win32/support.h --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is dual licensed under the MIT and the University of Illinois Open
-// Source Licenses. See LICENSE.TXT for details.
-//
-//===--===//
-
-#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-
-// Functions and constants used in libc++ that
-// are missing from the Windows C library.
-
-#include  // mbstate_t
-#include  // va_ macros
-// "builtins" not implemented here for Clang or GCC as they provide
-// implementations. Assuming required for elsewhere else, certainly MSVC.
-#if defined(_LIBCPP_COMPILER_MSVC)
-#include 
-#endif
-#define swprintf _snwprintf
-#define vswprintf _vsnwprintf
-
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-
-// The mingw headers already define these as static.
-#ifndef __MINGW32__
-extern "C" {
-
-int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
-int asprintf(char **sptr, const char *__restrict fmt, ...);
-size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
-  size_t nmc, size_t len, mbstate_t *__restrict ps);
-size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
-  size_t nwc, size_t len, mbstate_t *__restrict ps);
-}
-#endif // __MINGW32__
-
-#if defined(_LIBCPP_COMPILER_MSVC)
-
-// Bit builtin's make these assumptions when calling _BitScanForward/Reverse
-// etc. These assumptions are expected to be true for Win32/Win64 which this
-// file supports.
-static_assert(sizeof(unsigned long long) == 8, "");
-static_assert(sizeof(unsigned long) == 4, "");
-static_assert(sizeof(unsigned int) == 4, "");
-
-_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
-{
-  // Binary: 0101...
-  static const unsigned int m1 = 0x;
-  // Binary: 00110011..
-  static const unsigned int m2 = 0x;
-  // Binary:  4 zeros,  4 ones ...
-  static const unsigned int m4 = 0x0f0f0f0f;
-  

[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-09 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF added inline comments.



Comment at: include/__config:232-235
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+

compnerd wrote:
> bcraig wrote:
> > I can see this helping when we are build libc++, but I don't think it helps 
> > client apps.  They could have included windows.h before including our 
> > headers.
> > 
> > Don't get me wrong, I dislike the min and max macros, and bear no hard 
> > feelings towards people that define this project wide on the command line, 
> > I'm just not sure it will get things done right here.
> > 
> > In the past, I've just surrounded my min and max declarations with 
> > parenthesis to suppress macro expansion.
> Yeah, I don't think that this should be defined in `__config`.  Can we do 
> something like `#pragma push_macro` and `#pragma pop_macro` in the necessary 
> files?
Alright I'll remove the `NOMINMAX` define in `__config` and start to sprinkle 
the `__undef_min_max` include where it's currently needed.

@compnerd, using `push_macro` and `pop_macro` are going to be the correct way 
to handle this, but that's a change for another patch.



Comment at: include/support/win32/msvc_builtin_support.h:33
+
+_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
+{

majnemer wrote:
> compnerd wrote:
> > I think I prefer the following implementation:
> > 
> > _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int value) {
> >   return __popcnt(value);
> > }
> I think it'd be better not to call it `__builtin_anything`. MSVC uses the 
> __builtin_ namespace too, see https://godbolt.org/g/HwMskX
> 
> Maybe create a wrapper called `__libcpp_popcount`?
That's a change for another patch, one that's just meant to restructure the 
headers.

This patch simply moves this code to another file.



Comment at: include/support/win32/msvc_builtin_support.h:33-51
+_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
+{
+  // Binary: 0101...
+  static const unsigned int m1 = 0x;
+  // Binary: 00110011..
+  static const unsigned int m2 = 0x;
+  // Binary:  4 zeros,  4 ones ...

EricWF wrote:
> majnemer wrote:
> > compnerd wrote:
> > > I think I prefer the following implementation:
> > > 
> > > _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int value) {
> > >   return __popcnt(value);
> > > }
> > I think it'd be better not to call it `__builtin_anything`. MSVC uses the 
> > __builtin_ namespace too, see https://godbolt.org/g/HwMskX
> > 
> > Maybe create a wrapper called `__libcpp_popcount`?
> That's a change for another patch, one that's just meant to restructure the 
> headers.
> 
> This patch simply moves this code to another file.
That's a change for another patch, one that's just meant to restructure the 
headers.

This patch simply moves this code to another file.



Comment at: src/string.cpp:430
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif

compnerd wrote:
> This seems scary.  Why do we need to cast the function?
No idea.


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-09 Thread David Majnemer via Phabricator via cfe-commits
majnemer added inline comments.



Comment at: include/support/win32/msvc_builtin_support.h:33
+
+_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
+{

compnerd wrote:
> I think I prefer the following implementation:
> 
> _LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int value) {
>   return __popcnt(value);
> }
I think it'd be better not to call it `__builtin_anything`. MSVC uses the 
__builtin_ namespace too, see https://godbolt.org/g/HwMskX

Maybe create a wrapper called `__libcpp_popcount`?


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-09 Thread Saleem Abdulrasool via Phabricator via cfe-commits
compnerd requested changes to this revision.
compnerd added inline comments.



Comment at: include/__config:232-235
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+

bcraig wrote:
> I can see this helping when we are build libc++, but I don't think it helps 
> client apps.  They could have included windows.h before including our headers.
> 
> Don't get me wrong, I dislike the min and max macros, and bear no hard 
> feelings towards people that define this project wide on the command line, 
> I'm just not sure it will get things done right here.
> 
> In the past, I've just surrounded my min and max declarations with 
> parenthesis to suppress macro expansion.
Yeah, I don't think that this should be defined in `__config`.  Can we do 
something like `#pragma push_macro` and `#pragma pop_macro` in the necessary 
files?



Comment at: include/stdio.h:113-118
 #if defined(_LIBCPP_MSVCRT)
-extern "C++" {
-#include "support/win32/support.h"
+extern "C" {
+int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
+int asprintf(char **sptr, const char *__restrict fmt, ...);
 }
 #endif

Should this be hoisted above the `#ifdef __cplusplus`?  This seems like it 
should be defined by `stdio.h` but isn't in msvcrt?



Comment at: include/support/win32/locale_win32.h:24
+#define LC_MESSAGES_MASK _M_MESSAGES
+#define LC_ALL_MASK (  LC_COLLATE_MASK \
+ | LC_CTYPE_MASK \

Can you please clang-format this block?



Comment at: include/support/win32/msvc_builtin_support.h:33-51
+_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
+{
+  // Binary: 0101...
+  static const unsigned int m1 = 0x;
+  // Binary: 00110011..
+  static const unsigned int m2 = 0x;
+  // Binary:  4 zeros,  4 ones ...

I think I prefer the following implementation:

_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int value) {
  return __popcnt(value);
}



Comment at: include/support/win32/msvc_builtin_support.h:58-76
+_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x)
+{
+  // Binary: 0101...
+  static const unsigned long long m1 = 0x;
+  // Binary: 00110011..
+  static const unsigned long long m2 = 0x;
+  // Binary:  4 zeros,  4 ones ...

I think I prefer the following implementation:

_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long value) {
  return __popcnt64(value);
}



Comment at: src/string.cpp:430
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif

This seems scary.  Why do we need to cast the function?


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-09 Thread Ben Craig via Phabricator via cfe-commits
bcraig added inline comments.



Comment at: include/__config:232-235
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+

I can see this helping when we are build libc++, but I don't think it helps 
client apps.  They could have included windows.h before including our headers.

Don't get me wrong, I dislike the min and max macros, and bear no hard feelings 
towards people that define this project wide on the command line, I'm just not 
sure it will get things done right here.

In the past, I've just surrounded my min and max declarations with parenthesis 
to suppress macro expansion.


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-08 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF planned changes to this revision.
EricWF added a comment.

This breaks MinGW. I'll update soon.




Comment at: include/algorithm:647
 #endif
-#if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
-#include "support/win32/support.h"
+#if defined(_LIBCPP_COMPILER_MSVC)
+#include "support/win32/msvc_builtin_support.h"

This change is incorrect too.



Comment at: include/limits:114
 
-#if defined(_LIBCPP_MSVCRT)
-#include "support/win32/limits_win32.h"
+#if defined(_LIBCPP_COMPILER_MSVC)
+#include "support/win32/limits_msvc_win32.h"

This is incorrect for MinGW.


https://reviews.llvm.org/D32988



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-08 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF updated this revision to Diff 98246.
EricWF added a comment.

- Rename `msvc_support.h` to `msvc_builtin_support.h`.


https://reviews.llvm.org/D32988

Files:
  include/__config
  include/algorithm
  include/ctype.h
  include/limits
  include/stdio.h
  include/stdlib.h
  include/support/win32/limits_msvc_win32.h
  include/support/win32/limits_win32.h
  include/support/win32/locale_mgmt_win32.h
  include/support/win32/locale_win32.h
  include/support/win32/msvc_builtin_support.h
  include/support/win32/support.h
  include/wchar.h
  src/string.cpp
  src/support/runtime/exception_pointer_msvc.ipp

Index: src/support/runtime/exception_pointer_msvc.ipp
===
--- src/support/runtime/exception_pointer_msvc.ipp
+++ src/support/runtime/exception_pointer_msvc.ipp
@@ -10,6 +10,7 @@
 
 #include 
 #include 
+#include  // for _CRTIMP2_PURE
 
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*);
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*);
Index: src/string.cpp
===
--- src/string.cpp
+++ src/string.cpp
@@ -13,9 +13,6 @@
 #include "cerrno"
 #include "limits"
 #include "stdexcept"
-#ifdef _LIBCPP_MSVCRT
-#include "support/win32/support.h"
-#endif // _LIBCPP_MSVCRT
 #include 
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -430,7 +427,7 @@
 #ifndef _LIBCPP_MSVCRT
 return swprintf;
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif
 }
 
Index: include/wchar.h
===
--- include/wchar.h
+++ include/wchar.h
@@ -166,9 +166,12 @@
 }
 #endif
 
-#if defined(__cplusplus) && (defined(_LIBCPP_MSVCRT) || defined(__MINGW32__))
-extern "C++" {
-#include  // pull in *swprintf defines
+#if defined(__cplusplus) && defined(_LIBCPP_MSVCRT)
+extern "C" {
+size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
+  size_t nmc, size_t len, mbstate_t *__restrict ps);
+size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
+  size_t nwc, size_t len, mbstate_t *__restrict ps);
 }  // extern "C++"
 #endif  // __cplusplus && _LIBCPP_MSVCRT
 
Index: include/support/win32/support.h
===
--- include/support/win32/support.h
+++ /dev/null
@@ -1,177 +0,0 @@
-// -*- C++ -*-
-//===--- support/win32/support.h --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is dual licensed under the MIT and the University of Illinois Open
-// Source Licenses. See LICENSE.TXT for details.
-//
-//===--===//
-
-#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-
-// Functions and constants used in libc++ that
-// are missing from the Windows C library.
-
-#include  // mbstate_t
-#include  // va_ macros
-// "builtins" not implemented here for Clang or GCC as they provide
-// implementations. Assuming required for elsewhere else, certainly MSVC.
-#if defined(_LIBCPP_COMPILER_MSVC)
-#include 
-#endif
-#define swprintf _snwprintf
-#define vswprintf _vsnwprintf
-
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-
-// The mingw headers already define these as static.
-#ifndef __MINGW32__
-extern "C" {
-
-int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
-int asprintf(char **sptr, const char *__restrict fmt, ...);
-size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
-  size_t nmc, size_t len, mbstate_t *__restrict ps);
-size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
-  size_t nwc, size_t len, mbstate_t *__restrict ps);
-}
-#endif // __MINGW32__
-
-#if defined(_LIBCPP_COMPILER_MSVC)
-
-// Bit builtin's make these assumptions when calling _BitScanForward/Reverse
-// etc. These assumptions are expected to be true for Win32/Win64 which this
-// file supports.
-static_assert(sizeof(unsigned long long) == 8, "");
-static_assert(sizeof(unsigned long) == 4, "");
-static_assert(sizeof(unsigned int) == 4, "");
-
-_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x)
-{
-  // Binary: 0101...
-  static const unsigned int m1 = 0x;
-  // Binary: 00110011..
-  static const unsigned int m2 = 0x;
-  // Binary:  4 zeros,  4 ones ...
-  static const unsigned int m4 = 0x0f0f0f0f;
-  // The sum of 256 to the power of 0,1,2,3...
-  static const unsigned int h01 = 0x01010101;
-  // Put count of each 2 bits into those 2 bits.
-  x -= (x >> 1) & m1;
-  // Put count of each 4 bits into those 4 bits.
-  x = (x & m2) + ((x >> 2) & m2);
-  // Put count of each 8 bits into those 8 bits.
-  x = (x + (x >> 4)) & m4;
-  // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24).
-  return (x * h01) >> 24;
-}
-

[PATCH] D32988: [libc++] Refactor Windows support headers.

2017-05-08 Thread Eric Fiselier via Phabricator via cfe-commits
EricWF created this revision.

This patch refactors and tries to remove as much of the Windows support headers 
as possible. This is needed because they currently introduce super weird 
include cycles and dependencies between STL and libc headers.

The changes in this patch are:

- remove `support/win32/support.h` completely. The required parts have either 
been moved into `support/win32/msvc_support.h` (for `MSVC` only helpers not 
needed by Clang), or directly into their respective `foo.h` headers.

- Combine `locale_win32.h` and `locale_mgmt_win32.h` into a single headers, 
this header should only be included within `__locale` or `locale` to avoid 
include cycles.

- Remove the unneeded parts of `limits_win32.h` and re-name it to 
`limits_msvc_win32.h` since it's only needed by Clang.

I've tested this patch using Clang on Windows, but I suspect it might 
technically regress our non-existent support for MSVC. Is somebody able to 
double check?

This refactor is needed to support upcoming fixes to `` on Windows.


https://reviews.llvm.org/D32988

Files:
  include/__config
  include/algorithm
  include/ctype.h
  include/limits
  include/stdio.h
  include/stdlib.h
  include/support/win32/limits_msvc_win32.h
  include/support/win32/limits_win32.h
  include/support/win32/locale_mgmt_win32.h
  include/support/win32/locale_win32.h
  include/support/win32/msvc_support.h
  include/support/win32/support.h
  include/wchar.h
  src/string.cpp
  src/support/runtime/exception_pointer_msvc.ipp

Index: src/support/runtime/exception_pointer_msvc.ipp
===
--- src/support/runtime/exception_pointer_msvc.ipp
+++ src/support/runtime/exception_pointer_msvc.ipp
@@ -10,6 +10,7 @@
 
 #include 
 #include 
+#include  // for _CRTIMP2_PURE
 
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*);
 _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*);
Index: src/string.cpp
===
--- src/string.cpp
+++ src/string.cpp
@@ -13,9 +13,6 @@
 #include "cerrno"
 #include "limits"
 #include "stdexcept"
-#ifdef _LIBCPP_MSVCRT
-#include "support/win32/support.h"
-#endif // _LIBCPP_MSVCRT
 #include 
 
 _LIBCPP_BEGIN_NAMESPACE_STD
@@ -430,7 +427,7 @@
 #ifndef _LIBCPP_MSVCRT
 return swprintf;
 #else
-return static_cast(swprintf);
+return static_cast(_snwprintf);
 #endif
 }
 
Index: include/wchar.h
===
--- include/wchar.h
+++ include/wchar.h
@@ -166,9 +166,12 @@
 }
 #endif
 
-#if defined(__cplusplus) && (defined(_LIBCPP_MSVCRT) || defined(__MINGW32__))
-extern "C++" {
-#include  // pull in *swprintf defines
+#if defined(__cplusplus) && defined(_LIBCPP_MSVCRT)
+extern "C" {
+size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
+  size_t nmc, size_t len, mbstate_t *__restrict ps);
+size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
+  size_t nwc, size_t len, mbstate_t *__restrict ps);
 }  // extern "C++"
 #endif  // __cplusplus && _LIBCPP_MSVCRT
 
Index: include/support/win32/support.h
===
--- include/support/win32/support.h
+++ /dev/null
@@ -1,177 +0,0 @@
-// -*- C++ -*-
-//===--- support/win32/support.h --===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is dual licensed under the MIT and the University of Illinois Open
-// Source Licenses. See LICENSE.TXT for details.
-//
-//===--===//
-
-#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H
-
-// Functions and constants used in libc++ that
-// are missing from the Windows C library.
-
-#include  // mbstate_t
-#include  // va_ macros
-// "builtins" not implemented here for Clang or GCC as they provide
-// implementations. Assuming required for elsewhere else, certainly MSVC.
-#if defined(_LIBCPP_COMPILER_MSVC)
-#include 
-#endif
-#define swprintf _snwprintf
-#define vswprintf _vsnwprintf
-
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-
-// The mingw headers already define these as static.
-#ifndef __MINGW32__
-extern "C" {
-
-int vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
-int asprintf(char **sptr, const char *__restrict fmt, ...);
-size_t mbsnrtowcs(wchar_t *__restrict dst, const char **__restrict src,
-  size_t nmc, size_t len, mbstate_t *__restrict ps);
-size_t wcsnrtombs(char *__restrict dst, const wchar_t **__restrict src,
-  size_t nwc, size_t len, mbstate_t *__restrict ps);
-}
-#endif // __MINGW32__
-
-#if defined(_LIBCPP_COMPILER_MSVC)
-
-// Bit builtin's make these assumptions when calling _BitScanForward/Reverse
-// etc. These assumptions are expected to be true for Win32/Win64 which this