Skip to content

Commit 72402e8

Browse files
authored
[libc++] Support using Newlib as libc with locale support. (#167962)
Tested on Newlib v4.5.0. The newlib.h was mostly copied from linux.h within the same directory. Newlib locale support was initially added in http://llvm.org/D5385, but removed in #113721 , because of revision http://llvm.org/D49927.
1 parent 3ea796e commit 72402e8

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ set(files
533533
__locale_dir/support/fuchsia.h
534534
__locale_dir/support/linux.h
535535
__locale_dir/support/netbsd.h
536+
__locale_dir/support/newlib.h
536537
__locale_dir/support/no_locale/characters.h
537538
__locale_dir/support/no_locale/strtonum.h
538539
__locale_dir/support/windows.h

libcxx/include/__locale_dir/locale_base_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@
118118
# include <__locale_dir/support/fuchsia.h>
119119
# elif defined(__linux__)
120120
# include <__locale_dir/support/linux.h>
121+
# elif _LIBCPP_LIBC_NEWLIB
122+
# include <__locale_dir/support/newlib.h>
121123
# else
122124

123125
// TODO: This is a temporary definition to bridge between the old way we defined the locale base API
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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_NEWLIB_H
10+
#define _LIBCPP___LOCALE_DIR_SUPPORT_NEWLIB_H
11+
12+
#include <__config>
13+
#include <__cstddef/size_t.h>
14+
#include <__std_mbstate_t.h>
15+
#include <clocale> // std::lconv
16+
#include <cstdio>
17+
#include <cstdlib>
18+
#include <ctype.h>
19+
#include <stdarg.h>
20+
#include <string.h>
21+
#include <time.h>
22+
#if _LIBCPP_HAS_WIDE_CHARACTERS
23+
# include <cwchar>
24+
# include <wctype.h>
25+
#endif
26+
27+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28+
# pragma GCC system_header
29+
#endif
30+
31+
_LIBCPP_BEGIN_NAMESPACE_STD
32+
namespace __locale {
33+
34+
struct __locale_guard {
35+
_LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
36+
37+
_LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
38+
if (__old_loc_)
39+
::uselocale(__old_loc_);
40+
}
41+
42+
locale_t __old_loc_;
43+
44+
__locale_guard(__locale_guard const&) = delete;
45+
__locale_guard& operator=(__locale_guard const&) = delete;
46+
};
47+
48+
//
49+
// Locale management
50+
//
51+
#define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK
52+
#define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK
53+
#define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK
54+
#define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK
55+
#define _LIBCPP_TIME_MASK LC_TIME_MASK
56+
#define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK
57+
#define _LIBCPP_ALL_MASK LC_ALL_MASK
58+
#define _LIBCPP_LC_ALL LC_ALL
59+
60+
using __locale_t _LIBCPP_NODEBUG = ::locale_t;
61+
62+
#if defined(_LIBCPP_BUILDING_LIBRARY)
63+
using __lconv_t _LIBCPP_NODEBUG = std::lconv;
64+
65+
inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) {
66+
return ::newlocale(__category_mask, __locale, __base);
67+
}
68+
69+
inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); }
70+
71+
inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __locale) {
72+
return ::setlocale(__category, __locale);
73+
}
74+
75+
inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) {
76+
__locale_guard __current(__loc);
77+
return std::localeconv();
78+
}
79+
#endif // _LIBCPP_BUILDING_LIBRARY
80+
81+
//
82+
// Strtonum functions
83+
//
84+
inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) {
85+
return ::strtof_l(__nptr, __endptr, __loc);
86+
}
87+
88+
inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) {
89+
return ::strtod_l(__nptr, __endptr, __loc);
90+
}
91+
92+
inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) {
93+
return ::strtold_l(__nptr, __endptr, __loc);
94+
}
95+
96+
//
97+
// Character manipulation functions
98+
//
99+
#if defined(_LIBCPP_BUILDING_LIBRARY)
100+
inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t __loc) { return toupper_l(__c, __loc); }
101+
102+
inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t __loc) { return tolower_l(__c, __loc); }
103+
104+
inline _LIBCPP_HIDE_FROM_ABI int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) {
105+
return strcoll_l(__s1, __s2, __loc);
106+
}
107+
108+
inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) {
109+
return strxfrm_l(__dest, __src, __n, __loc);
110+
}
111+
112+
# if _LIBCPP_HAS_WIDE_CHARACTERS
113+
inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) {
114+
return iswctype_l(__c, __type, __loc);
115+
}
116+
117+
inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __c, __locale_t __loc) { return iswspace_l(__c, __loc); }
118+
119+
inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __c, __locale_t __loc) { return iswprint_l(__c, __loc); }
120+
121+
inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __c, __locale_t __loc) { return iswcntrl_l(__c, __loc); }
122+
123+
inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __c, __locale_t __loc) { return iswupper_l(__c, __loc); }
124+
125+
inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __c, __locale_t __loc) { return iswlower_l(__c, __loc); }
126+
127+
inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __c, __locale_t __loc) { return iswalpha_l(__c, __loc); }
128+
129+
inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __c, __locale_t __loc) { return iswblank_l(__c, __loc); }
130+
131+
inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __c, __locale_t __loc) { return iswdigit_l(__c, __loc); }
132+
133+
inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __c, __locale_t __loc) { return iswpunct_l(__c, __loc); }
134+
135+
inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __c, __locale_t __loc) { return iswxdigit_l(__c, __loc); }
136+
137+
inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __c, __locale_t __loc) { return towupper_l(__c, __loc); }
138+
139+
inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __c, __locale_t __loc) { return towlower_l(__c, __loc); }
140+
141+
inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) {
142+
return wcscoll_l(__ws1, __ws2, __loc);
143+
}
144+
145+
inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) {
146+
return wcsxfrm_l(__dest, __src, __n, __loc);
147+
}
148+
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
149+
150+
inline _LIBCPP_HIDE_FROM_ABI
151+
size_t __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) {
152+
return strftime_l(__s, __max, __format, __tm, __loc);
153+
}
154+
155+
//
156+
// Other functions
157+
//
158+
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) {
159+
__locale_guard __current(__loc);
160+
return MB_CUR_MAX;
161+
}
162+
163+
# if _LIBCPP_HAS_WIDE_CHARACTERS
164+
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __c, __locale_t __loc) {
165+
__locale_guard __current(__loc);
166+
return std::btowc(__c);
167+
}
168+
169+
inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __c, __locale_t __loc) {
170+
__locale_guard __current(__loc);
171+
return std::wctob(__c);
172+
}
173+
174+
inline _LIBCPP_HIDE_FROM_ABI size_t
175+
__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
176+
__locale_guard __current(__loc);
177+
return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard
178+
}
179+
180+
inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) {
181+
__locale_guard __current(__loc);
182+
return std::wcrtomb(__s, __wc, __ps);
183+
}
184+
185+
inline _LIBCPP_HIDE_FROM_ABI size_t
186+
__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
187+
__locale_guard __current(__loc);
188+
return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard
189+
}
190+
191+
inline _LIBCPP_HIDE_FROM_ABI size_t
192+
__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
193+
__locale_guard __current(__loc);
194+
return std::mbrtowc(__pwc, __s, __n, __ps);
195+
}
196+
197+
inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
198+
__locale_guard __current(__loc);
199+
return std::mbtowc(__pwc, __pmb, __max);
200+
}
201+
202+
inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
203+
__locale_guard __current(__loc);
204+
return std::mbrlen(__s, __n, __ps);
205+
}
206+
207+
inline _LIBCPP_HIDE_FROM_ABI size_t
208+
__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
209+
__locale_guard __current(__loc);
210+
return std::mbsrtowcs(__dest, __src, __len, __ps);
211+
}
212+
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
213+
#endif // _LIBCPP_BUILDING_LIBRARY
214+
215+
#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs
216+
_LIBCPP_HIDE_FROM_ABI
217+
#endif
218+
inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
219+
char* __s, size_t __n, __locale_t __loc, const char* __format, ...) {
220+
va_list __va;
221+
va_start(__va, __format);
222+
__locale_guard __current(__loc);
223+
int __res = std::vsnprintf(__s, __n, __format, __va);
224+
va_end(__va);
225+
return __res;
226+
}
227+
228+
#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs
229+
_LIBCPP_HIDE_FROM_ABI
230+
#endif
231+
inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
232+
char** __s, __locale_t __loc, const char* __format, ...) {
233+
va_list __va;
234+
va_start(__va, __format);
235+
__locale_guard __current(__loc);
236+
int __res = ::vasprintf(__s, __format, __va); // non-standard
237+
va_end(__va);
238+
return __res;
239+
}
240+
} // namespace __locale
241+
_LIBCPP_END_NAMESPACE_STD
242+
243+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_NEWLIB_H

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,7 @@ module std [system] {
15841584
textual header "__locale_dir/support/fuchsia.h"
15851585
textual header "__locale_dir/support/linux.h"
15861586
textual header "__locale_dir/support/netbsd.h"
1587+
textual header "__locale_dir/support/newlib.h"
15871588
textual header "__locale_dir/support/no_locale/characters.h"
15881589
textual header "__locale_dir/support/no_locale/strtonum.h"
15891590
textual header "__locale_dir/support/windows.h"

0 commit comments

Comments
 (0)