xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/format.verify.cpp (revision 355e0ce3c5366bd0b564a25e4b6675353da3c53e)
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... Args>
18 //   string format(format-string<Args...> fmt, const Args&... args);
19 // template<class... Args>
20 //   wstring format(wformat-string<Args...> fmt, const Args&... args);
21 
22 #include <format>
23 
24 #include "test_macros.h"
25 
26 // clang-format off
27 
28 void f() {
29   std::format("{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
30   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
31 
32   std::format("}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
33   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
34 
35   std::format("{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
36   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
37 
38   std::format("{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
39   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
40 
41   std::format("{:-}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
42   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
43 
44   std::format("{:#}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
45   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
46 
47   std::format("{:L}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
48   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
49 
50   std::format("{0:{0}}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
51   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
52 
53   std::format("{:.42d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
54   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
55 
56   std::format("{:d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
57   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
58 
59 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
60   std::format(L"{"); // 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(L"}"); // 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   std::format(L"{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
67   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
68 
69   std::format(L"{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
70   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
71 
72   std::format(L"{:-}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
73   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
74 
75   std::format(L"{:#}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
76   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
77 
78   std::format(L"{:L}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
79   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
80 
81   std::format(L"{0:{0}}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
82   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
83 
84   std::format(L"{:.42d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
85   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
86 
87   std::format(L"{:d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
88   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
89 #endif
90 }
91 
92 struct tiny {
93   int bit : 1;
94 };
95 
96 void P2418()
97 {
98   auto t = tiny{};
99   std::format("{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}}
100 
101 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
102   std::format(L"{}", t.bit); // expected-error{{non-const reference cannot bind to bit-field 'bit'}}
103 #endif
104 }
105