[libcxx-commits] [libcxx] [libc++] Remove <istream> and <ostream> includes from <iomanip> (PR #116223)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Nov 14 11:30:01 PST 2024
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/116223
>From 188d8890b9bcbd33baeef707733071ccb8346e6d Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 14 Nov 2024 14:04:38 +0100
Subject: [PATCH] [libc++] Remove <istream> and <ostream> includes from
<iomanip>
---
libcxx/include/CMakeLists.txt | 2 +
libcxx/include/__locale_dir/pad_and_output.h | 83 +++++++++++++++++++
libcxx/include/__ostream/basic_ostream.h | 28 +------
.../__ostream/put_character_sequence.h | 59 +++++++++++++
libcxx/include/iomanip | 19 ++++-
libcxx/include/locale | 61 +-------------
libcxx/include/module.modulemap | 5 +-
.../test/libcxx/transitive_includes/cxx23.csv | 28 -------
.../test/libcxx/transitive_includes/cxx26.csv | 28 -------
9 files changed, 167 insertions(+), 146 deletions(-)
create mode 100644 libcxx/include/__locale_dir/pad_and_output.h
create mode 100644 libcxx/include/__ostream/put_character_sequence.h
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 1610d1ee848a5f..d77770475a84a7 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -503,6 +503,7 @@ set(files
__locale_dir/locale_base_api/openbsd.h
__locale_dir/locale_base_api/win32.h
__locale_dir/locale_guard.h
+ __locale_dir/pad_and_output.h
__locale_dir/support/apple.h
__locale_dir/support/bsd_like.h
__locale_dir/support/freebsd.h
@@ -596,6 +597,7 @@ set(files
__numeric/transform_reduce.h
__ostream/basic_ostream.h
__ostream/print.h
+ __ostream/put_character_sequence.h
__pstl/backend.h
__pstl/backend_fwd.h
__pstl/backends/default.h
diff --git a/libcxx/include/__locale_dir/pad_and_output.h b/libcxx/include/__locale_dir/pad_and_output.h
new file mode 100644
index 00000000000000..c35c5df90801ef
--- /dev/null
+++ b/libcxx/include/__locale_dir/pad_and_output.h
@@ -0,0 +1,83 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_PAD_AND_OUTPUT_H
+#define _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
+
+#include <__config>
+#include <ios>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT, class _OutputIterator>
+_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
+ _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
+ streamsize __sz = __oe - __ob;
+ streamsize __ns = __iob.width();
+ if (__ns > __sz)
+ __ns -= __sz;
+ else
+ __ns = 0;
+ for (; __ob < __op; ++__ob, ++__s)
+ *__s = *__ob;
+ for (; __ns; --__ns, ++__s)
+ *__s = __fl;
+ for (; __ob < __oe; ++__ob, ++__s)
+ *__s = *__ob;
+ __iob.width(0);
+ return __s;
+}
+
+template <class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
+ ostreambuf_iterator<_CharT, _Traits> __s,
+ const _CharT* __ob,
+ const _CharT* __op,
+ const _CharT* __oe,
+ ios_base& __iob,
+ _CharT __fl) {
+ if (__s.__sbuf_ == nullptr)
+ return __s;
+ streamsize __sz = __oe - __ob;
+ streamsize __ns = __iob.width();
+ if (__ns > __sz)
+ __ns -= __sz;
+ else
+ __ns = 0;
+ streamsize __np = __op - __ob;
+ if (__np > 0) {
+ if (__s.__sbuf_->sputn(__ob, __np) != __np) {
+ __s.__sbuf_ = nullptr;
+ return __s;
+ }
+ }
+ if (__ns > 0) {
+ basic_string<_CharT, _Traits> __sp(__ns, __fl);
+ if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
+ __s.__sbuf_ = nullptr;
+ return __s;
+ }
+ }
+ __np = __oe - __op;
+ if (__np > 0) {
+ if (__s.__sbuf_->sputn(__op, __np) != __np) {
+ __s.__sbuf_ = nullptr;
+ return __s;
+ }
+ }
+ __iob.width(0);
+ return __s;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___LOCALE_DIR_PAD_AND_OUTPUT_H
diff --git a/libcxx/include/__ostream/basic_ostream.h b/libcxx/include/__ostream/basic_ostream.h
index c37566d2356caf..ad43c72a3c2eae 100644
--- a/libcxx/include/__ostream/basic_ostream.h
+++ b/libcxx/include/__ostream/basic_ostream.h
@@ -16,6 +16,7 @@
# include <__exception/operations.h>
# include <__memory/shared_ptr.h>
# include <__memory/unique_ptr.h>
+# include <__ostream/put_character_sequence.h>
# include <__system_error/error_code.h>
# include <__type_traits/conjunction.h>
# include <__type_traits/enable_if.h>
@@ -496,33 +497,6 @@ basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const
return *this;
}
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
-__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
-# if _LIBCPP_HAS_EXCEPTIONS
- try {
-# endif // _LIBCPP_HAS_EXCEPTIONS
- typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
- if (__s) {
- typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
- if (std::__pad_and_output(
- _Ip(__os),
- __str,
- (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
- __str + __len,
- __os,
- __os.fill())
- .failed())
- __os.setstate(ios_base::badbit | ios_base::failbit);
- }
-# if _LIBCPP_HAS_EXCEPTIONS
- } catch (...) {
- __os.__set_badbit_and_consider_rethrow();
- }
-# endif // _LIBCPP_HAS_EXCEPTIONS
- return __os;
-}
-
template <class _CharT, class _Traits>
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
return std::__put_character_sequence(__os, &__c, 1);
diff --git a/libcxx/include/__ostream/put_character_sequence.h b/libcxx/include/__ostream/put_character_sequence.h
new file mode 100644
index 00000000000000..2a143acaed6eb7
--- /dev/null
+++ b/libcxx/include/__ostream/put_character_sequence.h
@@ -0,0 +1,59 @@
+//===---------------------------------------------------------------------===//
+//
+// 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___OSTREAM_PRINT_H
+#define _LIBCPP___OSTREAM_PRINT_H
+
+#include <__config>
+
+#if _LIBCPP_HAS_LOCALIZATION
+
+# include <__cstddef/size_t.h>
+# include <__fwd/ostream.h>
+# include <__iterator/ostreambuf_iterator.h>
+# include <__locale_dir/pad_and_output.h>
+# include <ios>
+
+# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+# endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _CharT, class _Traits>
+_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
+__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
+# if _LIBCPP_HAS_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_EXCEPTIONS
+ typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
+ if (__s) {
+ typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
+ if (std::__pad_and_output(
+ _Ip(__os),
+ __str,
+ (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
+ __str + __len,
+ __os,
+ __os.fill())
+ .failed())
+ __os.setstate(ios_base::badbit | ios_base::failbit);
+ }
+# if _LIBCPP_HAS_EXCEPTIONS
+ } catch (...) {
+ __os.__set_badbit_and_consider_rethrow();
+ }
+# endif // _LIBCPP_HAS_EXCEPTIONS
+ return __os;
+}
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_HAS_LOCALIZATION
+
+#endif // _LIBCPP___OSTREAM_PRINT_H
diff --git a/libcxx/include/iomanip b/libcxx/include/iomanip
index 8c711c94f11762..6118f8fb5b417b 100644
--- a/libcxx/include/iomanip
+++ b/libcxx/include/iomanip
@@ -46,9 +46,9 @@ template <class charT, class traits, class Allocator>
#if _LIBCPP_HAS_LOCALIZATION
-# include <__ostream/basic_ostream.h>
+# include <__ostream/put_character_sequence.h>
# include <ios>
-# include <istream>
+# include <iosfwd>
# include <locale>
# include <version>
@@ -547,4 +547,19 @@ _LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_HAS_LOCALIZATION
+#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
+# include <array>
+# include <bitset>
+# include <deque>
+# include <format>
+# include <functional>
+# include <istream>
+# include <ostream>
+# include <print>
+# include <queue>
+# include <stack>
+# include <unordered_map>
+# include <vector>
+#endif
+
#endif // _LIBCPP_IOMANIP
diff --git a/libcxx/include/locale b/libcxx/include/locale
index c887140f38c224..65f5c6ba52f736 100644
--- a/libcxx/include/locale
+++ b/libcxx/include/locale
@@ -203,6 +203,7 @@ template <class charT> class messages_byname;
# include <__iterator/istreambuf_iterator.h>
# include <__iterator/ostreambuf_iterator.h>
# include <__locale>
+# include <__locale_dir/pad_and_output.h>
# include <__memory/unique_ptr.h>
# include <__type_traits/make_unsigned.h>
# include <cerrno>
@@ -1239,66 +1240,6 @@ protected:
template <class _CharT, class _OutputIterator>
locale::id num_put<_CharT, _OutputIterator>::id;
-template <class _CharT, class _OutputIterator>
-_LIBCPP_HIDE_FROM_ABI _OutputIterator __pad_and_output(
- _OutputIterator __s, const _CharT* __ob, const _CharT* __op, const _CharT* __oe, ios_base& __iob, _CharT __fl) {
- streamsize __sz = __oe - __ob;
- streamsize __ns = __iob.width();
- if (__ns > __sz)
- __ns -= __sz;
- else
- __ns = 0;
- for (; __ob < __op; ++__ob, ++__s)
- *__s = *__ob;
- for (; __ns; --__ns, ++__s)
- *__s = __fl;
- for (; __ob < __oe; ++__ob, ++__s)
- *__s = *__ob;
- __iob.width(0);
- return __s;
-}
-
-template <class _CharT, class _Traits>
-_LIBCPP_HIDE_FROM_ABI ostreambuf_iterator<_CharT, _Traits> __pad_and_output(
- ostreambuf_iterator<_CharT, _Traits> __s,
- const _CharT* __ob,
- const _CharT* __op,
- const _CharT* __oe,
- ios_base& __iob,
- _CharT __fl) {
- if (__s.__sbuf_ == nullptr)
- return __s;
- streamsize __sz = __oe - __ob;
- streamsize __ns = __iob.width();
- if (__ns > __sz)
- __ns -= __sz;
- else
- __ns = 0;
- streamsize __np = __op - __ob;
- if (__np > 0) {
- if (__s.__sbuf_->sputn(__ob, __np) != __np) {
- __s.__sbuf_ = nullptr;
- return __s;
- }
- }
- if (__ns > 0) {
- basic_string<_CharT, _Traits> __sp(__ns, __fl);
- if (__s.__sbuf_->sputn(__sp.data(), __ns) != __ns) {
- __s.__sbuf_ = nullptr;
- return __s;
- }
- }
- __np = __oe - __op;
- if (__np > 0) {
- if (__s.__sbuf_->sputn(__op, __np) != __np) {
- __s.__sbuf_ = nullptr;
- return __s;
- }
- }
- __iob.width(0);
- return __s;
-}
-
template <class _CharT, class _OutputIterator>
_OutputIterator
num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob, char_type __fl, bool __v) const {
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index cd08b2810e437b..2d8d7a3cae74c7 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1460,7 +1460,9 @@ module std [system] {
module locale {
header "locale"
- header "__locale_dir/locale_guard.h"
+
+ module locale_guard { header "__locale_dir/locale_guard.h" }
+ module pad_and_output { header "__locale_dir/pad_and_output.h" }
module support {
header "__locale_dir/locale_base_api.h"
@@ -1641,6 +1643,7 @@ module std [system] {
header "__ostream/print.h"
export *
}
+ module put_character_sequence { header "__ostream/put_character_sequence.h" }
header "ostream"
export *
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index 854ad1b5df6f1e..0b3feb4eb64fce 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -331,7 +331,6 @@ experimental/utility initializer_list
experimental/utility limits
experimental/utility utility
experimental/utility version
-filesystem bitset
filesystem cctype
filesystem cerrno
filesystem climits
@@ -349,7 +348,6 @@ filesystem initializer_list
filesystem iomanip
filesystem ios
filesystem iosfwd
-filesystem istream
filesystem limits
filesystem locale
filesystem new
@@ -410,13 +408,11 @@ forward_list limits
forward_list new
forward_list tuple
forward_list version
-fstream array
fstream bitset
fstream cctype
fstream cerrno
fstream climits
fstream clocale
-fstream cmath
fstream compare
fstream cstddef
fstream cstdint
@@ -427,7 +423,6 @@ fstream ctime
fstream cwchar
fstream cwctype
fstream filesystem
-fstream format
fstream initializer_list
fstream iomanip
fstream ios
@@ -436,9 +431,7 @@ fstream istream
fstream limits
fstream locale
fstream new
-fstream optional
fstream ostream
-fstream print
fstream ratio
fstream stdexcept
fstream streambuf
@@ -498,7 +491,6 @@ future tuple
future typeinfo
future version
initializer_list version
-iomanip bitset
iomanip cctype
iomanip cerrno
iomanip climits
@@ -515,7 +507,6 @@ iomanip cwctype
iomanip initializer_list
iomanip ios
iomanip iosfwd
-iomanip istream
iomanip limits
iomanip locale
iomanip new
@@ -552,13 +543,11 @@ ios tuple
ios typeinfo
ios version
iosfwd version
-iostream array
iostream bitset
iostream cctype
iostream cerrno
iostream climits
iostream clocale
-iostream cmath
iostream compare
iostream cstddef
iostream cstdint
@@ -568,7 +557,6 @@ iostream cstring
iostream ctime
iostream cwchar
iostream cwctype
-iostream format
iostream initializer_list
iostream ios
iostream iosfwd
@@ -576,9 +564,7 @@ iostream istream
iostream limits
iostream locale
iostream new
-iostream optional
iostream ostream
-iostream print
iostream ratio
iostream stdexcept
iostream streambuf
@@ -773,13 +759,11 @@ optional initializer_list
optional limits
optional new
optional version
-ostream array
ostream bitset
ostream cctype
ostream cerrno
ostream climits
ostream clocale
-ostream cmath
ostream compare
ostream cstddef
ostream cstdint
@@ -789,15 +773,12 @@ ostream cstring
ostream ctime
ostream cwchar
ostream cwctype
-ostream format
ostream initializer_list
ostream ios
ostream iosfwd
ostream limits
ostream locale
ostream new
-ostream optional
-ostream print
ostream ratio
ostream stdexcept
ostream streambuf
@@ -1081,13 +1062,11 @@ string_view limits
string_view new
string_view stdexcept
string_view version
-strstream array
strstream bitset
strstream cctype
strstream cerrno
strstream climits
strstream clocale
-strstream cmath
strstream compare
strstream cstddef
strstream cstdint
@@ -1097,7 +1076,6 @@ strstream cstring
strstream ctime
strstream cwchar
strstream cwctype
-strstream format
strstream initializer_list
strstream ios
strstream iosfwd
@@ -1105,9 +1083,7 @@ strstream istream
strstream limits
strstream locale
strstream new
-strstream optional
strstream ostream
-strstream print
strstream ratio
strstream stdexcept
strstream streambuf
@@ -1116,13 +1092,11 @@ strstream string_view
strstream tuple
strstream typeinfo
strstream version
-syncstream array
syncstream bitset
syncstream cctype
syncstream cerrno
syncstream climits
syncstream clocale
-syncstream cmath
syncstream compare
syncstream cstddef
syncstream cstdint
@@ -1132,7 +1106,6 @@ syncstream cstring
syncstream ctime
syncstream cwchar
syncstream cwctype
-syncstream format
syncstream initializer_list
syncstream ios
syncstream iosfwd
@@ -1143,7 +1116,6 @@ syncstream mutex
syncstream new
syncstream optional
syncstream ostream
-syncstream print
syncstream ratio
syncstream shared_mutex
syncstream stdexcept
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index ba2faaee5e3757..8d342b63c5bb09 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -331,7 +331,6 @@ experimental/utility initializer_list
experimental/utility limits
experimental/utility utility
experimental/utility version
-filesystem bitset
filesystem cctype
filesystem cerrno
filesystem climits
@@ -349,7 +348,6 @@ filesystem initializer_list
filesystem iomanip
filesystem ios
filesystem iosfwd
-filesystem istream
filesystem limits
filesystem locale
filesystem new
@@ -410,13 +408,11 @@ forward_list limits
forward_list new
forward_list tuple
forward_list version
-fstream array
fstream bitset
fstream cctype
fstream cerrno
fstream climits
fstream clocale
-fstream cmath
fstream compare
fstream cstddef
fstream cstdint
@@ -426,7 +422,6 @@ fstream cstring
fstream ctime
fstream cwchar
fstream cwctype
-fstream format
fstream initializer_list
fstream iomanip
fstream ios
@@ -435,9 +430,7 @@ fstream istream
fstream limits
fstream locale
fstream new
-fstream optional
fstream ostream
-fstream print
fstream ratio
fstream stdexcept
fstream streambuf
@@ -497,7 +490,6 @@ future tuple
future typeinfo
future version
initializer_list version
-iomanip bitset
iomanip cctype
iomanip cerrno
iomanip climits
@@ -514,7 +506,6 @@ iomanip cwctype
iomanip initializer_list
iomanip ios
iomanip iosfwd
-iomanip istream
iomanip limits
iomanip locale
iomanip new
@@ -551,13 +542,11 @@ ios tuple
ios typeinfo
ios version
iosfwd version
-iostream array
iostream bitset
iostream cctype
iostream cerrno
iostream climits
iostream clocale
-iostream cmath
iostream compare
iostream cstddef
iostream cstdint
@@ -567,7 +556,6 @@ iostream cstring
iostream ctime
iostream cwchar
iostream cwctype
-iostream format
iostream initializer_list
iostream ios
iostream iosfwd
@@ -575,9 +563,7 @@ iostream istream
iostream limits
iostream locale
iostream new
-iostream optional
iostream ostream
-iostream print
iostream ratio
iostream stdexcept
iostream streambuf
@@ -772,13 +758,11 @@ optional initializer_list
optional limits
optional new
optional version
-ostream array
ostream bitset
ostream cctype
ostream cerrno
ostream climits
ostream clocale
-ostream cmath
ostream compare
ostream cstddef
ostream cstdint
@@ -788,15 +772,12 @@ ostream cstring
ostream ctime
ostream cwchar
ostream cwctype
-ostream format
ostream initializer_list
ostream ios
ostream iosfwd
ostream limits
ostream locale
ostream new
-ostream optional
-ostream print
ostream ratio
ostream stdexcept
ostream streambuf
@@ -1080,13 +1061,11 @@ string_view limits
string_view new
string_view stdexcept
string_view version
-strstream array
strstream bitset
strstream cctype
strstream cerrno
strstream climits
strstream clocale
-strstream cmath
strstream compare
strstream cstddef
strstream cstdint
@@ -1096,7 +1075,6 @@ strstream cstring
strstream ctime
strstream cwchar
strstream cwctype
-strstream format
strstream initializer_list
strstream ios
strstream iosfwd
@@ -1104,9 +1082,7 @@ strstream istream
strstream limits
strstream locale
strstream new
-strstream optional
strstream ostream
-strstream print
strstream ratio
strstream stdexcept
strstream streambuf
@@ -1115,13 +1091,11 @@ strstream string_view
strstream tuple
strstream typeinfo
strstream version
-syncstream array
syncstream bitset
syncstream cctype
syncstream cerrno
syncstream climits
syncstream clocale
-syncstream cmath
syncstream compare
syncstream cstddef
syncstream cstdint
@@ -1131,7 +1105,6 @@ syncstream cstring
syncstream ctime
syncstream cwchar
syncstream cwctype
-syncstream format
syncstream initializer_list
syncstream ios
syncstream iosfwd
@@ -1142,7 +1115,6 @@ syncstream mutex
syncstream new
syncstream optional
syncstream ostream
-syncstream print
syncstream ratio
syncstream shared_mutex
syncstream stdexcept
More information about the libcxx-commits
mailing list