From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 22:53:50 -0400 Subject: [PATCH 07/36] Remove format_provider --- llvm/include/llvm/Support/Chrono.h | 114 ------------------------ llvm/include/llvm/Support/raw_ostream.h | 6 -- llvm/unittests/Support/Chrono.cpp | 67 -------------- 3 files changed, 187 deletions(-) diff --git a/llvm/include/llvm/Support/Chrono.h b/llvm/include/llvm/Support/Chrono.h index 9c9ba7002310eba5113c14957f769702c61f4326..b269ff8bb5db7bb3c62c3a87daf255b18ece4cd7 100644 --- a/llvm/include/llvm/Support/Chrono.h +++ b/llvm/include/llvm/Support/Chrono.h @@ -10,7 +10,6 @@ #define LLVM_SUPPORT_CHRONO_H #include "llvm/Support/Compiler.h" -#include "llvm/Support/FormatProviders.h" #include #include @@ -80,119 +79,6 @@ toTimePoint(std::time_t T, uint32_t nsec) { raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> TP); raw_ostream &operator<<(raw_ostream &OS, sys::UtcTime<> TP); -/// Format provider for TimePoint<> -/// -/// The options string is a strftime format string, with extensions: -/// - %L is millis: 000-999 -/// - %f is micros: 000000-999999 -/// - %N is nanos: 000000000 - 999999999 -/// -/// If no options are given, the default format is "%Y-%m-%d %H:%M:%S.%N". -template <> -struct format_provider> { - static void format(const sys::TimePoint<> &TP, llvm::raw_ostream &OS, - std::string_view Style); -}; - -template <> struct format_provider> { - static void format(const sys::UtcTime &TP, - llvm::raw_ostream &OS, StringRef Style); -}; - -namespace detail { -template struct unit { static const char value[]; }; -template const char unit::value[] = ""; - -template <> struct unit> { static const char value[]; }; -template <> struct unit> { static const char value[]; }; -template <> struct unit> { static const char value[]; }; -template <> struct unit { static const char value[]; }; -template <> struct unit { static const char value[]; }; -template <> struct unit { static const char value[]; }; -} // namespace detail - -/// 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. - -template -struct format_provider> { -private: - typedef std::chrono::duration Dur; - typedef std::conditional_t::value, - double, intmax_t> - InternalRep; - - template static InternalRep getAs(const Dur &D) { - using namespace std::chrono; - return duration_cast>(D).count(); - } - - static std::pair consumeUnit(std::string_view &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(std::string_view &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, std::string_view Style) { - InternalRep count; - std::string_view 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/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h index 9345348d9ba555022b31f94299684316ffa15939..862a1db876e9b8467a8839dae8f6632f18d5e7a0 100644 --- a/llvm/include/llvm/Support/raw_ostream.h +++ b/llvm/include/llvm/Support/raw_ostream.h @@ -27,12 +27,6 @@ namespace llvm { -class Duration; -class formatv_object_base; -class format_object_base; -class FormattedString; -class FormattedNumber; -class FormattedBytes; template class [[nodiscard]] Expected; namespace sys { diff --git a/llvm/unittests/Support/Chrono.cpp b/llvm/unittests/Support/Chrono.cpp index 7dfc5dd2c29348ea8df9ce87c80f357aaad1a73b..a4d166d435d6d679f773dcf3eab985f0631e12d2 100644 --- a/llvm/unittests/Support/Chrono.cpp +++ b/llvm/unittests/Support/Chrono.cpp @@ -29,43 +29,6 @@ TEST(Chrono, TimeTConversion) { EXPECT_EQ(TP, toTimePoint(toTimeT(TP))); } -TEST(Chrono, TimePointFormat) { - using namespace std::chrono; - struct tm TM {}; - TM.tm_year = 106; - TM.tm_mon = 0; - TM.tm_mday = 2; - TM.tm_hour = 15; - TM.tm_min = 4; - TM.tm_sec = 5; - TM.tm_isdst = -1; - TimePoint<> T = - system_clock::from_time_t(mktime(&TM)) + nanoseconds(123456789); - TimePoint<> T2 = - system_clock::from_time_t(mktime(&TM)) + nanoseconds(23456789); - - // operator<< uses the format YYYY-MM-DD HH:MM:SS.NNNNNNNNN - std::string S; - raw_string_ostream OS(S); - OS << T; - EXPECT_EQ("2006-01-02 15:04:05.123456789", OS.str()); - S.clear(); - OS << T2; - EXPECT_EQ("2006-01-02 15:04:05.023456789", OS.str()); - - // formatv default style matches operator<<. - EXPECT_EQ("2006-01-02 15:04:05.123456789", formatv("{0}", T).str()); - EXPECT_EQ("2006-01-02 15:04:05.023456789", formatv("{0}", T2).str()); - // formatv supports strftime-style format strings. - EXPECT_EQ("15:04:05", formatv("{0:%H:%M:%S}", T).str()); - // formatv supports our strftime extensions for sub-second precision. - EXPECT_EQ("123", formatv("{0:%L}", T).str()); - EXPECT_EQ("123456", formatv("{0:%f}", T).str()); - EXPECT_EQ("123456789", formatv("{0:%N}", T).str()); - // our extensions don't interfere with %% escaping. - EXPECT_EQ("%foo", formatv("{0:%%foo}", T).str()); -} - // Test that toTimePoint and toTimeT can be called with a arguments with varying // precisions. TEST(Chrono, ImplicitConversions) { @@ -83,34 +46,4 @@ 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