[libcxx-commits] [libcxx] [libc++] Update z/OS to use the new locale API (PR #198614)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Wed May 20 01:32:05 PDT 2026
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/198614
>From 6a04c0535861e8a7b4d46050b269d02787c19b7a Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Tue, 19 May 2026 21:00:40 +0200
Subject: [PATCH] [libc++] Update z/OS to use the new locale API
---
libcxx/include/CMakeLists.txt | 5 +-
libcxx/include/__locale_dir/locale_base_api.h | 171 +---------
.../locale_base_api/bsd_locale_fallbacks.h | 130 -------
.../__locale_dir/locale_base_api/ibm.h | 97 ------
libcxx/include/__locale_dir/support/mvs.h | 316 ++++++++++++++++++
.../include/__support/ibm/locale_mgmt_zos.h | 53 ---
.../__support/xlocale/__posix_l_fallback.h | 91 -----
libcxx/include/module.modulemap.in | 6 +-
libcxx/src/support/ibm/xlocale_zos.cpp | 2 +-
9 files changed, 322 insertions(+), 549 deletions(-)
delete mode 100644 libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
delete mode 100644 libcxx/include/__locale_dir/locale_base_api/ibm.h
create mode 100644 libcxx/include/__locale_dir/support/mvs.h
delete mode 100644 libcxx/include/__support/ibm/locale_mgmt_zos.h
delete mode 100644 libcxx/include/__support/xlocale/__posix_l_fallback.h
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 4ca7653026e47..f304b904d43ed 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -531,8 +531,6 @@ set(files
__locale_dir/check_grouping.h
__locale_dir/get_c_locale.h
__locale_dir/locale_base_api.h
- __locale_dir/locale_base_api/bsd_locale_fallbacks.h
- __locale_dir/locale_base_api/ibm.h
__locale_dir/messages.h
__locale_dir/money.h
__locale_dir/num.h
@@ -545,6 +543,7 @@ set(files
__locale_dir/support/fuchsia.h
__locale_dir/support/linux.h
__locale_dir/support/llvm_libc.h
+ __locale_dir/support/mvs.h
__locale_dir/support/netbsd.h
__locale_dir/support/newlib.h
__locale_dir/support/no_locale/characters.h
@@ -779,9 +778,7 @@ set(files
__string/constexpr_c_functions.h
__string/extern_template_lists.h
__support/ibm/gettod_zos.h
- __support/ibm/locale_mgmt_zos.h
__support/ibm/nanosleep.h
- __support/xlocale/__posix_l_fallback.h
__system_error/errc.h
__system_error/error_category.h
__system_error/error_code.h
diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
index e1e60e18fd7c6..6375fac4ee78b 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -126,175 +126,10 @@
# include <__locale_dir/support/newlib.h>
# elif defined(_AIX)
# include <__locale_dir/support/aix.h>
+# elif defined(__MVS__)
+# include <__locale_dir/support/mvs.h>
# else
-
-// TODO: This is a temporary definition to bridge between the old way we defined the locale base API
-// (by providing global non-reserved names) and the new API. As we move individual platforms
-// towards the new way of defining the locale base API, this should disappear since each platform
-// will define those directly.
-# include <__locale_dir/locale_base_api/ibm.h>
-
-# include <__locale_dir/locale_base_api/bsd_locale_fallbacks.h>
-
-# include <__cstddef/size_t.h>
-# include <__utility/forward.h>
-# include <ctype.h>
-# include <string.h>
-# include <time.h>
-# if _LIBCPP_HAS_WIDE_CHARACTERS
-# include <wctype.h>
-# endif
-_LIBCPP_BEGIN_NAMESPACE_STD
-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;
-
-# 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);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __locale) {
- return ::setlocale(__category, __locale);
-}
-
-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); }
-# endif // _LIBCPP_BUILDING_LIBRARY
-
-//
-// Strtonum functions
-//
-inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) {
- return strtof_l(__nptr, __endptr, __loc);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) {
- return strtod_l(__nptr, __endptr, __loc);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) {
- return strtold_l(__nptr, __endptr, __loc);
-}
-
-//
-// Character manipulation functions
-//
-# 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);
-}
-inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) {
- return strxfrm_l(__dest, __src, __n, __loc);
-}
-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
-inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __s1, const wchar_t* __s2, __locale_t __loc) {
- return wcscoll_l(__s1, __s2, __loc);
-}
-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);
-}
-inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __ch, wctype_t __type, __locale_t __loc) {
- return iswctype_l(__ch, __type, __loc);
-}
-inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __ch, __locale_t __loc) { return iswspace_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __ch, __locale_t __loc) { return iswprint_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __ch, __locale_t __loc) { return iswcntrl_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __ch, __locale_t __loc) { return iswupper_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __ch, __locale_t __loc) { return iswlower_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __ch, __locale_t __loc) { return iswalpha_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __ch, __locale_t __loc) { return iswblank_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __ch, __locale_t __loc) { return iswdigit_l(__ch, __loc); }
-inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __ch, __locale_t __loc) { return iswpunct_l(__ch, __loc); }
-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
-
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_ATTRIBUTE_FORMAT(__strftime__, 3, 0) size_t
- __strftime(char* __s, size_t __max, const char* __format, const tm* __tm, __locale_t __loc) {
- return strftime_l(__s, __max, __format, __tm, __loc);
-}
-
-//
-// Other functions
-//
-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
-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
-__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
- return __libcpp_wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __loc);
-}
-inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __ch, mbstate_t* __ps, __locale_t __loc) {
- return __libcpp_wcrtomb_l(__s, __ch, __ps, __loc);
-}
-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 __libcpp_mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __loc);
-}
-inline _LIBCPP_HIDE_FROM_ABI size_t
-__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
- return __libcpp_mbrtowc_l(__pwc, __s, __n, __ps, __loc);
-}
-inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
- return __libcpp_mbtowc_l(__pwc, __pmb, __max, __loc);
-}
-inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
- return __libcpp_mbrlen_l(__s, __n, __ps, __loc);
-}
-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 // _LIBCPP_HAS_WIDE_CHARACTERS
-# endif // _LIBCPP_BUILDING_LIBRARY
-
-_LIBCPP_DIAGNOSTIC_PUSH
-_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
-_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates
-# ifdef _LIBCPP_COMPILER_CLANG_BASED
-# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__)
-# else
-# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */
-# endif
-
-template <class... _Args>
-_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
- char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) {
- return std::__libcpp_snprintf_l(__s, __n, __loc, __format, std::forward<_Args>(__args)...);
-}
-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 std::__libcpp_asprintf_l(__s, __loc, __format, std::forward<_Args>(__args)...);
-}
-_LIBCPP_DIAGNOSTIC_POP
-# undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
-
-} // namespace __locale
-_LIBCPP_END_NAMESPACE_STD
-
+# warning "No known way to provide the locale base API"
# endif // Compatibility definition of locale base APIs
#endif // _LIBCPP_HAS_LOCALIZATION
diff --git a/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h b/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
deleted file mode 100644
index 8cdbe0cd15051..0000000000000
--- a/libcxx/include/__locale_dir/locale_base_api/bsd_locale_fallbacks.h
+++ /dev/null
@@ -1,130 +0,0 @@
-// -*- 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
-//
-//===----------------------------------------------------------------------===//
-// The BSDs have lots of *_l functions. This file provides reimplementations
-// of those functions for non-BSD platforms.
-//===----------------------------------------------------------------------===//
-
-#ifndef _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
-#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
-
-#include <locale.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#if _LIBCPP_HAS_WIDE_CHARACTERS
-# include <cwchar>
-#endif
-
-#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-# pragma GCC system_header
-#endif
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-struct __locale_guard {
- _LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
-
- _LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
- if (__old_loc_)
- ::uselocale(__old_loc_);
- }
-
- locale_t __old_loc_;
-
- __locale_guard(__locale_guard const&) = delete;
- __locale_guard& operator=(__locale_guard const&) = delete;
-};
-
-inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l) {
- __locale_guard __current(__l);
- return MB_CUR_MAX;
-}
-
-#if _LIBCPP_HAS_WIDE_CHARACTERS
-inline _LIBCPP_HIDE_FROM_ABI wint_t __libcpp_btowc_l(int __c, locale_t __l) {
- __locale_guard __current(__l);
- return btowc(__c);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI int __libcpp_wctob_l(wint_t __c, locale_t __l) {
- __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) {
- __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) {
- __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) {
- __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) {
- __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) {
- __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) {
- __locale_guard __current(__l);
- return mbrlen(__s, __n, __ps);
-}
-#endif // _LIBCPP_HAS_WIDE_CHARACTERS
-
-inline _LIBCPP_HIDE_FROM_ABI lconv* __libcpp_localeconv_l(locale_t& __l) {
- __locale_guard __current(__l);
- return localeconv();
-}
-
-#if _LIBCPP_HAS_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) {
- __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, ...) {
- va_list __va;
- va_start(__va, __format);
- __locale_guard __current(__l);
- int __res = vsnprintf(__s, __n, __format, __va);
- va_end(__va);
- return __res;
-}
-
-inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
- char** __s, locale_t __l, const char* __format, ...) {
- va_list __va;
- va_start(__va, __format);
- __locale_guard __current(__l);
- int __res = vasprintf(__s, __format, __va);
- va_end(__va);
- return __res;
-}
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_BSD_LOCALE_FALLBACKS_H
diff --git a/libcxx/include/__locale_dir/locale_base_api/ibm.h b/libcxx/include/__locale_dir/locale_base_api/ibm.h
deleted file mode 100644
index 47a83eac7df36..0000000000000
--- a/libcxx/include/__locale_dir/locale_base_api/ibm.h
+++ /dev/null
@@ -1,97 +0,0 @@
-// -*- 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_DIR_LOCALE_BASE_API_IBM_H
-#define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_IBM_H
-
-#if defined(__MVS__)
-# include <__support/ibm/locale_mgmt_zos.h>
-#endif // defined(__MVS__)
-
-#include <locale.h>
-#include <stdarg.h>
-#include <stdio.h>
-
-#include "cstdlib"
-
-#if defined(__MVS__)
-# include <wctype.h>
-// POSIX routines
-# include <__support/xlocale/__posix_l_fallback.h>
-#endif // defined(__MVS__)
-
-namespace {
-
-struct __setAndRestore {
- explicit __setAndRestore(locale_t locale) {
- if (locale == (locale_t)0) {
- __cloc = newlocale(LC_ALL_MASK, "C", /* base */ (locale_t)0);
- __stored = uselocale(__cloc);
- } else {
- __stored = uselocale(locale);
- }
- }
-
- ~__setAndRestore() {
- uselocale(__stored);
- if (__cloc)
- freelocale(__cloc);
- }
-
-private:
- locale_t __stored = (locale_t)0;
- locale_t __cloc = (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 double strtod_l(const char* __nptr, char** __endptr, 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) {
- __setAndRestore __newloc(locale);
- return ::strtof(__nptr, __endptr);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI long double strtold_l(const char* __nptr, char** __endptr, locale_t locale) {
- __setAndRestore __newloc(locale);
- return ::strtold(__nptr, __endptr);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI
-_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 2, 0) int vasprintf(char** strp, const char* fmt, va_list ap) {
- const size_t buff_size = 256;
- if ((*strp = (char*)malloc(buff_size)) == nullptr) {
- return -1;
- }
-
- va_list ap_copy;
- // va_copy may not be provided by the C library in C++03 mode.
-#if defined(_LIBCPP_CXX03_LANG) && __has_builtin(__builtin_va_copy)
- __builtin_va_copy(ap_copy, ap);
-#else
- va_copy(ap_copy, ap);
-#endif
- int str_size = vsnprintf(*strp, buff_size, fmt, ap_copy);
- va_end(ap_copy);
-
- if ((size_t)str_size >= buff_size) {
- if ((*strp = (char*)realloc(*strp, str_size + 1)) == nullptr) {
- return -1;
- }
- str_size = vsnprintf(*strp, str_size + 1, fmt, ap);
- }
- return str_size;
-}
-
-#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_IBM_H
diff --git a/libcxx/include/__locale_dir/support/mvs.h b/libcxx/include/__locale_dir/support/mvs.h
new file mode 100644
index 0000000000000..dc69ba1f13109
--- /dev/null
+++ b/libcxx/include/__locale_dir/support/mvs.h
@@ -0,0 +1,316 @@
+// -*- 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_DIR_SUPPORT_MVS_H
+#define _LIBCPP___LOCALE_DIR_SUPPORT_MVS_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <__utility/forward.h>
+#include <cstdlib>
+#include <ctype.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <wctype.h>
+
+#if _LIBCPP_HAS_WIDE_CHARACTERS
+# include <wchar.h>
+# include <wctype.h>
+#endif
+
+#include <string>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+typedef struct locale_struct {
+ int category_mask;
+ std::string lc_collate;
+ std::string lc_ctype;
+ std::string lc_monetary;
+ std::string lc_numeric;
+ std::string lc_time;
+ std::string lc_messages;
+}* locale_t;
+
+// z/OS does not have newlocale, freelocale and uselocale.
+// The functions below are workarounds in single thread mode.
+locale_t newlocale(int category_mask, const char* locale, locale_t base);
+void freelocale(locale_t locobj);
+locale_t uselocale(locale_t newloc);
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __locale {
+
+struct __setAndRestore {
+ explicit __setAndRestore(locale_t locale) {
+ if (locale == (locale_t)0) {
+ __cloc = newlocale(LC_ALL_MASK, "C", /* base */ (locale_t)0);
+ __stored = uselocale(__cloc);
+ } else {
+ __stored = uselocale(locale);
+ }
+ }
+
+ ~__setAndRestore() {
+ uselocale(__stored);
+ if (__cloc)
+ freelocale(__cloc);
+ }
+
+private:
+ locale_t __stored = (locale_t)0;
+ locale_t __cloc = (locale_t)0;
+};
+
+struct __locale_guard {
+ __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {}
+
+ ~__locale_guard() {
+ if (__old_loc_)
+ ::uselocale(__old_loc_);
+ }
+
+ locale_t __old_loc_;
+
+ __locale_guard(__locale_guard const&) = delete;
+ __locale_guard& operator=(__locale_guard const&) = delete;
+};
+
+//
+// Locale management
+//
+
+#define _LC_MAX LC_MESSAGES /* highest real category */
+#define _NCAT (_LC_MAX + 1) /* maximum + 1 */
+#define _CATMASK(n) (1 << (n))
+
+#define _LIBCPP_COLLATE_MASK _CATMASK(LC_COLLATE)
+#define _LIBCPP_CTYPE_MASK _CATMASK(LC_CTYPE)
+#define _LIBCPP_MONETARY_MASK _CATMASK(LC_MONETARY)
+#define _LIBCPP_NUMERIC_MASK _CATMASK(LC_NUMERIC)
+#define _LIBCPP_TIME_MASK _CATMASK(LC_TIME)
+#define _LIBCPP_MESSAGES_MASK _CATMASK(LC_MESSAGES)
+#define _LIBCPP_ALL_MASK (_CATMASK(_NCAT) - 1)
+#define _LIBCPP_LC_ALL LC_ALL
+
+using __locale_t _LIBCPP_NODEBUG = locale_t;
+
+#if defined(_LIBCPP_BUILDING_LIBRARY)
+using __lconv_t _LIBCPP_NODEBUG = lconv;
+
+inline __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) {
+ return newlocale(__category_mask, __name, __loc);
+}
+
+inline char* __setlocale(int __category, char const* __locale) { return ::setlocale(__category, __locale); }
+
+inline void __freelocale(__locale_t __loc) { freelocale(__loc); }
+
+inline __lconv_t* __localeconv(__locale_t& __loc) {
+ __locale_guard __current(__loc);
+ return localeconv();
+}
+#endif // _LIBCPP_BUILDING_LIBRARY
+
+//
+// Strtonum functions
+//
+inline float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) {
+ __setAndRestore __newloc(__loc);
+ return ::strtof(__nptr, __endptr);
+}
+
+inline double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) {
+ __setAndRestore __newloc(__loc);
+ return ::strtod(__nptr, __endptr);
+}
+
+inline long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) {
+ __setAndRestore __newloc(__loc);
+ return ::strtold(__nptr, __endptr);
+}
+
+//
+// Character manipulation functions
+//
+#if defined(_LIBCPP_BUILDING_LIBRARY)
+inline int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) { return ::strcoll(__s1, __s2); }
+
+inline size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) {
+ return ::strxfrm(__dest, __src, __n);
+}
+
+inline int __toupper(int __ch, __locale_t __loc) { return ::toupper(__ch); }
+inline int __tolower(int __ch, __locale_t __loc) { return ::tolower(__ch); }
+
+# if _LIBCPP_HAS_WIDE_CHARACTERS
+inline int __wcscoll(const wchar_t* __s1, const wchar_t* __s2, __locale_t __loc) { return ::wcscoll(__s1, __s2); }
+
+inline size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) {
+ return ::wcsxfrm(__dest, __src, __n);
+}
+
+inline int __iswctype(wint_t __ch, wctype_t __type, __locale_t __loc) { return ::iswctype(__ch, __type); }
+inline int __iswspace(wint_t __ch, __locale_t __loc) { return ::iswspace(__ch); }
+inline int __iswprint(wint_t __ch, __locale_t __loc) { return ::iswprint(__ch); }
+inline int __iswcntrl(wint_t __ch, __locale_t __loc) { return ::iswcntrl(__ch); }
+inline int __iswupper(wint_t __ch, __locale_t __loc) { return ::iswupper(__ch); }
+inline int __iswlower(wint_t __ch, __locale_t __loc) { return ::iswlower(__ch); }
+inline int __iswalpha(wint_t __ch, __locale_t __loc) { return ::iswalpha(__ch); }
+inline int __iswblank(wint_t __ch, __locale_t __loc) { return ::iswblank(__ch); }
+inline int __iswdigit(wint_t __ch, __locale_t __loc) { return ::iswdigit(__ch); }
+inline int __iswpunct(wint_t __ch, __locale_t __loc) { return ::iswpunct(__ch); }
+inline int __iswxdigit(wint_t __ch, __locale_t __loc) { return ::iswxdigit(__ch); }
+inline wint_t __towupper(wint_t __ch, __locale_t __loc) { return ::towupper(__ch); }
+inline wint_t __towlower(wint_t __ch, __locale_t __loc) { return ::towlower(__ch); }
+# endif
+
+inline _LIBCPP_ATTRIBUTE_FORMAT(__strftime__, 3, 0) size_t
+ __strftime(char* __s, size_t __max, const char* __format, const tm* __tm, __locale_t __loc) {
+ return ::strftime(__s, __max, __format, __tm);
+}
+
+//
+// Other functions
+//
+inline decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) {
+ __locale_guard __current(__loc);
+ return MB_CUR_MAX;
+}
+# if _LIBCPP_HAS_WIDE_CHARACTERS
+inline wint_t __btowc(int __ch, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return btowc(__ch);
+}
+
+inline int __wctob(wint_t __ch, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return wctob(__ch);
+}
+
+inline size_t
+__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
+}
+
+inline size_t __wcrtomb(char* __s, wchar_t __ch, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return wcrtomb(__s, __ch, __ps);
+}
+
+inline size_t
+__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
+}
+
+inline size_t __mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return mbrtowc(__pwc, __s, __n, __ps);
+}
+
+inline int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return mbtowc(__pwc, __pmb, __max);
+}
+
+inline size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return mbrlen(__s, __n, __ps);
+}
+
+inline size_t __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
+ __locale_guard __current(__loc);
+ return mbsrtowcs(__dest, __src, __len, __ps);
+}
+# endif // _LIBCPP_HAS_WIDE_CHARACTERS
+#endif // _LIBCPP_BUILDING_LIBRARY
+
+_LIBCPP_DIAGNOSTIC_PUSH
+_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
+_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates
+#ifdef _LIBCPP_COMPILER_CLANG_BASED
+# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__)
+#else
+# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */
+#endif
+
+// FIXME: This should be inlined
+inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __libcpp_snprintf_l(
+ char* __s, size_t __n, locale_t __l, const char* __format, ...) {
+ va_list __va;
+ va_start(__va, __format);
+ __locale_guard __current(__l);
+ int __res = vsnprintf(__s, __n, __format, __va);
+ va_end(__va);
+ return __res;
+}
+
+template <class... _Args>
+_LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5)
+int __snprintf(char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) {
+ return __locale::__libcpp_snprintf_l(__s, __n, __loc, __format, std::forward<_Args>(__args)...);
+}
+
+// FIXME: This should be inlined
+inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 2, 0) int vasprintf(char** strp, const char* fmt, va_list ap) {
+ const size_t buff_size = 256;
+ if ((*strp = (char*)malloc(buff_size)) == nullptr) {
+ return -1;
+ }
+
+ va_list ap_copy;
+ // va_copy may not be provided by the C library in C++03 mode.
+#if defined(_LIBCPP_CXX03_LANG) && __has_builtin(__builtin_va_copy)
+ __builtin_va_copy(ap_copy, ap);
+#else
+ va_copy(ap_copy, ap);
+#endif
+ int str_size = vsnprintf(*strp, buff_size, fmt, ap_copy);
+ va_end(ap_copy);
+
+ if ((size_t)str_size >= buff_size) {
+ if ((*strp = (char*)realloc(*strp, str_size + 1)) == nullptr) {
+ return -1;
+ }
+ str_size = vsnprintf(*strp, str_size + 1, fmt, ap);
+ }
+ return str_size;
+}
+
+// FIXME: This should be inlined
+inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __libcpp_asprintf_l(
+ char** __s, locale_t __l, const char* __format, ...) {
+ va_list __va;
+ va_start(__va, __format);
+ __locale_guard __current(__l);
+ int __res = vasprintf(__s, __format, __va);
+ va_end(__va);
+ return __res;
+}
+
+template <class... _Args>
+_LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4)
+int __asprintf(char** __s, __locale_t __loc, const char* __format, _Args&&... __args) {
+ return __locale::__libcpp_asprintf_l(__s, __loc, __format, std::forward<_Args>(__args)...);
+}
+_LIBCPP_DIAGNOSTIC_POP
+#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
+
+} // namespace __locale
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_DIR_SUPPORT_MVS_H
diff --git a/libcxx/include/__support/ibm/locale_mgmt_zos.h b/libcxx/include/__support/ibm/locale_mgmt_zos.h
deleted file mode 100644
index 5fc04b6b4b299..0000000000000
--- a/libcxx/include/__support/ibm/locale_mgmt_zos.h
+++ /dev/null
@@ -1,53 +0,0 @@
-// -*- 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___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
-#define _LIBCPP___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
-
-#if defined(__MVS__)
-# include <locale.h>
-# include <string>
-
-# ifdef __cplusplus
-extern "C" {
-# endif
-
-# define _LC_MAX LC_MESSAGES /* highest real category */
-# define _NCAT (_LC_MAX + 1) /* maximum + 1 */
-
-# define _CATMASK(n) (1 << (n))
-# define LC_COLLATE_MASK _CATMASK(LC_COLLATE)
-# define LC_CTYPE_MASK _CATMASK(LC_CTYPE)
-# define LC_MONETARY_MASK _CATMASK(LC_MONETARY)
-# define LC_NUMERIC_MASK _CATMASK(LC_NUMERIC)
-# define LC_TIME_MASK _CATMASK(LC_TIME)
-# define LC_MESSAGES_MASK _CATMASK(LC_MESSAGES)
-# define LC_ALL_MASK (_CATMASK(_NCAT) - 1)
-
-typedef struct locale_struct {
- int category_mask;
- std::string lc_collate;
- std::string lc_ctype;
- std::string lc_monetary;
- std::string lc_numeric;
- std::string lc_time;
- std::string lc_messages;
-}* locale_t;
-
-// z/OS does not have newlocale, freelocale and uselocale.
-// The functions below are workarounds in single thread mode.
-locale_t newlocale(int category_mask, const char* locale, locale_t base);
-void freelocale(locale_t locobj);
-locale_t uselocale(locale_t newloc);
-
-# ifdef __cplusplus
-}
-# endif
-#endif // defined(__MVS__)
-#endif // _LIBCPP___SUPPORT_IBM_LOCALE_MGMT_ZOS_H
diff --git a/libcxx/include/__support/xlocale/__posix_l_fallback.h b/libcxx/include/__support/xlocale/__posix_l_fallback.h
deleted file mode 100644
index a56de0a5d78cd..0000000000000
--- a/libcxx/include/__support/xlocale/__posix_l_fallback.h
+++ /dev/null
@@ -1,91 +0,0 @@
-// -*- 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
-//
-//===----------------------------------------------------------------------===//
-// These are reimplementations of some extended locale functions ( *_l ) that
-// are normally part of POSIX. This shared implementation provides parts of the
-// extended locale support for libc's that normally don't have any (like
-// Android's bionic and Newlib).
-//===----------------------------------------------------------------------===//
-
-#ifndef _LIBCPP___SUPPORT_XLOCALE_POSIX_L_FALLBACK_H
-#define _LIBCPP___SUPPORT_XLOCALE_POSIX_L_FALLBACK_H
-
-#include <__config>
-#include <ctype.h>
-#include <string.h>
-#include <time.h>
-
-#if _LIBCPP_HAS_WIDE_CHARACTERS
-# include <wchar.h>
-# include <wctype.h>
-#endif
-
-inline _LIBCPP_HIDE_FROM_ABI int isdigit_l(int __c, locale_t) { return ::isdigit(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int islower_l(int __c, locale_t) { return ::islower(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int isupper_l(int __c, locale_t) { return ::isupper(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int isxdigit_l(int __c, locale_t) { return ::isxdigit(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int toupper_l(int __c, locale_t) { return ::toupper(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int tolower_l(int __c, locale_t) { return ::tolower(__c); }
-
-#if _LIBCPP_HAS_WIDE_CHARACTERS
-inline _LIBCPP_HIDE_FROM_ABI int iswctype_l(wint_t __c, wctype_t __type, locale_t) { return ::iswctype(__c, __type); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswalpha_l(wint_t __c, locale_t) { return ::iswalpha(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswblank_l(wint_t __c, locale_t) { return ::iswblank(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswcntrl_l(wint_t __c, locale_t) { return ::iswcntrl(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswdigit_l(wint_t __c, locale_t) { return ::iswdigit(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswlower_l(wint_t __c, locale_t) { return ::iswlower(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswprint_l(wint_t __c, locale_t) { return ::iswprint(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswpunct_l(wint_t __c, locale_t) { return ::iswpunct(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswspace_l(wint_t __c, locale_t) { return ::iswspace(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswupper_l(wint_t __c, locale_t) { return ::iswupper(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI int iswxdigit_l(wint_t __c, locale_t) { return ::iswxdigit(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI wint_t towupper_l(wint_t __c, locale_t) { return ::towupper(__c); }
-
-inline _LIBCPP_HIDE_FROM_ABI wint_t towlower_l(wint_t __c, locale_t) { return ::towlower(__c); }
-#endif // _LIBCPP_HAS_WIDE_CHARACTERS
-
-inline _LIBCPP_HIDE_FROM_ABI int strcoll_l(const char* __s1, const char* __s2, locale_t) {
- return ::strcoll(__s1, __s2);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI size_t strxfrm_l(char* __dest, const char* __src, size_t __n, locale_t) {
- return ::strxfrm(__dest, __src, __n);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI size_t
-strftime_l(char* __s, size_t __max, const char* __format, const struct tm* __tm, locale_t) {
- return ::strftime(__s, __max, __format, __tm);
-}
-
-#if _LIBCPP_HAS_WIDE_CHARACTERS
-inline _LIBCPP_HIDE_FROM_ABI int wcscoll_l(const wchar_t* __ws1, const wchar_t* __ws2, locale_t) {
- return ::wcscoll(__ws1, __ws2);
-}
-
-inline _LIBCPP_HIDE_FROM_ABI size_t wcsxfrm_l(wchar_t* __dest, const wchar_t* __src, size_t __n, locale_t) {
- return ::wcsxfrm(__dest, __src, __n);
-}
-#endif // _LIBCPP_HAS_WIDE_CHARACTERS
-
-#endif // _LIBCPP___SUPPORT_XLOCALE_POSIX_L_FALLBACK_H
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 5fa74b5b5db78..07e6d60f258ae 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1607,6 +1607,7 @@ module std [system] {
textual header "__locale_dir/support/fuchsia.h"
textual header "__locale_dir/support/linux.h"
textual header "__locale_dir/support/llvm_libc.h"
+ textual header "__locale_dir/support/mvs.h"
textual header "__locale_dir/support/netbsd.h"
textual header "__locale_dir/support/no_locale/conversions.h"
textual header "__locale_dir/support/no_locale/formatting.h"
@@ -1616,11 +1617,6 @@ module std [system] {
textual header "__locale_dir/support/openbsd.h"
textual header "__locale_dir/support/windows.h"
}
-
- module locale_base_api {
- textual header "__locale_dir/locale_base_api/bsd_locale_fallbacks.h"
- textual header "__locale_dir/locale_base_api/ibm.h"
- }
export *
}
diff --git a/libcxx/src/support/ibm/xlocale_zos.cpp b/libcxx/src/support/ibm/xlocale_zos.cpp
index 136999ec0b02f..e2b9f434df655 100644
--- a/libcxx/src/support/ibm/xlocale_zos.cpp
+++ b/libcxx/src/support/ibm/xlocale_zos.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include <__assert>
-#include <__support/ibm/xlocale.h>
+#include <__locale_dir/support/mvs.h>
#include <sstream>
#include <vector>
More information about the libcxx-commits
mailing list