xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/format_to_n.verify.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // TODO FMT This test should not require std::to_chars(floating-point)
12 // XFAIL: availability-fp_to_chars-missing
13 
14 // Basic test to validate ill-formed code is properly detected.
15 
16 // <format>
17 
18 // template<class Out, class... Args>
19 //   format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
20 //                                       format-string<Args...> fmt, const Args&... args);
21 // template<class Out, class... Args>
22 //   format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
23 //                                       wformat-string<Args...> fmt, const Args&... args);
24 
25 #include <format>
26 
27 #include "test_macros.h"
28 
29 extern char* out;
30 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
31 extern wchar_t* wout;
32 #endif
33 
34 // clang-format off
35 
36 void f() {
37   std::format_to_n(out, 42, "{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
38   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
39 
40   std::format_to_n(out, 42, "}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
41   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
42 
43   std::format_to_n(out, 42, "{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
44   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
45 
46   std::format_to_n(out, 42, "{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
47   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
48 
49   std::format_to_n(out, 42, "{:-}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
50   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
51 
52   std::format_to_n(out, 42, "{:#}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
53   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
54 
55   std::format_to_n(out, 42, "{:L}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
56   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
57 
58   std::format_to_n(out, 42, "{0:{0}}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
59   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
60 
61   std::format_to_n(out, 42, "{:.42d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
62   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
63 
64   std::format_to_n(out, 42, "{:d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
65   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
66 
67 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
68   std::format_to_n(wout, 42, L"{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
69   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
70 
71   std::format_to_n(wout, 42, L"}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
72   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
73 
74   std::format_to_n(wout, 42, L"{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
75   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
76 
77   std::format_to_n(wout, 42, L"{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
78   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
79 
80   std::format_to_n(wout, 42, L"{:-}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
81   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
82 
83   std::format_to_n(wout, 42, L"{:#}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
84   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
85 
86   std::format_to_n(wout, 42, L"{:L}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
87   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
88 
89   std::format_to_n(wout, 42, L"{0:{0}}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
90   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
91 
92   std::format_to_n(wout, 42, L"{:.42d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
93   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
94 
95   std::format_to_n(wout, 42, L"{:d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
96   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
97 #endif
98 }
99