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