[llvm] 3dbda5f - [Support] Format provider improvements

Vladislav Vinogradov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 9 05:55:13 PST 2022


Author: Vladislav Vinogradov
Date: 2022-11-09T16:55:04+03:00
New Revision: 3dbda5ff88518912bbb72f03d95805634507ac17

URL: https://github.com/llvm/llvm-project/commit/3dbda5ff88518912bbb72f03d95805634507ac17
DIFF: https://github.com/llvm/llvm-project/commit/3dbda5ff88518912bbb72f03d95805634507ac17.diff

LOG: [Support] Format provider improvements

Remove `std::forward` call for `iterator_range` iterator de-reference.
It fixes formatting usage for some tricky cases, like special ranges,
which de-reference to value type.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D94769

Added: 
    

Modified: 
    llvm/include/llvm/Support/FormatProviders.h
    llvm/unittests/Support/FormatVariadicTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h
index 2bdc316a68a2..c25453e9e881 100644
--- a/llvm/include/llvm/Support/FormatProviders.h
+++ b/llvm/include/llvm/Support/FormatProviders.h
@@ -355,7 +355,6 @@ struct range_item_has_provider
 
 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 @@ template <typename IterT> class format_provider<llvm::iterator_range<IterT>> {
     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;
     }

diff  --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index 40c94b7fdbff..44e4477eb0b0 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -697,3 +697,31 @@ TEST(FormatVariadicTest, FormatError) {
   EXPECT_EQ("X", formatv("{0}", fmt_consume(std::move(E1))).str());
   EXPECT_FALSE(E1.isA<StringError>()); // consumed
 }
+
+TEST(FormatVariadicTest, FormatFilterRange) {
+  std::vector<int> Vec{0, 1, 2};
+  auto Range = map_range(Vec, [](int V) { return V + 1; });
+  EXPECT_EQ("1, 2, 3", formatv("{0}", Range).str());
+}
+
+namespace {
+
+class IntegerValuesRange final
+    : public indexed_accessor_range<IntegerValuesRange, NoneType, int, int *,
+                                    int> {
+public:
+  using indexed_accessor_range<IntegerValuesRange, NoneType, int, int *,
+                               int>::indexed_accessor_range;
+
+  static int dereference(const NoneType &, ptr
diff _t Index) {
+    return static_cast<int>(Index);
+  }
+};
+
+TEST(FormatVariadicTest, FormatRangeNonRef) {
+  IntegerValuesRange Range(None, 0, 3);
+  EXPECT_EQ("0, 1, 2",
+            formatv("{0}", make_range(Range.begin(), Range.end())).str());
+}
+
+} // namespace


        


More information about the llvm-commits mailing list