Skip to content

Commit b39d0f1

Browse files
committed
[libc++] Redefine Fuchsia locale base support on top of the new API
This follows the same path we've been doing for all platforms so far, moving away from the old definition of the locale base API.
1 parent c664a7f commit b39d0f1

File tree

7 files changed

+295
-22
lines changed

7 files changed

+295
-22
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,16 @@ set(files
499499
__locale_dir/locale_base_api.h
500500
__locale_dir/locale_base_api/android.h
501501
__locale_dir/locale_base_api/bsd_locale_fallbacks.h
502-
__locale_dir/locale_base_api/fuchsia.h
503502
__locale_dir/locale_base_api/ibm.h
504503
__locale_dir/locale_base_api/musl.h
505504
__locale_dir/locale_base_api/openbsd.h
506505
__locale_dir/pad_and_output.h
507506
__locale_dir/support/apple.h
508507
__locale_dir/support/bsd_like.h
509508
__locale_dir/support/freebsd.h
509+
__locale_dir/support/fuchsia.h
510+
__locale_dir/support/no_locale/characters.h
511+
__locale_dir/support/no_locale/strtonum.h
510512
__locale_dir/support/windows.h
511513
__math/abs.h
512514
__math/copysign.h

libcxx/include/__locale_dir/locale_base_api.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
# include <__locale_dir/support/freebsd.h>
100100
#elif defined(_LIBCPP_MSVCRT_LIKE)
101101
# include <__locale_dir/support/windows.h>
102+
#elif defined(__Fuchsia__)
103+
# include <__locale_dir/support/fuchsia.h>
102104
#else
103105

104106
// TODO: This is a temporary definition to bridge between the old way we defined the locale base API
@@ -111,8 +113,6 @@
111113
# include <__locale_dir/locale_base_api/android.h>
112114
# elif defined(__OpenBSD__)
113115
# include <__locale_dir/locale_base_api/openbsd.h>
114-
# elif defined(__Fuchsia__)
115-
# include <__locale_dir/locale_base_api/fuchsia.h>
116116
# elif defined(__wasi__) || _LIBCPP_HAS_MUSL_LIBC
117117
# include <__locale_dir/locale_base_api/musl.h>
118118
# endif

libcxx/include/__locale_dir/locale_base_api/fuchsia.h

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//===-----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___LOCALE_DIR_SUPPORT_FUCHSIA_H
10+
#define _LIBCPP___LOCALE_DIR_SUPPORT_FUCHSIA_H
11+
12+
#include <__config>
13+
#include <cstdlib>
14+
#include <cwchar>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
# pragma GCC system_header
18+
#endif
19+
20+
_LIBCPP_BEGIN_NAMESPACE_STD
21+
namespace __locale {
22+
23+
struct __locale_guard {
24+
_LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
25+
26+
_LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
27+
if (__old_loc_)
28+
::uselocale(__old_loc_);
29+
}
30+
31+
locale_t __old_loc_;
32+
33+
__locale_guard(__locale_guard const&) = delete;
34+
__locale_guard& operator=(__locale_guard const&) = delete;
35+
};
36+
37+
//
38+
// Locale management
39+
//
40+
using __locale_t = locale_t;
41+
42+
inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) {
43+
return ::newlocale(__category_mask, __name, __loc);
44+
}
45+
46+
inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); }
47+
48+
inline _LIBCPP_HIDE_FROM_ABI lconv* __localeconv(__locale_t& __loc) {
49+
__locale_guard __current(__loc);
50+
return std::localeconv();
51+
}
52+
53+
//
54+
// Other functions
55+
//
56+
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) {
57+
__locale_guard __current(__loc);
58+
return MB_CUR_MAX;
59+
}
60+
#if _LIBCPP_HAS_WIDE_CHARACTERS
61+
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __ch, __locale_t __loc) {
62+
__locale_guard __current(__loc);
63+
return std::btowc(__ch);
64+
}
65+
inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __ch, __locale_t __loc) {
66+
__locale_guard __current(__loc);
67+
return std::wctob(__ch);
68+
}
69+
inline _LIBCPP_HIDE_FROM_ABI size_t
70+
__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
71+
__locale_guard __current(__loc);
72+
return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard
73+
}
74+
inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __ch, mbstate_t* __ps, __locale_t __loc) {
75+
__locale_guard __current(__loc);
76+
return std::wcrtomb(__s, __ch, __ps);
77+
}
78+
inline _LIBCPP_HIDE_FROM_ABI size_t
79+
__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
80+
__locale_guard __current(__loc);
81+
return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard
82+
}
83+
inline _LIBCPP_HIDE_FROM_ABI size_t
84+
__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
85+
__locale_guard __current(__loc);
86+
return std::mbrtowc(__pwc, __s, __n, __ps);
87+
}
88+
inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
89+
__locale_guard __current(__loc);
90+
return std::mbtowc(__pwc, __pmb, __max);
91+
}
92+
inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
93+
__locale_guard __current(__loc);
94+
return std::mbrlen(__s, __n, __ps);
95+
}
96+
inline _LIBCPP_HIDE_FROM_ABI size_t
97+
__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
98+
__locale_guard __current(__loc);
99+
return ::mbsrtowcs(__dest, __src, __len, __ps);
100+
}
101+
#endif
102+
103+
_LIBCPP_DIAGNOSTIC_PUSH
104+
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
105+
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates
106+
#ifdef _LIBCPP_COMPILER_CLANG_BASED
107+
# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__)
108+
#else
109+
# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */
110+
#endif
111+
112+
template <class... _Args>
113+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
114+
char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) {
115+
__locale_guard __current(__loc);
116+
return std::snprintf(__s, __n, __format, std::forward<_Args>(__args)...);
117+
}
118+
template <class... _Args>
119+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
120+
char** __s, __locale_t __loc, const char* __format, _Args&&... __args) {
121+
__locale_guard __current(__loc);
122+
return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard
123+
}
124+
template <class... _Args>
125+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __sscanf(
126+
const char* __s, __locale_t __loc, const char* __format, _Args&&... __args) {
127+
__locale_guard __current(__loc);
128+
return std::sscanf(__s, __format, std::forward<_Args>(__args)...);
129+
}
130+
131+
_LIBCPP_DIAGNOSTIC_POP
132+
#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
133+
134+
} // namespace __locale
135+
_LIBCPP_END_NAMESPACE_STD
136+
137+
#include <__locale_dir/support/no_locale/characters.h>
138+
#include <__locale_dir/support/no_locale/strtonum.h>
139+
140+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_FUCHSIA_H
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===-----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_CHARACTERS_H
10+
#define _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_CHARACTERS_H
11+
12+
#include <__config>
13+
#include <__cstddef/size_t.h>
14+
#include <cctype>
15+
#include <cstdlib>
16+
#include <cstring>
17+
#include <ctime>
18+
#if _LIBCPP_HAS_WIDE_CHARACTERS
19+
# include <cwctype>
20+
#endif
21+
22+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23+
# pragma GCC system_header
24+
#endif
25+
26+
_LIBCPP_BEGIN_NAMESPACE_STD
27+
namespace __locale {
28+
29+
//
30+
// Character manipulation functions
31+
//
32+
inline _LIBCPP_HIDE_FROM_ABI int __islower(int __c, __locale_t) { return std::islower(__c); }
33+
34+
inline _LIBCPP_HIDE_FROM_ABI int __isupper(int __c, __locale_t) { return std::isupper(__c); }
35+
36+
inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __c, __locale_t) { return std::isdigit(__c); }
37+
38+
inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __c, __locale_t) { return std::isxdigit(__c); }
39+
40+
inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t) { return std::toupper(__c); }
41+
42+
inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t) { return std::tolower(__c); }
43+
44+
inline _LIBCPP_HIDE_FROM_ABI int __strcoll(const char* __s1, const char* __s2, __locale_t) {
45+
return std::strcoll(__s1, __s2);
46+
}
47+
48+
inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t) {
49+
return std::strxfrm(__dest, __src, __n);
50+
}
51+
52+
#if _LIBCPP_HAS_WIDE_CHARACTERS
53+
inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t) {
54+
return std::iswctype(__c, __type);
55+
}
56+
57+
inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __c, __locale_t) { return std::iswspace(__c); }
58+
59+
inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __c, __locale_t) { return std::iswprint(__c); }
60+
61+
inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __c, __locale_t) { return std::iswcntrl(__c); }
62+
63+
inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __c, __locale_t) { return std::iswupper(__c); }
64+
65+
inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __c, __locale_t) { return std::iswlower(__c); }
66+
67+
inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __c, __locale_t) { return std::iswalpha(__c); }
68+
69+
inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __c, __locale_t) { return std::iswblank(__c); }
70+
71+
inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __c, __locale_t) { return std::iswdigit(__c); }
72+
73+
inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __c, __locale_t) { return std::iswpunct(__c); }
74+
75+
inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __c, __locale_t) { return std::iswxdigit(__c); }
76+
77+
inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __c, __locale_t) { return std::towupper(__c); }
78+
79+
inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __c, __locale_t) { return std::towlower(__c); }
80+
81+
inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t) {
82+
return std::wcscoll(__ws1, __ws2);
83+
}
84+
85+
inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t) {
86+
return std::wcsxfrm(__dest, __src, __n);
87+
}
88+
#endif // _LIBCPP_HAS_WIDE_CHARACTERS
89+
90+
inline _LIBCPP_HIDE_FROM_ABI size_t
91+
__strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t) {
92+
return std::strftime(__s, __max, __format, __tm);
93+
}
94+
95+
} // namespace __locale
96+
_LIBCPP_END_NAMESPACE_STD
97+
98+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_CHARACTERS_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_STRTONUM_H
10+
#define _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_STRTONUM_H
11+
12+
#include <__config>
13+
#include <cstdlib>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
namespace __locale {
21+
22+
//
23+
// Strtonum functions
24+
//
25+
inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t) {
26+
return std::strtof(__nptr, __endptr);
27+
}
28+
29+
inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t) {
30+
return std::strtod(__nptr, __endptr);
31+
}
32+
33+
inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t) {
34+
return std::strtold(__nptr, __endptr);
35+
}
36+
37+
inline _LIBCPP_HIDE_FROM_ABI long long __strtoll(const char* __nptr, char** __endptr, int __base, __locale_t) {
38+
return std::strtoll(__nptr, __endptr, __base);
39+
}
40+
41+
inline _LIBCPP_HIDE_FROM_ABI unsigned long long
42+
__strtoull(const char* __nptr, char** __endptr, int __base, __locale_t) {
43+
return std::strtoull(__nptr, __endptr, __base);
44+
}
45+
46+
} // namespace __locale
47+
_LIBCPP_END_NAMESPACE_STD
48+
49+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_NO_LOCALE_STRTONUM_H

libcxx/include/module.modulemap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,13 +1478,15 @@ module std [system] {
14781478
textual header "__locale_dir/support/apple.h"
14791479
textual header "__locale_dir/support/bsd_like.h"
14801480
textual header "__locale_dir/support/freebsd.h"
1481+
textual header "__locale_dir/support/fuchsia.h"
1482+
textual header "__locale_dir/support/no_locale/characters.h"
1483+
textual header "__locale_dir/support/no_locale/strtonum.h"
14811484
textual header "__locale_dir/support/windows.h"
14821485
}
14831486

14841487
module locale_base_api {
14851488
textual header "__locale_dir/locale_base_api/android.h"
14861489
textual header "__locale_dir/locale_base_api/bsd_locale_fallbacks.h"
1487-
textual header "__locale_dir/locale_base_api/fuchsia.h"
14881490
textual header "__locale_dir/locale_base_api/ibm.h"
14891491
textual header "__locale_dir/locale_base_api/musl.h"
14901492
textual header "__locale_dir/locale_base_api/openbsd.h"

0 commit comments

Comments
 (0)