Skip to content

Commit

Permalink
Call parse on empty specs at compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 16, 2023
1 parent 8e87d3a commit 661b23e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,9 @@ template <typename Char, typename... Args> class format_string_checker {
#endif
}

FMT_CONSTEXPR void on_replacement_field(int, const Char*) {}
FMT_CONSTEXPR void on_replacement_field(int id, const Char* begin) {
on_format_specs(id, begin, begin); // Call parse() on empty specs.
}

FMT_CONSTEXPR auto on_format_specs(int id, const Char* begin, const Char*)
-> const Char* {
Expand Down
18 changes: 9 additions & 9 deletions test/core-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ struct custom_context {
bool called = false;

template <typename T> struct formatter_type {
auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(fmt::format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand All @@ -290,7 +290,7 @@ struct test_struct {};

FMT_BEGIN_NAMESPACE
template <typename Char> struct formatter<test_struct, Char> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand Down Expand Up @@ -573,7 +573,7 @@ struct disabled_formatter_convertible {

FMT_BEGIN_NAMESPACE
template <> struct formatter<enabled_formatter> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
auto format(enabled_formatter, format_context& ctx) const
Expand All @@ -583,7 +583,7 @@ template <> struct formatter<enabled_formatter> {
};

template <> struct formatter<enabled_ptr_formatter*> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
auto format(enabled_ptr_formatter*, format_context& ctx) const
Expand All @@ -607,7 +607,7 @@ struct nonconst_formattable {};

FMT_BEGIN_NAMESPACE
template <> struct formatter<const_formattable> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand All @@ -619,7 +619,7 @@ template <> struct formatter<const_formattable> {
};

template <> struct formatter<nonconst_formattable> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand All @@ -641,7 +641,7 @@ struct convertible_to_pointer_formattable {

FMT_BEGIN_NAMESPACE
template <> struct formatter<convertible_to_pointer_formattable> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand Down Expand Up @@ -721,7 +721,7 @@ struct convertible_to_cstring {

FMT_BEGIN_NAMESPACE
template <> struct formatter<convertible_to_int> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
auto format(convertible_to_int, format_context& ctx) const
Expand Down Expand Up @@ -842,7 +842,7 @@ struct its_a_trap {

FMT_BEGIN_NAMESPACE
template <> struct formatter<its_a_trap> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand Down
2 changes: 1 addition & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ struct check_back_appender {};

FMT_BEGIN_NAMESPACE
template <> struct formatter<check_back_appender> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}

Expand Down

0 comments on commit 661b23e

Please sign in to comment.