Skip to content

Commit c832462

Browse files
committed
[libc++] Use proper functions instead of macros in bsd_locale_defaults.h
We were using macros instead of functions, leading to the inability to properly qualify calls to those symbols inside <locale>. This is also a step towards making the locale API modules-correct.
1 parent e146c18 commit c832462

File tree

4 files changed

+108
-26
lines changed

4 files changed

+108
-26
lines changed

libcxx/include/__locale_dir/locale_base_api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H
1010
#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H
1111

12+
#include <__config>
13+
1214
#if defined(_LIBCPP_MSVCRT_LIKE)
1315
# include <__locale_dir/locale_base_api/win32.h>
1416
#elif defined(_AIX) || defined(__MVS__)
@@ -27,6 +29,12 @@
2729
# include <__locale_dir/locale_base_api/freebsd.h>
2830
#endif
2931

32+
#ifdef _LIBCPP_LOCALE__L_EXTENSIONS
33+
# include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
34+
#else
35+
# include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
36+
#endif
37+
3038
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
3139
# pragma GCC system_header
3240
#endif

libcxx/include/__locale_dir/locale_base_api/bsd_locale_defaults.h

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,106 @@
1414
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_DEFAULTS_H
1515
#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_DEFAULTS_H
1616

17+
// <xlocale.h> must come first since it tells the other C headers to include additional declarations.
18+
// For some reason we must include <xlocale.h> before each C header.
19+
// clang-format off
20+
#include <xlocale.h>
21+
#include <stdio.h>
22+
23+
#include <xlocale.h>
24+
#include <stdlib.h>
25+
26+
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
27+
# include <xlocale.h>
28+
# include <wchar.h>
29+
#endif
30+
31+
#include <xlocale.h>
32+
#include <ctype.h>
33+
// clang-format on
34+
35+
#include <__config>
36+
#include <__cstddef/size_t.h>
37+
#include <__std_mbstate_t.h>
38+
#include <cstdarg>
39+
1740
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1841
# pragma GCC system_header
1942
#endif
2043

21-
#define __libcpp_mb_cur_max_l(loc) MB_CUR_MAX_L(loc)
22-
#define __libcpp_btowc_l(ch, loc) btowc_l(ch, loc)
23-
#define __libcpp_wctob_l(wch, loc) wctob_l(wch, loc)
24-
#define __libcpp_wcsnrtombs_l(dst, src, nwc, len, ps, loc) wcsnrtombs_l(dst, src, nwc, len, ps, loc)
25-
#define __libcpp_wcrtomb_l(src, wc, ps, loc) wcrtomb_l(src, wc, ps, loc)
26-
#define __libcpp_mbsnrtowcs_l(dst, src, nms, len, ps, loc) mbsnrtowcs_l(dst, src, nms, len, ps, loc)
27-
#define __libcpp_mbrtowc_l(pwc, s, n, ps, l) mbrtowc_l(pwc, s, n, ps, l)
28-
#define __libcpp_mbtowc_l(pwc, pmb, max, l) mbtowc_l(pwc, pmb, max, l)
29-
#define __libcpp_mbrlen_l(s, n, ps, l) mbrlen_l(s, n, ps, l)
30-
#define __libcpp_localeconv_l(l) localeconv_l(l)
31-
#define __libcpp_mbsrtowcs_l(dest, src, len, ps, l) mbsrtowcs_l(dest, src, len, ps, l)
32-
#define __libcpp_snprintf_l(...) snprintf_l(__VA_ARGS__)
33-
#define __libcpp_asprintf_l(...) asprintf_l(__VA_ARGS__)
34-
#define __libcpp_sscanf_l(...) sscanf_l(__VA_ARGS__)
44+
_LIBCPP_BEGIN_NAMESPACE_STD
45+
46+
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __loc) { return MB_CUR_MAX_L(__loc); }
47+
48+
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
49+
inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, locale_t __loc) { return ::btowc_l(__c, __loc); }
50+
51+
inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, locale_t __loc) { return ::wctob_l(__c, __loc); }
52+
53+
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcsnrtombs_l(
54+
char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, locale_t __loc) {
55+
return ::wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __loc);
56+
}
57+
58+
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcrtomb_l(char* __s, wchar_t __wc, mbstate_t* __ps, locale_t __loc) {
59+
return ::wcrtomb_l(__s, __wc, __ps, __loc);
60+
}
61+
62+
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbsnrtowcs_l(
63+
wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, locale_t __loc) {
64+
return ::mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __loc);
65+
}
66+
67+
inline _LIBCPP_HIDE_FROM_ABI size_t
68+
__libcpp_mbrtowc_l(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, locale_t __loc) {
69+
return ::mbrtowc_l(__pwc, __s, __n, __ps, __loc);
70+
}
71+
72+
inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mbtowc_l(wchar_t* __pwc, const char* __pmb, size_t __max, locale_t __loc) {
73+
return ::mbtowc_l(__pwc, __pmb, __max, __loc);
74+
}
75+
76+
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbrlen_l(const char* __s, size_t __n, mbstate_t* __ps, locale_t __loc) {
77+
return ::mbrlen_l(__s, __n, __ps, __loc);
78+
}
79+
#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
80+
81+
inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(locale_t __loc) { return ::localeconv_l(__loc); }
82+
83+
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
84+
inline _LIBCPP_HIDE_FROM_ABI size_t
85+
__libcpp_mbsrtowcs_l(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, locale_t __loc) {
86+
return ::mbsrtowcs_l(__dest, __src, __len, __ps, __loc);
87+
}
88+
#endif
89+
90+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
91+
char* __s, size_t __n, locale_t __loc, const char* __format, ...) {
92+
va_list __va;
93+
va_start(__va, __format);
94+
int __res = ::vsnprintf_l(__s, __n, __loc, __format, __va);
95+
va_end(__va);
96+
return __res;
97+
}
98+
99+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
100+
char** __s, locale_t __loc, const char* __format, ...) {
101+
va_list __va;
102+
va_start(__va, __format);
103+
int __res = ::vasprintf_l(__s, __loc, __format, __va);
104+
va_end(__va);
105+
return __res;
106+
}
107+
108+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __libcpp_sscanf_l(
109+
const char* __s, locale_t __loc, const char* __format, ...) {
110+
va_list __va;
111+
va_start(__va, __format);
112+
int __res = ::vsscanf_l(__s, __loc, __format, __va);
113+
va_end(__va);
114+
return __res;
115+
}
116+
117+
_LIBCPP_END_NAMESPACE_STD
35118

