[libcxx-commits] [libcxx] [libc++] instantiate format_to externally (PR #71009)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 1 19:24:45 PDT 2023


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/71009

None

>From ab2e1d1c712dee22d85352c342c38a8375e624dc Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 22 Oct 2023 11:54:09 +0200
Subject: [PATCH] [libc++] instantiate format_to externally

---
 libcxx/include/__availability              | 10 ++++++++++
 libcxx/include/__format/format_error.h     |  4 ++++
 libcxx/include/__format/format_functions.h | 16 +++++++++++++--
 libcxx/src/CMakeLists.txt                  |  1 +
 libcxx/src/format.cpp                      | 23 ++++++++++++++++++++++
 5 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 libcxx/src/format.cpp

diff --git a/libcxx/include/__availability b/libcxx/include/__availability
index 99a16c968de3c60..b698754a7c6ce6e 100644
--- a/libcxx/include/__availability
+++ b/libcxx/include/__availability
@@ -150,6 +150,10 @@
 #  define _LIBCPP_AVAILABILITY_HAS_NO_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
 #endif
 
+// Enable extern instantiations for <format> functions. This significantly reduces the amount of code generated in every
+// TU that uses most of the <format> and <print> utilities.
+// #  define _LIBCPP_AVAILABILITY_HAS_NO_FORMAT_EXPLICIT_INSTANTIATIONS
+
 #elif defined(__APPLE__)
 
 #  if (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 50000)
@@ -247,6 +251,12 @@
       (defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__) && __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ < 80000)
 #    define _LIBCPP_AVAILABILITY_HAS_NO_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
 #  endif
+
+// TODO: Replace this with the correct conditions once Apple ships a dylib with the <format> functions
+#  if 1
+#    define _LIBCPP_AVAILABILITY_HAS_NO_FORMAT_EXPLICIT_INSTANTIATIONS
+#  endif
+
 #else
 
 // ...New vendors can add availability markup here...
diff --git a/libcxx/include/__format/format_error.h b/libcxx/include/__format/format_error.h
index 51d6c58230910e7..a186ba2a79c6d35 100644
--- a/libcxx/include/__format/format_error.h
+++ b/libcxx/include/__format/format_error.h
@@ -32,8 +32,12 @@ class _LIBCPP_EXPORTED_FROM_ABI format_error : public runtime_error {
       : runtime_error(__s) {}
   _LIBCPP_HIDE_FROM_ABI format_error(const format_error&) = default;
   _LIBCPP_HIDE_FROM_ABI format_error& operator=(const format_error&) = default;
+#ifdef _LIBCPP_AVAILABILITY_HAS_NO_FORMAT_EXPLICIT_INSTANTIATIONS
   _LIBCPP_HIDE_FROM_ABI_VIRTUAL
   ~format_error() noexcept override = default;
+#else
+  ~format_error() noexcept override;
+#endif
 };
 _LIBCPP_DIAGNOSTIC_POP
 
diff --git a/libcxx/include/__format/format_functions.h b/libcxx/include/__format/format_functions.h
index bb62c1ce10c15cb..bd8774e003bcf1b 100644
--- a/libcxx/include/__format/format_functions.h
+++ b/libcxx/include/__format/format_functions.h
@@ -397,19 +397,31 @@ requires(output_iterator<_OutIt, const _CharT&>) _LIBCPP_HIDE_FROM_ABI _OutIt
 // https://reviews.llvm.org/D110499#inline-1180704
 // TODO FMT Evaluate whether we want to file a Clang bug report regarding this.
 template <output_iterator<const char&> _OutIt>
-_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
+_LIBCPP_ALWAYS_INLINE _OutIt
 vformat_to(_OutIt __out_it, string_view __fmt, format_args __args) {
   return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
 }
 
 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
 template <output_iterator<const wchar_t&> _OutIt>
-_LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
+_LIBCPP_ALWAYS_INLINE _OutIt
 vformat_to(_OutIt __out_it, wstring_view __fmt, wformat_args __args) {
   return _VSTD::__vformat_to(_VSTD::move(__out_it), __fmt, __args);
 }
 #endif
 
+#ifndef _LIBCPP_AVAILABILITY_HAS_NO_FORMAT_EXPLICIT_INSTANTIATIONS
+
+extern template _LIBCPP_EXPORTED_FROM_ABI back_insert_iterator<string>
+    vformat_to<back_insert_iterator<string>>(back_insert_iterator<string>, string_view, format_args);
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+extern template _LIBCPP_EXPORTED_FROM_ABI back_insert_iterator<wstring>
+    vformat_to<back_insert_iterator<wstring>>(back_insert_iterator<wstring>, wstring_view, wformat_args);
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+#endif // _LIBCPP_AVAILABILITY_HAS_NO_FORMAT_EXPLICIT_INSTANTIATIONS
+
 template <output_iterator<const char&> _OutIt, class... _Args>
 _LIBCPP_ALWAYS_INLINE _LIBCPP_HIDE_FROM_ABI _OutIt
 format_to(_OutIt __out_it, format_string<_Args...> __fmt, _Args&&... __args) {
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index e57fbf1468acb2b..3557d13ae3f2825 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -17,6 +17,7 @@ set(LIBCXX_SOURCES
   filesystem/filesystem_error.cpp
   filesystem/path_parser.h
   filesystem/path.cpp
+  format.cpp
   functional.cpp
   future.cpp
   hash.cpp
diff --git a/libcxx/src/format.cpp b/libcxx/src/format.cpp
new file mode 100644
index 000000000000000..08cb9129dd120d6
--- /dev/null
+++ b/libcxx/src/format.cpp
@@ -0,0 +1,23 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <format>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template back_insert_iterator<string>
+    vformat_to<back_insert_iterator<string>>(back_insert_iterator<string>, string_view, format_args);
+
+#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
+template back_insert_iterator<wstring>
+    vformat_to<back_insert_iterator<wstring>>(back_insert_iterator<wstring>, wstring_view, wformat_args);
+#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
+
+format_error::~format_error() noexcept = default;
+
+_LIBCPP_END_NAMESPACE_STD



More information about the libcxx-commits mailing list