File: concept.formattable.compile.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (417 lines) | stat: -rw-r--r-- 14,931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//===----------------------------------------------------------------------===//
// 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

// This test uses std::filesystem::path, which is not always available
// XFAIL: availability-filesystem-missing

// <format>

// template<class T, class charT>
// concept formattable = ...

#include <array>
#include <bitset>
#include <bitset>
#include <chrono>
#include <complex>
#include <concepts>
#include <deque>
#include <filesystem>
#include <format>
#include <forward_list>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <queue>
#include <set>
#include <span>
#include <stack>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <variant>

#include "test_macros.h"
#include "min_allocator.h"

#ifndef TEST_HAS_NO_LOCALIZATION
#  include <regex>
#endif
#ifndef TEST_HAS_NO_THREADS
#  include <thread>
#endif

template <class T, class CharT>
void assert_is_not_formattable() {
  // clang-format off
  static_assert(!std::formattable<      T   , CharT>);
  static_assert(!std::formattable<      T&  , CharT>);
  static_assert(!std::formattable<      T&& , CharT>);
  static_assert(!std::formattable<const T   , CharT>);
  static_assert(!std::formattable<const T&  , CharT>);
  static_assert(!std::formattable<const T&& , CharT>);
  // clang-format on
}

template <class T, class CharT>
void assert_is_formattable() {
  // Only formatters for CharT == char || CharT == wchar_t are enabled for the
  // standard formatters. When CharT is a different type the formatter should
  // be disabled.
  if constexpr (std::same_as<CharT, char>
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
                || std::same_as<CharT, wchar_t>
#endif
  ) {
    // clang-format off
    static_assert(std::formattable<      T   , CharT>);
    static_assert(std::formattable<      T&  , CharT>);
    static_assert(std::formattable<      T&& , CharT>);
    static_assert(std::formattable<const T   , CharT>);
    static_assert(std::formattable<const T&  , CharT>);
    static_assert(std::formattable<const T&& , CharT>);
    // clang-format on
  } else
    assert_is_not_formattable<T, CharT>();
}

// Tests for P0645 Text Formatting
template <class CharT>
void test_P0645() {
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  // Tests the special formatter that converts a char to a wchar_t.
  assert_is_formattable<char, wchar_t>();
#endif
  assert_is_formattable<CharT, CharT>();

  assert_is_formattable<CharT*, CharT>();
  assert_is_formattable<const CharT*, CharT>();
  assert_is_formattable<CharT[42], CharT>();
  if constexpr (!std::same_as<CharT, int>) { // string and string_view only work with proper character types
    assert_is_formattable<std::basic_string<CharT>, CharT>();
    assert_is_formattable<std::basic_string_view<CharT>, CharT>();
  }

  assert_is_formattable<bool, CharT>();

  assert_is_formattable<signed char, CharT>();
  assert_is_formattable<signed short, CharT>();
  assert_is_formattable<signed int, CharT>();
  assert_is_formattable<signed long, CharT>();
  assert_is_formattable<signed long long, CharT>();
#ifndef TEST_HAS_NO_INT128
  assert_is_formattable<__int128_t, CharT>();
#endif

  assert_is_formattable<unsigned char, CharT>();
  assert_is_formattable<unsigned short, CharT>();
  assert_is_formattable<unsigned int, CharT>();
  assert_is_formattable<unsigned long, CharT>();
  assert_is_formattable<unsigned long long, CharT>();
#ifndef TEST_HAS_NO_INT128
  assert_is_formattable<__uint128_t, CharT>();
#endif

  // floating-point types are tested in concept.formattable.float.compile.pass.cpp

  assert_is_formattable<std::nullptr_t, CharT>();
  assert_is_formattable<void*, CharT>();
  assert_is_formattable<const void*, CharT>();
}

