Index: llvm/trunk/include/llvm/Support/FormatVariadicDetails.h =================================================================== --- llvm/trunk/include/llvm/Support/FormatVariadicDetails.h +++ llvm/trunk/include/llvm/Support/FormatVariadicDetails.h @@ -56,6 +56,7 @@ // // void format(raw_ostream &, StringRef); // +// It is assumed T is a non-reference type. template class has_FormatMember { public: static bool const value = false; @@ -63,8 +64,11 @@ template class has_FormatMember::value>::type> { - using Signature_format = void (T::*)(llvm::raw_ostream &S, StringRef Options); + typename std::enable_if::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 *); @@ -72,7 +76,25 @@ template static double test2(...); public: - static bool const value = (sizeof(test2(nullptr)) == 1); + 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; }; // Test if format_provider is defined on T and contains a member function @@ -98,15 +120,18 @@ // based format() invocation. template struct uses_format_member - : public std::integral_constant::value> {}; + : public std::integral_constant< + bool, + has_FormatMember::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 // will only be true if there is not ALSO a format member. template struct uses_format_provider - : public std::integral_constant::value && - has_FormatProvider::value> {}; + : public std::integral_constant< + bool, !uses_format_member::value && has_FormatProvider::value> { +}; // Simple template that decides whether a type T has neither a member-function // nor format_provider based implementation that it can use. Mostly used so @@ -114,8 +139,9 @@ // implementation can be located. template struct uses_missing_provider - : public std::integral_constant::value && - !has_FormatProvider::value> {}; + : public std::integral_constant::value && + !uses_format_provider::value> {}; template typename std::enable_if::value, Index: llvm/trunk/unittests/Support/FormatVariadicTest.cpp =================================================================== --- llvm/trunk/unittests/Support/FormatVariadicTest.cpp +++ llvm/trunk/unittests/Support/FormatVariadicTest.cpp @@ -13,6 +13,35 @@ using namespace llvm; +// 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"; } +}; + +using detail::uses_format_member; + +static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); +static_assert(uses_format_member::value, ""); +static_assert(not uses_format_member::value, ""); +static_assert(not uses_format_member::value, ""); +static_assert(not uses_format_member::value, ""); +static_assert(not 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) { auto Replacements = formatv_object_base::parseFormatString(""); EXPECT_EQ(0U, Replacements.size()); @@ -511,7 +540,7 @@ public: explicit Negative(int N) : N(N) {} - void format(raw_ostream &S, StringRef Options) { S << -N; } + void format(raw_ostream &S, StringRef Options) const { S << -N; } }; EXPECT_EQ("-7", formatv("{0}", Negative(7)).str()); @@ -535,4 +564,27 @@ SmallString<4> S2 = formatv("{0} {1}", 1, 2); EXPECT_EQ("1 2", S2); -} \ No newline at end of file +} + +TEST(FormatVariadicTest, FormatMember) { + EXPECT_EQ("Format", formatv("{0}", Format()).str()); + + Format var; + EXPECT_EQ("Format", formatv("{0}", var).str()); + EXPECT_EQ("Format", formatv("{0}", std::move(var)).str()); + + // Not supposed to compile + // const Format cvar{}; + // 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()); +}