Skip to content

Commit b5ba4f0

Browse files
ldionnemysterymath
andauthored
[libc++] Redefine Fuchsia locale base support on top of the new API (#122489)
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. Co-authored-by: Daniel Thornburgh <mysterymath@gmail.com>
1 parent 6556628 commit b5ba4f0

File tree

7 files changed

+298
-22
lines changed

7 files changed

+298
-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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 <__utility/forward.h>
14+
#include <clocale> // uselocale & friends
15+
#include <cstdio>
16+
#include <cstdlib>
17+
#include <cwchar>
18+
19+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20+
# pragma GCC system_header
21+
#endif
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
namespace __locale {
25+
26+
struct __locale_guard {
27+
_LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
28+
29+
_LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
30+
if (__old_loc_)
31+
::uselocale(__old_loc_);
32+
}
33+
34+
locale_t __old_loc_;
35+
36+
__locale_guard(__locale_guard const&) = delete;
37+
__locale_guard& operator=(__locale_guard const&) = delete;
38+
};
39+
40+
//
41+
// Locale management
42+
//
43+
using __locale_t = locale_t;
44+
45+
inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) {
46+
return ::newlocale(__category_mask, __name, __loc);
47+
}
48+
49+
inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); }
50+
51+
inline _LIBCPP_HIDE_FROM_ABI lconv* __localeconv(__locale_t& __loc) {
52+
__locale_guard __current(__loc);
53+
return std::localeconv();
54+
}
55+
56+
//
57+
// Other functions
58+
//
59+
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) {
60+
__locale_guard __current(__loc);
61+
return MB_CUR_MAX;
62+
}
63+
#if _LIBCPP_HAS_WIDE_CHARACTERS
64+
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __ch, __locale_t __loc) {
65+
__locale_guard __current(__loc);
66+
return std::btowc(__ch);
67+
}
68+
inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __ch, __locale_t __loc) {
69+
__locale_guard __current(__loc);
70+
return std::wctob(__ch);
71+
}
72+
inline _LIBCPP_HIDE_FROM_ABI size_t
73+
__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
74+
__locale_guard __current(__loc);
75+
return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard
76+
}
77+
inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __ch, mbstate_t* __ps, __locale_t __loc) {
78+
__locale_guard __current(__loc);
79+
return std::wcrtomb(__s, __ch, __ps);
80+
}
81+
inline _LIBCPP_HIDE_FROM_ABI size_t
82+
__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
83+
__locale_guard __current(__loc);
84+
return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard
85+
}
86+
inline _LIBCPP_HIDE_FROM_ABI size_t
87+
__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
88+
__locale_guard __current(__loc);
89+
return std::mbrtowc(__pwc, __s, __n, __ps);
90+
}
91+
inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
92+
__locale_guard __current(__loc);
93+
return std::mbtowc(__pwc, __pmb, __max);
94+
}
95+
inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
96+
__locale_guard __current(__loc);
97+
return std::mbrlen(__s, __n, __ps);
98+
}
99+
inline _LIBCPP_HIDE_FROM_ABI size_t
100+
__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
101+
__locale_guard __current(__loc);
102+
return ::mbsrtowcs(__dest, __src, __len, __ps);
103+
}
104+
#endif
105+
106+
_LIBCPP_DIAGNOSTIC_PUSH
107+
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
108+
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates
109+
#ifdef _LIBCPP_COMPILER_CLANG_BASED
110+
# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__)
111+
#else
112+
# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */
113+
#endif
114+
115+
template <class... _Args>
116+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
117+
char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) {
118+
__locale_guard __current(__loc);
119+
return std::snprintf(__s, __n, __format, std::forward<_Args>(__args)...);
120+
}
121+
template <class... _Args>
122+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
123+
char** __s, __locale_t __loc, const char* __format, _Args&&... __args) {
124+
__locale_guard __current(__loc);
125+
return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard
126+
}
127+
template <class... _Args>
128+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __sscanf(
129+
const char* __s, __locale_t __loc, const char* __format, _Args&&... __args) {
130+
__locale_guard __current(__loc);
131+
return std::sscanf(__s, __format, std::forward<_Args>(__args)...);
132+
}
133+
134+
_LIBCPP_DIAGNOSTIC_POP
135+
#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
136+
137+
} // namespace __locale
138+
_LIBCPP_END_NAMESPACE_STD
139+
140+
#include <__locale_dir/support/no_locale/characters.h>
141+
#include <__locale_dir/support/no_locale/strtonum.h>
142+
143+
#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
@@ -1480,13 +1480,15 @@ module std [system] {
14801480
textual header "__locale_dir/support/apple.h"
14811481
textual header "__locale_dir/support/bsd_like.h"
14821482
textual header "__locale_dir/support/freebsd.h"
1483+
textual header "__locale_dir/support/fuchsia.h"
1484+
textual header "__locale_dir/support/no_locale/characters.h"
1485+
textual header "__locale_dir/support/no_locale/strtonum.h"
14831486
textual header "__locale_dir/support/windows.h"
14841487
}
14851488

14861489
module locale_base_api {
14871490
textual header "__locale_dir/locale_base_api/android.h"
14881491
textual header "__locale_dir/locale_base_api/bsd_locale_fallbacks.h"
1489-
textual header "__locale_dir/locale_base_api/fuchsia.h"
14901492
textual header "__locale_dir/locale_base_api/ibm.h"
14911493
textual header "__locale_dir/locale_base_api/musl.h"
14921494
textual header "__locale_dir/locale_base_api/openbsd.h"

0 commit comments

Comments
 (0)