Skip to content

Commit

Permalink
[libc++] Rename locale_t to __libcpp_locale_t
Browse files Browse the repository at this point in the history
This is a step towards refactoring our amazingly complicated interface
with the system's localization library. Instead of using locale_t and
just assuming that the system provides it, use a proper libc++ internal
name (and introduce that name properly).
  • Loading branch information
ldionne committed Oct 24, 2024
1 parent 8aa69a0 commit 6904b61
Show file tree
Hide file tree
Showing 22 changed files with 236 additions and 166 deletions.
2 changes: 2 additions & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,10 @@ set(files
__locale
__locale_dir/locale_base_api.h
__locale_dir/locale_base_api/android.h
__locale_dir/locale_base_api/apple.h
__locale_dir/locale_base_api/bsd_locale_defaults.h
__locale_dir/locale_base_api/bsd_locale_fallbacks.h
__locale_dir/locale_base_api/freebsd.h
__locale_dir/locale_base_api/fuchsia.h
__locale_dir/locale_base_api/ibm.h
__locale_dir/locale_base_api/locale_guard.h
Expand Down
10 changes: 5 additions & 5 deletions libcxx/include/__locale
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class _LIBCPP_TEMPLATE_VIS collate_byname;

template <>
class _LIBCPP_EXPORTED_FROM_ABI collate_byname<char> : public collate<char> {
locale_t __l_;
__libcpp_locale_t __l_;

public:
typedef char char_type;
Expand All @@ -266,7 +266,7 @@ protected:
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
template <>
class _LIBCPP_EXPORTED_FROM_ABI collate_byname<wchar_t> : public collate<wchar_t> {
locale_t __l_;
__libcpp_locale_t __l_;

public:
typedef wchar_t char_type;
Expand Down Expand Up @@ -616,7 +616,7 @@ class _LIBCPP_TEMPLATE_VIS ctype_byname;

template <>
class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<char> : public ctype<char> {
locale_t __l_;
__libcpp_locale_t __l_;

public:
explicit ctype_byname(const char*, size_t = 0);
Expand All @@ -633,7 +633,7 @@ protected:
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
template <>
class _LIBCPP_EXPORTED_FROM_ABI ctype_byname<wchar_t> : public ctype<wchar_t> {
locale_t __l_;
__libcpp_locale_t __l_;

public:
explicit ctype_byname(const char*, size_t = 0);
Expand Down Expand Up @@ -824,7 +824,7 @@ protected:
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
template <>
class _LIBCPP_EXPORTED_FROM_ABI codecvt<wchar_t, char, mbstate_t> : public locale::facet, public codecvt_base {
locale_t __l_;
__libcpp_locale_t __l_;

public:
typedef wchar_t intern_type;
Expand Down
13 changes: 7 additions & 6 deletions libcxx/include/__locale_dir/locale_base_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H
#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_H

#include <__config>

#if defined(_LIBCPP_MSVCRT_LIKE)
# include <__locale_dir/locale_base_api/win32.h>
#elif defined(_AIX) || defined(__MVS__)
# include <__locale_dir/locale_base_api/ibm.h>
#elif defined(__ANDROID__)
# include <__locale_dir/locale_base_api/android.h>
#elif defined(__sun__)
# include <__locale_dir/locale_base_api/solaris.h>
#elif defined(_NEWLIB_VERSION)
# include <__locale_dir/locale_base_api/newlib.h>
#elif defined(__OpenBSD__)
Expand All @@ -25,8 +25,10 @@
# include <__locale_dir/locale_base_api/fuchsia.h>
#elif defined(__wasi__) || defined(_LIBCPP_HAS_MUSL_LIBC)
# include <__locale_dir/locale_base_api/musl.h>
#elif defined(__APPLE__) || defined(__FreeBSD__)
# include <xlocale.h>
#elif defined(__APPLE__)
# include <__locale_dir/locale_base_api/apple.h>
#elif defined(__FreeBSD__)
# include <__locale_dir/locale_base_api/freebsd.h>
#endif

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand All @@ -36,8 +38,7 @@
/*
The platform-specific headers have to provide the following interface:
// TODO: rename this to __libcpp_locale_t
using locale_t = implementation-defined;
using __libcpp_locale_t = implementation-defined;
implementation-defined __libcpp_mb_cur_max_l(locale_t);
wint_t __libcpp_btowc_l(int, locale_t);
Expand Down
6 changes: 4 additions & 2 deletions libcxx/include/__locale_dir/locale_base_api/android.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ extern "C" {
# include <__support/xlocale/__strtonum_fallback.h>
# elif __ANDROID_API__ < 26

inline _LIBCPP_HIDE_FROM_ABI float strtof_l(const char* __nptr, char** __endptr, locale_t) {
inline _LIBCPP_HIDE_FROM_ABI float strtof_l(const char* __nptr, char** __endptr, __libcpp_locale_t) {
return ::strtof(__nptr, __endptr);
}

inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr, locale_t) {
inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr, __libcpp_locale_t) {
return ::strtod(__nptr, __endptr);
}

Expand All @@ -47,4 +47,6 @@ inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr
# endif // __NDK_MAJOR__ <= 16
#endif // __has_include(<android/ndk-version.h>)

using __libcpp_locale_t = ::locale_t;

#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_ANDROID_H
17 changes: 17 additions & 0 deletions libcxx/include/__locale_dir/locale_base_api/apple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -*- C++ -*-
//===-----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___LOCALE_LOCALE_BASE_API_APPLE_H
#define _LIBCPP___LOCALE_LOCALE_BASE_API_APPLE_H

#include <xlocale.h>

using __libcpp_locale_t = locale_t;

#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_APPLE_H
35 changes: 19 additions & 16 deletions libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,74 @@

_LIBCPP_BEGIN_NAMESPACE_STD

inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(__libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return MB_CUR_MAX;
}

#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return btowc(__c);
}

inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return wctob(__c);
}

inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_wcsnrtombs_l(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcsnrtombs_l(
char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
}

inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_wcrtomb_l(char* __s, wchar_t __wc, mbstate_t* __ps, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_wcrtomb_l(char* __s, wchar_t __wc, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return wcrtomb(__s, __wc, __ps);
}

inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_mbsnrtowcs_l(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbsnrtowcs_l(
wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
}

inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_mbrtowc_l(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
__libcpp_mbrtowc_l(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return mbrtowc(__pwc, __s, __n, __ps);
}

inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mbtowc_l(wchar_t* __pwc, const char* __pmb, size_t __max, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI int
__libcpp_mbtowc_l(wchar_t* __pwc, const char* __pmb, size_t __max, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return mbtowc(__pwc, __pmb, __max);
}

inline _LIBCPP_HIDE_FROM_ABI size_t __libcpp_mbrlen_l(const char* __s, size_t __n, mbstate_t* __ps, locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_mbrlen_l(const char* __s, size_t __n, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return mbrlen(__s, __n, __ps);
}
#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS

inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(locale_t __l) {
inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(__libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return localeconv();
}

#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
inline _LIBCPP_HIDE_FROM_ABI size_t
__libcpp_mbsrtowcs_l(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, locale_t __l) {
__libcpp_mbsrtowcs_l(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __libcpp_locale_t __l) {
__libcpp_locale_guard __current(__l);
return mbsrtowcs(__dest, __src, __len, __ps);
}
#endif

inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
char* __s, size_t __n, locale_t __l, const char* __format, ...) {
char* __s, size_t __n, __libcpp_locale_t __l, const char* __format, ...) {
va_list __va;
va_start(__va, __format);
__libcpp_locale_guard __current(__l);
Expand All @@ -102,7 +105,7 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
}

inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
char** __s, locale_t __l, const char* __format, ...) {
char** __s, __libcpp_locale_t __l, const char* __format, ...) {
va_list __va;
va_start(__va, __format);
__libcpp_locale_guard __current(__l);
Expand All @@ -112,7 +115,7 @@ inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
}

inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __libcpp_sscanf_l(
const char* __s, locale_t __l, const char* __format, ...) {
const char* __s, __libcpp_locale_t __l, const char* __format, ...) {
va_list __va;
va_start(__va, __format);
__libcpp_locale_guard __current(__l);
Expand Down
17 changes: 17 additions & 0 deletions libcxx/include/__locale_dir/locale_base_api/freebsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -*- C++ -*-
//===-----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___LOCALE_LOCALE_BASE_API_FREEBSD_H
#define _LIBCPP___LOCALE_LOCALE_BASE_API_FREEBSD_H

#include <xlocale.h>

using __libcpp_locale_t = locale_t;

#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_FREEBSD_H
2 changes: 2 additions & 0 deletions libcxx/include/__locale_dir/locale_base_api/fuchsia.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
#include <cstdlib>
#include <cwchar>

using __libcpp_locale_t = locale_t;

#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_FUCHSIA_H
23 changes: 13 additions & 10 deletions libcxx/include/__locale_dir/locale_base_api/ibm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
# include <__support/xlocale/__posix_l_fallback.h>
#endif // defined(__MVS__)

using __libcpp_locale_t = locale_t;

namespace {

struct __setAndRestore {
explicit __setAndRestore(locale_t locale) {
if (locale == (locale_t)0) {
__cloc = newlocale(LC_ALL_MASK, "C", /* base */ (locale_t)0);
explicit __setAndRestore(__libcpp_locale_t locale) {
if (locale == (__libcpp_locale_t)0) {
__cloc = newlocale(LC_ALL_MASK, "C", /* base */ (__libcpp_locale_t)0);
__stored = uselocale(__cloc);
} else {
__stored = uselocale(locale);
Expand All @@ -45,36 +47,37 @@ struct __setAndRestore {
}

private:
locale_t __stored = (locale_t)0;
locale_t __cloc = (locale_t)0;
__libcpp_locale_t __stored = (__libcpp_locale_t)0;
__libcpp_locale_t __cloc = (__libcpp_locale_t)0;
};

} // namespace

// The following are not POSIX routines. These are quick-and-dirty hacks
// to make things pretend to work
inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
inline _LIBCPP_HIDE_FROM_ABI long long
strtoll_l(const char* __nptr, char** __endptr, int __base, __libcpp_locale_t locale) {
__setAndRestore __newloc(locale);
return ::strtoll(__nptr, __endptr, __base);
}

inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr, locale_t locale) {
inline _LIBCPP_HIDE_FROM_ABI double strtod_l(const char* __nptr, char** __endptr, __libcpp_locale_t locale) {
__setAndRestore __newloc(locale);
return ::strtod(__nptr, __endptr);
}

inline _LIBCPP_HIDE_FROM_ABI float strtof_l(const char* __nptr, char** __endptr, locale_t locale) {
inline _LIBCPP_HIDE_FROM_ABI float strtof_l(const char* __nptr, char** __endptr, __libcpp_locale_t locale) {
__setAndRestore __newloc(locale);
return ::strtof(__nptr, __endptr);
}

inline _LIBCPP_HIDE_FROM_ABI long double strtold_l(const char* __nptr, char** __endptr, locale_t locale) {
inline _LIBCPP_HIDE_FROM_ABI long double strtold_l(const char* __nptr, char** __endptr, __libcpp_locale_t locale) {
__setAndRestore __newloc(locale);
return ::strtold(__nptr, __endptr);
}

inline _LIBCPP_HIDE_FROM_ABI unsigned long long
strtoull_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
strtoull_l(const char* __nptr, char** __endptr, int __base, __libcpp_locale_t locale) {
__setAndRestore __newloc(locale);
return ::strtoull(__nptr, __endptr, __base);
}
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__locale_dir/locale_base_api/locale_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H

#include <__config>
#include <__locale> // for locale_t
#include <__locale> // for __libcpp_locale_t
#include <clocale>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand All @@ -21,21 +21,21 @@ _LIBCPP_BEGIN_NAMESPACE_STD

#if !defined(_LIBCPP_LOCALE__L_EXTENSIONS)
struct __libcpp_locale_guard {
_LIBCPP_HIDE_FROM_ABI __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
_LIBCPP_HIDE_FROM_ABI __libcpp_locale_guard(__libcpp_locale_t& __loc) : __old_loc_(uselocale(__loc)) {}

_LIBCPP_HIDE_FROM_ABI ~__libcpp_locale_guard() {
if (__old_loc_)
uselocale(__old_loc_);
}

locale_t __old_loc_;
__libcpp_locale_t __old_loc_;

__libcpp_locale_guard(__libcpp_locale_guard const&) = delete;
__libcpp_locale_guard& operator=(__libcpp_locale_guard const&) = delete;
};
#elif defined(_LIBCPP_MSVCRT_LIKE)
struct __libcpp_locale_guard {
__libcpp_locale_guard(locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
__libcpp_locale_guard(__libcpp_locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
// Setting the locale can be expensive even when the locale given is
// already the current locale, so do an explicit check to see if the
// current locale is already the one we want.
Expand Down
7 changes: 5 additions & 2 deletions libcxx/include/__locale_dir/locale_base_api/musl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#include <cstdlib>
#include <cwchar>

inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, locale_t) {
using __libcpp_locale_t = locale_t;

inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, __libcpp_locale_t) {
return ::strtoll(__nptr, __endptr, __base);
}

inline _LIBCPP_HIDE_FROM_ABI unsigned long long strtoull_l(const char* __nptr, char** __endptr, int __base, locale_t) {
inline _LIBCPP_HIDE_FROM_ABI unsigned long long
strtoull_l(const char* __nptr, char** __endptr, int __base, __libcpp_locale_t) {
return ::strtoull(__nptr, __endptr, __base);
}

Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__locale_dir/locale_base_api/openbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
#include <ctype.h>
#include <cwctype>

using __libcpp_locale_t = locale_t;

#endif // _LIBCPP___LOCALE_LOCALE_BASE_API_OPENBSD_H
Loading

0 comments on commit 6904b61

Please sign in to comment.