[libcxx-commits] [libcxx] [libc++][AIX] Move to new locale APIs (PR #172068)

Xing Xue via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 15 14:04:47 PST 2025


https://github.com/xingxue-ibm updated https://github.com/llvm/llvm-project/pull/172068

>From e5ffa2c3aadbb3dadb3e06529fed91cee5b23aec Mon Sep 17 00:00:00 2001
From: Xing Xue <xingxue at outlook.com>
Date: Fri, 12 Dec 2025 13:39:23 -0500
Subject: [PATCH 1/4] Move to new locale APIs for AIX.

---
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__locale_dir/locale_base_api.h |   4 +-
 libcxx/include/__locale_dir/support/aix.h     | 325 ++++++++++++++++++
 libcxx/include/module.modulemap.in            |   1 +
 4 files changed, 330 insertions(+), 1 deletion(-)
 create mode 100644 libcxx/include/__locale_dir/support/aix.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 53ca1f7d3ded2..e6d5d444cc1c2 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -527,6 +527,7 @@ set(files
   __locale_dir/num.h
   __locale_dir/pad_and_output.h
   __locale_dir/scan_keyword.h
+  __locale_dir/support/aix.h
   __locale_dir/support/apple.h
   __locale_dir/support/bsd_like.h
   __locale_dir/support/freebsd.h
diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
index a5849dfa3a075..0474c1db359de 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -120,13 +120,15 @@
 #    include <__locale_dir/support/linux.h>
 #  elif _LIBCPP_LIBC_NEWLIB
 #    include <__locale_dir/support/newlib.h>
+#  elif defined(_AIX)
+#    include <__locale_dir/support/aix.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.
-#    if defined(_AIX) || defined(__MVS__)
+#    if defined(__MVS__)
 #      include <__locale_dir/locale_base_api/ibm.h>
 #    elif defined(__OpenBSD__)
 #      include <__locale_dir/locale_base_api/openbsd.h>
diff --git a/libcxx/include/__locale_dir/support/aix.h b/libcxx/include/__locale_dir/support/aix.h
new file mode 100644
index 0000000000000..c71bbbb85b890
--- /dev/null
+++ b/libcxx/include/__locale_dir/support/aix.h
@@ -0,0 +1,325 @@
+//===-----------------------------------------------------------------------===//
+//
+// 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_AIX_H
+#define _LIBCPP___LOCALE_DIR_SUPPORT_AIX_H
+
+#include <__config>
+#include <__cstddef/size_t.h>
+#include <__std_mbstate_t.h>
+#include <__utility/forward.h>
+#include <clocale> // std::lconv
+#include <cstdio>
+#include <cstdlib>
+#include <ctype.h>
+#include <stdarg.h>
+#include <string.h>
+#include <time.h>
+#if _LIBCPP_HAS_WIDE_CHARACTERS
+#  include <cwchar>
+#  include <wctype.h>
+#endif
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+namespace __locale {
+
+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;
+};
+
+//
+// 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
+
+// Get MB_CUE_MAX before __locale_t is defined because it uses a defferent definition in <sys/localedef.h>.
+static inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_cur_max() {
+  return MB_CUR_MAX;
+}
+
+using __locale_t _LIBCPP_NODEBUG = ::locale_t;
+
+#if defined(_LIBCPP_BUILDING_LIBRARY)
+using __lconv_t _LIBCPP_NODEBUG = 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);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __locale) {
+  return ::setlocale(__category, __locale);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) {
+  __locale_guard __current(__loc);
+  return std::localeconv();
+}
+#endif // _LIBCPP_BUILDING_LIBRARY
+
+// The following are not POSIX routines.  These are quick-and-dirty hacks
+// to make things pretend to work
+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
+
+inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, 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) {
+  __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 unsigned long long
+strtoull_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
+  __setAndRestore __newloc(locale);
+  return ::strtoull(__nptr, __endptr, __base);
+}
+
+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;
+}
+
+//
+// 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 __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); }
+
+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);
+}
+
+#  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);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __c, __locale_t __loc) { return iswspace_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __c, __locale_t __loc) { return iswprint_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __c, __locale_t __loc) { return iswcntrl_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __c, __locale_t __loc) { return iswupper_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __c, __locale_t __loc) { return iswlower_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __c, __locale_t __loc) { return iswalpha_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __c, __locale_t __loc) { return iswblank_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __c, __locale_t __loc) { return iswdigit_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __c, __locale_t __loc) { return iswpunct_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __c, __locale_t __loc) { return iswxdigit_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __c, __locale_t __loc) { return towupper_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __c, __locale_t __loc) { return towlower_l(__c, __loc); }
+
+inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) {
+  return wcscoll_l(__ws1, __ws2, __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);
+}
+#  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) {
+  return strftime_l(__s, __max, __format, __tm, __loc);
+}
+
+//
+// Other functions
+//
+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
+inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __c, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::btowc(__c);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __c, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::wctob(__c);
+}
+
+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) {
+  __locale_guard __current(__loc);
+  return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard
+}
+
+inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::wcrtomb(__s, __wc, __ps);
+}
+
+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) {
+  __locale_guard __current(__loc);
+  return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard
+}
+
+inline _LIBCPP_HIDE_FROM_ABI size_t
+__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::mbrtowc(__pwc, __s, __n, __ps);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::mbtowc(__pwc, __pmb, __max);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::mbrlen(__s, __n, __ps);
+}
+
+inline _LIBCPP_HIDE_FROM_ABI size_t
+__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) {
+  __locale_guard __current(__loc);
+  return std::mbsrtowcs(__dest, __src, __len, __ps);
+}
+#  endif // _LIBCPP_HAS_WIDE_CHARACTERS
+#endif   // _LIBCPP_BUILDING_LIBRARY
+
+#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs
+_LIBCPP_HIDE_FROM_ABI
+#endif
+inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
+    char* __s, size_t __n, __locale_t __loc, const char* __format, ...) {
+  va_list __va;
+  va_start(__va, __format);
+  __locale_guard __current(__loc);
+  int __res = std::vsnprintf(__s, __n, __format, __va);
+  va_end(__va);
+  return __res;
+}
+
+inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
+    char** __s, __locale_t __loc, const char* __format, ...) {
+  va_list __va;
+  va_start(__va, __format);
+  __locale_guard __current(__loc);
+  int __res = vasprintf(__s, __format, __va); // non-standard
+  va_end(__va);
+  return __res;
+}
+} // namespace __locale
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_DIR_SUPPORT_AIX_H
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 04be734e37384..feb6249e1e6db 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1578,6 +1578,7 @@ module std [system] {
     }
 
     module support_impl {
+      textual header "__locale_dir/support/aix.h"
       textual header "__locale_dir/support/apple.h"
       textual header "__locale_dir/support/bsd_like.h"
       textual header "__locale_dir/support/freebsd.h"

>From 10c59b9cc7fc0fb8b6e0f6ad50aef2fd3f6b26a5 Mon Sep 17 00:00:00 2001
From: Xing Xue <xingxue at outlook.com>
Date: Fri, 12 Dec 2025 14:10:45 -0500
Subject: [PATCH 2/4] Fixed the formatting.

---
 libcxx/include/__locale_dir/support/aix.h | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/libcxx/include/__locale_dir/support/aix.h b/libcxx/include/__locale_dir/support/aix.h
index c71bbbb85b890..c8d5542ea3db2 100644
--- a/libcxx/include/__locale_dir/support/aix.h
+++ b/libcxx/include/__locale_dir/support/aix.h
@@ -59,9 +59,7 @@ struct __locale_guard {
 #define _LIBCPP_LC_ALL LC_ALL
 
 // Get MB_CUE_MAX before __locale_t is defined because it uses a defferent definition in <sys/localedef.h>.
-static inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_cur_max() {
-  return MB_CUR_MAX;
-}
+static inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_cur_max() { return MB_CUR_MAX; }
 
 using __locale_t _LIBCPP_NODEBUG = ::locale_t;
 
@@ -232,8 +230,8 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __
 }
 #  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) {
+inline _LIBCPP_HIDE_FROM_ABI
+size_t __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) {
   return strftime_l(__s, __max, __format, __tm, __loc);
 }
 

