From 071142033cafec4875eee7718df77de5db7ab8e5 Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Sun, 8 Nov 2020 04:39:41 +0300 Subject: [PATCH] add compile-time formatting switch and empty test --- include/fmt/core.h | 11 +++++++++++ test/CMakeLists.txt | 6 ++++++ test/compile-time-formatting-test.cc | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 test/compile-time-formatting-test.cc diff --git a/include/fmt/core.h b/include/fmt/core.h index 4ab3e2ec..ae43a6a1 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -252,6 +252,17 @@ # pragma execution_character_set("utf-8") #endif +#if defined(FMT_ENABLE_COMPILE_TIME_FORMATTING) +# if !defined(FMT_HEADER_ONLY) || __cplusplus < 202002L +# error "compile time formatting could not be enabled" +# endif +# define FMT_CTF_CONSTEXPR constexpr +# define FMT_USE_CUSTOM_BACK_INSERT_ITERATOR 1 +#else +# define FMT_CTF_CONSTEXPR +# define FMT_USE_CUSTOM_BACK_INSERT_ITERATOR 0 +#endif + FMT_BEGIN_NAMESPACE // Implementations of enable_if_t and other metafunctions for older systems. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7ae5659d..35ca8d2d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -146,6 +146,12 @@ else () target_compile_definitions(header-only-test PRIVATE FMT_HEADER_ONLY=1) endif () +add_fmt_executable(compile-time-formatting-test + compile-time-formatting-test.cc test-main.cc) +target_link_libraries(compile-time-formatting-test gmock) +target_include_directories(compile-time-formatting-test SYSTEM PUBLIC gtest gmock) +target_link_libraries(compile-time-formatting-test fmt::fmt-header-only) + message(STATUS "FMT_PEDANTIC: ${FMT_PEDANTIC}") if (FMT_PEDANTIC) diff --git a/test/compile-time-formatting-test.cc b/test/compile-time-formatting-test.cc new file mode 100644 index 00000000..ebcc182a --- /dev/null +++ b/test/compile-time-formatting-test.cc @@ -0,0 +1,21 @@ +#include +#include + +#define FMT_ENABLE_COMPILE_TIME_FORMATTING 1 +#include "fmt/format.h" +#include "gmock.h" +#include "gtest-extra.h" + +template struct constexpr_buffer_helper { + template + constexpr constexpr_buffer_helper& modify(Func func) { + func(buffer); + return *this; + } + + template constexpr bool operator==(const T& rhs) const noexcept { + return (std::string_view(rhs).compare(buffer.data()) == 0); + } + + std::array buffer{}; +};