From d70729215fba1d54862e407b626abf86ddf409bf Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 14 Jan 2024 06:34:25 -0800 Subject: [PATCH] Fix constness --- include/fmt/base.h | 6 ++++-- include/fmt/format.h | 26 ++++++++++++++------------ test/compile-fp-test.cc | 2 +- test/compile-test.cc | 6 +++--- test/core-test.cc | 12 ++++++------ test/format-test.cc | 15 ++++++++------- test/ostream-test.cc | 3 ++- test/posix-mock-test.cc | 5 +++-- test/printf-test.cc | 10 +++++++--- test/scan-test.cc | 3 +-- test/xchar-test.cc | 30 ++++++++++++++++++------------ 11 files changed, 67 insertions(+), 51 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 402fd315..fc0ea7b9 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -12,6 +12,7 @@ #include // FILE #include // strlen +// is also included transitively from . #include // std::byte #include // std::enable_if @@ -1332,8 +1333,9 @@ template class value { parse_ctx.advance_to(f.parse(parse_ctx)); using qualified_type = conditional_t(), const T, T>; - // Calling format through a mutable reference is deprecated. - ctx.advance_to(f.format(*static_cast(arg), ctx)); + // format must be const for compatibility with std::format and compilation. + const auto& cf = f; + ctx.advance_to(cf.format(*static_cast(arg), ctx)); } }; diff --git a/include/fmt/format.h b/include/fmt/format.h index efd89df2..f061489b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -4121,12 +4121,13 @@ template <> struct formatter { } template - auto format(bytes b, FormatContext& ctx) -> decltype(ctx.out()) { - detail::handle_dynamic_spec(specs_.width, - specs_.width_ref, ctx); + auto format(bytes b, FormatContext& ctx) const -> decltype(ctx.out()) { + auto specs = specs_; + detail::handle_dynamic_spec(specs.width, + specs.width_ref, ctx); detail::handle_dynamic_spec( - specs_.precision, specs_.precision_ref, ctx); - return detail::write_bytes(ctx.out(), b.data_, specs_); + specs.precision, specs.precision_ref, ctx); + return detail::write_bytes(ctx.out(), b.data_, specs); } }; @@ -4162,15 +4163,16 @@ template struct formatter> : formatter { } template - auto format(group_digits_view t, FormatContext& ctx) + auto format(group_digits_view t, FormatContext& ctx) const -> decltype(ctx.out()) { - detail::handle_dynamic_spec(specs_.width, - specs_.width_ref, ctx); + auto specs = specs_; + detail::handle_dynamic_spec(specs.width, + specs.width_ref, ctx); detail::handle_dynamic_spec( - specs_.precision, specs_.precision_ref, ctx); - return detail::write_int( - ctx.out(), static_cast>(t.value), 0, specs_, - detail::digit_grouping("\3", ",")); + specs.precision, specs.precision_ref, ctx); + return detail::write_int(ctx.out(), + static_cast>(t.value), + 0, specs, detail::digit_grouping("\3", ",")); } }; diff --git a/test/compile-fp-test.cc b/test/compile-fp-test.cc index 92c57af2..3afb7690 100644 --- a/test/compile-fp-test.cc +++ b/test/compile-fp-test.cc @@ -58,4 +58,4 @@ TEST(compile_time_formatting_test, floating_point) { EXPECT_EQ("-inf", test_format<5>(FMT_COMPILE("{}"), -inf)); } -#endif // FMT_USE_CONSTEVAL +#endif // FMT_USE_CONSTEVAL diff --git a/test/compile-test.cc b/test/compile-test.cc index 3fca5a46..a4c350bc 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -35,7 +35,7 @@ struct type_with_get { FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { template - auto format(type_with_get, FormatContext& ctx) -> decltype(ctx.out()) { + auto format(type_with_get, FormatContext& ctx) const -> decltype(ctx.out()) { return formatter::format(42, ctx); } }; @@ -296,8 +296,8 @@ TEST(compile_test, compile_format_string_literal) { // (compiler file // 'D:\a\_work\1\s\src\vctools\Compiler\CxxFE\sl\p1\c\constexpr\constexpr.cpp', // line 8635) -#if FMT_USE_CONSTEVAL && \ - (!FMT_MSC_VERSION || \ +#if FMT_USE_CONSTEVAL && \ + (!FMT_MSC_VERSION || \ (FMT_MSC_VERSION >= 1928 && FMT_MSC_VERSION < 1930)) && \ defined(__cpp_lib_is_constant_evaluated) template struct test_string { diff --git a/test/core-test.cc b/test/core-test.cc index 5b573f9b..f01432ee 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -9,8 +9,6 @@ #include "test-assert.h" // clang-format on -#include "fmt/base.h" - #include // INT_MAX #include // std::strlen #include // std::equal_to @@ -19,6 +17,7 @@ #include // std::string #include // std::is_same +#include "fmt/base.h" #include "gmock/gmock.h" using fmt::string_view; @@ -280,7 +279,7 @@ struct custom_context { return ctx.begin(); } - const char* format(const T&, custom_context& ctx) { + const char* format(const T&, custom_context& ctx) const { ctx.called = true; return nullptr; } @@ -602,7 +601,7 @@ template <> struct formatter { return ctx.begin(); } - auto format(const const_formattable&, format_context& ctx) + auto format(const const_formattable&, format_context& ctx) const -> decltype(ctx.out()) { return copy("test", ctx.out()); } @@ -613,7 +612,7 @@ template <> struct formatter { return ctx.begin(); } - auto format(nonconst_formattable&, format_context& ctx) + auto format(nonconst_formattable&, format_context& ctx) const -> decltype(ctx.out()) { return copy("test", ctx.out()); } @@ -807,7 +806,8 @@ template <> struct formatter { return ctx.begin(); } - auto format(its_a_trap, format_context& ctx) const -> decltype(ctx.out()) { + auto format(its_a_trap, format_context& ctx) const + -> decltype(ctx.out()) const { auto out = ctx.out(); *out++ = 'x'; return out; diff --git a/test/format-test.cc b/test/format-test.cc index b38a3bd4..e78fc6b1 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1590,7 +1590,8 @@ struct string_viewable {}; FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { - auto format(string_viewable, format_context& ctx) -> decltype(ctx.out()) { + auto format(string_viewable, format_context& ctx) const + -> decltype(ctx.out()) { return formatter::format("foo", ctx); } }; @@ -1608,8 +1609,8 @@ struct explicitly_convertible_to_std_string_view { template <> struct fmt::formatter : formatter { - auto format(explicitly_convertible_to_std_string_view v, format_context& ctx) - -> decltype(ctx.out()) { + auto format(explicitly_convertible_to_std_string_view v, + format_context& ctx) const -> decltype(ctx.out()) { return fmt::format_to(ctx.out(), "'{}'", std::string_view(v)); } }; @@ -1631,7 +1632,7 @@ template <> struct formatter { return it; } - auto format(const date& d, format_context& ctx) -> decltype(ctx.out()) { + auto format(const date& d, format_context& ctx) const -> decltype(ctx.out()) { // Namespace-qualify to avoid ambiguity with std::format_to. fmt::format_to(ctx.out(), "{}-{}-{}", d.year(), d.month(), d.day()); return ctx.out(); @@ -1640,7 +1641,7 @@ template <> struct formatter { template <> struct formatter : formatter { template - auto format(Answer, FormatContext& ctx) -> decltype(ctx.out()) { + auto format(Answer, FormatContext& ctx) const -> decltype(ctx.out()) { return formatter::format(42, ctx); } }; @@ -1903,7 +1904,7 @@ template void write(OutputIt, foo) = delete; FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { - auto format(adl_test::fmt::detail::foo, format_context& ctx) + auto format(adl_test::fmt::detail::foo, format_context& ctx) const -> decltype(ctx.out()) { return formatter::format("foo", ctx); } @@ -2072,7 +2073,7 @@ template <> struct formatter { } template - auto format(check_back_appender, Context& ctx) -> decltype(ctx.out()) { + auto format(check_back_appender, Context& ctx) const -> decltype(ctx.out()) { auto out = ctx.out(); static_assert(std::is_same::value, "needs to satisfy weakly_incrementable"); diff --git a/test/ostream-test.cc b/test/ostream-test.cc index fd02a656..cc28885a 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -201,7 +201,8 @@ auto operator<<(std::ostream& os, test_template) -> std::ostream& { namespace fmt { template struct formatter> : formatter { - auto format(test_template, format_context& ctx) -> decltype(ctx.out()) { + auto format(test_template, format_context& ctx) const + -> decltype(ctx.out()) { return formatter::format(2, ctx); } }; diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index 07072a02..bcd330d4 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -393,7 +393,8 @@ TEST(buffered_file_test, open_retry) { TEST(buffered_file_test, close_no_retry_in_dtor) { auto pipe = fmt::pipe(); - std::unique_ptr f(new buffered_file(pipe.read_end.fdopen("r"))); + std::unique_ptr f( + new buffered_file(pipe.read_end.fdopen("r"))); int saved_fclose_count = 0; EXPECT_WRITE( stderr, @@ -428,7 +429,7 @@ TEST(buffered_file_test, fileno_no_retry) { struct test_mock { static test_mock* instance; -} * test_mock::instance; +}* test_mock::instance; TEST(scoped_mock, scope) { { diff --git a/test/printf-test.cc b/test/printf-test.cc index aea0436a..d3f03b7f 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -310,10 +310,14 @@ TEST(printf_test, dynamic_precision) { } } -template struct make_signed { using type = T; }; +template struct make_signed { + using type = T; +}; -#define SPECIALIZE_MAKE_SIGNED(T, S) \ - template <> struct make_signed { using type = S; } +#define SPECIALIZE_MAKE_SIGNED(T, S) \ + template <> struct make_signed { \ + using type = S; \ + } SPECIALIZE_MAKE_SIGNED(char, signed char); SPECIALIZE_MAKE_SIGNED(unsigned char, signed char); diff --git a/test/scan-test.cc b/test/scan-test.cc index 6891bf20..803ba069 100644 --- a/test/scan-test.cc +++ b/test/scan-test.cc @@ -177,11 +177,10 @@ TEST(scan_test, lock) { }; std::thread consumer1(fun); std::thread consumer2(fun); - + producer.join(); consumer1.join(); consumer2.join(); EXPECT_EQ(count, 1000); - } #endif // FMT_USE_FCNTL diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 8ecfebe4..93461c99 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -45,10 +45,12 @@ TYPED_TEST(has_to_string_view_test, has_to_string_view) { EXPECT_TRUE(fmt::detail::has_to_string_view::value); EXPECT_TRUE(fmt::detail::has_to_string_view::value); EXPECT_TRUE(fmt::detail::has_to_string_view::value); - EXPECT_TRUE(fmt::detail::has_to_string_view>::value); - EXPECT_TRUE(fmt::detail::has_to_string_view>::value); EXPECT_TRUE( - fmt::detail::has_to_string_view>::value); + fmt::detail::has_to_string_view>::value); + EXPECT_TRUE(fmt::detail::has_to_string_view< + fmt::basic_string_view>::value); + EXPECT_TRUE(fmt::detail::has_to_string_view< + derived_from_string_view>::value); using fmt_string_view = fmt::detail::std_string_view; EXPECT_TRUE(std::is_empty::value != fmt::detail::has_to_string_view::value); @@ -576,18 +578,19 @@ template struct formatter, charT> { template typename FormatContext::iterator format(const std::complex& c, - FormatContext& ctx) { + FormatContext& ctx) const { + auto specs = specs_; detail::handle_dynamic_spec( - specs_.precision, specs_.precision_ref, ctx); - auto specs = std::string(); - if (specs_.precision > 0) specs = fmt::format(".{}", specs_.precision); - if (specs_.type == presentation_type::fixed_lower) specs += 'f'; + specs.precision, specs.precision_ref, ctx); + auto fspecs = std::string(); + if (specs.precision > 0) fspecs = fmt::format(".{}", specs.precision); + if (specs.type == presentation_type::fixed_lower) fspecs += 'f'; auto real = fmt::format(ctx.locale().template get(), - fmt::runtime("{:" + specs + "}"), c.real()); + fmt::runtime("{:" + fspecs + "}"), c.real()); auto imag = fmt::format(ctx.locale().template get(), - fmt::runtime("{:" + specs + "}"), c.imag()); + fmt::runtime("{:" + fspecs + "}"), c.imag()); auto fill_align_width = std::string(); - if (specs_.width > 0) fill_align_width = fmt::format(">{}", specs_.width); + if (specs.width > 0) fill_align_width = fmt::format(">{}", specs.width); return fmt::format_to(ctx.out(), runtime("{:" + fill_align_width + "}"), c.real() != 0 ? fmt::format("({}+{}i)", real, imag) : fmt::format("{}i", imag)); @@ -609,7 +612,10 @@ TEST(locale_test, chrono_weekday) { EXPECT_EQ(fmt::format(L"{}", sat), L"Sat"); if (loc != std::locale::classic()) { // L'\xE1' is 'รก'. - auto saturdays = std::vector{L"s\xE1""b", L"s\xE1."}; + auto saturdays = std::vector{ + L"s\xE1" + "b", + L"s\xE1."}; EXPECT_THAT(saturdays, Contains(fmt::format(loc, L"{:L}", sat))); } std::locale::global(loc_old);