// Tests for P1361 Integration of chrono with text formatting
//
// Some tests are commented out since these types haven't been implemented in
// chrono yet. After P1361 has been implemented these formatters should be all
// enabled.
template <class CharT>
void test_P1361() {
// The chrono formatters require localization support.
// [time.format]/7
//   If the chrono-specs is omitted, the chrono object is formatted as if by
//   streaming it to std::ostringstream os with the formatting
//   locale imbued and copying os.str() through the output iterator of the
//   context with additional padding and adjustments as specified by the format
//   specifiers.
// In libc++ std:::ostringstream requires localization support.
#ifndef TEST_HAS_NO_LOCALIZATION

  assert_is_formattable<std::chrono::microseconds, CharT>();

  assert_is_formattable<std::chrono::sys_time<std::chrono::microseconds>, CharT>();
  //assert_is_formattable<std::chrono::utc_time<std::chrono::microseconds>, CharT>();
  //assert_is_formattable<std::chrono::tai_time<std::chrono::microseconds>, CharT>();
  //assert_is_formattable<std::chrono::gps_time<std::chrono::microseconds>, CharT>();
  assert_is_formattable<std::chrono::file_time<std::chrono::microseconds>, CharT>();
  assert_is_formattable<std::chrono::local_time<std::chrono::microseconds>, CharT>();

  assert_is_formattable<std::chrono::day, CharT>();
  assert_is_formattable<std::chrono::month, CharT>();
  assert_is_formattable<std::chrono::year, CharT>();

  assert_is_formattable<std::chrono::weekday, CharT>();
  assert_is_formattable<std::chrono::weekday_indexed, CharT>();
  assert_is_formattable<std::chrono::weekday_last, CharT>();

  assert_is_formattable<std::chrono::month_day, CharT>();
  assert_is_formattable<std::chrono::month_day_last, CharT>();
  assert_is_formattable<std::chrono::month_weekday, CharT>();
  assert_is_formattable<std::chrono::month_weekday_last, CharT>();

  assert_is_formattable<std::chrono::year_month, CharT>();
  assert_is_formattable<std::chrono::year_month_day, CharT>();
  assert_is_formattable<std::chrono::year_month_day_last, CharT>();
  assert_is_formattable<std::chrono::year_month_weekday, CharT>();
  assert_is_formattable<std::chrono::year_month_weekday_last, CharT>();

  assert_is_formattable<std::chrono::hh_mm_ss<std::chrono::microseconds>, CharT>();

#  if !defined(TEST_HAS_NO_EXPERIMENTAL_TZDB)
  assert_is_formattable<std::chrono::sys_info, CharT>();
  assert_is_formattable<std::chrono::local_info, CharT>();

  //assert_is_formattable<std::chrono::zoned_time, CharT>();
#  endif // !defined(TEST_HAS_NO_EXPERIMENTAL_TZDB)

#endif // TEST_HAS_NO_LOCALIZATION
}

// Tests for P1636 Formatters for library types
//
// The paper hasn't been voted in so currently all formatters are disabled.
// Note the paper has been abandoned, the types are kept since other papers may
// introduce these formatters.
template <class CharT>
void test_P1636() {
  assert_is_not_formattable<std::basic_streambuf<CharT>, CharT>();
  assert_is_not_formattable<std::bitset<42>, CharT>();
  assert_is_not_formattable<std::complex<double>, CharT>();
  assert_is_not_formattable<std::error_code, CharT>();
  assert_is_not_formattable<std::filesystem::path, CharT>();
  assert_is_not_formattable<std::shared_ptr<int>, CharT>();
#ifndef TEST_HAS_NO_LOCALIZATION
  if constexpr (!std::same_as<CharT, int>) // sub_match only works with proper character types
    assert_is_not_formattable<std::sub_match<CharT*>, CharT>();
#endif
#ifndef TEST_HAS_NO_THREADS
  assert_is_formattable<std::thread::id, CharT>();
#endif
  assert_is_not_formattable<std::unique_ptr<int>, CharT>();
}