>From 8e420efb3864491ddac5d3352b30bdcf1691d13c Mon Sep 17 00:00:00 2001
From: Xing Xue <xingxue at outlook.com>
Date: Mon, 15 Dec 2025 16:31:38 -0500
Subject: [PATCH 3/4] Addressed comments. * Removed anonymous namespace. *
 Inlined _l workarounds into call sites. * Switched from C varargs to variadic
 templates.

---
 libcxx/include/__locale_dir/support/aix.h | 134 +++++++++-------------
 1 file changed, 54 insertions(+), 80 deletions(-)

diff --git a/libcxx/include/__locale_dir/support/aix.h b/libcxx/include/__locale_dir/support/aix.h
index c8d5542ea3db2..4428074c21aa6 100644
--- a/libcxx/include/__locale_dir/support/aix.h
+++ b/libcxx/include/__locale_dir/support/aix.h
@@ -42,7 +42,7 @@ struct __locale_guard {
 
   locale_t __old_loc_;
 
-  __locale_guard(__locale_guard const&)            = delete;
+  __locale_guard(__locale_guard const&) = delete;
   __locale_guard& operator=(__locale_guard const&) = delete;
 };
 
@@ -82,10 +82,8 @@ inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) {
 }
 #endif // _LIBCPP_BUILDING_LIBRARY
 
