[libcxx-commits] [libcxx] [libc++][print] Includes <format>. (PR #76333)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Sun Dec 24 06:36:14 PST 2023


https://github.com/mordante updated https://github.com/llvm/llvm-project/pull/76333

>From 6d8ab1f9a88312694c406bb3d00def6959cb6375 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sun, 24 Dec 2023 14:25:01 +0100
Subject: [PATCH] [libc++][print] Includes <format>.

The Standard does not require that <print> includes <format>. However
without including <format> formatting ranges does not work. Libc++ uses
granularized headers so it's possible to implement <print> without
<format>. Both libstdc++ and MSVC STL don't use granularized headers so
the example likely works once these libraries implement print.

Even without these implementations doing that it seems reasonable to
expect examples that work for `std::format` also work with `std::print`
without adding additional includes.

Instead of only including the ranges part of format include the entire
header. This ensures future additions to <format> will be available in
<print> and <ostream> too.

Addresses: https://github.com/llvm/llvm-project/issues/71925
---
 libcxx/include/print                          |  8 +----
 .../print.fun/includes.compile.pass.cpp       | 34 +++++++++++++++++++
 .../test/libcxx/transitive_includes/cxx03.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx11.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx14.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx17.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx20.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx23.csv | 10 +-----
 .../test/libcxx/transitive_includes/cxx26.csv | 10 +-----
 9 files changed, 42 insertions(+), 70 deletions(-)
 create mode 100644 libcxx/test/libcxx/input.output/iostream.format/print.fun/includes.compile.pass.cpp

diff --git a/libcxx/include/print b/libcxx/include/print
index 0f8e73f8eb5c74..3c878bde7580e9 100644
--- a/libcxx/include/print
+++ b/libcxx/include/print
@@ -34,17 +34,11 @@ namespace std {
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__concepts/same_as.h>
 #include <__config>
-#include <__format/buffer.h>
-#include <__format/format_arg_store.h>
-#include <__format/format_args.h>
-#include <__format/format_context.h>
-#include <__format/format_error.h>
-#include <__format/format_functions.h>
-#include <__format/unicode.h>
 #include <__system_error/system_error.h>
 #include <__utility/forward.h>
 #include <cerrno>
 #include <cstdio>
+#include <format>
 #include <string>
 #include <string_view>
 #include <version>
diff --git a/libcxx/test/libcxx/input.output/iostream.format/print.fun/includes.compile.pass.cpp b/libcxx/test/libcxx/input.output/iostream.format/print.fun/includes.compile.pass.cpp
new file mode 100644
index 00000000000000..c6cbe6834b5e9b
--- /dev/null
+++ b/libcxx/test/libcxx/input.output/iostream.format/print.fun/includes.compile.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// UNSUPPORTED: no-filesystem
+// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
+
+// XFAIL: availability-fp_to_chars-missing
+
+// <print>
+
+// The Standard does not require that <print> includes <format>. However
+// without including <format> formatting ranges does not work. Libc++ uses
+// granularized headers so it's possible to implement <print> without <format>.
+// Both libstdc++ and MSVC STL don't use granularized headers so the code in
+// this tests most likely works in these implementations. This validates it
+// works with our implementation too.
+//
+// https://github.com/llvm/llvm-project/issues/71925
+
+#include <print>
+#include <vector>
+#include <iostream>
+
+void test() {
+  std::vector<int> v{1, 2, 3};
+  std::print("{} {}", 42, v);
+  std::print(stdout, "{} {}", 42, v);
+  std::print(std::cout, "{} {}", 42, v);
+}
diff --git a/libcxx/test/libcxx/transitive_includes/cxx03.csv b/libcxx/test/libcxx/transitive_includes/cxx03.csv
index 3f066342717624..994d3f5faf0f35 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx03.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx03.csv
@@ -600,21 +600,13 @@ ostream string_view
 ostream tuple
 ostream type_traits
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue concepts
diff --git a/libcxx/test/libcxx/transitive_includes/cxx11.csv b/libcxx/test/libcxx/transitive_includes/cxx11.csv
index 7b443e5a0ec0f9..dd6263f4d77da2 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx11.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx11.csv
@@ -605,21 +605,13 @@ ostream string_view
 ostream tuple
 ostream type_traits
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue concepts
diff --git a/libcxx/test/libcxx/transitive_includes/cxx14.csv b/libcxx/test/libcxx/transitive_includes/cxx14.csv
index a5b77ec79bb5c3..147dcd10da0280 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx14.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx14.csv
@@ -607,21 +607,13 @@ ostream string_view
 ostream tuple
 ostream type_traits
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue concepts
diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv
index a5b77ec79bb5c3..147dcd10da0280 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx17.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv
@@ -607,21 +607,13 @@ ostream string_view
 ostream tuple
 ostream type_traits
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue concepts
diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv
index c8c84867fda459..fc7e4f03c7bda4 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx20.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv
@@ -612,21 +612,13 @@ ostream string_view
 ostream tuple
 ostream type_traits
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue concepts
diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv
index a4fa50dc014aba..c4c66ab97d212c 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx23.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv
@@ -416,21 +416,13 @@ ostream string
 ostream string_view
 ostream tuple
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue cstddef
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index a4fa50dc014aba..c4c66ab97d212c 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -416,21 +416,13 @@ ostream string
 ostream string_view
 ostream tuple
 ostream version
-print array
 print cerrno
-print cmath
 print cstddef
-print cstdint
 print cstdio
-print initializer_list
-print limits
-print locale
-print new
-print optional
+print format
 print stdexcept
 print string
 print string_view
-print tuple
 print version
 queue compare
 queue cstddef



More information about the libcxx-commits mailing list