[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 Nov 26 11:18:00 PST 2024


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/117764

>From fb4d85bfcd9457ce757b8e3ecfccc22e51825431 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 1/2] [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        | 41 +++++++++++++++++++
 3 files changed, 56 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 8ed4c29cb8732f..89bb2a724fff09 100644
--- a/libcxx/include/__locale_dir/locale_base_api.h
+++ b/libcxx/include/__locale_dir/locale_base_api.h
@@ -23,13 +23,17 @@
 // 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: I think __uselocale() is not necessary if we refactor a bit.
 // 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
 //  __locale_t  __uselocale(__locale_t);
 //  __locale_t  __newlocale(int, const char*, __locale_t);
 //  void        __freelocale(__locale_t);
@@ -51,8 +55,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);
@@ -89,9 +93,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 cce6de64673b0e..ef9bf8c07543b2 100644
--- a/libcxx/include/__locale_dir/support/bsd_like.h
+++ b/libcxx/include/__locale_dir/support/bsd_like.h
@@ -144,19 +144,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..94298de0289fd3
--- /dev/null
+++ b/libcxx/test/libcxx/xopen_source.gen.py
@@ -0,0 +1,41 @@
+# ===----------------------------------------------------------------------===##
+#
+# 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}>
+"""
+        )

>From 1d1e32bad6e9ab6608b52105ef86109b49a7faaf Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 26 Nov 2024 14:17:15 -0500
Subject: [PATCH 2/2] Formatting

---
 libcxx/include/__locale_dir/support/bsd_like.h | 8 ++++----
 libcxx/test/libcxx/xopen_source.gen.py         | 5 +++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__locale_dir/support/bsd_like.h b/libcxx/include/__locale_dir/support/bsd_like.h
index ef9bf8c07543b2..c5001bc54f8de7 100644
--- a/libcxx/include/__locale_dir/support/bsd_like.h
+++ b/libcxx/include/__locale_dir/support/bsd_like.h
@@ -144,23 +144,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
+#  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
+#  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
+#  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
+#  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
index 94298de0289fd3..39b8bd0e8ecef4 100644
--- a/libcxx/test/libcxx/xopen_source.gen.py
+++ b/libcxx/test/libcxx/xopen_source.gen.py
@@ -26,10 +26,11 @@
 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:
+        if header == "fstream" and version == 500:
             continue
 
-        print(f"""\
+        print(
+            f"""\
 //--- {header}.xopen_source_{version}.compile.pass.cpp
 {lit_header_restrictions.get(header, '')}
 {lit_header_undeprecations.get(header, '')}



More information about the libcxx-commits mailing list