Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MSVC workaround with FMT_ENFORCE_COMPILE_STRING #2049

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,18 +769,37 @@ template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_integral<Rep>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int) {
static FMT_CONSTEXPR_DECL const Char format[] = {'{', '}', 0};
return format_to(out, compile_string_to_view(format), val);

// Note(12/3/2020): Workaround an as-of-yet unfixed compiler error in MSVC.
#if FMT_MSC_VER
return vformat_to(out, to_string_view(format),
make_format_args<buffer_context<Char>>(val));
#else
return format_to(out, FMT_STRING(format), val);
#endif
}

template <typename Char, typename Rep, typename OutputIt,
FMT_ENABLE_IF(std::is_floating_point<Rep>::value)>
OutputIt format_duration_value(OutputIt out, Rep val, int precision) {
static FMT_CONSTEXPR_DECL const Char pr_f[] = {'{', ':', '.', '{',
'}', 'f', '}', 0};
if (precision >= 0)
return format_to(out, compile_string_to_view(pr_f), val, precision);
if (precision >= 0) {
#if FMT_MSC_VER && FMT_MSC_VER <= 1928
return vformat_to(out, to_string_view(pr_f),
make_format_args<buffer_context<Char>>(val, precision));
#else
return format_to(out, FMT_STRING(pr_f), val, precision);
#endif
}
static FMT_CONSTEXPR_DECL const Char fp_f[] = {'{', ':', 'g', '}', 0};
return format_to(out, compile_string_to_view(fp_f), val);

#if FMT_MSC_VER && FMT_MSC_VER <= 1928
return vformat_to(out, to_string_view(fp_f),
make_format_args<buffer_context<Char>>(val));
#else
return format_to(out, FMT_STRING(fp_f), val);
#endif
}

template <typename Char, typename OutputIt>
Expand All @@ -801,12 +820,23 @@ OutputIt format_duration_unit(OutputIt out) {
if (const char* unit = get_units<Period>())
return copy_unit(string_view(unit), out, Char());
static FMT_CONSTEXPR_DECL const Char num_f[] = {'[', '{', '}', ']', 's', 0};
if (const_check(Period::den == 1))
return format_to(out, compile_string_to_view(num_f), Period::num);
if (const_check(Period::den == 1)) {
#if FMT_MSC_VER
return vformat_to(out, to_string_view(num_f),
make_format_args<buffer_context<Char>>(Period::num));
#else
return format_to(out, FMT_STRING(num_f), Period::num);
#endif
}
static FMT_CONSTEXPR_DECL const Char num_def_f[] = {'[', '{', '}', '/', '{',
'}', ']', 's', 0};
return format_to(out, compile_string_to_view(num_def_f), Period::num,
Period::den);
#if FMT_MSC_VER
return vformat_to(
out, to_string_view(num_def_f),
make_format_args<buffer_context<Char>>(Period::num, Period::den));
#else
return format_to(out, FMT_STRING(num_def_f), Period::num, Period::den);
#endif
}

template <typename FormatContext, typename OutputIt, typename Rep,
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ add_fmt_test(printf-test)
add_fmt_test(ranges-test)
add_fmt_test(scan-test)


add_fmt_test(enforce-compiletime-test)
target_compile_definitions(enforce-compiletime-test PRIVATE "-DFMT_ENFORCE_COMPILE_STRING")

if (NOT DEFINED MSVC_STATIC_RUNTIME AND MSVC)
foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
Expand Down
29 changes: 29 additions & 0 deletions test/enforce-compiletime-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Formatting library for C++ - formatting library tests
//
// Copyright (c) 2012 - present, Victor Zverovich
// All rights reserved.
//
// For the license information refer to format.h.


#ifdef WIN32
# define _CRT_SECURE_NO_WARNINGS
#endif

#include "fmt/chrono.h"
#include "fmt/format.h"

#include "gmock.h"
#include "gtest-extra.h"

#ifndef FMT_STATIC_THOUSANDS_SEPARATOR

TEST(ChronoTest, FormatDefault) {
EXPECT_EQ("42s", fmt::format(FMT_STRING("{}"), std::chrono::seconds(42)));
}

TEST(ChronoTest, FormatWide) {
EXPECT_EQ(L"42s", fmt::format(FMT_STRING(L"{}"), std::chrono::seconds(42)));
}

#endif // FMT_STATIC_THOUSANDS_SEPARATOR