Skip to content

[libc++] Reduce the dependency of the locale base API on the base system from the headers #117764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions libcxx/include/__locale_dir/locale_base_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@
// Variadic functions may be implemented as templates with a parameter pack instead
// of C-style variadic functions.
//
// Most of these functions are only required when building the library. Functions that are also
// required when merely using the headers are marked as such below.
//
// TODO: __localeconv shouldn't take a reference, but the Windows implementation doesn't allow copying __locale_t
// TODO: Eliminate the need for any of these functions from the headers.
//
// Locale management
// -----------------
// namespace __locale {
// using __locale_t = implementation-defined;
// using __locale_t = implementation-defined; // required by the headers
// using __lconv_t = implementation-defined;
// __locale_t __newlocale(int, const char*, __locale_t);
// void __freelocale(__locale_t);
// char* __setlocale(int, const char*);
// __lconv_t* __localeconv(__locale_t&);
// }
//
// // required by the headers
// #define _LIBCPP_COLLATE_MASK /* implementation-defined */
// #define _LIBCPP_CTYPE_MASK /* implementation-defined */
// #define _LIBCPP_MONETARY_MASK /* implementation-defined */
Expand All @@ -48,6 +53,7 @@
// Strtonum functions
// ------------------
// namespace __locale {
// // required by the headers
// float __strtof(const char*, char**, __locale_t);
// double __strtod(const char*, char**, __locale_t);
// long double __strtold(const char*, char**, __locale_t);
Expand All @@ -60,8 +66,8 @@
// namespace __locale {
// int __islower(int, __locale_t);
// int __isupper(int, __locale_t);
// int __isdigit(int, __locale_t);
// int __isxdigit(int, __locale_t);
// int __isdigit(int, __locale_t); // required by the headers
// int __isxdigit(int, __locale_t); // required by the headers
// int __toupper(int, __locale_t);
// int __tolower(int, __locale_t);
// int __strcoll(const char*, const char*, __locale_t);
Expand Down Expand Up @@ -99,9 +105,10 @@
// int __mbtowc(wchar_t*, const char*, size_t, __locale_t);
// size_t __mbrlen(const char*, size_t, mbstate_t*, __locale_t);
// size_t __mbsrtowcs(wchar_t*, const char**, size_t, mbstate_t*, __locale_t);
// int __snprintf(char*, size_t, __locale_t, const char*, ...);
// int __asprintf(char**, __locale_t, const char*, ...);
// int __sscanf(const char*, __locale_t, const char*, ...);
//
// int __snprintf(char*, size_t, __locale_t, const char*, ...); // required by the headers
// int __asprintf(char**, __locale_t, const char*, ...); // required by the headers
// int __sscanf(const char*, __locale_t, const char*, ...); // required by the headers
// }

#if defined(__APPLE__)
Expand Down Expand Up @@ -143,8 +150,19 @@ namespace __locale {
//
// Locale management
//
# define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK
# define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK
# define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK
# define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK
# define _LIBCPP_TIME_MASK LC_TIME_MASK
# define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK
# define _LIBCPP_ALL_MASK LC_ALL_MASK
# define _LIBCPP_LC_ALL LC_ALL

using __locale_t _LIBCPP_NODEBUG = locale_t;
using __lconv_t _LIBCPP_NODEBUG = lconv;

# if defined(_LIBCPP_BUILDING_LIBRARY)
using __lconv_t _LIBCPP_NODEBUG = lconv;

inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) {
return newlocale(__category_mask, __name, __loc);
Expand All @@ -157,15 +175,7 @@ inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __loc
inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { freelocale(__loc); }

inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) { return __libcpp_localeconv_l(__loc); }

# define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK
# define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK
# define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK
# define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK
# define _LIBCPP_TIME_MASK LC_TIME_MASK
# define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK
# define _LIBCPP_ALL_MASK LC_ALL_MASK
# define _LIBCPP_LC_ALL LC_ALL
# endif // _LIBCPP_BUILDING_LIBRARY

