Skip to content

Commit 1f2b7ae

Browse files
authored
[libc++] Refactor locale_guard (#113694)
Rename __libcpp_locale_guard to just __locale_guard, since there's no reason for it to have __libcpp_ in its name -- it's just an internal utility. Also, define __locale_guard unconditionally of _LIBCPP_LOCALE__L_EXTENSIONS, since that header is only used on Windows (where it has a custom definition) or from bsd_locale_fallbacks.h, which is only included when the L extensions are not provided.
1 parent 5c20891 commit 1f2b7ae

File tree

6 files changed

+56
-58
lines changed

6 files changed

+56
-58
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,11 @@ set(files
496496
__locale_dir/locale_base_api/bsd_locale_fallbacks.h
497497
__locale_dir/locale_base_api/fuchsia.h
498498
__locale_dir/locale_base_api/ibm.h
499-
__locale_dir/locale_base_api/locale_guard.h
500499
__locale_dir/locale_base_api/musl.h
501500
__locale_dir/locale_base_api/newlib.h
502501
__locale_dir/locale_base_api/openbsd.h
503502
__locale_dir/locale_base_api/win32.h
503+
__locale_dir/locale_guard.h
504504
__math/abs.h
505505
__math/copysign.h
506506
__math/error_functions.h

libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
1414
#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
1515

16-
#include <__locale_dir/locale_base_api/locale_guard.h>
16+
#include <__locale_dir/locale_guard.h>
1717
#include <cstdio>
1818
#include <stdarg.h>
1919
#include <stdlib.h>
@@ -29,64 +29,64 @@
2929
_LIBCPP_BEGIN_NAMESPACE_STD
3030

3131
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l) {
32-
__libcpp_locale_guard __current(__l);
32+
__locale_guard __current(__l);
3333
return MB_CUR_MAX;
3434
}
3535

3636
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
3737
inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, locale_t __l) {
38-
__libcpp_locale_guard __current(__l);
38+
__locale_guard __current(__l);
3939
return btowc(__c);
4040
}
4141

4242
inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, locale_t __l) {
43-
__libcpp_locale_guard __current(__l);
43+
__locale_guard __current(__l);
4444
return wctob(__c);
4545
}
4646

4747
inline _LIBCPP_HIDE_FROM_ABI size_t
4848
__libcpp_wcsnrtombs_l(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, locale_t __l) {
49-
__libcpp_locale_guard __current(__l);
49+
__locale_guard __current(__l);
5050
return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
5151
}
5252

5353
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcrtomb_l(char* __s, wchar_t __wc, mbstate_t* __ps, locale_t __l) {
54-
__libcpp_locale_guard __current(__l);
54+
__locale_guard __current(__l);
5555
return wcrtomb(__s, __wc, __ps);
5656
}
5757

5858
inline _LIBCPP_HIDE_FROM_ABI size_t
5959
__libcpp_mbsnrtowcs_l(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, locale_t __l) {
60-
__libcpp_locale_guard __current(__l);
60+
__locale_guard __current(__l);
6161
return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
6262
}
6363

6464
inline _LIBCPP_HIDE_FROM_ABI size_t
6565
__libcpp_mbrtowc_l(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
66-
__libcpp_locale_guard __current(__l);
66+
__locale_guard __current(__l);
6767
return mbrtowc(__pwc, __s, __n, __ps);
6868
}
6969

7070
inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mbtowc_l(wchar_t* __pwc, const char* __pmb, size_t __max, locale_t __l) {
71-
__libcpp_locale_guard __current(__l);
71+
__locale_guard __current(__l);
7272
return mbtowc(__pwc, __pmb, __max);
7373
}
7474

7575
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbrlen_l(const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
76-
__libcpp_locale_guard __current(__l);
76+
__locale_guard __current(__l);
7777
return mbrlen(__s, __n, __ps);
7878
}
7979
#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
8080

8181
inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(locale_t __l) {
82-
__libcpp_locale_guard __current(__l);
82+
__locale_guard __current(__l);
8383
return localeconv();
8484
}
8585