36119
#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_DEFAULTS_H

libcxx/include/locale

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ template <class charT> class messages_byname;
215215
# include <streambuf>
216216
# include <version>
217217

218-
// TODO: Fix __bsd_locale_defaults.h
218+
// TODO: Properly qualify calls now that __bsd_locale_defaults.h defines functions instead of macros
219219
// NOLINTBEGIN(libcpp-robust-against-adl)
220220

221221
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
@@ -230,16 +230,6 @@ template <class charT> class messages_byname;
230230
# define _LIBCPP_HAS_CATOPEN 0
231231
# endif
232232

233-
# ifdef _LIBCPP_LOCALE__L_EXTENSIONS
234-
# include <__locale_dir/locale_base_api/bsd_locale_defaults.h>
235-
# else
236-
# include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
237-
# endif
238-
239-
# if defined(__APPLE__) || defined(__FreeBSD__)
240-
# include <xlocale.h>
241-
# endif
242-
243233
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
244234
# pragma GCC system_header
245235
# endif

libcxx/include/module.modulemap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,14 +1447,15 @@ module std [system] {
14471447
module locale_base_api {
14481448
textual header "__locale_dir/locale_base_api/android.h"
14491449
textual header "__locale_dir/locale_base_api/apple.h"
1450-
textual header "__locale_dir/locale_base_api/bsd_locale_defaults.h"
1450+
header "__locale_dir/locale_base_api/bsd_locale_defaults.h"
14511451
textual header "__locale_dir/locale_base_api/bsd_locale_fallbacks.h"
14521452
textual header "__locale_dir/locale_base_api/freebsd.h"
14531453
textual header "__locale_dir/locale_base_api/fuchsia.h"
14541454
textual header "__locale_dir/locale_base_api/ibm.h"
14551455
textual header "__locale_dir/locale_base_api/musl.h"
14561456
textual header "__locale_dir/locale_base_api/openbsd.h"
14571457
textual header "__locale_dir/locale_base_api/win32.h"
1458+
export *
14581459
}
14591460
export *
14601461
}

0 commit comments

Comments
 (0)