From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: PJ Reiniger Date: Sat, 7 May 2022 22:53:50 -0400 Subject: [PATCH 08/31] Remove format_provider --- llvm/include/llvm/Support/Chrono.h | 109 ------------------------ llvm/include/llvm/Support/raw_ostream.h | 6 -- llvm/unittests/Support/Chrono.cpp | 61 ------------- 3 files changed, 176 deletions(-) diff --git a/llvm/include/llvm/Support/Chrono.h b/llvm/include/llvm/Support/Chrono.h index a7dea19d9193bcff4bc6b553b80a10b2bc7b64af..9f9a2b5cab270327898cee3f97d9ae7cf77eb564 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 @@ -59,114 +58,6 @@ toTimePoint(std::time_t T, uint32_t nsec) { raw_ostream &operator<<(raw_ostream &OS, sys::TimePoint<> 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); -}; - -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 c8a94f46fab18dabc123fd709974138c8b0b0beb..0bf165c55921d7e09208a8fa8df7f9f5ad68d1e2 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 9a08a5c1bfdff409c2240b7d15727d32d6339399..3c049de18c0a80465f4b0a8c054df2602d5e9b1c 100644 --- a/llvm/unittests/Support/Chrono.cpp +++ b/llvm/unittests/Support/Chrono.cpp @@ -30,37 +30,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); - - // 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()); - - // formatv default style matches operator<<. - EXPECT_EQ("2006-01-02 15:04:05.123456789", formatv("{0}", T).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) { @@ -78,34 +47,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