-// The following are not POSIX routines.  These are quick-and-dirty hacks
-// to make things pretend to work
-namespace {
-
+// The following structure is a quick-and-dirty workaround for routines that AIX
+// does not provide in the "_l" (locale-aware) variants.
 struct __setAndRestore {
   explicit __setAndRestore(locale_t locale) {
     if (locale == (locale_t)0) {
@@ -107,73 +105,22 @@ struct __setAndRestore {
   locale_t __cloc   = (locale_t)0;
 };
 
-} // namespace
-
-inline _LIBCPP_HIDE_FROM_ABI long long strtoll_l(const char* __nptr, char** __endptr, int __base, 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) {
-  __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 unsigned long long
-strtoull_l(const char* __nptr, char** __endptr, int __base, locale_t locale) {
-  __setAndRestore __newloc(locale);
-  return ::strtoull(__nptr, __endptr, __base);
-}
-
-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;
-}
-
 //
 // Strtonum functions
 //
 inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) {
-  return strtof_l(__nptr, __endptr, __loc);
+  __setAndRestore __newloc(__loc);
+  return ::strtof(__nptr, __endptr);
 }
 
 inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) {
-  return strtod_l(__nptr, __endptr, __loc);
+  __setAndRestore __newloc(__loc);
+  return ::strtod(__nptr, __endptr);
 }
 
 inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) {
-  return strtold_l(__nptr, __endptr, __loc);
+  __setAndRestore __newloc(__loc);
+  return ::strtold(__nptr, __endptr);
 }
 
 //
@@ -230,8 +177,8 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __
 }
 #  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) {
+inline _LIBCPP_HIDE_FROM_ABI size_t
+__strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) {
   return strftime_l(__s, __max, __format, __tm, __loc);
 }
 
@@ -295,28 +242,55 @@ __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps,
 #  endif // _LIBCPP_HAS_WIDE_CHARACTERS
 #endif   // _LIBCPP_BUILDING_LIBRARY
 
-#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs
-_LIBCPP_HIDE_FROM_ABI
+_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
-inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
-    char* __s, size_t __n, __locale_t __loc, const char* __format, ...) {
-  va_list __va;
-  va_start(__va, __format);
+
+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) {
   __locale_guard __current(__loc);
-  int __res = std::vsnprintf(__s, __n, __format, __va);
-  va_end(__va);
-  return __res;
+  return std::snprintf(__s, __n, __format, std::forward<_Args>(__args)...);
 }
 
-inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
-    char** __s, __locale_t __loc, const char* __format, ...) {
-  va_list __va;
-  va_start(__va, __format);
+template <class... _Args>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(
+    char** __s, __locale_t __loc, const char* __format, _Args&&... __args) {
   __locale_guard __current(__loc);
-  int __res = vasprintf(__s, __format, __va); // non-standard
-  va_end(__va);
-  return __res;
+
+  // Probe exact size.
+  int n = std::snprintf(nullptr, 0, __format, std::forward<_Args>(__args)...);
+  if (n < 0) {
+    *__s = nullptr;
+    return -1;
+  }
+
+  // Allocate and render once
+  size_t buf_size = static_cast<size_t>(n) + 1;
+  char* buf       = static_cast<char*>(std::malloc(buf_size));
+  if (!buf) {
+    *__s = nullptr;
+    return -1;
+  }
+
+  int written_size = std::snprintf(buf, buf_size, __format, std::forward<_Args>(__args)...);
+  if (written_size < 0) {
+    std::free(buf);
+    *__s = nullptr;
+    return -1;
+  }
+
+  *__s = buf;
+  return written_size;
 }
+_LIBCPP_DIAGNOSTIC_POP
+#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
+
 } // namespace __locale
 _LIBCPP_END_NAMESPACE_STD
 

>From ccbacaa540469c97fa2c11deb33aa46d86aa4675 Mon Sep 17 00:00:00 2001
From: Xing Xue <xingxue at outlook.com>
Date: Mon, 15 Dec 2025 17:03:39 -0500
Subject: [PATCH 4/4] Fix the formatting.

---
 libcxx/include/__locale_dir/support/aix.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__locale_dir/support/aix.h b/libcxx/include/__locale_dir/support/aix.h
index 4428074c21aa6..92f1d39f1c204 100644
--- a/libcxx/include/__locale_dir/support/aix.h
+++ b/libcxx/include/__locale_dir/support/aix.h
@@ -17,7 +17,6 @@
 #include <cstdio>
 #include <cstdlib>
 #include <ctype.h>
-#include <stdarg.h>
 #include <string.h>
 #include <time.h>
 #if _LIBCPP_HAS_WIDE_CHARACTERS
@@ -42,7 +41,7 @@ struct __locale_guard {
 
   locale_t __old_loc_;
 
-  __locale_guard(__locale_guard const&) = delete;
+  __locale_guard(__locale_guard const&)            = delete;
   __locale_guard& operator=(__locale_guard const&) = delete;
 };
 
@@ -177,8 +176,8 @@ inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __
 }
 #  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) {
+inline _LIBCPP_HIDE_FROM_ABI
+size_t __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) {
   return strftime_l(__s, __max, __format, __tm, __loc);
 }
 



More information about the libcxx-commits mailing list