[libcxx-commits] [libcxx] [libcxx] proper guarding for locale usage in filesystem on Windows (PR #165470)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 28 13:29:11 PDT 2025


https://github.com/moleium updated https://github.com/llvm/llvm-project/pull/165470

>From 005ac72d989d7798a7985b104fa8ca65c98bdb49 Mon Sep 17 00:00:00 2001
From: moleium <molenoch at protonmail.com>
Date: Tue, 28 Oct 2025 22:42:35 +0300
Subject: [PATCH 1/2] [libcxx] proper guarding for locale usage in filesystem
 on Windows

- Resolves build issues when localization support is disabled on Windows.
- Resolves dependencies on localization in filesystem header implementations.

Related PR #164602
Fixes #164074
---
 libcxx/include/__filesystem/path.h   |  6 ++--
 libcxx/include/__filesystem/u8path.h | 42 +++++++++++++++-------------
 2 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/libcxx/include/__filesystem/path.h b/libcxx/include/__filesystem/path.h
index b3f324342f800..990833116aebe 100644
--- a/libcxx/include/__filesystem/path.h
+++ b/libcxx/include/__filesystem/path.h
@@ -324,6 +324,7 @@ struct _PathCVT<char> {
   }
 };
 
+#    if _LIBCPP_HAS_LOCALIZATION
 template <class _ECharT>
 struct _PathExport {
   typedef __narrow_to_utf8<sizeof(wchar_t) * __CHAR_BIT__> _Narrower;
@@ -364,7 +365,7 @@ struct _PathExport<char16_t> {
   }
 };
 
-#    if _LIBCPP_HAS_CHAR8_T
+#      if _LIBCPP_HAS_CHAR8_T
 template <>
 struct _PathExport<char8_t> {
   typedef __narrow_to_utf8<sizeof(wchar_t) * __CHAR_BIT__> _Narrower;
@@ -374,7 +375,8 @@ struct _PathExport<char8_t> {
     _Narrower()(back_inserter(__dest), __src.data(), __src.data() + __src.size());
   }
 };
-#    endif // _LIBCPP_HAS_CHAR8_T
+#      endif // _LIBCPP_HAS_CHAR8_T
+#    endif // _LIBCPP_HAS_LOCALIZATION
 #  endif   /* _LIBCPP_WIN32API */
 
 class _LIBCPP_EXPORTED_FROM_ABI path {
diff --git a/libcxx/include/__filesystem/u8path.h b/libcxx/include/__filesystem/u8path.h
index 885372b0d1f86..7077b96ce48e6 100644
--- a/libcxx/include/__filesystem/u8path.h
+++ b/libcxx/include/__filesystem/u8path.h
@@ -13,7 +13,10 @@
 #include <__algorithm/unwrap_iter.h>
 #include <__config>
 #include <__filesystem/path.h>
+
+#if _LIBCPP_HAS_LOCALIZATION
 #include <__locale>
+#endif
 #include <string>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -24,28 +27,30 @@
 
 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
 
+#  if !defined(_LIBCPP_WIN32API) || _LIBCPP_HAS_LOCALIZATION
 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _InputIt __l) {
   static_assert(
-#  if _LIBCPP_HAS_CHAR8_T
-      is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
-#  endif
-          is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
-      "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
-      " or 'char8_t'");
-#  if defined(_LIBCPP_WIN32API)
-  string __tmp(__f, __l);
-  using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
-  std::wstring __w;
-  __w.reserve(__tmp.size());
-  _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
-  return path(__w);
-#  else
-  return path(__f, __l);
-#  endif /* !_LIBCPP_WIN32API */
+#    if _LIBCPP_HAS_CHAR8_T
+        is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
+#    endif
+            is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
+        "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
+        " or 'char8_t'");
+#    if defined(_LIBCPP_WIN32API)
+    string __tmp(__f, __l);
+    using _CVT = __widen_from_utf8<sizeof(wchar_t) * __CHAR_BIT__>;
+    std::wstring __w;
+    __w.reserve(__tmp.size());
+    _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
+    return path(__w);
+#    else
+    return path(__f, __l);
+#    endif /* !_LIBCPP_WIN32API */
 }
+#  endif /* !_LIBCPP_WIN32API || _LIBCPP_HAS_LOCALIZATION */
 
-#  if defined(_LIBCPP_WIN32API)
+#  if defined(_LIBCPP_WIN32API) && _LIBCPP_HAS_LOCALIZATION
 template <class _InputIt, __enable_if_t<__is_pathable<_InputIt>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f, _NullSentinel) {
   static_assert(
@@ -65,7 +70,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(_InputIt __f,
   _CVT()(back_inserter(__w), __tmp.data(), __tmp.data() + __tmp.size());
   return path(__w);
 }
-#  endif /* _LIBCPP_WIN32API */
+#  endif /* _LIBCPP_WIN32API && _LIBCPP_HAS_LOCALIZATION */
 
 template <class _Source, __enable_if_t<__is_pathable<_Source>::value, int> = 0>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source& __s) {
@@ -85,7 +90,6 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source&
 }
 
 _LIBCPP_END_NAMESPACE_FILESYSTEM
-
 #endif // _LIBCPP_STD_VER >= 17
 
 #endif // _LIBCPP___FILESYSTEM_U8PATH_H

>From de27e342bfe35a81388c37c189968246704a94b6 Mon Sep 17 00:00:00 2001
From: moleium <molenoch at protonmail.com>
Date: Tue, 28 Oct 2025 23:28:49 +0300
Subject: [PATCH 2/2] [libcxx] Fix indentation for macros in filesystem header

Co-authored-by: Hxnter999 <Hxnter999 at users.noreply.github.com>
---
 libcxx/include/__filesystem/u8path.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__filesystem/u8path.h b/libcxx/include/__filesystem/u8path.h
index 7077b96ce48e6..bbf7723c30aff 100644
--- a/libcxx/include/__filesystem/u8path.h
+++ b/libcxx/include/__filesystem/u8path.h
@@ -15,7 +15,7 @@
 #include <__filesystem/path.h>
 
 #if _LIBCPP_HAS_LOCALIZATION
-#include <__locale>
+  #include <__locale>
 #endif
 #include <string>
 
@@ -90,6 +90,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_WITH_CHAR8_T path u8path(const _Source&
 }
 
 _LIBCPP_END_NAMESPACE_FILESYSTEM
+
 #endif // _LIBCPP_STD_VER >= 17
 
 #endif // _LIBCPP___FILESYSTEM_U8PATH_H



More information about the libcxx-commits mailing list