diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -222,8 +222,6 @@ __bit/popcount.h __bit/rotate.h __bit_reference - __bsd_locale_defaults.h - __bsd_locale_fallbacks.h __charconv/chars_format.h __charconv/from_chars_integral.h __charconv/from_chars_result.h @@ -439,6 +437,9 @@ __iterator/unreachable_sentinel.h __iterator/wrap_iter.h __locale + __locale_dir/locale_base_api/bsd_locale_defaults.h + __locale_dir/locale_base_api/bsd_locale_fallbacks.h + __locale_dir/locale_base_api/locale_guard.h __mbstate_t.h __memory/addressof.h __memory/align.h diff --git a/libcxx/include/__locale b/libcxx/include/__locale --- a/libcxx/include/__locale +++ b/libcxx/include/__locale @@ -52,63 +52,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD -#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS) -struct __libcpp_locale_guard { - _LIBCPP_INLINE_VISIBILITY - __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {} - - _LIBCPP_INLINE_VISIBILITY - ~__libcpp_locale_guard() { - if (__old_loc_) - uselocale(__old_loc_); - } - - locale_t __old_loc_; -private: - __libcpp_locale_guard(__libcpp_locale_guard const&); - __libcpp_locale_guard& operator=(__libcpp_locale_guard const&); -}; -#elif defined(_LIBCPP_MSVCRT_LIKE) -struct __libcpp_locale_guard { - __libcpp_locale_guard(locale_t __l) : - __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) { - // Setting the locale can be expensive even when the locale given is - // already the current locale, so do an explicit check to see if the - // current locale is already the one we want. - const char* __lc = __setlocale(nullptr); - // If every category is the same, the locale string will simply be the - // locale name, otherwise it will be a semicolon-separated string listing - // each category. In the second case, we know at least one category won't - // be what we want, so we only have to check the first case. - if (_VSTD::strcmp(__l.__get_locale(), __lc) != 0) { - __locale_all = _strdup(__lc); - if (__locale_all == nullptr) - __throw_bad_alloc(); - __setlocale(__l.__get_locale()); - } - } - ~__libcpp_locale_guard() { - // The CRT documentation doesn't explicitly say, but setlocale() does the - // right thing when given a semicolon-separated list of locale settings - // for the different categories in the same format as returned by - // setlocale(LC_ALL, nullptr). - if (__locale_all != nullptr) { - __setlocale(__locale_all); - free(__locale_all); - } - _configthreadlocale(__status); - } - static const char* __setlocale(const char* __locale) { - const char* __new_locale = setlocale(LC_ALL, __locale); - if (__new_locale == nullptr) - __throw_bad_alloc(); - return __new_locale; - } - int __status; - char* __locale_all = nullptr; -}; -#endif - class _LIBCPP_TYPE_VIS locale; template diff --git a/libcxx/include/__bsd_locale_defaults.h b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_defaults.h rename from libcxx/include/__bsd_locale_defaults.h rename to libcxx/include/__locale_dir/locale_base_api/bsd_locale_defaults.h --- a/libcxx/include/__bsd_locale_defaults.h +++ b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_defaults.h @@ -11,8 +11,8 @@ // we will define the mapping from an internal macro to the real BSD symbol. //===----------------------------------------------------------------------===// -#ifndef _LIBCPP___BSD_LOCALE_DEFAULTS_H -#define _LIBCPP___BSD_LOCALE_DEFAULTS_H +#ifndef _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_DEFAULTS_H +#define _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_DEFAULTS_H #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header @@ -33,4 +33,4 @@ #define __libcpp_asprintf_l(...) asprintf_l(__VA_ARGS__) #define __libcpp_sscanf_l(...) sscanf_l(__VA_ARGS__) -#endif // _LIBCPP___BSD_LOCALE_DEFAULTS_H +#endif // _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_DEFAULTS_H diff --git a/libcxx/include/__bsd_locale_fallbacks.h b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h rename from libcxx/include/__bsd_locale_fallbacks.h rename to libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h --- a/libcxx/include/__bsd_locale_fallbacks.h +++ b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h @@ -10,9 +10,12 @@ // of those functions for non-BSD platforms. //===----------------------------------------------------------------------===// -#ifndef _LIBCPP___BSD_LOCALE_FALLBACKS_H -#define _LIBCPP___BSD_LOCALE_FALLBACKS_H +#ifndef _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_FALLBACKS_H +#define _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_FALLBACKS_H +#include <__locale_dir/locale_base_api/locale_guard.h> +#include +#include #include #include @@ -139,4 +142,4 @@ _LIBCPP_END_NAMESPACE_STD -#endif // _LIBCPP___BSD_LOCALE_FALLBACKS_H +#endif // _LIBCPP___SUPPORT_XLOCALE_BSD_LOCALE_FALLBACKS_H diff --git a/libcxx/include/__locale_dir/locale_base_api/locale_guard.h b/libcxx/include/__locale_dir/locale_base_api/locale_guard.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__locale_dir/locale_base_api/locale_guard.h @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H +#define _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H + +#include <__config> +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS) +struct __libcpp_locale_guard { + _LIBCPP_INLINE_VISIBILITY __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {} + + _LIBCPP_INLINE_VISIBILITY ~__libcpp_locale_guard() { + if (__old_loc_) + uselocale(__old_loc_); + } + + locale_t __old_loc_; + +private: + __libcpp_locale_guard(__libcpp_locale_guard const&); + __libcpp_locale_guard& operator=(__libcpp_locale_guard const&); +}; +#elif defined(_LIBCPP_MSVCRT_LIKE) +struct __libcpp_locale_guard { + __libcpp_locale_guard(locale_t __l) : + __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) { + // Setting the locale can be expensive even when the locale given is + // already the current locale, so do an explicit check to see if the + // current locale is already the one we want. + const char* __lc = __setlocale(nullptr); + // If every category is the same, the locale string will simply be the + // locale name, otherwise it will be a semicolon-separated string listing + // each category. In the second case, we know at least one category won't + // be what we want, so we only have to check the first case. + if (_VSTD::strcmp(__l.__get_locale(), __lc) != 0) { + __locale_all = _strdup(__lc); + if (__locale_all == nullptr) + __throw_bad_alloc(); + __setlocale(__l.__get_locale()); + } + } + ~__libcpp_locale_guard() { + // The CRT documentation doesn't explicitly say, but setlocale() does the + // right thing when given a semicolon-separated list of locale settings + // for the different categories in the same format as returned by + // setlocale(LC_ALL, nullptr). + if (__locale_all != nullptr) { + __setlocale(__locale_all); + free(__locale_all); + } + _configthreadlocale(__status); + } + static const char* __setlocale(const char* __locale) { + const char* __new_locale = setlocale(LC_ALL, __locale); + if (__new_locale == nullptr) + __throw_bad_alloc(); + return __new_locale; + } + int __status; + char* __locale_all = nullptr; +}; +#endif + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H diff --git a/libcxx/include/libcxx.imp b/libcxx/include/libcxx.imp --- a/libcxx/include/libcxx.imp +++ b/libcxx/include/libcxx.imp @@ -33,6 +33,7 @@ { include: [ "@<__fwd/.*>", "private", "", "public" ] }, { include: [ "@<__ios/.*>", "private", "", "public" ] }, { include: [ "@<__iterator/.*>", "private", "", "public" ] }, + { include: [ "@<__locale_dir/.*>", "private", "", "public" ] }, { include: [ "@<__memory/.*>", "private", "", "public" ] }, { include: [ "@<__memory_resource/.*>", "private", "", "public" ] }, { include: [ "@<__mutex/.*>", "private", "", "public" ] }, diff --git a/libcxx/include/locale b/libcxx/include/locale --- a/libcxx/include/locale +++ b/libcxx/include/locale @@ -224,9 +224,9 @@ #endif #ifdef _LIBCPP_LOCALE__L_EXTENSIONS -#include <__bsd_locale_defaults.h> +# include <__locale_dir/locale_base_api/bsd_locale_defaults.h> #else -#include <__bsd_locale_fallbacks.h> +# include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h> #endif #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) diff --git a/libcxx/test/libcxx/private_headers.verify.cpp b/libcxx/test/libcxx/private_headers.verify.cpp --- a/libcxx/test/libcxx/private_headers.verify.cpp +++ b/libcxx/test/libcxx/private_headers.verify.cpp @@ -470,6 +470,9 @@ #include <__iterator/unreachable_sentinel.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/unreachable_sentinel.h'}} #include <__iterator/wrap_iter.h> // expected-error@*:* {{use of private header from outside its module: '__iterator/wrap_iter.h'}} #include <__locale> // expected-error@*:* {{use of private header from outside its module: '__locale'}} +#include <__locale_dir/locale_base_api/bsd_locale_defaults.h> // expected-error@*:* {{use of private header from outside its module: '__locale_dir/locale_base_api/bsd_locale_defaults.h'}} +#include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h> // expected-error@*:* {{use of private header from outside its module: '__locale_dir/locale_base_api/bsd_locale_fallbacks.h'}} +#include <__locale_dir/locale_base_api/locale_guard.h> // expected-error@*:* {{use of private header from outside its module: '__locale_dir/locale_base_api/locale_guard.h'}} #include <__mbstate_t.h> // expected-error@*:* {{use of private header from outside its module: '__mbstate_t.h'}} #include <__memory/addressof.h> // expected-error@*:* {{use of private header from outside its module: '__memory/addressof.h'}} #include <__memory/align.h> // expected-error@*:* {{use of private header from outside its module: '__memory/align.h'}} diff --git a/libcxx/test/libcxx/transitive_includes/cxx03.csv b/libcxx/test/libcxx/transitive_includes/cxx03.csv --- a/libcxx/test/libcxx/transitive_includes/cxx03.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx03.csv @@ -493,6 +493,7 @@ list version locale atomic locale cctype +locale clocale locale concepts locale cstdarg locale cstddef @@ -501,6 +502,7 @@ locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/test/libcxx/transitive_includes/cxx11.csv b/libcxx/test/libcxx/transitive_includes/cxx11.csv --- a/libcxx/test/libcxx/transitive_includes/cxx11.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx11.csv @@ -493,6 +493,7 @@ list version locale atomic locale cctype +locale clocale locale concepts locale cstdarg locale cstddef @@ -501,6 +502,7 @@ locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/test/libcxx/transitive_includes/cxx14.csv b/libcxx/test/libcxx/transitive_includes/cxx14.csv --- a/libcxx/test/libcxx/transitive_includes/cxx14.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx14.csv @@ -495,6 +495,7 @@ list version locale atomic locale cctype +locale clocale locale concepts locale cstdarg locale cstddef @@ -503,6 +504,7 @@ locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv --- a/libcxx/test/libcxx/transitive_includes/cxx17.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv @@ -495,6 +495,7 @@ list version locale atomic locale cctype +locale clocale locale concepts locale cstdarg locale cstddef @@ -503,6 +504,7 @@ locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv --- a/libcxx/test/libcxx/transitive_includes/cxx20.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv @@ -501,6 +501,7 @@ list version locale atomic locale cctype +locale clocale locale concepts locale cstdarg locale cstddef @@ -509,6 +510,7 @@ locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/test/libcxx/transitive_includes/cxx2b.csv b/libcxx/test/libcxx/transitive_includes/cxx2b.csv --- a/libcxx/test/libcxx/transitive_includes/cxx2b.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx2b.csv @@ -351,12 +351,14 @@ list tuple list version locale cctype +locale clocale locale cstddef locale cstdint locale cstdio locale cstdlib locale cstring locale ctime +locale cwchar locale initializer_list locale ios locale iosfwd diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt --- a/libcxx/utils/data/ignore_format.txt +++ b/libcxx/utils/data/ignore_format.txt @@ -236,8 +236,6 @@ libcxx/include/__bit_reference libcxx/include/__bit/rotate.h libcxx/include/bitset -libcxx/include/__bsd_locale_defaults.h -libcxx/include/__bsd_locale_fallbacks.h libcxx/include/cctype libcxx/include/chrono libcxx/include/__chrono/calendar.h @@ -458,6 +456,8 @@ libcxx/include/list libcxx/include/__locale libcxx/include/locale +libcxx/include/__loclale_dir/locale_base_api/bsd_locale_defaults.h +libcxx/include/__loclale_dir/locale_base_api/bsd_locale_fallbacks.h libcxx/include/locale.h libcxx/include/map libcxx/include/math.h diff --git a/libcxx/utils/generate_header_tests.py b/libcxx/utils/generate_header_tests.py --- a/libcxx/utils/generate_header_tests.py +++ b/libcxx/utils/generate_header_tests.py @@ -64,7 +64,7 @@ } private_headers_still_public_in_modules = [ - '__assert', '__bsd_locale_defaults.h', '__bsd_locale_fallbacks.h', '__config', + '__assert', '__config', '__config_site.in', '__debug', '__hash_table', '__threading_support', '__tree', '__undef_macros', '__verbose_abort' ] diff --git a/libcxx/utils/generate_iwyu_mapping.py b/libcxx/utils/generate_iwyu_mapping.py --- a/libcxx/utils/generate_iwyu_mapping.py +++ b/libcxx/utils/generate_iwyu_mapping.py @@ -30,7 +30,7 @@ c_headers.append(i.name) result = [] - temporary_mappings = {'__tuple_dir': 'tuple'} + temporary_mappings = {'__tuple_dir': 'tuple', '__locale_dir' : 'locale'} for i in detail_directories: public_header = temporary_mappings.get(i, i.lstrip('_')) result.append(f'{generate(f"@<{i}/.*>", public_header)},') @@ -41,8 +41,6 @@ elif i == '__availability': continue elif i == '__bit_reference': continue elif i == '__bits': public = ['bits'] - elif i == '__bsd_locale_defaults.h': continue - elif i == '__bsd_locale_fallbacks.h': continue elif i == '__config_site.in': continue elif i == '__config': continue elif i == '__debug': continue