//
// Strtonum functions
Expand Down Expand Up @@ -194,10 +204,15 @@ __strtoull(const char* __nptr, char** __endptr, int __base, __locale_t __loc) {
//
// Character manipulation functions
//
# if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __islower(int __ch, __locale_t __loc) { return islower_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI int __isupper(int __ch, __locale_t __loc) { return isupper_l(__ch, __loc); }
# endif

inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __ch, __locale_t __loc) { return isdigit_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __ch, __locale_t __loc) { return isxdigit_l(__ch, __loc); }

# if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) {
return strcoll_l(__s1, __s2, __loc);
}
Expand All @@ -207,7 +222,7 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, s
inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __ch, __locale_t __loc) { return toupper_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __ch, __locale_t __loc) { return tolower_l(__ch, __loc); }

# if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __s1, const wchar_t* __s2, __locale_t __loc) {
return wcscoll_l(__s1, __s2, __loc);
}
Expand All @@ -229,7 +244,7 @@ inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __ch, __locale_t __loc) { ret
inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __ch, __locale_t __loc) { return iswxdigit_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __ch, __locale_t __loc) { return towupper_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __ch, __locale_t __loc) { return towlower_l(__ch, __loc); }
# endif
# endif

inline _LIBCPP_HIDE_FROM_ABI size_t
__strftime(char* __s, size_t __max, const char* __format, const tm* __tm, __locale_t __loc) {
Expand All @@ -242,7 +257,7 @@ __strftime(char* __s, size_t __max, const char* __format, const tm* __tm, __loca
inline _LIBCPP_HIDE_FROM_ABI decltype(__libcpp_mb_cur_max_l(__locale_t())) __mb_len_max(__locale_t __loc) {
return __libcpp_mb_cur_max_l(__loc);
}
# if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __ch, __locale_t __loc) { return __libcpp_btowc_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __ch, __locale_t __loc) { return __libcpp_wctob_l(__ch, __loc); }
inline _LIBCPP_HIDE_FROM_ABI size_t
Expand Down Expand Up @@ -270,7 +285,8 @@ inline _LIBCPP_HIDE_FROM_ABI size_t
__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return __libcpp_mbsrtowcs_l(__dest, __src, __len, __ps, __loc);
}
# endif
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
# endif // _LIBCPP_BUILDING_LIBRARY

_LIBCPP_DIAGNOSTIC_PUSH
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
Expand Down
22 changes: 14 additions & 8 deletions libcxx/include/__locale_dir/support/bsd_like.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ namespace __locale {
#define _LIBCPP_LC_ALL LC_ALL

using __locale_t = ::locale_t;
using __lconv_t = std::lconv;
#if defined(_LIBCPP_BUILDING_LIBRARY)
using __lconv_t = std::lconv;

inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) {
return ::newlocale(__category_mask, __locale, __base);
Expand All @@ -59,6 +60,7 @@ inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __loc
}

inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) { return ::localeconv_l(__loc); }
#endif // _LIBCPP_BUILDING_LIBRARY

//
// Strtonum functions
Expand Down Expand Up @@ -87,14 +89,17 @@ __strtoull(const char* __nptr, char** __endptr, int __base, __locale_t __loc) {
//
// Character manipulation functions
//
#if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __islower(int __c, __locale_t __loc) { return ::islower_l(__c, __loc); }

inline _LIBCPP_HIDE_FROM_ABI int __isupper(int __c, __locale_t __loc) { return ::isupper_l(__c, __loc); }
#endif

inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __c, __locale_t __loc) { return ::isdigit_l(__c, __loc); }

inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __c, __locale_t __loc) { return ::isxdigit_l(__c, __loc); }

#if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t __loc) { return ::toupper_l(__c, __loc); }

inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t __loc) { return ::tolower_l(__c, __loc); }
Expand All @@ -107,7 +112,7 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, s
return ::strxfrm_l(__dest, __src, __n, __loc);
}

#if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) {
return ::iswctype_l(__c, __type, __loc);
}
Expand Down Expand Up @@ -143,7 +148,7 @@ inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t*
inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) {
return ::wcsxfrm_l(__dest, __src, __n, __loc);
}
#endif // _LIBCPP_HAS_WIDE_CHARACTERS
# endif // _LIBCPP_HAS_WIDE_CHARACTERS

