From 6f3190d6829dfd902f94005a8ce3dac423c019c1 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 7 Feb 2017 18:11:33 +0000 Subject: [PATCH] [Support] Add FormatVariadic support for chrono types Summary: The formatter has three knobs: - the user can choose which time unit to use for formatting (default: whatever is the unit of the input) - he can choose whether the unit gets displayed (default: yes) - he can affect the way the number itself is formatted via standard number formatting options (default:default) Reviewers: zturner, inglorion Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29481 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294326 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/Chrono.h | 101 ++++++++++++++++++++++++++++++++++ lib/Support/Chrono.cpp | 7 +++ unittests/Support/Chrono.cpp | 31 +++++++++++ 3 files changed, 139 insertions(+) diff --git a/include/llvm/Support/Chrono.h b/include/llvm/Support/Chrono.h index 203439cab91..c46ae5a32db 100644 --- a/include/llvm/Support/Chrono.h +++ b/include/llvm/Support/Chrono.h @@ -11,6 +11,7 @@ #define LLVM_SUPPORT_CHRONO_H #include "llvm/Support/Compiler.h" +#include "llvm/Support/FormatProviders.h" #include #include @@ -50,6 +51,106 @@ toTimePoint(std::time_t T) { raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP); +/// Implementation of format_provider for duration types. +/// +/// The options string of a duration type has the grammar: +/// +/// duration_options ::= [unit][show_unit [number_options]] +/// unit ::= `h`|`m`|`s`|`ms|`us`|`ns` +/// show_unit ::= `+` | `-` +/// number_options ::= options string for a integral or floating point type +/// +/// Examples +/// ================================= +/// | options | Input | Output | +/// ================================= +/// | "" | 1s | 1 s | +/// | "ms" | 1s | 1000 ms | +/// | "ms-" | 1s | 1000 | +/// | "ms-n" | 1s | 1,000 | +/// | "" | 1.0s | 1.00 s | +/// ================================= +/// +/// If the unit of the duration type is not one of the units specified above, +/// it is still possible to format it, provided you explicitly request a +/// display unit or you request that the unit is not displayed. + +namespace detail { +template struct unit { static constexpr char value[] = ""; }; +template constexpr char unit::value[]; + +template <> struct unit> { + static constexpr char value[] = "h"; +}; + +template <> struct unit> { + static constexpr char value[] = "m"; +}; + +template <> struct unit> { static constexpr char value[] = "s"; }; +template <> struct unit { static constexpr char value[] = "ms"; }; +template <> struct unit { static constexpr char value[] = "us"; }; +template <> struct unit { static constexpr char value[] = "ns"; }; +} // namespace detail + +template +struct format_provider> { +private: + typedef std::chrono::duration Dur; + typedef typename std::conditional< + std::chrono::treat_as_floating_point::value, double, intmax_t>::type + InternalRep; + + template static InternalRep getAs(const Dur &D) { + using namespace std::chrono; + return duration_cast>(D).count(); + } + + static std::pair consumeUnit(StringRef &Style, + const Dur &D) { + using namespace std::chrono; + if (Style.consume_front("ns")) + return {getAs(D), "ns"}; + if (Style.consume_front("us")) + return {getAs(D), "us"}; + if (Style.consume_front("ms")) + return {getAs(D), "ms"}; + if (Style.consume_front("s")) + return {getAs>(D), "s"}; + if (Style.consume_front("m")) + return {getAs>(D), "m"}; + if (Style.consume_front("h")) + return {getAs>(D), "h"}; + return {D.count(), detail::unit::value}; + } + + static bool consumeShowUnit(StringRef &Style) { + if (Style.empty()) + return true; + if (Style.consume_front("-")) + return false; + if (Style.consume_front("+")) + return true; + assert(0 && "Unrecognised duration format"); + return true; + } + +public: + static void format(const Dur &D, llvm::raw_ostream &Stream, StringRef Style) { + InternalRep count; + StringRef unit; + std::tie(count, unit) = consumeUnit(Style, D); + bool show_unit = consumeShowUnit(Style); + + format_provider::format(count, Stream, Style); + + if (show_unit) { + assert(!unit.empty()); + Stream << " " << unit; + } + } +}; + } // namespace llvm #endif // LLVM_SUPPORT_CHRONO_H diff --git a/lib/Support/Chrono.cpp b/lib/Support/Chrono.cpp index cdadbd87997..ef81edc9269 100644 --- a/lib/Support/Chrono.cpp +++ b/lib/Support/Chrono.cpp @@ -16,6 +16,13 @@ namespace llvm { using namespace sys; +constexpr char detail::unit>::value[]; +constexpr char detail::unit>::value[]; +constexpr char detail::unit>::value[]; +constexpr char detail::unit::value[]; +constexpr char detail::unit::value[]; +constexpr char detail::unit::value[]; + static inline struct tm getStructTM(TimePoint<> TP) { struct tm Storage; std::time_t OurTime = toTimeT(TP); diff --git a/unittests/Support/Chrono.cpp b/unittests/Support/Chrono.cpp index 3d578780756..1410baf848b 100644 --- a/unittests/Support/Chrono.cpp +++ b/unittests/Support/Chrono.cpp @@ -9,6 +9,7 @@ #include "llvm/Support/Chrono.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/Support/FormatVariadic.h" #include "gtest/gtest.h" using namespace llvm; @@ -76,4 +77,34 @@ TEST(Chrono, ImplicitConversions) { EXPECT_EQ(TimeT, toTimeT(Nano)); } +TEST(Chrono, DurationFormat) { + EXPECT_EQ("1 h", formatv("{0}", hours(1)).str()); + EXPECT_EQ("1 m", formatv("{0}", minutes(1)).str()); + EXPECT_EQ("1 s", formatv("{0}", seconds(1)).str()); + EXPECT_EQ("1 ms", formatv("{0}", milliseconds(1)).str()); + EXPECT_EQ("1 us", formatv("{0}", microseconds(1)).str()); + EXPECT_EQ("1 ns", formatv("{0}", nanoseconds(1)).str()); + + EXPECT_EQ("1 s", formatv("{0:+}", seconds(1)).str()); + EXPECT_EQ("1", formatv("{0:-}", seconds(1)).str()); + + EXPECT_EQ("1000 ms", formatv("{0:ms}", seconds(1)).str()); + EXPECT_EQ("1000000 us", formatv("{0:us}", seconds(1)).str()); + EXPECT_EQ("1000", formatv("{0:ms-}", seconds(1)).str()); + + EXPECT_EQ("1,000 ms", formatv("{0:+n}", milliseconds(1000)).str()); + EXPECT_EQ("0x3e8", formatv("{0:-x}", milliseconds(1000)).str()); + EXPECT_EQ("010", formatv("{0:-3}", milliseconds(10)).str()); + EXPECT_EQ("10,000", formatv("{0:ms-n}", seconds(10)).str()); + + EXPECT_EQ("1.00 s", formatv("{0}", duration(1)).str()); + EXPECT_EQ("0.123 s", formatv("{0:+3}", duration(0.123f)).str()); + EXPECT_EQ("1.230e-01 s", formatv("{0:+e3}", duration(0.123f)).str()); + + typedef duration> + microfortnights; + EXPECT_EQ("1.00", formatv("{0:-}", microfortnights(1)).str()); + EXPECT_EQ("1209.60 ms", formatv("{0:ms}", microfortnights(1)).str()); +} + } // anonymous namespace