template <class CharT, class Vector>
void test_P2286_vector_bool() {
  assert_is_formattable<Vector, CharT>();
  assert_is_formattable<typename Vector::reference, CharT>();

  // The const_reference shall be a bool.
  // However libc++ uses a __bit_const_reference<vector> when
  // _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL is defined.
  assert_is_formattable<const Vector&, CharT>();
  assert_is_formattable<typename Vector::const_reference, CharT>();
}

// Tests for P2286 Formatting ranges
template <class CharT>
void test_P2286() {
  assert_is_formattable<std::array<int, 42>, CharT>();
  assert_is_formattable<std::vector<int>, CharT>();
  assert_is_formattable<std::deque<int>, CharT>();
  assert_is_formattable<std::forward_list<int>, CharT>();
  assert_is_formattable<std::list<int>, CharT>();

  assert_is_formattable<std::set<int>, CharT>();
  assert_is_formattable<std::map<int, int>, CharT>();
  assert_is_formattable<std::multiset<int>, CharT>();
  assert_is_formattable<std::multimap<int, int>, CharT>();

  assert_is_formattable<std::unordered_set<int>, CharT>();
  assert_is_formattable<std::unordered_map<int, int>, CharT>();
  assert_is_formattable<std::unordered_multiset<int>, CharT>();
  assert_is_formattable<std::unordered_multimap<int, int>, CharT>();

  assert_is_formattable<std::stack<int>, CharT>();
  assert_is_formattable<std::queue<int>, CharT>();
  assert_is_formattable<std::priority_queue<int>, CharT>();

  assert_is_formattable<std::span<int>, CharT>();

  assert_is_formattable<std::valarray<int>, CharT>();

  assert_is_formattable<std::pair<int, int>, CharT>();
  assert_is_formattable<std::tuple<int>, CharT>();

  test_P2286_vector_bool<CharT, std::vector<bool>>();
  test_P2286_vector_bool<CharT, std::vector<bool, std::allocator<bool>>>();
  test_P2286_vector_bool<CharT, std::vector<bool, min_allocator<bool>>>();
}

// Tests volatile qualified objects are no longer formattable.
template <class CharT>
void test_LWG3631() {
  assert_is_not_formattable<volatile CharT, CharT>();

  assert_is_not_formattable<volatile bool, CharT>();

  assert_is_not_formattable<volatile signed int, CharT>();
  assert_is_not_formattable<volatile unsigned int, CharT>();

  assert_is_not_formattable<volatile std::chrono::microseconds, CharT>();
  assert_is_not_formattable<volatile std::chrono::sys_time<std::chrono::microseconds>, CharT>();
  assert_is_not_formattable<volatile std::chrono::day, CharT>();

  assert_is_not_formattable<std::array<volatile int, 42>, CharT>();

  assert_is_not_formattable<std::pair<volatile int, int>, CharT>();
  assert_is_not_formattable<std::pair<int, volatile int>, CharT>();
  assert_is_not_formattable<std::pair<volatile int, volatile int>, CharT>();
}

