[libcxx-commits] [libcxx] [libc++] Reduce the dependency of the locale base API on the base system from the headers (PR #117764)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 4 13:52:21 PST 2025
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/117764
>From a48659399e399a4cf9112e1a4c37dea5d2484bd6 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 26 Nov 2024 12:27:08 -0500
Subject: [PATCH] [libc++] Reduce the dependency of the locale base API on the
base system from the headers
Many parts of the locale base API are only required when building the
shared/static library, but not from the headers. Document those functions
and carve out a few of those that don't work when _XOPEN_SOURCE is defined
to something old.
Fixes #117630
---
libcxx/include/__locale_dir/locale_base_api.h | 17 +++++---
.../include/__locale_dir/support/bsd_like.h | 4 ++
libcxx/test/libcxx/xopen_source.gen.py | 42 +++++++++++++++++++
3 files changed, 57 insertions(+), 6 deletions(-)
create mode 100644 libcxx/test/libcxx/xopen_source.gen.py
diff --git a/libcxx/include/__locale_dir/locale_base_api.h b/libcxx/include/__locale_dir/locale_base_api.h
index bbee9f49867fd9..e9d868148bb47f 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -23,12 +23,16 @@
// Variadic functions may be implemented as templates with a parameter pack instead
// of C-style variadic functions.
//
+// Most of these functions are only required when building the library. Functions that are also
+// required when merely using the headers are marked as such below.
+//
// TODO: __localeconv shouldn't take a reference, but the Windows implementation doesn't allow copying __locale_t
+// TODO: Eliminate the need for any of these functions from the headers.
//
// Locale management
// -----------------
// namespace __locale {
-// using __locale_t = implementation-defined;
+// using __locale_t = implementation-defined; // required by the headers
// using __lconv_t = implementation-defined;
// __locale_t __newlocale(int, const char*, __locale_t);
// void __freelocale(__locale_t);
@@ -60,8 +64,8 @@
// namespace __locale {
// int __islower(int, __locale_t);
// int __isupper(int, __locale_t);
-// int __isdigit(int, __locale_t);
-// int __isxdigit(int, __locale_t);
+// int __isdigit(int, __locale_t); // required by the headers
+// int __isxdigit(int, __locale_t); // required by the headers
// int __toupper(int, __locale_t);
// int __tolower(int, __locale_t);
// int __strcoll(const char*, const char*, __locale_t);
@@ -99,9 +103,10 @@
// int __mbtowc(wchar_t*, const char*, size_t, __locale_t);
// size_t __mbrlen(const char*, size_t, mbstate_t*, __locale_t);
// size_t __mbsrtowcs(wchar_t*, const char**, size_t, mbstate_t*, __locale_t);
-// int __snprintf(char*, size_t, __locale_t, const char*, ...);
-// int __asprintf(char**, __locale_t, const char*, ...);
-// int __sscanf(const char*, __locale_t, const char*, ...);
+//
+// int __snprintf(char*, size_t, __locale_t, const char*, ...); // required by the headers
+// int __asprintf(char**, __locale_t, const char*, ...); // required by the headers
+// int __sscanf(const char*, __locale_t, const char*, ...); // required by the headers
// }
#if defined(__APPLE__)
diff --git a/libcxx/include/__locale_dir/support/bsd_like.h b/libcxx/include/__locale_dir/support/bsd_like.h
index c0080b13a08cf3..61d5bb45d05f11 100644
--- a/libcxx/include/__locale_dir/support/bsd_like.h
+++ b/libcxx/include/__locale_dir/support/bsd_like.h
@@ -160,19 +160,23 @@ inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __c, __locale_t __loc) { return
inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __c, __locale_t __loc) { return ::wctob_l(__c, __loc); }
+# ifdef _LIBCPP_BUILDING_LIBRARY
inline _LIBCPP_HIDE_FROM_ABI size_t
__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return ::wcsnrtombs_l(__dest, __src, __nwc, __len, __ps, __loc);
}
+# endif
inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) {
return ::wcrtomb_l(__s, __wc, __ps, __loc);
}
+# ifdef _LIBCPP_BUILDING_LIBRARY
inline _LIBCPP_HIDE_FROM_ABI size_t
__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) {
return ::mbsnrtowcs_l(__dest, __src, __nms, __len, __ps, __loc);
}
+# endif
inline _LIBCPP_HIDE_FROM_ABI size_t
__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) {
diff --git a/libcxx/test/libcxx/xopen_source.gen.py b/libcxx/test/libcxx/xopen_source.gen.py
new file mode 100644
index 00000000000000..39b8bd0e8ecef4
--- /dev/null
+++ b/libcxx/test/libcxx/xopen_source.gen.py
@@ -0,0 +1,42 @@
+# ===----------------------------------------------------------------------===##
+#
+# 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
+#
+# ===----------------------------------------------------------------------===##
+
+# Make sure that libc++ headers work when defining _XOPEN_SOURCE=500.
+# We may not want to guarantee this forever, but since this works today and
+# it's something that users rely on, it makes sense to put a test on it.
+#
+# https://github.com/llvm/llvm-project/issues/117630
+
+# RUN: %{python} %s %{libcxx-dir}/utils
+
+import sys
+
+sys.path.append(sys.argv[1])
+from libcxx.header_information import (
+ lit_header_restrictions,
+ lit_header_undeprecations,
+ public_headers,
+)
+
+for header in public_headers:
+ for version in (500, 600, 700):
+ # TODO: <fstream> currently uses ::fseeko unguarded, which fails with _XOPEN_SOURCE=500.
+ if header == "fstream" and version == 500:
+ continue
+
+ print(
+ f"""\
+//--- {header}.xopen_source_{version}.compile.pass.cpp
+{lit_header_restrictions.get(header, '')}
+{lit_header_undeprecations.get(header, '')}
+
+// ADDITIONAL_COMPILE_FLAGS: -D_XOPEN_SOURCE={version}
+
+#include <{header}>
+"""
+ )
More information about the libcxx-commits
mailing list