Skip to content

Commit 4fcce8d

Browse files
committed
Cleanups and improvements.
1 parent c132aa5 commit 4fcce8d

File tree

10 files changed

+454
-186
lines changed

10 files changed

+454
-186
lines changed

libcxx/include/__format/buffer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include <__algorithm/copy_n.h>
1414
#include <__algorithm/fill_n.h>
15-
#include <__algorithm/for_each.h>
1615
#include <__algorithm/max.h>
1716
#include <__algorithm/min.h>
1817
#include <__algorithm/ranges_copy.h>
@@ -35,13 +34,11 @@
3534
#include <__memory/construct_at.h>
3635
#include <__memory/destroy.h>
3736
#include <__memory/uninitialized_algorithms.h>
38-
#include <__system_error/system_error.h>
3937
#include <__type_traits/add_pointer.h>
4038
#include <__type_traits/conditional.h>
4139
#include <__utility/exception_guard.h>
4240
#include <__utility/move.h>
4341
#include <stdexcept>
44-
#include <stdio.h> // Uses the POSIX/Windows unlocked stream I/O
4542
#include <string_view>
4643

4744
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__ostream/print.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ _LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, fo
111111
ostream::sentry __s(__os);
112112
if (__s) {
113113
# ifndef _LIBCPP_WIN32API
114-
__print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
114+
__print::__vprint_unicode_posix<__print::__lock_policy::__stdio>(__file, __fmt, __args, __write_nl, true);
115115
# elif _LIBCPP_HAS_WIDE_CHARACTERS
116-
__print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
116+
__print::__vprint_unicode_windows<__print::__lock_policy::__stdio>(__file, __fmt, __args, __write_nl, true);
117117
# else
118118
# error "Windows builds with wchar_t disabled are not supported."
119119
# endif

libcxx/include/print

Lines changed: 105 additions & 152 deletions
Large diffs are not rendered by default.

libcxx/modules/std/print.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export namespace std {
1414
using std::println;
1515

1616
using std::vprint_nonunicode;
17+
using ::std::vprint_nonunicode_buffered;
1718
# if _LIBCPP_HAS_UNICODE
1819
using std::vprint_unicode;
1920
using std::vprint_unicode_buffered;

libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_posix.pass.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// <print>
2222

2323
// Tests the implementation of
24+
// template <__print::__lock_policy __policy>
2425
// void __print::__vprint_unicode_posix(FILE* __stream, string_view __fmt,
2526
// format_args __args, bool __write_nl,
2627
// bool __is_terminal);
@@ -39,7 +40,8 @@
3940

4041
#include "test_macros.h"
4142

42-
int main(int, char**) {
43+
template <std::__print::__lock_policy policy>
44+
static void test() {
4345
std::array<char, 100> buffer;
4446
std::ranges::fill(buffer, '*');
4547

@@ -55,15 +57,15 @@ int main(int, char**) {
5557
#endif
5658

5759
// Test writing to a "non-terminal" stream does not flush.
58-
std::__print::__vprint_unicode_posix(file, " world", std::make_format_args(), false, false);
60+
std::__print::__vprint_unicode_posix<policy>(file, " world", std::make_format_args(), false, false);
5961
assert(std::ftell(file) == 11);
6062
#if defined(TEST_HAS_GLIBC) && \
6163
!(__has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || __has_feature(memory_sanitizer))
6264
assert(std::ranges::all_of(buffer, [](char c) { return c == '*'; }));
6365
#endif
6466

6567
// Test writing to a "terminal" stream flushes before writing.
66-
std::__print::__vprint_unicode_posix(file, "!", std::make_format_args(), false, true);
68+
std::__print::__vprint_unicode_posix<policy>(file, "!", std::make_format_args(), false, true);
6769
assert(std::ftell(file) == 12);
6870
assert(std::string_view(buffer.data(), buffer.data() + 11) == "Hello world");
6971
#if defined(TEST_HAS_GLIBC)
@@ -74,6 +76,11 @@ int main(int, char**) {
7476
// Test everything is written when closing the stream.
7577
std::fclose(file);
7678
assert(std::string_view(buffer.data(), buffer.data() + 12) == "Hello world!");
79+
}
80+
81+
int main(int, char**) {
82+
test<std::__print::__lock_policy::__manual>();
83+
test<std::__print::__lock_policy::__stdio>();
7784

7885
return 0;
7986
}

libcxx/test/libcxx/input.output/iostream.format/print.fun/vprint_unicode_windows.pass.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// <print>
2121

2222
// Tests the implementation of
23+
// template <__print::__lock_policy __policy>
2324
// void __print::__vprint_unicode_windows(FILE* __stream, string_view __fmt,
2425
// format_args __args, bool __write_nl,
2526
// bool __is_terminal);
@@ -47,8 +48,8 @@ TEST_GCC_DIAGNOSTIC_IGNORED("-Wuse-after-free")
4748

4849
#define SV(S) MAKE_STRING_VIEW(wchar_t, S)
4950

50-
bool calling = false;
51-
std::wstring_view expected = L" world";
51+
bool calling = true;
52+
std::wstring_view expected;
5253

5354
void write_to_console(FILE*, std::wstring_view data) {
5455
assert(calling);
@@ -58,29 +59,35 @@ void write_to_console(FILE*, std::wstring_view data) {
5859
scoped_test_env env;
5960
std::string filename = env.create_file("output.txt");
6061

62+
template <std::__print::__lock_policy policy>
6163
static void test_basics() {
6264
FILE* file = std::fopen(filename.c_str(), "wb");
6365
assert(file);
6466

67+
calling = false;
68+
expected = L" world";
69+
6570
// Test writing to a "non-terminal" stream does not call WriteConsoleW.
66-
std::__print::__vprint_unicode_windows(file, "Hello", std::make_format_args(), false, false);
71+
std::__print::__vprint_unicode_windows<policy>(file, "Hello", std::make_format_args(), false, false);
6772
assert(std::ftell(file) == 5);
6873

6974
// It's not possible to reliably test whether writing to a "terminal" stream
7075
// flushes before writing. Testing flushing a closed stream worked on some
7176
// platforms, but was unreliable.
7277
calling = true;
73-
std::__print::__vprint_unicode_windows(file, " world", std::make_format_args(), false, true);
78+
std::__print::__vprint_unicode_windows<policy>(file, " world", std::make_format_args(), false, true);
79+
std::fclose(file);
7480
}
7581

7682
// When the output is a file the data is written as-is.
7783
// When the output is a "terminal" invalid UTF-8 input is flagged.
84+
template <std::__print::__lock_policy policy>
7885
static void test(std::wstring_view output, std::string_view input) {
7986
// *** File ***
8087
FILE* file = std::fopen(filename.c_str(), "wb");
8188
assert(file);
8289

83-
std::__print::__vprint_unicode_windows(file, input, std::make_format_args(), false, false);
90+
std::__print::__vprint_unicode_windows<policy>(file, input, std::make_format_args(), false, false);
8491
assert(std::ftell(file) == static_cast<long>(input.size()));
8592
std::fclose(file);
8693

@@ -95,12 +102,13 @@ static void test(std::wstring_view output, std::string_view input) {
95102

96103
// *** Terminal ***
97104
expected = output;
98-
std::__print::__vprint_unicode_windows(file, input, std::make_format_args(), false, true);
105+
std::__print::__vprint_unicode_windows<policy>(file, input, std::make_format_args(), false, true);
99106
}
100107

108+
template <std::__print::__lock_policy policy>
101109
static void test() {
102110
// *** Test valid UTF-8 ***
103-
#define TEST(S) test(SV(S), S)
111+
#define TEST(S) test<policy>(SV(S), S)
104112
TEST("hello world");
105113

106114
// copied from benchmarks/std_format_spec_string_unicode.bench.cpp
@@ -112,24 +120,29 @@ static void test() {
112120
#undef TEST
113121

114122
// *** Test invalid utf-8 ***
115-
test(SV("\ufffd"), "\xc3");
116-
test(SV("\ufffd("), "\xc3\x28");
123+
test<policy>(SV("\ufffd"), "\xc3");
124+
test<policy>(SV("\ufffd("), "\xc3\x28");
117125

118126
// surrogate range
119-
test(SV("\ufffd"), "\xed\xa0\x80"); // U+D800
120-
test(SV("\ufffd"), "\xed\xaf\xbf"); // U+DBFF
121-
test(SV("\ufffd"), "\xed\xbf\x80"); // U+DC00
122-
test(SV("\ufffd"), "\xed\xbf\xbf"); // U+DFFF
127+
test<policy>(SV("\ufffd"), "\xed\xa0\x80"); // U+D800
128+
test<policy>(SV("\ufffd"), "\xed\xaf\xbf"); // U+DBFF
129+
test<policy>(SV("\ufffd"), "\xed\xbf\x80"); // U+DC00
130+
test<policy>(SV("\ufffd"), "\xed\xbf\xbf"); // U+DFFF
123131

124132
// beyond valid values
125-
test(SV("\ufffd"), "\xf4\x90\x80\x80"); // U+110000
126-
test(SV("\ufffd"), "\xf4\xbf\xbf\xbf"); // U+11FFFF
133+
test<policy>(SV("\ufffd"), "\xf4\x90\x80\x80"); // U+110000
134+
test<policy>(SV("\ufffd"), "\xf4\xbf\xbf\xbf"); // U+11FFFF
127135

128136
// Validates http://unicode.org/review/pr-121.html option 3.
129-
test(SV("\u0061\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u0062"), "\x61\xf1\x80\x80\xe1\x80\xc2\x62");
137+
test<policy>(SV("\u0061\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u0062"), "\x61\xf1\x80\x80\xe1\x80\xc2\x62");
130138
}
131139

132140
int main(int, char**) {
133-
test_basics();
134-
test();
141+
test_basics<std::__print::__lock_policy::__manual>();
142+
test<std::__print::__lock_policy::__manual>();
143+
144+
test_basics<std::__print::__lock_policy::__stdio>();
145+
test<std::__print::__lock_policy::__stdio>();
146+
147+
return 0;
135148
}

libcxx/test/libcxx/transitive_includes/cxx23.csv

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ queue deque
764764
queue initializer_list
765765
queue iosfwd
766766
queue limits
767-
queue optional
768767
queue stdexcept
769768
queue string
770769
queue string_view
@@ -831,7 +830,6 @@ regex deque
831830
regex initializer_list
832831
regex iosfwd
833832
regex limits
834-
regex optional
835833
regex stdexcept
836834
regex string
837835
regex string_view
@@ -1076,7 +1074,6 @@ thread iosfwd
10761074
thread istream
10771075
thread limits
10781076
thread locale
1079-
thread optional
10801077
thread ratio
10811078
thread sstream
10821079
thread stdexcept
@@ -1148,7 +1145,6 @@ vector cwctype
11481145
vector initializer_list
11491146
vector iosfwd
11501147
vector limits
1151-
vector optional
11521148
vector stdexcept
11531149
vector string
11541150
vector string_view

libcxx/test/libcxx/transitive_includes/cxx26.csv

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,6 @@ queue deque
763763
queue initializer_list
764764
queue iosfwd
765765
queue limits
766-
queue optional
767766
queue stdexcept
768767
queue string
769768
queue string_view
@@ -830,7 +829,6 @@ regex deque
830829
regex initializer_list
831830
regex iosfwd
832831
regex limits
833-
regex optional
834832
regex stdexcept
835833
regex string
836834
regex string_view
@@ -1075,7 +1073,6 @@ thread iosfwd
10751073
thread istream
10761074
thread limits
10771075
thread locale
1078-
thread optional
10791076
thread ratio
10801077
thread sstream
10811078
thread stdexcept
@@ -1147,7 +1144,6 @@ vector cwctype
11471144
vector initializer_list
11481145
vector iosfwd
11491146
vector limits
1150-
vector optional
11511147
vector stdexcept
11521148
vector string
11531149
vector string_view

0 commit comments

Comments
 (0)