From 8a34ca167028b6f5dfc1dcf9a4273319e1910123 Mon Sep 17 00:00:00 2001 From: Ian Sherman Date: Mon, 5 Feb 2024 14:00:09 -0800 Subject: [PATCH] Fix clang-14 fmt compilation errors --- .../pango_core/include/pangolin/utils/fmt.h | 25 ++++++++++++++++++- .../include/pangolin/utils/logging.h | 4 +-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/components/pango_core/include/pangolin/utils/fmt.h b/components/pango_core/include/pangolin/utils/fmt.h index 410f37aa5..c2fc30883 100644 --- a/components/pango_core/include/pangolin/utils/fmt.h +++ b/components/pango_core/include/pangolin/utils/fmt.h @@ -9,10 +9,33 @@ namespace pangolin { +// Wraps fmt::format to conform to its compile-time checked APIs. +// +// With more recent versions of fmt and more recent compilers, +// more compile-time checking is enabled in fmt by default. +// +// Pangolin logging currently relies on some runtime +// format string construction, so we add a layer of indirection here. +// +// Alternatively we could eliminate the use of runtime format strings in Pangolin. +template +std::string format(TFmt&& fmt, TArgs&&... args) { + if constexpr (std::is_same_v>) { + // The type of the fmt string is known at compile-time, forward as-is + return fmt::format(fmt, std::forward(args)...); + } else if constexpr (std::is_convertible_v) { + // The type of the fmt string is not known at compile-time, wrap in fmt::runtime + return fmt::format(fmt::runtime(fmt), std::forward(args)...); + } else { + // The fmt string is some other type, like a `fmt::text_style`, forward as-is + return fmt::format(fmt, std::forward(args)...); + } +} + template void println(TArgs&&... args) { - fmt::print(std::forward(args)...); + fmt::print(fmt::runtime(format(std::forward(args)...))); std::cout << std::endl; } diff --git a/components/pango_core/include/pangolin/utils/logging.h b/components/pango_core/include/pangolin/utils/logging.h index fccd856b2..c0cfc9192 100644 --- a/components/pango_core/include/pangolin/utils/logging.h +++ b/components/pango_core/include/pangolin/utils/logging.h @@ -124,7 +124,7 @@ struct Log { const char* assertion_statement, Args... args) { if constexpr (sizeof...(args) > 0) { - const std::string arg_string = fmt::format(std::forward(args)...); + const std::string arg_string = pangolin::format(std::forward(args)...); logImpl(kind, sFile, sFunction, nLine, assertion_statement, arg_string); } else { logImpl(kind, sFile, sFunction, nLine, assertion_statement, ""); @@ -137,7 +137,7 @@ struct Log { const char* assertion_statement, Args... args) { if constexpr (sizeof...(args) > 0) { - const std::string arg_string = fmt::format(std::forward(args)...); + const std::string arg_string = pangolin::format(std::forward(args)...); logImpl(kind, sFile, sFunction, nLine, assertion_statement, arg_string); throw std::runtime_error(arg_string); } else {