8686
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
8787
inline _LIBCPP_HIDE_FROM_ABI size_t
8888
__libcpp_mbsrtowcs_l(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, locale_t __l) {
89-
__libcpp_locale_guard __current(__l);
89+
__locale_guard __current(__l);
9090
return mbsrtowcs(__dest, __src, __len, __ps);
9191
}
9292
#endif
@@ -95,7 +95,7 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
9595
char* __s, size_t __n, locale_t __l, const char* __format, ...) {
9696
va_list __va;
9797
va_start(__va, __format);
98-
__libcpp_locale_guard __current(__l);
98+
__locale_guard __current(__l);
9999
int __res = vsnprintf(__s, __n, __format, __va);
100100
va_end(__va);
101101
return __res;
@@ -105,7 +105,7 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
105105
char** __s, locale_t __l, const char* __format, ...) {
106106
va_list __va;
107107
va_start(__va, __format);
108-
__libcpp_locale_guard __current(__l);
108+
__locale_guard __current(__l);
109109
int __res = vasprintf(__s, __format, __va);
110110
va_end(__va);
111111
return __res;
@@ -115,7 +115,7 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __libcpp_sscanf_l(
115115
const char* __s, locale_t __l, const char* __format, ...) {
116116
va_list __va;
117117
va_start(__va, __format);
118-
__libcpp_locale_guard __current(__l);
118+
__locale_guard __current(__l);
119119
int __res = vsscanf(__s, __format, __va);
120120
va_end(__va);
121121
return __res;

libcxx/include/__locale_dir/locale_base_api/locale_guard.h renamed to libcxx/include/__locale_dir/locale_guard.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_LOCALE_GUARD_H
10-
#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_LOCALE_GUARD_H
9+
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_GUARD_H
10+
#define _LIBCPP___LOCALE_DIR_LOCALE_GUARD_H
1111

1212
#include <__config>
1313
#include <__locale> // for locale_t
@@ -19,23 +19,9 @@
1919

2020
_LIBCPP_BEGIN_NAMESPACE_STD
2121

22-
#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
23-
struct __libcpp_locale_guard {
24-
_LIBCPP_HIDE_FROM_ABI __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
25-
26-
_LIBCPP_HIDE_FROM_ABI ~__libcpp_locale_guard() {
27-
if (__old_loc_)
28-
uselocale(__old_loc_);
29-
}
30-
31-
locale_t __old_loc_;
32-
33-
__libcpp_locale_guard(__libcpp_locale_guard const&) = delete;
34-
__libcpp_locale_guard& operator=(__libcpp_locale_guard const&) = delete;
35-
};
36-
#elif defined(_LIBCPP_MSVCRT_LIKE)
37-
struct __libcpp_locale_guard {
38-
__libcpp_locale_guard(locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
22+
#if defined(_LIBCPP_MSVCRT_LIKE)
23+
struct __locale_guard {
24+
__locale_guard(locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
3925
// Setting the locale can be expensive even when the locale given is
4026
// already the current locale, so do an explicit check to see if the
4127
// current locale is already the one we want.
@@ -51,7 +37,7 @@ struct __libcpp_locale_guard {
5137
__setlocale(__l.__get_locale());
5238
}
5339
}
54-
~__libcpp_locale_guard() {
40+
~__locale_guard() {
5541
// The CRT documentation doesn't explicitly say, but setlocale() does the
5642
// right thing when given a semicolon-separated list of locale settings
5743
// for the different categories in the same format as returned by
@@ -71,8 +57,22 @@ struct __libcpp_locale_guard {
7157
int __status;
7258
char* __locale_all = nullptr;
7359
};
60+
#else
61+
struct __locale_guard {
62+
_LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
63+
64+
_LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
65+
if (__old_loc_)
66+
uselocale(__old_loc_);
67+
}
68+
69+
locale_t __old_loc_;
70+
71+
__locale_guard(__locale_guard const&) = delete;
72+
__locale_guard& operator=(__locale_guard const&) = delete;
73+
};
7474
#endif
7575

7676
_LIBCPP_END_NAMESPACE_STD
7777

78-
#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_LOCALE_GUARD_H
78+
#endif // _LIBCPP___LOCALE_DIR_LOCALE_GUARD_H

libcxx/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ module std [system] {
14431443
module locale {
14441444
header "locale"
14451445
header "__locale_dir/locale_base_api.h"
1446-
header "__locale_dir/locale_base_api/locale_guard.h"
1446+
header "__locale_dir/locale_guard.h"
14471447
module locale_base_api {
14481448
textual header "__locale_dir/locale_base_api/android.h"
14491449
textual header "__locale_dir/locale_base_api/bsd_locale_defaults.h"

libcxx/src/iostream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <string>
1313

1414
#ifdef _LIBCPP_MSVCRT_LIKE
15-
# include <__locale_dir/locale_base_api/locale_guard.h>
15+
# include <__locale_dir/locale_guard.h>
1616
#endif
1717

1818
#define _str(s) #s
@@ -109,7 +109,7 @@ static void force_locale_initialization() {
109109
static bool once = []() {
110110
auto loc = newlocale(LC_ALL_MASK, "C", 0);
111111
{
112-
__libcpp_locale_guard g(loc); // forces initialization of locale TLS
112+
__locale_guard g(loc); // forces initialization of locale TLS
113113
((void)g);
114114
}
115115
freelocale(loc);

libcxx/src/support/win32/locale_win32.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
#include <memory>
1212
#include <type_traits>
1313

14-
#include <__locale_dir/locale_base_api/locale_guard.h>
14+
#include <__locale_dir/locale_guard.h>
1515

1616
int __libcpp_vasprintf(char** sptr, const char* __restrict fmt, va_list ap);
1717

18-
using std::__libcpp_locale_guard;
19-
2018
// FIXME: base and mask currently unused. Needs manual work to construct the new locale
2119
locale_t newlocale(int /*mask*/, const char* locale, locale_t /*base*/) {
2220
return {_create_locale(LC_ALL, locale), locale};
@@ -26,33 +24,33 @@ decltype(MB_CUR_MAX) MB_CUR_MAX_L(locale_t __l) {
2624
#if defined(_LIBCPP_MSVCRT)
2725
return ___mb_cur_max_l_func(__l);
2826
#else
29-
__libcpp_locale_guard __current(__l);
27+
std::__locale_guard __current(__l);
3028
return MB_CUR_MAX;
3129
#endif
3230
}
3331

3432
lconv* localeconv_l(locale_t& loc) {
35-
__libcpp_locale_guard __current(loc);
33+
std::__locale_guard __current(loc);
3634
lconv* lc = localeconv();
3735
if (!lc)
3836
return lc;
3937
return loc.__store_lconv(lc);
4038
}
4139
size_t mbrlen_l(const char* __restrict s, size_t n, mbstate_t* __restrict ps, locale_t loc) {
42-
__libcpp_locale_guard __current(loc);
40+
std::__locale_guard __current(loc);
4341
return mbrlen(s, n, ps);
4442
}
4543
size_t
4644
mbsrtowcs_l(wchar_t* __restrict dst, const char** __restrict src, size_t len, mbstate_t* __restrict ps, locale_t loc) {
47-
__libcpp_locale_guard __current(loc);
45+
std::__locale_guard __current(loc);
4846
return mbsrtowcs(dst, src, len, ps);
4947
}
5048
size_t wcrtomb_l(char* __restrict s, wchar_t wc, mbstate_t* __restrict ps, locale_t loc) {
51-
__libcpp_locale_guard __current(loc);
49+
std::__locale_guard __current(loc);
5250
return wcrtomb(s, wc, ps);
5351
}
5452
size_t mbrtowc_l(wchar_t* __restrict pwc, const char* __restrict s, size_t n, mbstate_t* __restrict ps, locale_t loc) {
55-
__libcpp_locale_guard __current(loc);
53+
std::__locale_guard __current(loc);
5654
return mbrtowc(pwc, s, n, ps);
5755
}
5856
size_t mbsnrtowcs_l(wchar_t* __restrict dst,
@@ -61,7 +59,7 @@ size_t mbsnrtowcs_l(wchar_t* __restrict dst,
6159
size_t len,
6260
mbstate_t* __restrict ps,
6361
locale_t loc) {
64-
__libcpp_locale_guard __current(loc);
62+
std::__locale_guard __current(loc);
6563
return mbsnrtowcs(dst, src, nms, len, ps);
6664
}
6765
size_t wcsnrtombs_l(char* __restrict dst,
@@ -70,15 +68,15 @@ size_t wcsnrtombs_l(char* __restrict dst,
7068
size_t len,
7169
mbstate_t* __restrict ps,
7270
locale_t loc) {
73-
__libcpp_locale_guard __current(loc);
71+
std::__locale_guard __current(loc);
7472
return wcsnrtombs(dst, src, nwc, len, ps);
7573
}
7674
wint_t btowc_l(int c, locale_t loc) {
77-
__libcpp_locale_guard __current(loc);
75+
std::__locale_guard __current(loc);
7876
return btowc(c);
7977
}
8078
int wctob_l(wint_t c, locale_t loc) {
81-
__libcpp_locale_guard __current(loc);
79+
std::__locale_guard __current(loc);
8280
return wctob(c);
8381
}
8482

@@ -90,7 +88,7 @@ int snprintf_l(char* ret, size_t n, locale_t loc, const char* format, ...) {
9088
int result = __stdio_common_vsprintf(
9189
_CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR, ret, n, format, loc, ap);
9290
#else
93-
__libcpp_locale_guard __current(loc);
91+
std::__locale_guard __current(loc);
9492
_LIBCPP_DIAGNOSTIC_PUSH
9593
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
9694
int result = vsnprintf(ret, n, format, ap);
@@ -108,25 +106,25 @@ int asprintf_l(char** ret, locale_t loc, const char* format, ...) {
108106
return result;
109107
}
110108
int vasprintf_l(char** ret, locale_t loc, const char* format, va_list ap) {
111-
__libcpp_locale_guard __current(loc);
109+
std::__locale_guard __current(loc);
112110
return __libcpp_vasprintf(ret, format, ap);
113111
}
114112

115113
#if !defined(_LIBCPP_MSVCRT)
116114
float strtof_l(const char* nptr, char** endptr, locale_t loc) {
117-
__libcpp_locale_guard __current(loc);
115+
std::__locale_guard __current(loc);
118116
return strtof(nptr, endptr);
119117
}
120118

121119
long double strtold_l(const char* nptr, char** endptr, locale_t loc) {
122-
__libcpp_locale_guard __current(loc);
120+
std::__locale_guard __current(loc);
123121
return strtold(nptr, endptr);
124122
}
125123
#endif
126124

127125
#if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
128126
size_t strftime_l(char* ret, size_t n, const char* format, const struct tm* tm, locale_t loc) {
129-
__libcpp_locale_guard __current(loc);
127+
std::__locale_guard __current(loc);
130128
return strftime(ret, n, format, tm);
131129
}
132130
#endif

0 commit comments

Comments
 (0)