2022-12-15 18:07:41 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2024-07-31 12:57:00 -04:00
|
|
|
//
|
2022-12-15 18:07:41 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef TEST_SUPPORT_FORMAT_FUNCTIONS_COMMON_H
|
|
|
|
#define TEST_SUPPORT_FORMAT_FUNCTIONS_COMMON_H
|
|
|
|
|
|
|
|
// Contains the common part of the formatter tests for different papers.
|
|
|
|
|
2022-12-24 13:33:21 +01:00
|
|
|
#include <algorithm>
|
2023-02-02 23:54:22 -08:00
|
|
|
#include <cctype>
|
2022-12-24 13:33:21 +01:00
|
|
|
#include <charconv>
|
2023-11-05 05:32:51 -08:00
|
|
|
#include <cstddef>
|
2024-09-16 15:06:20 -04:00
|
|
|
#include <cstdint>
|
2023-11-05 05:32:51 -08:00
|
|
|
#include <cstdlib>
|
2022-12-15 18:07:41 +01:00
|
|
|
#include <format>
|
2022-05-05 18:57:32 +02:00
|
|
|
#include <ranges>
|
2022-05-05 18:57:32 +02:00
|
|
|
#include <string_view>
|
2024-09-16 15:06:20 -04:00
|
|
|
#include <string>
|
2022-05-05 18:57:32 +02:00
|
|
|
#include <vector>
|
2022-12-15 18:07:41 +01:00
|
|
|
|
|
|
|
#include "make_string.h"
|
|
|
|
|
|
|
|
#define STR(S) MAKE_STRING(CharT, S)
|
|
|
|
#define SV(S) MAKE_STRING_VIEW(CharT, S)
|
|
|
|
#define CSTR(S) MAKE_CSTRING(CharT, S)
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct context {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct context<char> {
|
|
|
|
using type = std::format_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
|
|
|
|
template <>
|
|
|
|
struct context<wchar_t> {
|
|
|
|
using type = std::wformat_context;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
using context_t = typename context<T>::type;
|
|
|
|
|
|
|
|
// A user-defined type used to test the handle formatter.
|
2023-03-12 17:11:29 +01:00
|
|
|
enum class status : std::uint16_t { foo = 0xAAAA, bar = 0x5555, foobar = 0xAA55 };
|
2022-12-15 18:07:41 +01:00
|
|
|
|
|
|
|
// The formatter for a user-defined type used to test the handle formatter.
|
|
|
|
template <class CharT>
|
|
|
|
struct std::formatter<status, CharT> {
|
[libc++][format] Implements LWG3892.
This LWG issue is based on the discussion regarding
P2733R3 Fix handling of empty specifiers in std::format
This paper was disussed and changed a few times in LEWG during the
Issaquah meeting. The paper was not voted in, instead LEWG asked for
a DR against C++26.
This LWG issue contains the direction voted by LEWG. This issue has not
been voted in yet. However it fixes some of the defencies on the
container based formatting. Without this fix the range-default-formatter
for strings looks bad when used in containers.
The changes of this issue match the intended changes of P27333.
type fmt before after (if changed)
---------------------------------------------------------------
char {} a
char {:?} 'a'
array<char, 1> {} ['a']
array<char, 1> {::} [a]
array<char, 1> {::c} [a]
array<char, 1> {::?} ['a']
map<char, char> {} {a: a} -> {'a': 'a'}
map<char, char> {::} {'a': 'a'}
set<char> {} {'a'}
set<char> {::} {a}
set<char> {::c} {a}
set<char> {::?} {'a'}
tuple<char> {} ('a')
stack<char> {} ['a']
stack<char> {::} [a]
stack<char> {::c} [a]
stack<char> {::?} ['a']
array<array<char, 1>, 1> {} [[a]] -> {'a': 'a'}
array<array<char, 1>, 1> {::} [['a']]
array<array<char, 1>, 1> {:::} [[a]]
array<array<char, 1>, 1> {:::c} [[a]]
array<array<char, 1>, 1> {:::?} [['a']]
array<tuple<char>, 1> {} [(a)] -> [('a')]
tuple<tuple<char>> {} ((a)) -> (('a'))
tuple<array<char, 1>> {} ([a]) -> (['a'])
Note the optimization text as mentioned in the tuple formatter can't be
done. The call to parse may affect the formatter so its state needs to
be preserved.
Reviewed By: ldionne, #libc, EricWF
Differential Revision: https://reviews.llvm.org/D145847
2023-03-03 20:23:51 +01:00
|
|
|
// During the 2023 Issaquah meeting LEWG made it clear a formatter is
|
|
|
|
// required to call its parse function. LWG3892 Adds the wording for that
|
|
|
|
// requirement. Therefore this formatter is initialized in an invalid state.
|
|
|
|
// A call to parse sets it in a valid state and a call to format validates
|
|
|
|
// the state.
|
|
|
|
int type = -1;
|
2022-12-15 18:07:41 +01:00
|
|
|
|
|
|
|
constexpr auto parse(basic_format_parse_context<CharT>& parse_ctx) -> decltype(parse_ctx.begin()) {
|
|
|
|
auto begin = parse_ctx.begin();
|
[libc++][format] Implements LWG3892.
This LWG issue is based on the discussion regarding
P2733R3 Fix handling of empty specifiers in std::format
This paper was disussed and changed a few times in LEWG during the
Issaquah meeting. The paper was not voted in, instead LEWG asked for
a DR against C++26.
This LWG issue contains the direction voted by LEWG. This issue has not
been voted in yet. However it fixes some of the defencies on the
container based formatting. Without this fix the range-default-formatter
for strings looks bad when used in containers.
The changes of this issue match the intended changes of P27333.
type fmt before after (if changed)
---------------------------------------------------------------
char {} a
char {:?} 'a'
array<char, 1> {} ['a']
array<char, 1> {::} [a]
array<char, 1> {::c} [a]
array<char, 1> {::?} ['a']
map<char, char> {} {a: a} -> {'a': 'a'}
map<char, char> {::} {'a': 'a'}
set<char> {} {'a'}
set<char> {::} {a}
set<char> {::c} {a}
set<char> {::?} {'a'}
tuple<char> {} ('a')
stack<char> {} ['a']
stack<char> {::} [a]
stack<char> {::c} [a]
stack<char> {::?} ['a']
array<array<char, 1>, 1> {} [[a]] -> {'a': 'a'}
array<array<char, 1>, 1> {::} [['a']]
array<array<char, 1>, 1> {:::} [[a]]
array<array<char, 1>, 1> {:::c} [[a]]
array<array<char, 1>, 1> {:::?} [['a']]
array<tuple<char>, 1> {} [(a)] -> [('a')]
tuple<tuple<char>> {} ((a)) -> (('a'))
tuple<array<char, 1>> {} ([a]) -> (['a'])
Note the optimization text as mentioned in the tuple formatter can't be
done. The call to parse may affect the formatter so its state needs to
be preserved.
Reviewed By: ldionne, #libc, EricWF
Differential Revision: https://reviews.llvm.org/D145847
2023-03-03 20:23:51 +01:00
|
|
|
auto end = parse_ctx.end();
|
|
|
|
type = 0;
|
2022-12-15 18:07:41 +01:00
|
|
|
if (begin == end)
|
|
|
|
return begin;
|
|
|
|
|
|
|
|
switch (*begin) {
|
|
|
|
case CharT('x'):
|
|
|
|
break;
|
|
|
|
case CharT('X'):
|
|
|
|
type = 1;
|
|
|
|
break;
|
|
|
|
case CharT('s'):
|
|
|
|
type = 2;
|
|
|
|
break;
|
|
|
|
case CharT('}'):
|
|
|
|
return begin;
|
|
|
|
default:
|
2023-06-04 21:32:10 +02:00
|
|
|
throw_format_error("The type option contains an invalid value for a status formatting argument");
|
2022-12-15 18:07:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
++begin;
|
|
|
|
if (begin != end && *begin != CharT('}'))
|
2023-06-04 21:32:10 +02:00
|
|
|
throw_format_error("The format specifier should consume the input or end with a '}'");
|
2022-12-15 18:07:41 +01:00
|
|
|
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Out>
|
|
|
|
auto format(status s, basic_format_context<Out, CharT>& ctx) const -> decltype(ctx.out()) {
|
|
|
|
const char* names[] = {"foo", "bar", "foobar"};
|
|
|
|
char buffer[7];
|
|
|
|
const char* begin = names[0];
|
|
|
|
const char* end = names[0];
|
|
|
|
switch (type) {
|
[libc++][format] Implements LWG3892.
This LWG issue is based on the discussion regarding
P2733R3 Fix handling of empty specifiers in std::format
This paper was disussed and changed a few times in LEWG during the
Issaquah meeting. The paper was not voted in, instead LEWG asked for
a DR against C++26.
This LWG issue contains the direction voted by LEWG. This issue has not
been voted in yet. However it fixes some of the defencies on the
container based formatting. Without this fix the range-default-formatter
for strings looks bad when used in containers.
The changes of this issue match the intended changes of P27333.
type fmt before after (if changed)
---------------------------------------------------------------
char {} a
char {:?} 'a'
array<char, 1> {} ['a']
array<char, 1> {::} [a]
array<char, 1> {::c} [a]
array<char, 1> {::?} ['a']
map<char, char> {} {a: a} -> {'a': 'a'}
map<char, char> {::} {'a': 'a'}
set<char> {} {'a'}
set<char> {::} {a}
set<char> {::c} {a}
set<char> {::?} {'a'}
tuple<char> {} ('a')
stack<char> {} ['a']
stack<char> {::} [a]
stack<char> {::c} [a]
stack<char> {::?} ['a']
array<array<char, 1>, 1> {} [[a]] -> {'a': 'a'}
array<array<char, 1>, 1> {::} [['a']]
array<array<char, 1>, 1> {:::} [[a]]
array<array<char, 1>, 1> {:::c} [[a]]
array<array<char, 1>, 1> {:::?} [['a']]
array<tuple<char>, 1> {} [(a)] -> [('a')]
tuple<tuple<char>> {} ((a)) -> (('a'))
tuple<array<char, 1>> {} ([a]) -> (['a'])
Note the optimization text as mentioned in the tuple formatter can't be
done. The call to parse may affect the formatter so its state needs to
be preserved.
Reviewed By: ldionne, #libc, EricWF
Differential Revision: https://reviews.llvm.org/D145847
2023-03-03 20:23:51 +01:00
|
|
|
case -1:
|
|
|
|
throw_format_error("The formatter's parse function has not been called.");
|
|
|
|
|
2022-12-15 18:07:41 +01:00
|
|
|
case 0:
|
|
|
|
begin = buffer;
|
|
|
|
buffer[0] = '0';
|
|
|
|
buffer[1] = 'x';
|
2023-03-12 17:11:29 +01:00
|
|
|
end = std::to_chars(&buffer[2], std::end(buffer), static_cast<std::uint16_t>(s), 16).ptr;
|
2022-12-15 18:07:41 +01:00
|
|
|
buffer[6] = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
begin = buffer;
|
|
|
|
buffer[0] = '0';
|
|
|
|
buffer[1] = 'X';
|
2023-03-12 17:11:29 +01:00
|
|
|
end = std::to_chars(&buffer[2], std::end(buffer), static_cast<std::uint16_t>(s), 16).ptr;
|
2022-12-15 18:07:41 +01:00
|
|
|
std::transform(static_cast<const char*>(&buffer[2]), end, &buffer[2], [](char c) {
|
|
|
|
return static_cast<char>(std::toupper(c)); });
|
|
|
|
buffer[6] = '\0';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
switch (s) {
|
|
|
|
case status::foo:
|
|
|
|
begin = names[0];
|
|
|
|
break;
|
|
|
|
case status::bar:
|
|
|
|
begin = names[1];
|
|
|
|
break;
|
|
|
|
case status::foobar:
|
|
|
|
begin = names[2];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end = begin + strlen(begin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::copy(begin, end, ctx.out());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-05-25 19:45:50 +02:00
|
|
|
[[noreturn]] void throw_format_error([[maybe_unused]] const char* s) const {
|
2022-12-15 18:07:41 +01:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
|
|
|
throw std::format_error(s);
|
|
|
|
#else
|
|
|
|
std::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
[libc++][format] Implements LWG3892.
This LWG issue is based on the discussion regarding
P2733R3 Fix handling of empty specifiers in std::format
This paper was disussed and changed a few times in LEWG during the
Issaquah meeting. The paper was not voted in, instead LEWG asked for
a DR against C++26.
This LWG issue contains the direction voted by LEWG. This issue has not
been voted in yet. However it fixes some of the defencies on the
container based formatting. Without this fix the range-default-formatter
for strings looks bad when used in containers.
The changes of this issue match the intended changes of P27333.
type fmt before after (if changed)
---------------------------------------------------------------
char {} a
char {:?} 'a'
array<char, 1> {} ['a']
array<char, 1> {::} [a]
array<char, 1> {::c} [a]
array<char, 1> {::?} ['a']
map<char, char> {} {a: a} -> {'a': 'a'}
map<char, char> {::} {'a': 'a'}
set<char> {} {'a'}
set<char> {::} {a}
set<char> {::c} {a}
set<char> {::?} {'a'}
tuple<char> {} ('a')
stack<char> {} ['a']
stack<char> {::} [a]
stack<char> {::c} [a]
stack<char> {::?} ['a']
array<array<char, 1>, 1> {} [[a]] -> {'a': 'a'}
array<array<char, 1>, 1> {::} [['a']]
array<array<char, 1>, 1> {:::} [[a]]
array<array<char, 1>, 1> {:::c} [[a]]
array<array<char, 1>, 1> {:::?} [['a']]
array<tuple<char>, 1> {} [(a)] -> [('a')]
tuple<tuple<char>> {} ((a)) -> (('a'))
tuple<array<char, 1>> {} ([a]) -> (['a'])
Note the optimization text as mentioned in the tuple formatter can't be
done. The call to parse may affect the formatter so its state needs to
be preserved.
Reviewed By: ldionne, #libc, EricWF
Differential Revision: https://reviews.llvm.org/D145847
2023-03-03 20:23:51 +01:00
|
|
|
struct parse_call_validator {
|
|
|
|
struct parse_function_not_called {};
|
|
|
|
|
|
|
|
friend constexpr auto operator<=>(const parse_call_validator& lhs, const parse_call_validator& rhs) {
|
|
|
|
return &lhs <=> &rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The formatter for a user-defined type used to test the handle formatter.
|
|
|
|
//
|
|
|
|
// Like std::formatter<status, CharT> this formatter validates that parse is
|
|
|
|
// called. This formatter is intended to be used when the formatter's parse is
|
|
|
|
// called directly and not with format. In that case the format-spec does not
|
|
|
|
// require a terminating }. The tests must be written in a fashion where this
|
|
|
|
// formatter is always called with an empty format-spec. This requirement
|
|
|
|
// allows testing of certain code paths that are never reached by using a
|
|
|
|
// well-formed format-string in the format functions.
|
|
|
|
template <class CharT>
|
|
|
|
struct std::formatter<parse_call_validator, CharT> {
|
|
|
|
bool parse_called{false};
|
|
|
|
|
|
|
|
constexpr auto parse(basic_format_parse_context<CharT>& parse_ctx) -> decltype(parse_ctx.begin()) {
|
|
|
|
auto begin = parse_ctx.begin();
|
|
|
|
auto end = parse_ctx.end();
|
|
|
|
assert(begin == end);
|
|
|
|
parse_called = true;
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto format(parse_call_validator, auto& ctx) const -> decltype(ctx.out()) {
|
|
|
|
if (!parse_called)
|
|
|
|
throw_error<parse_call_validator::parse_function_not_called>();
|
|
|
|
return ctx.out();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <class T>
|
|
|
|
[[noreturn]] void throw_error() const {
|
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
|
|
|
throw T{};
|
|
|
|
#else
|
|
|
|
std::abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-05 18:57:32 +02:00
|
|
|
// Creates format string for the invalid types.
|
|
|
|
//
|
|
|
|
// valid contains a list of types that are valid.
|
|
|
|
// - The type ?s is the only type requiring 2 characters, use S for that type.
|
|
|
|
// - Whether n is a type or not depends on the context, is is always used.
|
|
|
|
//
|
|
|
|
// The return value is a collection of basic_strings, instead of
|
|
|
|
// basic_string_views since the values are temporaries.
|
|
|
|
namespace detail {
|
2023-03-14 21:27:03 +01:00
|
|
|
template <class CharT, std::size_t N>
|
2022-05-05 18:57:32 +02:00
|
|
|
std::basic_string<CharT> get_colons() {
|
|
|
|
static std::basic_string<CharT> result(N, CharT(':'));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::string_view get_format_types() {
|
2022-04-03 17:43:52 +02:00
|
|
|
return "aAbBcdeEfFgGopPsxX"
|
2022-05-05 18:57:32 +02:00
|
|
|
#if TEST_STD_VER > 20
|
|
|
|
"?"
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class CharT, /*format_types types,*/ size_t N>
|
|
|
|
std::vector<std::basic_string<CharT>> fmt_invalid_types(std::string_view valid) {
|
|
|
|
// std::ranges::to is not available in C++20.
|
|
|
|
std::vector<std::basic_string<CharT>> result;
|
|
|
|
std::ranges::copy(
|
|
|
|
get_format_types() | std::views::filter([&](char type) { return valid.find(type) == std::string_view::npos; }) |
|
|
|
|
std::views::transform([&](char type) { return std::format(SV("{{{}{}}}"), get_colons<CharT, N>(), type); }),
|
|
|
|
std::back_inserter(result));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// Creates format string for the invalid types.
|
|
|
|
//
|
|
|
|
// valid contains a list of types that are valid.
|
|
|
|
//
|
|
|
|
// The return value is a collection of basic_strings, instead of
|
|
|
|
// basic_string_views since the values are temporaries.
|
|
|
|
template <class CharT>
|
|
|
|
std::vector<std::basic_string<CharT>> fmt_invalid_types(std::string_view valid) {
|
|
|
|
return detail::fmt_invalid_types<CharT, 1>(valid);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like fmt_invalid_types but when the format spec is for an underlying formatter.
|
|
|
|
template <class CharT>
|
|
|
|
std::vector<std::basic_string<CharT>> fmt_invalid_nested_types(std::string_view valid) {
|
|
|
|
return detail::fmt_invalid_types<CharT, 2>(valid);
|
|
|
|
}
|
|
|
|
|
2022-12-15 18:07:41 +01:00
|
|
|
#endif // TEST_SUPPORT_FORMAT_FUNCTIONS_COMMON_H
|