class c {
  void f();
  void fc() const;
  static void sf();
};
enum e { a };
enum class ec { a };
template <class CharT>
void test_disabled() {
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  assert_is_not_formattable<const char*, wchar_t>();
#endif
  assert_is_not_formattable<const char*, char8_t>();
  assert_is_not_formattable<const char*, char16_t>();
  assert_is_not_formattable<const char*, char32_t>();

  assert_is_not_formattable<c, CharT>();
  assert_is_not_formattable<const c, CharT>();
  assert_is_not_formattable<volatile c, CharT>();
  assert_is_not_formattable<const volatile c, CharT>();

  assert_is_not_formattable<e, CharT>();
  assert_is_not_formattable<const e, CharT>();
  assert_is_not_formattable<volatile e, CharT>();
  assert_is_not_formattable<const volatile e, CharT>();

  assert_is_not_formattable<ec, CharT>();
  assert_is_not_formattable<const ec, CharT>();
  assert_is_not_formattable<volatile ec, CharT>();
  assert_is_not_formattable<const volatile ec, CharT>();

  assert_is_not_formattable<int*, CharT>();
  assert_is_not_formattable<const int*, CharT>();
  assert_is_not_formattable<volatile int*, CharT>();
  assert_is_not_formattable<const volatile int*, CharT>();

  assert_is_not_formattable<c*, CharT>();
  assert_is_not_formattable<const c*, CharT>();
  assert_is_not_formattable<volatile c*, CharT>();
  assert_is_not_formattable<const volatile c*, CharT>();

  assert_is_not_formattable<e*, CharT>();
  assert_is_not_formattable<const e*, CharT>();
  assert_is_not_formattable<volatile e*, CharT>();
  assert_is_not_formattable<const volatile e*, CharT>();

  assert_is_not_formattable<ec*, CharT>();
  assert_is_not_formattable<const ec*, CharT>();
  assert_is_not_formattable<volatile ec*, CharT>();
  assert_is_not_formattable<const volatile ec*, CharT>();

  assert_is_not_formattable<void (*)(), CharT>();
  assert_is_not_formattable<void (c::*)(), CharT>();
  assert_is_not_formattable<void (c::*)() const, CharT>();

  assert_is_not_formattable<std::optional<int>, CharT>();
  assert_is_not_formattable<std::variant<int>, CharT>();

  assert_is_not_formattable<std::shared_ptr<c>, CharT>();
  assert_is_not_formattable<std::unique_ptr<c>, CharT>();

  assert_is_not_formattable<std::array<c, 42>, CharT>();
  assert_is_not_formattable<std::vector<c>, CharT>();
  assert_is_not_formattable<std::deque<c>, CharT>();
  assert_is_not_formattable<std::forward_list<c>, CharT>();
  assert_is_not_formattable<std::list<c>, CharT>();

  assert_is_not_formattable<std::set<c>, CharT>();
  assert_is_not_formattable<std::map<c, int>, CharT>();
  assert_is_not_formattable<std::multiset<c>, CharT>();
  assert_is_not_formattable<std::multimap<c, int>, CharT>();

  assert_is_not_formattable<std::unordered_set<c>, CharT>();
  assert_is_not_formattable<std::unordered_map<c, int>, CharT>();
  assert_is_not_formattable<std::unordered_multiset<c>, CharT>();
  assert_is_not_formattable<std::unordered_multimap<c, int>, CharT>();

  assert_is_not_formattable<std::stack<c>, CharT>();
  assert_is_not_formattable<std::queue<c>, CharT>();
  assert_is_not_formattable<std::priority_queue<c>, CharT>();

  assert_is_not_formattable<std::span<c>, CharT>();

  assert_is_not_formattable<std::valarray<c>, CharT>();

  assert_is_not_formattable<std::pair<c, int>, CharT>();
  assert_is_not_formattable<std::tuple<c>, CharT>();

  assert_is_not_formattable<std::optional<c>, CharT>();
  assert_is_not_formattable<std::variant<c>, CharT>();
}

struct abstract {
  virtual ~abstract() = 0;
};

template <class CharT>
  requires std::same_as<CharT, char>
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
        || std::same_as<CharT, wchar_t>
#endif
struct std::formatter<abstract, CharT> {
  template <class ParseContext>
  constexpr typename ParseContext::iterator parse(ParseContext& parse_ctx) {
    return parse_ctx.begin();
  }

  template <class FormatContext>
  typename FormatContext::iterator format(const abstract&, FormatContext& ctx) const {
    return ctx.out();
  }
};

template <class CharT>
void test_abstract_class() {
  assert_is_formattable<abstract, CharT>();
}

template <class CharT>
void test() {
  test_P0645<CharT>();
  test_P1361<CharT>();
  test_P1636<CharT>();
  test_P2286<CharT>();
  test_LWG3631<CharT>();
  test_abstract_class<CharT>();
  test_disabled<CharT>();
}

void test() {
  test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
  test<wchar_t>();
#endif
  test<char8_t>();
  test<char16_t>();
  test<char32_t>();
}