[libcxx-commits] [libcxx] [libc++] Move _LIBCPP_GET_C_LOCALE to the locale base API (PR #222924)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Sep 11 07:00:17 PDT 2026
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/222924
>From 64be99c79c50f9c3f8f9722b7aaeeb4b515f4c10 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Fri, 11 Sep 2026 13:11:40 +0200
Subject: [PATCH] [libc++] Move _LIBCPP_GET_C_LOCALE to the locale base API
---
libcxx/include/CMakeLists.txt | 2 +-
libcxx/include/__locale_dir/get_c_locale.h | 42 -------------------
libcxx/include/__locale_dir/money.h | 3 +-
libcxx/include/__locale_dir/num.h | 15 ++++---
libcxx/include/__locale_dir/support/aix.h | 2 +
.../include/__locale_dir/support/bsd_like.h | 10 +++++
libcxx/include/__locale_dir/support/fuchsia.h | 1 +
.../__locale_dir/support/get_c_locale.h | 32 ++++++++++++++
libcxx/include/__locale_dir/support/linux.h | 2 +
.../include/__locale_dir/support/llvm_libc.h | 1 +
libcxx/include/__locale_dir/support/newlib.h | 2 +
libcxx/include/__locale_dir/support/openbsd.h | 2 +
libcxx/include/__locale_dir/support/windows.h | 2 +
libcxx/include/__locale_dir/time.h | 3 +-
libcxx/include/module.modulemap.in | 2 +-
libcxx/src/locale.cpp | 8 ++--
16 files changed, 69 insertions(+), 60 deletions(-)
delete mode 100644 libcxx/include/__locale_dir/get_c_locale.h
create mode 100644 libcxx/include/__locale_dir/support/get_c_locale.h
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 113d4fde7ca59..0f2fe27c43a22 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -544,7 +544,6 @@ set(files
__locale_dir/collate.h
__locale_dir/ctype.h
__locale_dir/ctype_base.h
- __locale_dir/get_c_locale.h
__locale_dir/locale.h
__locale_dir/locale_base_api.h
__locale_dir/locale_base_api/bsd_locale_fallbacks.h
@@ -559,6 +558,7 @@ set(files
__locale_dir/support/bsd_like.h
__locale_dir/support/freebsd.h
__locale_dir/support/fuchsia.h
+ __locale_dir/support/get_c_locale.h
__locale_dir/support/linux.h
__locale_dir/support/llvm_libc.h
__locale_dir/support/netbsd.h
diff --git a/libcxx/include/__locale_dir/get_c_locale.h b/libcxx/include/__locale_dir/get_c_locale.h
deleted file mode 100644
index 677827f2d8f1d..0000000000000
--- a/libcxx/include/__locale_dir/get_c_locale.h
+++ /dev/null
@@ -1,42 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// 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_GET_C_LOCALE_H
-#define _LIBCPP___LOCALE_DIR_GET_C_LOCALE_H
-
-#include <__config>
-#include <__locale_dir/locale_base_api.h>
-
-#if _LIBCPP_HAS_LOCALIZATION
-
-# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-# pragma GCC system_header
-# endif
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-// FIXME: This should really be part of the locale base API
-
-# if defined(__APPLE__) || defined(__FreeBSD__)
-# define _LIBCPP_GET_C_LOCALE 0
-# elif defined(__NetBSD__)
-# define _LIBCPP_GET_C_LOCALE LC_C_LOCALE
-# else
-# define _LIBCPP_GET_C_LOCALE __cloc()
-// Get the C locale object
-_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
-_LIBCPP_EXPORTED_FROM_ABI __locale::__locale_t __cloc();
-_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
-# define __cloc_defined
-# endif
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP_HAS_LOCALIZATION
-
-#endif // _LIBCPP___LOCALE_DIR_GET_C_LOCALE_H
diff --git a/libcxx/include/__locale_dir/money.h b/libcxx/include/__locale_dir/money.h
index c39f31cc4f0da..552f03b94c5ef 100644
--- a/libcxx/include/__locale_dir/money.h
+++ b/libcxx/include/__locale_dir/money.h
@@ -17,7 +17,6 @@
#include <__locale_dir/check_grouping.h>
#include <__locale_dir/ctype.h>
#include <__locale_dir/ctype_base.h>
-#include <__locale_dir/get_c_locale.h>
#include <__locale_dir/locale.h>
#include <__locale_dir/pad_and_output.h>
#include <__memory/unique_ptr.h>
@@ -767,7 +766,7 @@ _OutputIterator money_put<_CharT, _OutputIterator>::do_put(
unique_ptr<char_type, void (*)(void*)> __hd(0, free);
// secure memory for digit storage
if (static_cast<size_t>(__n) > __bs - 1) {
- __n = __locale::__asprintf(&__bb, _LIBCPP_GET_C_LOCALE, "%.0Lf", __units);
+ __n = __locale::__asprintf(&__bb, __locale::__get_c_locale(), "%.0Lf", __units);
if (__n == -1)
std::__throw_bad_alloc();
__hn.reset(__bb);
diff --git a/libcxx/include/__locale_dir/num.h b/libcxx/include/__locale_dir/num.h
index d5bc5e4695012..e924c88d0955c 100644
--- a/libcxx/include/__locale_dir/num.h
+++ b/libcxx/include/__locale_dir/num.h
@@ -20,7 +20,6 @@
#include <__iterator/ostreambuf_iterator.h>
#include <__locale_dir/check_grouping.h>
#include <__locale_dir/ctype.h>
-#include <__locale_dir/get_c_locale.h>
#include <__locale_dir/locale.h>
#include <__locale_dir/pad_and_output.h>
#include <__locale_dir/scan_keyword.h>
@@ -307,17 +306,17 @@ _LIBCPP_HIDE_FROM_ABI _Tp __do_strtod(const char* __a, char** __p2);
template <>
inline _LIBCPP_HIDE_FROM_ABI float __do_strtod<float>(const char* __a, char** __p2) {
- return __locale::__strtof(__a, __p2, _LIBCPP_GET_C_LOCALE);
+ return __locale::__strtof(__a, __p2, __locale::__get_c_locale());
}
template <>
inline _LIBCPP_HIDE_FROM_ABI double __do_strtod<double>(const char* __a, char** __p2) {
- return __locale::__strtod(__a, __p2, _LIBCPP_GET_C_LOCALE);
+ return __locale::__strtod(__a, __p2, __locale::__get_c_locale());
}
template <>
inline _LIBCPP_HIDE_FROM_ABI long double __do_strtod<long double>(const char* __a, char** __p2) {
- return __locale::__strtold(__a, __p2, _LIBCPP_GET_C_LOCALE);
+ return __locale::__strtold(__a, __p2, __locale::__get_c_locale());
}
template <class _Tp>
@@ -1046,15 +1045,15 @@ _LIBCPP_HIDE_FROM_ABI inline _OutputIterator num_put<_CharT, _OutputIterator>::_
_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral")
if (__specify_precision)
- __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
+ __nc = __locale::__snprintf(__nb, __nbuf, __locale::__get_c_locale(), __fmt, (int)__iob.precision(), __v);
else
- __nc = __locale::__snprintf(__nb, __nbuf, _LIBCPP_GET_C_LOCALE, __fmt, __v);
+ __nc = __locale::__snprintf(__nb, __nbuf, __locale::__get_c_locale(), __fmt, __v);
unique_ptr<char, void (*)(void*)> __nbh(nullptr, free);
if (__nc > static_cast<int>(__nbuf - 1)) {
if (__specify_precision)
- __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, (int)__iob.precision(), __v);
+ __nc = __locale::__asprintf(&__nb, __locale::__get_c_locale(), __fmt, (int)__iob.precision(), __v);
else
- __nc = __locale::__asprintf(&__nb, _LIBCPP_GET_C_LOCALE, __fmt, __v);
+ __nc = __locale::__asprintf(&__nb, __locale::__get_c_locale(), __fmt, __v);
if (__nc == -1)
std::__throw_bad_alloc();
__nbh.reset(__nb);
diff --git a/libcxx/include/__locale_dir/support/aix.h b/libcxx/include/__locale_dir/support/aix.h
index ef2536338435b..e9800a4f9908f 100644
--- a/libcxx/include/__locale_dir/support/aix.h
+++ b/libcxx/include/__locale_dir/support/aix.h
@@ -300,4 +300,6 @@ _LIBCPP_END_NAMESPACE_STD
#define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE 0
+#include <__locale_dir/support/get_c_locale.h>
+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_AIX_H
diff --git a/libcxx/include/__locale_dir/support/bsd_like.h b/libcxx/include/__locale_dir/support/bsd_like.h
index efc29da670f14..ebe3f726e34ff 100644
--- a/libcxx/include/__locale_dir/support/bsd_like.h
+++ b/libcxx/include/__locale_dir/support/bsd_like.h
@@ -65,6 +65,16 @@ 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
+inline __locale_t __get_c_locale() {
+#if defined(__APPLE__) || defined(__FreeBSD__)
+ return 0;
+#elif defined(__NetBSD__)
+ return LC_C_LOCALE;
+#else
+# error "BSD-like platforms need to extend this list."
+#endif
+}
+
//
// Strtonum functions
//
diff --git a/libcxx/include/__locale_dir/support/fuchsia.h b/libcxx/include/__locale_dir/support/fuchsia.h
index 46790b83290af..d5e05f99107a4 100644
--- a/libcxx/include/__locale_dir/support/fuchsia.h
+++ b/libcxx/include/__locale_dir/support/fuchsia.h
@@ -152,6 +152,7 @@ _LIBCPP_DIAGNOSTIC_POP
} // namespace __locale
_LIBCPP_END_NAMESPACE_STD
+#include <__locale_dir/support/get_c_locale.h>
#include <__locale_dir/support/no_locale/characters.h>
#include <__locale_dir/support/no_locale/strtonum.h>
diff --git a/libcxx/include/__locale_dir/support/get_c_locale.h b/libcxx/include/__locale_dir/support/get_c_locale.h
new file mode 100644
index 0000000000000..dec95f4c52824
--- /dev/null
+++ b/libcxx/include/__locale_dir/support/get_c_locale.h
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_GET_C_LOCALE_H
+#define _LIBCPP___LOCALE_DIR_SUPPORT_GET_C_LOCALE_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// Get the C locale object
+_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
+_LIBCPP_EXPORTED_FROM_ABI __locale::__locale_t __cloc();
+_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
+#define __cloc_defined
+
+namespace __locale {
+inline __locale_t __get_c_locale() { return std::__cloc(); }
+} // namespace __locale
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_DIR_SUPPORT_GET_C_LOCALE_H
diff --git a/libcxx/include/__locale_dir/support/linux.h b/libcxx/include/__locale_dir/support/linux.h
index 74abf5ab7c598..985874ac2b371 100644
--- a/libcxx/include/__locale_dir/support/linux.h
+++ b/libcxx/include/__locale_dir/support/linux.h
@@ -256,4 +256,6 @@ _LIBCPP_END_NAMESPACE_STD
# define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE 0
#endif
+#include <__locale_dir/support/get_c_locale.h>
+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_LINUX_H
diff --git a/libcxx/include/__locale_dir/support/llvm_libc.h b/libcxx/include/__locale_dir/support/llvm_libc.h
index fe9d3639ecf3f..ccb3abe22df5a 100644
--- a/libcxx/include/__locale_dir/support/llvm_libc.h
+++ b/libcxx/include/__locale_dir/support/llvm_libc.h
@@ -54,6 +54,7 @@ inline _LIBCPP_HIDE_FROM_ABI const char* __get_locale_encoding(__locale_t) { ret
} // namespace __locale
_LIBCPP_END_NAMESPACE_STD
+#include <__locale_dir/support/get_c_locale.h>
#include <__locale_dir/support/no_locale/characters.h>
#include <__locale_dir/support/no_locale/conversions.h>
#include <__locale_dir/support/no_locale/formatting.h>
diff --git a/libcxx/include/__locale_dir/support/newlib.h b/libcxx/include/__locale_dir/support/newlib.h
index 6e34de84a6c48..2b418b4eca0af 100644
--- a/libcxx/include/__locale_dir/support/newlib.h
+++ b/libcxx/include/__locale_dir/support/newlib.h
@@ -247,4 +247,6 @@ _LIBCPP_END_NAMESPACE_STD
#define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE 0
+#include <__locale_dir/support/get_c_locale.h>
+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_NEWLIB_H
diff --git a/libcxx/include/__locale_dir/support/openbsd.h b/libcxx/include/__locale_dir/support/openbsd.h
index ac66d393ade7c..a4039aaa3ad6e 100644
--- a/libcxx/include/__locale_dir/support/openbsd.h
+++ b/libcxx/include/__locale_dir/support/openbsd.h
@@ -228,4 +228,6 @@ _LIBCPP_END_NAMESPACE_STD
#define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE 1
+#include <__locale_dir/support/get_c_locale.h>
+
#endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_OPENBSD_H
diff --git a/libcxx/include/__locale_dir/support/windows.h b/libcxx/include/__locale_dir/support/windows.h
index 2c480d289c0e4..f3b4b143bbf85 100644
--- a/libcxx/include/__locale_dir/support/windows.h
+++ b/libcxx/include/__locale_dir/support/windows.h
@@ -311,4 +311,6 @@ _LIBCPP_END_NAMESPACE_STD
#define _LIBCPP_PROVIDES_DEFAULT_RUNE_TABLE 0
+#include <__locale_dir/support/get_c_locale.h>
+
#endif // _LIBCPP___LOCALE_DIR_SUPPORT_WINDOWS_H
diff --git a/libcxx/include/__locale_dir/time.h b/libcxx/include/__locale_dir/time.h
index 34adb52d2e3bc..1e02f73543efe 100644
--- a/libcxx/include/__locale_dir/time.h
+++ b/libcxx/include/__locale_dir/time.h
@@ -13,7 +13,6 @@
#include <__config>
#include <__locale_dir/ctype.h>
#include <__locale_dir/ctype_base.h>
-#include <__locale_dir/get_c_locale.h>
#include <__locale_dir/locale.h>
#include <__locale_dir/scan_keyword.h>
#include <ios>
@@ -656,7 +655,7 @@ class _LIBCPP_EXPORTED_FROM_ABI __time_put {
__locale::__locale_t __loc_;
protected:
- _LIBCPP_HIDE_FROM_ABI __time_put() : __loc_(_LIBCPP_GET_C_LOCALE) {}
+ _LIBCPP_HIDE_FROM_ABI __time_put() : __loc_(__locale::__get_c_locale()) {}
__time_put(const char* __nm);
__time_put(const string& __nm);
~__time_put();
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index d22a9ce36d02c..8f769fd7b38b1 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -1608,7 +1608,6 @@ module std {
module locale {
header "locale"
module check_grouping { header "__locale_dir/check_grouping.h" }
- module get_c_locale { header "__locale_dir/get_c_locale.h" }
module messages { header "__locale_dir/messages.h" }
module money { header "__locale_dir/money.h" }
module num { header "__locale_dir/num.h" }
@@ -1629,6 +1628,7 @@ module std {
textual header "__locale_dir/support/bsd_like.h"
textual header "__locale_dir/support/freebsd.h"
textual header "__locale_dir/support/fuchsia.h"
+ textual header "__locale_dir/support/get_c_locale.h"
textual header "__locale_dir/support/linux.h"
textual header "__locale_dir/support/llvm_libc.h"
textual header "__locale_dir/support/netbsd.h"
diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index 2228624901ac4..058bc0ec6f901 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -903,7 +903,7 @@ const ctype<char>::mask* ctype<char>::classic_table() noexcept {
# elif defined(__NetBSD__)
return _C_ctype_tab_ + 1;
# elif defined(__GLIBC__)
- return _LIBCPP_GET_C_LOCALE->__ctype_b;
+ return __locale::__get_c_locale()->__ctype_b;
# elif defined(_WIN32)
return __pctype_func();
# elif defined(__EMSCRIPTEN__)
@@ -1225,7 +1225,7 @@ int codecvt<char, char, mbstate_t>::do_max_length() const noexcept { return 1; }
#if _LIBCPP_HAS_WIDE_CHARACTERS
constinit locale::id codecvt<wchar_t, char, mbstate_t>::id;
-codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs) : locale::facet(refs), __l_(_LIBCPP_GET_C_LOCALE) {}
+codecvt<wchar_t, char, mbstate_t>::codecvt(size_t refs) : locale::facet(refs), __l_(__locale::__get_c_locale()) {}
codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
: locale::facet(refs), __l_(__locale::__newlocale(_LIBCPP_ALL_MASK, nm, 0)) {
@@ -1238,7 +1238,7 @@ codecvt<wchar_t, char, mbstate_t>::codecvt(const char* nm, size_t refs)
}
codecvt<wchar_t, char, mbstate_t>::~codecvt() {
- if (__l_ != _LIBCPP_GET_C_LOCALE)
+ if (__l_ != __locale::__get_c_locale())
__locale::__freelocale(__l_);
}
@@ -4863,7 +4863,7 @@ __time_put::__time_put(const string& nm) : __loc_(__locale::__newlocale(_LIBCPP_
}
__time_put::~__time_put() {
- if (__loc_ != _LIBCPP_GET_C_LOCALE)
+ if (__loc_ != __locale::__get_c_locale())
__locale::__freelocale(__loc_);
}
More information about the libcxx-commits
mailing list