[PATCH] D94769: [Support] Format provider improvements

Vladislav Vinogradov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 22 02:10:38 PST 2021


vinograd47 updated this revision to Diff 318468.
vinograd47 added a comment.

One more comment.

Even while `has_StreamOperator` checks only for const correct stream operator,
the `formatv` implementation still might call non-const version if presented
(due to perfect forwarding).

So, IMHO, either `formatv` should be stricter or `has_StreamOperator` should be
relaxed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94769/new/

https://reviews.llvm.org/D94769

Files:
  llvm/include/llvm/Support/FormatProviders.h
  llvm/include/llvm/Support/FormatVariadicDetails.h
  llvm/unittests/Support/FormatVariadicTest.cpp


Index: llvm/unittests/Support/FormatVariadicTest.cpp
===================================================================
--- llvm/unittests/Support/FormatVariadicTest.cpp
+++ llvm/unittests/Support/FormatVariadicTest.cpp
@@ -698,3 +698,25 @@
   EXPECT_EQ("X", formatv("{0}", fmt_consume(std::move(E1))).str());
   EXPECT_FALSE(E1.isA<StringError>()); // consumed
 }
+
+namespace {
+
+class PassByRef {
+public:
+  int Val = 0;
+  PassByRef(int Val) : Val(Val) {}
+};
+
+raw_ostream &operator<<(raw_ostream &OS, PassByRef &Obj) {
+  return OS << Obj.Val;
+}
+
+static_assert(detail::has_StreamOperator<PassByRef &>::value, "");
+static_assert(detail::uses_stream_operator<PassByRef &>::value, "");
+
+TEST(FormatVariadicTest, StreamOperatorPassByRef) {
+  PassByRef Obj(10);
+  EXPECT_EQ("10", formatv("{0}", Obj).str());
+}
+
+} // namespace
Index: llvm/include/llvm/Support/FormatVariadicDetails.h
===================================================================
--- llvm/include/llvm/Support/FormatVariadicDetails.h
+++ llvm/include/llvm/Support/FormatVariadicDetails.h
@@ -75,8 +75,6 @@
 // Test if raw_ostream& << T -> raw_ostream& is findable via ADL.
 template <class T> class has_StreamOperator {
 public:
-  using ConstRefT = const std::decay_t<T> &;
-
   template <typename U>
   static char test(
       std::enable_if_t<std::is_same<decltype(std::declval<llvm::raw_ostream &>()
@@ -86,7 +84,8 @@
 
   template <typename U> static double test(...);
 
-  static bool const value = (sizeof(test<ConstRefT>(nullptr)) == 1);
+  // NOLINTNEXTLINE(readability-identifier-naming)
+  static bool const value = (sizeof(test<T>(nullptr)) == 1);
 };
 
 // Simple template that decides whether a type T should use the member-function
Index: llvm/include/llvm/Support/FormatProviders.h
===================================================================
--- llvm/include/llvm/Support/FormatProviders.h
+++ llvm/include/llvm/Support/FormatProviders.h
@@ -355,7 +355,6 @@
 
 template <typename IterT> class format_provider<llvm::iterator_range<IterT>> {
   using value = typename std::iterator_traits<IterT>::value_type;
-  using reference = typename std::iterator_traits<IterT>::reference;
 
   static StringRef consumeOneOption(StringRef &Style, char Indicator,
                                     StringRef Default) {
@@ -403,15 +402,13 @@
     auto Begin = V.begin();
     auto End = V.end();
     if (Begin != End) {
-      auto Adapter =
-          detail::build_format_adapter(std::forward<reference>(*Begin));
+      auto Adapter = detail::build_format_adapter(*Begin);
       Adapter.format(Stream, ArgStyle);
       ++Begin;
     }
     while (Begin != End) {
       Stream << Sep;
-      auto Adapter =
-          detail::build_format_adapter(std::forward<reference>(*Begin));
+      auto Adapter = detail::build_format_adapter(*Begin);
       Adapter.format(Stream, ArgStyle);
       ++Begin;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94769.318468.patch
Type: text/x-patch
Size: 2911 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210122/8cead4c0/attachment.bin>


More information about the llvm-commits mailing list