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: no-localization
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... Args>
19// size_t formatted_size(const locale& loc,
20// format-string<Args...> fmt, const Args&... args);
21// template<class... Args>
22// size_t formatted_size(const locale& loc,
23// wformat-string<Args...> fmt, const Args&... args);
24
25#include <format>
26#include <locale>
27
28#include "test_macros.h"
29
30// clang-format off
31
32void f() {
33 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
34 // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
35
36 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "}"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{}"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{0}"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{:-}", "Forty-two"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{:#}", "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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{:L}", "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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{0:{0}}", "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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{:.42d}", "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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), "{:d}", "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#ifndef TEST_HAS_NO_WIDE_CHARACTERS
64 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{0}"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{:-}", L"Forty-two"); // 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{: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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{0:{0}}", 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{:.42d}", 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 TEST_IGNORE_NODISCARD std::formatted_size(std::locale(), L"{:d}", 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#endif
94}
95

source code of libcxx/test/std/utilities/format/format.functions/formatted_size.locale.verify.cpp