inline _LIBCPP_HIDE_FROM_ABI size_t
__strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) {
Expand All @@ -155,14 +160,14 @@ __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm,
//
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { return MB_CUR_MAX_L(__loc); }

#if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __c, __locale_t __loc) { return ::btowc_l(__c, __loc); }

inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __c, __locale_t __loc) { return ::wctob_l(__c, __loc); }

inline _LIBCPP_HIDE_FROM_ABI size_t
__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return ::wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __loc);
return ::wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __loc); // wcsnrtombs is a POSIX extension
}

inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) {
Expand All @@ -171,7 +176,7 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t

inline _LIBCPP_HIDE_FROM_ABI size_t
__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return ::mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __loc);
return ::mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __loc); // mbsnrtowcs is a POSIX extension
}

inline _LIBCPP_HIDE_FROM_ABI size_t
Expand All @@ -191,7 +196,8 @@ inline _LIBCPP_HIDE_FROM_ABI size_t
__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return ::mbsrtowcs_l(__dest, __src, __len, __ps, __loc);
}
#endif
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
#endif // _LIBCPP_BUILDING_LIBRARY

_LIBCPP_DIAGNOSTIC_PUSH
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
Expand All @@ -211,7 +217,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __
template <class... _Args>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
char** __s, __locale_t __loc, const char* __format, _Args&&... __args) {
return ::asprintf_l(__s, __loc, __format, std::forward<_Args>(__args)...);
return ::asprintf_l(__s, __loc, __format, std::forward<_Args>(__args)...); // non-standard
}

template <class... _Args>
Expand Down
9 changes: 6 additions & 3 deletions libcxx/include/__locale_dir/support/fuchsia.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ struct __locale_guard {
#define _LIBCPP_LC_ALL LC_ALL

using __locale_t = locale_t;
using __lconv_t = std::lconv;

#if defined(_LIBCPP_BUILDING_LIBRARY)
using __lconv_t = std::lconv;

inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) {
return ::newlocale(__category_mask, __name, __loc);
Expand All @@ -74,7 +76,7 @@ inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc)
__locale_guard __current(__loc);
return MB_CUR_MAX;
}
#if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __ch, __locale_t __loc) {
__locale_guard __current(__loc);
return std::btowc(__ch);
Expand Down Expand Up @@ -115,7 +117,8 @@ __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps,
__locale_guard __current(__loc);
return ::mbsrtowcs(__dest, __src, __len, __ps);
}
#endif
# endif // _LIBCPP_HAS_WIDE_CHARACTERS
#endif // _LIBCPP_BUILDING_LIBRARY

_LIBCPP_DIAGNOSTIC_PUSH
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
Expand Down
8 changes: 6 additions & 2 deletions libcxx/include/__locale_dir/support/no_locale/characters.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ namespace __locale {
//
// Character manipulation functions
//
#if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __islower(int __c, __locale_t) { return std::islower(__c); }

inline _LIBCPP_HIDE_FROM_ABI int __isupper(int __c, __locale_t) { return std::isupper(__c); }
#endif

inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __c, __locale_t) { return std::isdigit(__c); }

inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __c, __locale_t) { return std::isxdigit(__c); }

#if defined(_LIBCPP_BUILDING_LIBRARY)
inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t) { return std::toupper(__c); }

inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t) { return std::tolower(__c); }
Expand All @@ -49,7 +52,7 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, s
return std::strxfrm(__dest, __src, __n);
}

#if _LIBCPP_HAS_WIDE_CHARACTERS
# if _LIBCPP_HAS_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t) {
return std::iswctype(__c, __type);
}
Expand Down Expand Up @@ -85,12 +88,13 @@ inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t*
inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t) {
return std::wcsxfrm(__dest, __src, __n);
}
#endif // _LIBCPP_HAS_WIDE_CHARACTERS
# endif // _LIBCPP_HAS_WIDE_CHARACTERS

inline _LIBCPP_HIDE_FROM_ABI size_t
__strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t) {
return std::strftime(__s, __max, __format, __tm);
}
#endif // _LIBCPP_BUILDING_LIBRARY

} // namespace __locale
_LIBCPP_END_NAMESPACE_STD
Expand Down
Loading
Loading