From 5d50aa32041596e2c32c50e8cf767cac5e98ba72 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 10 May 2017 20:57:45 +0000 Subject: [PATCH] [libc++] Refactor Windows support headers. Summary: 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. Reviewers: bcraig, rmaprath, compnerd, EricWF Reviewed By: EricWF Subscribers: majnemer, cfe-commits Differential Revision: https://reviews.llvm.org/D32988 llvm-svn: 302727 --- libcxx/include/__mutex_base | 1 + libcxx/include/__threading_support | 1 + libcxx/include/algorithm | 4 +-- libcxx/include/ctype.h | 9 ------ libcxx/include/limits | 4 +-- libcxx/include/stdio.h | 5 +-- libcxx/include/stdlib.h | 4 --- .../win32/{limits_win32.h => limits_msvc_win32.h} | 27 ++++++---------- libcxx/include/support/win32/locale_mgmt_win32.h | 33 ------------------- libcxx/include/support/win32/locale_win32.h | 32 +++++++++++++------ .../win32/{support.h => msvc_builtin_support.h} | 37 +++++----------------- libcxx/include/wchar.h | 9 ++++-- libcxx/src/string.cpp | 5 +-- .../src/support/runtime/exception_pointer_msvc.ipp | 1 + 14 files changed, 58 insertions(+), 114 deletions(-) rename libcxx/include/support/win32/{limits_win32.h => limits_msvc_win32.h} (80%) delete mode 100644 libcxx/include/support/win32/locale_mgmt_win32.h rename libcxx/include/support/win32/{support.h => msvc_builtin_support.h} (84%) diff --git a/libcxx/include/__mutex_base b/libcxx/include/__mutex_base index a6d5e79..7f5e2ea 100644 --- a/libcxx/include/__mutex_base +++ b/libcxx/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 diff --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support index 080ebd2..385fff3 100644 --- a/libcxx/include/__threading_support +++ b/libcxx/include/__threading_support @@ -30,6 +30,7 @@ #include #include #include +#include <__undef_min_max> #endif #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \ diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index c3517a1..896d58e 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -644,8 +644,8 @@ template #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> diff --git a/libcxx/include/ctype.h b/libcxx/include/ctype.h index 22d6c49..e97ff3c 100644 --- a/libcxx/include/ctype.h +++ b/libcxx/include/ctype.h @@ -40,15 +40,6 @@ int toupper(int c); #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 diff --git a/libcxx/include/limits b/libcxx/include/limits index 609c4d4..4755c57 100644 --- a/libcxx/include/limits +++ b/libcxx/include/limits @@ -111,8 +111,8 @@ template<> class numeric_limits; #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__) diff --git a/libcxx/include/stdio.h b/libcxx/include/stdio.h index 56fb2d8..dc53497 100644 --- a/libcxx/include/stdio.h +++ b/libcxx/include/stdio.h @@ -111,8 +111,9 @@ void perror(const char* s); // 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 diff --git a/libcxx/include/stdlib.h b/libcxx/include/stdlib.h index 12fd676..f11c5e7 100644 --- a/libcxx/include/stdlib.h +++ b/libcxx/include/stdlib.h @@ -97,10 +97,6 @@ void *aligned_alloc(size_t alignment, size_t size); // C11 extern "C++" { -#ifdef _LIBCPP_MSVCRT -#include "support/win32/locale_win32.h" -#endif // _LIBCPP_MSVCRT - #undef abs #undef div #undef labs diff --git a/libcxx/include/support/win32/limits_win32.h b/libcxx/include/support/win32/limits_msvc_win32.h similarity index 80% rename from libcxx/include/support/win32/limits_win32.h rename to libcxx/include/support/win32/limits_msvc_win32.h index 406cd30..1ab2e0b 100644 --- a/libcxx/include/support/win32/limits_win32.h +++ b/libcxx/include/support/win32/limits_msvc_win32.h @@ -1,5 +1,5 @@ // -*- C++ -*- -//===--------------------- support/win32/limits_win32.h -------------------===// +//===------------------ support/win32/limits_msvc_win32.h -----------------===// // // The LLVM Compiler Infrastructure // @@ -8,17 +8,21 @@ // //===----------------------------------------------------------------------===// -#ifndef _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H -#define _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H +#ifndef _LIBCPP_SUPPORT_WIN32_LIMITS_MSVC_WIN32_H +#define _LIBCPP_SUPPORT_WIN32_LIMITS_MSVC_WIN32_H #if !defined(_LIBCPP_MSVCRT) #error "This header complements the Microsoft C Runtime library, and should not be included otherwise." -#else +#endif +#if defined(__clang__) +#error "This header should only be included when using Microsofts C1XX frontend" +#endif #include // CHAR_BIT #include // limit constants +#include // HUGE_VAL +#include // internal MSVC header providing the needed functionality -#if ! defined(__clang__) #define __CHAR_BIT__ CHAR_BIT #define __FLT_MANT_DIG__ FLT_MANT_DIG @@ -61,19 +65,8 @@ #define __LDBL_DENORM_MIN__ 3.64519953188247460253e-4951L // __builtin replacements/workarounds -#include // HUGE_VAL -#include // internal MSVC header providing the needed functionality -#define __builtin_huge_val() HUGE_VAL -#define __builtin_huge_valf() _FInf._Float #define __builtin_huge_vall() _LInf._Long_double -#define __builtin_nan(__dummy) _Nan._Double -#define __builtin_nanf(__dummy) _FNan._Float #define __builtin_nanl(__dummmy) _LNan._Long_double -#define __builtin_nans(__dummy) _Snan._Double -#define __builtin_nansf(__dummy) _FSnan._Float #define __builtin_nansl(__dummy) _LSnan._Long_double -#endif // ! defined(__clang__) - -#endif // _LIBCPP_MSVCRT -#endif // _LIBCPP_SUPPORT_WIN32_LIMITS_WIN32_H +#endif // _LIBCPP_SUPPORT_WIN32_LIMITS_MSVC_WIN32_H diff --git a/libcxx/include/support/win32/locale_mgmt_win32.h b/libcxx/include/support/win32/locale_mgmt_win32.h deleted file mode 100644 index b3316d6..0000000 --- a/libcxx/include/support/win32/locale_mgmt_win32.h +++ /dev/null @@ -1,33 +0,0 @@ -// -*- C++ -*- -//===----------------- support/win32/locale_mgmt_win32.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_LOCALE_MGMT_WIN32_H -#define _LIBCPP_SUPPORT_WIN32_LOCALE_MGMT_WIN32_H - -#include // _locale_t -#define locale_t _locale_t -#define LC_COLLATE_MASK _M_COLLATE -#define LC_CTYPE_MASK _M_CTYPE -#define LC_MONETARY_MASK _M_MONETARY -#define LC_NUMERIC_MASK _M_NUMERIC -#define LC_TIME_MASK _M_TIME -#define LC_MESSAGES_MASK _M_MESSAGES -#define LC_ALL_MASK ( LC_COLLATE_MASK \ - | LC_CTYPE_MASK \ - | LC_MESSAGES_MASK \ - | LC_MONETARY_MASK \ - | LC_NUMERIC_MASK \ - | LC_TIME_MASK ) -#define freelocale _free_locale -// FIXME: base currently unused. Needs manual work to construct the new locale -locale_t newlocale( int mask, const char * locale, locale_t base ); -locale_t uselocale( locale_t newloc ); - -#endif // _LIBCPP_SUPPORT_WIN32_LOCALE_MGMT_WIN32_H diff --git a/libcxx/include/support/win32/locale_win32.h b/libcxx/include/support/win32/locale_win32.h index bc717d9..7a6c44c 100644 --- a/libcxx/include/support/win32/locale_win32.h +++ b/libcxx/include/support/win32/locale_win32.h @@ -12,9 +12,30 @@ #define _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H #include <__config> -#include "support/win32/support.h" -#include "support/win32/locale_mgmt_win32.h" #include +#include // _locale_t + +#define LC_COLLATE_MASK _M_COLLATE +#define LC_CTYPE_MASK _M_CTYPE +#define LC_MONETARY_MASK _M_MONETARY +#define LC_NUMERIC_MASK _M_NUMERIC +#define LC_TIME_MASK _M_TIME +#define LC_MESSAGES_MASK _M_MESSAGES +#define LC_ALL_MASK ( LC_COLLATE_MASK \ + | LC_CTYPE_MASK \ + | LC_MESSAGES_MASK \ + | LC_MONETARY_MASK \ + | LC_NUMERIC_MASK \ + | LC_TIME_MASK ) + +#define locale_t _locale_t + +// Locale management functions +#define freelocale _free_locale +// FIXME: base currently unused. Needs manual work to construct the new locale +locale_t newlocale( int mask, const char * locale, locale_t base ); +locale_t uselocale( locale_t newloc ); + lconv *localeconv_l( locale_t loc ); size_t mbrlen_l( const char *__restrict s, size_t n, @@ -88,7 +109,6 @@ _LIBCPP_FUNC_VIS int snprintf_l(char *ret, size_t n, locale_t loc, const char *f _LIBCPP_FUNC_VIS int asprintf_l( char **ret, locale_t loc, const char *format, ... ); _LIBCPP_FUNC_VIS int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap ); - // not-so-pressing FIXME: use locale to determine blank characters inline int isblank_l( int c, locale_t /*loc*/ ) { @@ -99,10 +119,4 @@ inline int iswblank_l( wint_t c, locale_t /*loc*/ ) return ( c == L' ' || c == L'\t' ); } -#if defined(_LIBCPP_MSVCRT) -inline int isblank( int c, locale_t /*loc*/ ) -{ return ( c == ' ' || c == '\t' ); } -inline int iswblank( wint_t c, locale_t /*loc*/ ) -{ return ( c == L' ' || c == L'\t' ); } -#endif // _LIBCPP_MSVCRT #endif // _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H diff --git a/libcxx/include/support/win32/support.h b/libcxx/include/support/win32/msvc_builtin_support.h similarity index 84% rename from libcxx/include/support/win32/support.h rename to libcxx/include/support/win32/msvc_builtin_support.h index e48b08d..0ef6320 100644 --- a/libcxx/include/support/win32/support.h +++ b/libcxx/include/support/win32/msvc_builtin_support.h @@ -8,40 +8,20 @@ // //===----------------------------------------------------------------------===// -#ifndef _LIBCPP_SUPPORT_WIN32_SUPPORT_H -#define _LIBCPP_SUPPORT_WIN32_SUPPORT_H +#ifndef _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_SUPPORT_H +#define _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_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 +#ifndef _LIBCPP_COMPILER_MSVC +#error "This header can only be included when using Microsoft's C1XX frontend" #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) +// "builtins" not implemented here for Clang or GCC as they provide +// implementations. Assuming required for elsewhere else, certainly MSVC. +#include // Bit builtin's make these assumptions when calling _BitScanForward/Reverse // etc. These assumptions are expected to be true for Win32/Win64 which this @@ -172,6 +152,5 @@ _LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x) { return __builtin_clzl(x); } -#endif // _LIBCPP_MSVC -#endif // _LIBCPP_SUPPORT_WIN32_SUPPORT_H +#endif // _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_SUPPORT_H diff --git a/libcxx/include/wchar.h b/libcxx/include/wchar.h index c0c6ef7..25a318f 100644 --- a/libcxx/include/wchar.h +++ b/libcxx/include/wchar.h @@ -166,9 +166,12 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_PREFERRED_OVERLOAD } #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 diff --git a/libcxx/src/string.cpp b/libcxx/src/string.cpp index cd64433..d7ebdd3 100644 --- a/libcxx/src/string.cpp +++ b/libcxx/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 @@ get_swprintf() #ifndef _LIBCPP_MSVCRT return swprintf; #else - return static_cast(swprintf); + return static_cast(_snwprintf); #endif } diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp index a8cd0e8..eab5d30 100644 --- a/libcxx/src/support/runtime/exception_pointer_msvc.ipp +++ b/libcxx/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*); -- 2.7.4