|
| 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 | +// <format> |
| 12 | + |
| 13 | +// template<class Context = format_context, class... Args> |
| 14 | +// format-arg-store<Context, Args...> make_format_args(Args&... args); |
| 15 | +// |
| 16 | +// Preconditions: The type typename Context::template formatter_type<remove_const_t<Ti>> |
| 17 | +// meets the BasicFormatter requirements ([formatter.requirements]) for each Ti in Args. |
| 18 | +// |
| 19 | +// When the precondition is violated libc++ diagnoses the issue with the |
| 20 | +// formatter specialization. |
| 21 | + |
| 22 | +#include <format> |
| 23 | + |
| 24 | +#include "test_macros.h" |
| 25 | + |
| 26 | +struct no_formatter_specialization {}; |
| 27 | +void test_no_formatter_specialization() { |
| 28 | + no_formatter_specialization t; |
| 29 | + // expected-error@*:* {{static assertion failed: The required formatter specialization has not been provided.}} |
| 30 | + (void)std::make_format_args(t); |
| 31 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 32 | + // expected-error@*:* {{static assertion failed: The required formatter specialization has not been provided.}} |
| 33 | + (void)std::make_wformat_args(t); |
| 34 | +#endif |
| 35 | +} |
| 36 | + |
| 37 | +struct correct_formatter_specialization {}; |
| 38 | +template <class CharT> |
| 39 | +struct std::formatter<correct_formatter_specialization, CharT> { |
| 40 | + template <class ParseContext> |
| 41 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 42 | + |
| 43 | + template <class FormatContext> |
| 44 | + typename FormatContext::iterator format(correct_formatter_specialization&, FormatContext&) const; |
| 45 | +}; |
| 46 | +void test_correct_formatter_specialization() { |
| 47 | + correct_formatter_specialization t; |
| 48 | + (void)std::make_format_args(t); |
| 49 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 50 | + (void)std::make_wformat_args(t); |
| 51 | +#endif |
| 52 | +} |
| 53 | + |
| 54 | +struct formatter_not_semiregular {}; |
| 55 | +template <class CharT> |
| 56 | +struct std::formatter<formatter_not_semiregular, CharT> { |
| 57 | + formatter(int); |
| 58 | + |
| 59 | + template <class ParseContext> |
| 60 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 61 | + |
| 62 | + template <class FormatContext> |
| 63 | + typename FormatContext::iterator format(formatter_not_semiregular&, FormatContext&) const; |
| 64 | +}; |
| 65 | +void test_formatter_not_semiregular() { |
| 66 | + formatter_not_semiregular t; |
| 67 | + // expected-error@*:* {{static assertion failed: The required formatter specialization is not semiregular.}} |
| 68 | + (void)std::make_format_args(t); |
| 69 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 70 | + // expected-error@*:* {{static assertion failed: The required formatter specialization is not semiregular.}} |
| 71 | + (void)std::make_wformat_args(t); |
| 72 | +#endif |
| 73 | +} |
| 74 | + |
| 75 | +struct formatter_no_parse_function {}; |
| 76 | +template <class CharT> |
| 77 | +struct std::formatter<formatter_no_parse_function, CharT> { |
| 78 | + template <class FormatContext> |
| 79 | + typename FormatContext::iterator format(formatter_no_parse_function&, FormatContext&) const; |
| 80 | +}; |
| 81 | +void test_formatter_no_parse_function() { |
| 82 | + formatter_no_parse_function t; |
| 83 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}} |
| 84 | + (void)std::make_format_args(t); |
| 85 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 86 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}} |
| 87 | + (void)std::make_wformat_args(t); |
| 88 | +#endif |
| 89 | +} |
| 90 | + |
| 91 | +struct parse_function_invalid_arguments {}; |
| 92 | +template <class CharT> |
| 93 | +struct std::formatter<parse_function_invalid_arguments, CharT> { |
| 94 | + template <class ParseContext> |
| 95 | + constexpr typename ParseContext::iterator parse(ParseContext&, int); |
| 96 | + |
| 97 | + template <class FormatContext> |
| 98 | + typename FormatContext::iterator format(parse_function_invalid_arguments&, FormatContext&) const; |
| 99 | +}; |
| 100 | +void test_parse_function_invalid_arguments() { |
| 101 | + parse_function_invalid_arguments t; |
| 102 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}} |
| 103 | + (void)std::make_format_args(t); |
| 104 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 105 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a parse function taking the proper arguments.}} |
| 106 | + (void)std::make_wformat_args(t); |
| 107 | +#endif |
| 108 | +} |
| 109 | + |
| 110 | +struct parse_function_invalid_return_type {}; |
| 111 | +template <class CharT> |
| 112 | +struct std::formatter<parse_function_invalid_return_type, CharT> { |
| 113 | + template <class ParseContext> |
| 114 | + constexpr int parse(ParseContext&); |
| 115 | + |
| 116 | + template <class FormatContext> |
| 117 | + typename FormatContext::iterator format(parse_function_invalid_return_type&, FormatContext&) const; |
| 118 | +}; |
| 119 | +void test_parse_function_invalid_return_type() { |
| 120 | + parse_function_invalid_return_type t; |
| 121 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's parse function does not return the required type.}} |
| 122 | + (void)std::make_format_args(t); |
| 123 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 124 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's parse function does not return the required type.}} |
| 125 | + (void)std::make_wformat_args(t); |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +struct no_format_function {}; |
| 130 | +template <class CharT> |
| 131 | +struct std::formatter<no_format_function, CharT> { |
| 132 | + template <class ParseContext> |
| 133 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 134 | +}; |
| 135 | +void test_no_format_function() { |
| 136 | + no_format_function t; |
| 137 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}} |
| 138 | + (void)std::make_format_args(t); |
| 139 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 140 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}} |
| 141 | + (void)std::make_wformat_args(t); |
| 142 | +#endif |
| 143 | +} |
| 144 | + |
| 145 | +struct format_function_invalid_arguments {}; |
| 146 | +template <class CharT> |
| 147 | +struct std::formatter<format_function_invalid_arguments, CharT> { |
| 148 | + template <class ParseContext> |
| 149 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 150 | + |
| 151 | + template <class FormatContext> |
| 152 | + typename FormatContext::iterator format(format_function_invalid_arguments&) const; |
| 153 | +}; |
| 154 | +void test_format_function_invalid_arguments() { |
| 155 | + format_function_invalid_arguments t; |
| 156 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}} |
| 157 | + (void)std::make_format_args(t); |
| 158 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 159 | + // expected-error@*:* {{static assertion failed: The required formatter specialization does not have a format function taking the proper arguments.}} |
| 160 | + (void)std::make_wformat_args(t); |
| 161 | +#endif |
| 162 | +} |
| 163 | + |
| 164 | +struct format_function_invalid_return_type {}; |
| 165 | +template <class CharT> |
| 166 | +struct std::formatter<format_function_invalid_return_type, CharT> { |
| 167 | + template <class ParseContext> |
| 168 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 169 | + |
| 170 | + template <class FormatContext> |
| 171 | + int format(format_function_invalid_return_type&, FormatContext&) const; |
| 172 | +}; |
| 173 | +void test_format_function_invalid_return_type() { |
| 174 | + format_function_invalid_return_type t; |
| 175 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's format function does not return the required type.}} |
| 176 | + (void)std::make_format_args(t); |
| 177 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 178 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's format function does not return the required type.}} |
| 179 | + (void)std::make_wformat_args(t); |
| 180 | +#endif |
| 181 | +} |
| 182 | + |
| 183 | +struct format_function_not_const_qualified {}; |
| 184 | +template <class CharT> |
| 185 | +struct std::formatter<format_function_not_const_qualified, CharT> { |
| 186 | + template <class ParseContext> |
| 187 | + constexpr typename ParseContext::iterator parse(ParseContext&); |
| 188 | + |
| 189 | + template <class FormatContext> |
| 190 | + typename FormatContext::iterator format(format_function_not_const_qualified&, FormatContext&); |
| 191 | +}; |
| 192 | +void test_format_function_not_const_qualified() { |
| 193 | + format_function_not_const_qualified t; |
| 194 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's format function is not const qualified.}} |
| 195 | + (void)std::make_format_args(t); |
| 196 | +#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 197 | + // expected-error@*:* {{static assertion failed: The required formatter specialization's format function is not const qualified.}} |
| 198 | + (void)std::make_wformat_args(t); |
| 199 | +#endif |
| 200 | +} |
0 commit comments