Index: docs/ProgrammersManual.rst =================================================================== --- docs/ProgrammersManual.rst +++ docs/ProgrammersManual.rst @@ -331,16 +331,15 @@ to extend the mechanism for formatting a type that the library already knows how to format. For that, we need something else. -2. Provide a **format adapter** with a non-static format method. +2. Provide a **format adapter** inheriting from ``llvm::FormatAdapter``. .. code-block:: c++ namespace anything { - struct format_int_custom { - int N; - explicit format_int_custom(int N) : N(N) {} - void format(llvm::raw_ostream &Stream, StringRef Style) { - // Do whatever is necessary to format ``N`` into ``Stream`` + struct format_int_custom : public llvm::FormatAdapter { + explicit format_int_custom(int N) : llvm::FormatAdapter(N) {} + void format(llvm::raw_ostream &Stream, StringRef Style) override { + // Do whatever is necessary to format ``this->Item`` into ``Stream`` } }; } @@ -350,9 +349,8 @@ } } - If the search for a specialization of ``format_provider`` for the given type - fails, ``formatv`` will subsequently check the argument for an instance method - named ``format`` with the signature described above. If so, it will call the + If the type is detected to be derived from ``FormatAdapter``, ``formatv`` + will call the ``format`` method on the argument passing in the specified style. This allows one to provide custom formatting of any type, including one which already has a builtin format provider. Index: include/llvm/Support/FormatAdapters.h =================================================================== --- include/llvm/Support/FormatAdapters.h +++ include/llvm/Support/FormatAdapters.h @@ -17,57 +17,58 @@ #include "llvm/Support/raw_ostream.h" namespace llvm { -template class AdapterBase { +template class FormatAdapter : public detail::format_adapter { protected: - explicit AdapterBase(T &&Item) : Item(Item) {} + explicit FormatAdapter(T &&Item) : Item(Item) {} T Item; + static_assert(!detail::uses_missing_provider::value, "Item does not have a format provider!"); }; namespace detail { -template class AlignAdapter : public AdapterBase { +template class AlignAdapter final : public FormatAdapter { AlignStyle Where; size_t Amount; public: AlignAdapter(T &&Item, AlignStyle Where, size_t Amount) - : AdapterBase(std::forward(Item)), Where(Where), Amount(Amount) {} + : FormatAdapter(std::forward(Item)), Where(Where), Amount(Amount) {} void format(llvm::raw_ostream &Stream, StringRef Style) { - auto Wrapper = detail::build_format_wrapper(std::forward(this->Item)); - FmtAlign(Wrapper, Where, Amount).format(Stream, Style); + auto Adapter = detail::build_format_adapter(std::forward(this->Item)); + FmtAlign(Adapter, Where, Amount).format(Stream, Style); } }; -template class PadAdapter : public AdapterBase { +template class PadAdapter final : public FormatAdapter { size_t Left; size_t Right; public: PadAdapter(T &&Item, size_t Left, size_t Right) - : AdapterBase(std::forward(Item)), Left(Left), Right(Right) {} + : FormatAdapter(std::forward(Item)), Left(Left), Right(Right) {} void format(llvm::raw_ostream &Stream, StringRef Style) { - auto Wrapper = detail::build_format_wrapper(std::forward(this->Item)); + auto Adapter = detail::build_format_adapter(std::forward(this->Item)); Stream.indent(Left); - Wrapper.format(Stream, Style); + Adapter.format(Stream, Style); Stream.indent(Right); } }; -template class RepeatAdapter : public AdapterBase { +template class RepeatAdapter final : public FormatAdapter { size_t Count; public: RepeatAdapter(T &&Item, size_t Count) - : AdapterBase(std::forward(Item)), Count(Count) {} + : FormatAdapter(std::forward(Item)), Count(Count) {} void format(llvm::raw_ostream &Stream, StringRef Style) { - auto Wrapper = detail::build_format_wrapper(std::forward(this->Item)); + auto Adapter = detail::build_format_adapter(std::forward(this->Item)); for (size_t I = 0; I < Count; ++I) { - Wrapper.format(Stream, Style); + Adapter.format(Stream, Style); } } }; @@ -89,4 +90,4 @@ } } -#endif \ No newline at end of file +#endif Index: include/llvm/Support/FormatCommon.h =================================================================== --- include/llvm/Support/FormatCommon.h +++ include/llvm/Support/FormatCommon.h @@ -18,12 +18,12 @@ enum class AlignStyle { Left, Center, Right }; struct FmtAlign { - detail::format_wrapper &Wrapper; + detail::format_adapter &Adapter; AlignStyle Where; size_t Amount; - FmtAlign(detail::format_wrapper &Wrapper, AlignStyle Where, size_t Amount) - : Wrapper(Wrapper), Where(Where), Amount(Amount) {} + FmtAlign(detail::format_adapter &Adapter, AlignStyle Where, size_t Amount) + : Adapter(Adapter), Where(Where), Amount(Amount) {} void format(raw_ostream &S, StringRef Options) { // If we don't need to align, we can format straight into the underlying @@ -32,13 +32,13 @@ // TODO: Make the format method return the number of bytes written, that // way we can also skip the intermediate stream for left-aligned output. if (Amount == 0) { - Wrapper.format(S, Options); + Adapter.format(S, Options); return; } SmallString<64> Item; raw_svector_ostream Stream(Item); - Wrapper.format(Stream, Options); + Adapter.format(Stream, Options); if (Amount <= Item.size()) { S << Item; return; @@ -66,4 +66,4 @@ }; } -#endif \ No newline at end of file +#endif Index: include/llvm/Support/FormatProviders.h =================================================================== --- include/llvm/Support/FormatProviders.h +++ include/llvm/Support/FormatProviders.h @@ -394,16 +394,16 @@ auto Begin = V.begin(); auto End = V.end(); if (Begin != End) { - auto Wrapper = - detail::build_format_wrapper(std::forward(*Begin)); - Wrapper.format(Stream, ArgStyle); + auto Adapter = + detail::build_format_adapter(std::forward(*Begin)); + Adapter.format(Stream, ArgStyle); ++Begin; } while (Begin != End) { Stream << Sep; - auto Wrapper = - detail::build_format_wrapper(std::forward(*Begin)); - Wrapper.format(Stream, ArgStyle); + auto Adapter = + detail::build_format_adapter(std::forward(*Begin)); + Adapter.format(Stream, ArgStyle); ++Begin; } } Index: include/llvm/Support/FormatVariadic.h =================================================================== --- include/llvm/Support/FormatVariadic.h +++ include/llvm/Support/FormatVariadic.h @@ -71,15 +71,15 @@ // parameters in a template class that derives from a non-template superclass. // Essentially, we are converting a std::tuple> to a // std::vector. - struct create_wrappers { + struct create_adapters { template - std::vector operator()(Ts &... Items) { - return std::vector{&Items...}; + std::vector operator()(Ts &... Items) { + return std::vector{&Items...}; } }; StringRef Fmt; - std::vector Wrappers; + std::vector Adapters; std::vector Replacements; static bool consumeFieldLayout(StringRef &Spec, AlignStyle &Where, @@ -91,7 +91,7 @@ public: formatv_object_base(StringRef Fmt, std::size_t ParamCount) : Fmt(Fmt), Replacements(parseFormatString(Fmt)) { - Wrappers.reserve(ParamCount); + Adapters.reserve(ParamCount); } void format(raw_ostream &S) const { @@ -102,12 +102,12 @@ S << R.Spec; continue; } - if (R.Index >= Wrappers.size()) { + if (R.Index >= Adapters.size()) { S << R.Spec; continue; } - auto W = Wrappers[R.Index]; + auto W = Adapters[R.Index]; FmtAlign Align(*W, R.Where, R.Align); Align.format(S, R.Options); @@ -138,7 +138,7 @@ }; template class formatv_object : public formatv_object_base { - // Storage for the parameter wrappers. Since the base class erases the type + // Storage for the parameter adapters. Since the base class erases the type // of the parameters, we have to own the storage for the parameters here, and // have the base class store type-erased pointers into this tuple. Tuple Parameters; @@ -147,7 +147,7 @@ formatv_object(StringRef Fmt, Tuple &&Params) : formatv_object_base(Fmt, std::tuple_size::value), Parameters(std::move(Params)) { - Wrappers = apply_tuple(create_wrappers(), Parameters); + Adapters = apply_tuple(create_adapters(), Parameters); } }; @@ -234,12 +234,12 @@ // template inline auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object(Vals))...))> { + std::make_tuple(detail::build_format_adapter(std::forward(Vals))...))> { using ParamTuple = decltype( - std::make_tuple(detail::build_format_wrapper(std::forward(Vals))...)); + std::make_tuple(detail::build_format_adapter(std::forward(Vals))...)); return formatv_object( Fmt, - std::make_tuple(detail::build_format_wrapper(std::forward(Vals))...)); + std::make_tuple(detail::build_format_adapter(std::forward(Vals))...)); } } // end namespace llvm Index: include/llvm/Support/FormatVariadicDetails.h =================================================================== --- include/llvm/Support/FormatVariadicDetails.h +++ include/llvm/Support/FormatVariadicDetails.h @@ -19,83 +19,26 @@ template struct format_provider {}; namespace detail { - -class format_wrapper { +class format_adapter { protected: - virtual ~format_wrapper() {} + virtual ~format_adapter() {} public: - virtual void format(llvm::raw_ostream &S, StringRef Options) = 0; + virtual void format(raw_ostream &S, StringRef Options) = 0; }; -template class member_format_wrapper : public format_wrapper { +template class provider_format_adapter : public format_adapter { T Item; public: - explicit member_format_wrapper(T &&Item) : Item(Item) {} - - void format(llvm::raw_ostream &S, StringRef Options) override { - Item.format(S, Options); - } -}; - -template class provider_format_wrapper : public format_wrapper { - T Item; - -public: - explicit provider_format_wrapper(T &&Item) : Item(Item) {} + explicit provider_format_adapter(T &&Item) : Item(Item) {} void format(llvm::raw_ostream &S, StringRef Options) override { format_provider::type>::format(Item, S, Options); } }; -template class missing_format_wrapper; - -// Test if T is a class that contains a member function with the signature: -// -// void format(raw_ostream &, StringRef); -// -// It is assumed T is a non-reference type. -template class has_FormatMember { -public: - static bool const value = false; -}; - -template -class has_FormatMember::value && - std::is_const::value>::type> { - using CleanT = typename std::remove_volatile::type; - using Signature_format = void (CleanT::*)(llvm::raw_ostream &S, - StringRef Options) const; - - template - static char test2(SameType *); - - template static double test2(...); - -public: - static bool const value = (sizeof(test2(nullptr)) == 1); -}; - -template -class has_FormatMember< - T, typename std::enable_if::value && - !std::is_const::value>::type> { - using CleanT = typename std::remove_cv::type; - using Signature_format = void (CleanT::*)(llvm::raw_ostream &S, - StringRef Options); - - template - static char test2(SameType *); - - template static double test2(...); - -public: - static bool const value = - (sizeof(test2(nullptr)) == 1) || has_FormatMember::value; -}; +template class missing_format_adapter; // Test if format_provider is defined on T and contains a member function // with the signature: @@ -122,7 +65,8 @@ struct uses_format_member : public std::integral_constant< bool, - has_FormatMember::type>::value> {}; + std::is_base_of::type>::value> {}; // Simple template that decides whether a type T should use the format_provider // based format() invocation. The member function takes priority, so this test @@ -144,24 +88,23 @@ !uses_format_provider::value> {}; template -typename std::enable_if::value, - member_format_wrapper>::type -build_format_wrapper(T &&Item) { - return member_format_wrapper(std::forward(Item)); +typename std::enable_if::value, T>::type +build_format_adapter(T &&Item) { + return std::forward(Item); } template typename std::enable_if::value, - provider_format_wrapper>::type -build_format_wrapper(T &&Item) { - return provider_format_wrapper(std::forward(Item)); + provider_format_adapter>::type +build_format_adapter(T &&Item) { + return provider_format_adapter(std::forward(Item)); } template typename std::enable_if::value, - missing_format_wrapper>::type -build_format_wrapper(T &&Item) { - return missing_format_wrapper(); + missing_format_adapter>::type +build_format_adapter(T &&Item) { + return missing_format_adapter(); } } } Index: unittests/Support/FormatVariadicTest.cpp =================================================================== --- unittests/Support/FormatVariadicTest.cpp +++ unittests/Support/FormatVariadicTest.cpp @@ -15,12 +15,9 @@ // Compile-time tests for the uses_format_member template namespace { -struct ConstFormat { - void format(raw_ostream &OS, StringRef Opt) const { OS << "ConstFormat"; } -}; - -struct Format { - void format(raw_ostream &OS, StringRef Opt) { OS << "Format"; } +struct Format : public FormatAdapter { + Format(int N) : FormatAdapter(N) {} + void format(raw_ostream &OS, StringRef Opt) override { OS << "Format"; } }; using detail::uses_format_member; @@ -28,18 +25,10 @@ static_assert(uses_format_member::value, ""); static_assert(uses_format_member::value, ""); static_assert(uses_format_member::value, ""); -static_assert(!uses_format_member::value, ""); -static_assert(!uses_format_member::value, ""); -static_assert(!uses_format_member::value, ""); -static_assert(!uses_format_member::value, ""); - -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); -static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); } TEST(FormatVariadicTest, EmptyFormatString) { @@ -535,12 +524,10 @@ } TEST(FormatVariadicTest, Adapter) { - class Negative { - int N; - + class Negative : public FormatAdapter { public: - explicit Negative(int N) : N(N) {} - void format(raw_ostream &S, StringRef Options) const { S << -N; } + explicit Negative(int N) : FormatAdapter(N) {} + void format(raw_ostream &S, StringRef Options) override { S << -Item; } }; EXPECT_EQ("-7", formatv("{0}", Negative(7)).str()); @@ -567,24 +554,13 @@ } TEST(FormatVariadicTest, FormatMember) { - EXPECT_EQ("Format", formatv("{0}", Format()).str()); + EXPECT_EQ("Format", formatv("{0}", Format(1)).str()); - Format var; + Format var(1); EXPECT_EQ("Format", formatv("{0}", var).str()); EXPECT_EQ("Format", formatv("{0}", std::move(var)).str()); // Not supposed to compile - // const Format cvar{}; + // const Format cvar(1); // EXPECT_EQ("Format", formatv("{0}", cvar).str()); } - -TEST(FormatVariadicTest, FormatMemberConst) { - EXPECT_EQ("ConstFormat", formatv("{0}", ConstFormat()).str()); - - ConstFormat var; - EXPECT_EQ("ConstFormat", formatv("{0}", var).str()); - EXPECT_EQ("ConstFormat", formatv("{0}", std::move(var)).str()); - - const ConstFormat cvar{}; - EXPECT_EQ("ConstFormat", formatv("{0}", cvar).str()); -}