[llvm] [llvm][ADT] Use ADL to find begin()/end() in interleave* (PR #87669)

Jon Roelofs via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 4 16:31:05 PDT 2024


https://github.com/jroelofs updated https://github.com/llvm/llvm-project/pull/87669

>From 5cdb3e5737eaa52d7511f6cf34310e743b5bc4fa Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:09:59 -0700
Subject: [PATCH 1/8] [llvm][ADT] Use ADL to find begin()/end() in interleave*

---
 llvm/include/llvm/ADT/STLExtras.h |  4 ++--
 llvm/unittests/ADT/CMakeLists.txt |  1 +
 llvm/unittests/ADT/Interleave.cpp | 36 +++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 llvm/unittests/ADT/Interleave.cpp

diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 02a3074ae1f0d7..b5eb319ccf346e 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2151,7 +2151,7 @@ template <typename Container, typename UnaryFunctor, typename NullaryFunctor,
               !std::is_constructible<StringRef, NullaryFunctor>::value>>
 inline void interleave(const Container &c, UnaryFunctor each_fn,
                        NullaryFunctor between_fn) {
-  interleave(c.begin(), c.end(), each_fn, between_fn);
+  interleave(adl_begin(c), adl_end(c), each_fn, between_fn);
 }
 
 /// Overload of interleave for the common case of string separator.
@@ -2159,7 +2159,7 @@ template <typename Container, typename UnaryFunctor, typename StreamT,
           typename T = detail::ValueOfRange<Container>>
 inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn,
                        const StringRef &separator) {
-  interleave(c.begin(), c.end(), each_fn, [&] { os << separator; });
+  interleave(adl_begin(c), adl_end(c), each_fn, [&] { os << separator; });
 }
 template <typename Container, typename StreamT,
           typename T = detail::ValueOfRange<Container>>
diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt
index 12d7325036bf05..17c5c9d1c59cec 100644
--- a/llvm/unittests/ADT/CMakeLists.txt
+++ b/llvm/unittests/ADT/CMakeLists.txt
@@ -44,6 +44,7 @@ add_llvm_unittest(ADTTests
   ImmutableMapTest.cpp
   ImmutableSetTest.cpp
   IntEqClassesTest.cpp
+  Interleave.cpp
   IntervalMapTest.cpp
   IntervalTreeTest.cpp
   IntrusiveRefCntPtrTest.cpp
diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
new file mode 100644
index 00000000000000..75dac34bee5833
--- /dev/null
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -0,0 +1,36 @@
+//===- unittests/ADT/IListTest.cpp - ilist unit tests ---------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/raw_ostream.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+TEST(InterleaveTest, Interleave) {
+    std::string Str;
+    raw_string_ostream OS(Str);
+
+    // Check that interleave works on a SmallVector.
+    SmallVector<const char *> Doodles = { "golden", "berna", "labra" };
+    interleave(Doodles, OS, [&](const char *Name) { OS << Name << "doodle"; }, ", ");
+
+    EXPECT_EQ(OS.str(), "goldendoodle, bernadoodle, labradoodle");
+}
+
+TEST(InterleaveTest, InterleaveComma) {
+    std::string Str;
+    raw_string_ostream OS(Str);
+
+    // Check that interleaveComma uses ADL to find begin/end on an array.
+    const StringRef LongDogs[] = { "dachshund", "doxie", "dackel", "teckel" };
+    interleaveComma(LongDogs, OS);
+
+    EXPECT_EQ(OS.str(), "dachshund, doxie, dackel, teckel");
+}

>From b784176cb36e1c1847c1e13439b38ccc5473252c Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:11:47 -0700
Subject: [PATCH 2/8] clang-format

---
 llvm/unittests/ADT/Interleave.cpp | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index 75dac34bee5833..a5ec19d35e0526 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -14,23 +14,24 @@
 using namespace llvm;
 
 TEST(InterleaveTest, Interleave) {
-    std::string Str;
-    raw_string_ostream OS(Str);
+  std::string Str;
+  raw_string_ostream OS(Str);
 
-    // Check that interleave works on a SmallVector.
-    SmallVector<const char *> Doodles = { "golden", "berna", "labra" };
-    interleave(Doodles, OS, [&](const char *Name) { OS << Name << "doodle"; }, ", ");
+  // Check that interleave works on a SmallVector.
+  SmallVector<const char *> Doodles = {"golden", "berna", "labra"};
+  interleave(
+      Doodles, OS, [&](const char *Name) { OS << Name << "doodle"; }, ", ");
 
-    EXPECT_EQ(OS.str(), "goldendoodle, bernadoodle, labradoodle");
+  EXPECT_EQ(OS.str(), "goldendoodle, bernadoodle, labradoodle");
 }
 
 TEST(InterleaveTest, InterleaveComma) {
-    std::string Str;
-    raw_string_ostream OS(Str);
+  std::string Str;
+  raw_string_ostream OS(Str);
 
-    // Check that interleaveComma uses ADL to find begin/end on an array.
-    const StringRef LongDogs[] = { "dachshund", "doxie", "dackel", "teckel" };
-    interleaveComma(LongDogs, OS);
+  // Check that interleaveComma uses ADL to find begin/end on an array.
+  const StringRef LongDogs[] = {"dachshund", "doxie", "dackel", "teckel"};
+  interleaveComma(LongDogs, OS);
 
-    EXPECT_EQ(OS.str(), "dachshund, doxie, dackel, teckel");
+  EXPECT_EQ(OS.str(), "dachshund, doxie, dackel, teckel");
 }

>From 45a00778c255f198ff8e58d243cb39a5f2b2995a Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:16:32 -0700
Subject: [PATCH 3/8] fix a copy-pasta

---
 llvm/unittests/ADT/Interleave.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index a5ec19d35e0526..5986d364fa83e5 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -1,4 +1,4 @@
-//===- unittests/ADT/IListTest.cpp - ilist unit tests ---------------------===//
+//===- unittests/ADT/IListTest.cpp - interleave unit tests ----------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From c6eb79014d5547bbfbda71d067361b97303ec350 Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:37:21 -0700
Subject: [PATCH 4/8] fix another copy-pasta

---
 llvm/unittests/ADT/Interleave.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index 5986d364fa83e5..883ec5edc3bf84 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -1,4 +1,4 @@
-//===- unittests/ADT/IListTest.cpp - interleave unit tests ----------------===//
+//===- unittests/ADT/Interleave.cpp - interleave unit tests ----------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From dab49b343f28d1a189aa2393c69d03f1b8711795 Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:37:34 -0700
Subject: [PATCH 5/8] include smallvector.h

---
 llvm/unittests/ADT/Interleave.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index 883ec5edc3bf84..c7679d7bce41f0 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/raw_ostream.h"
 

>From 57af3d2e1da231f632f29e6a1d99b864d981cde8 Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:38:20 -0700
Subject: [PATCH 6/8] capitalization

---
 llvm/unittests/ADT/Interleave.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index c7679d7bce41f0..64c265926963f7 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -1,4 +1,4 @@
-//===- unittests/ADT/Interleave.cpp - interleave unit tests ----------------===//
+//===- unittests/ADT/Interleave.cpp - Interleave unit tests ----------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From c79706dbc57982d7675714ce5b2043854e7c8d1e Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 11:42:12 -0700
Subject: [PATCH 7/8] appease clang-format

---
 llvm/unittests/ADT/Interleave.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index 64c265926963f7..07fa66dba949fa 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -1,4 +1,4 @@
-//===- unittests/ADT/Interleave.cpp - Interleave unit tests ----------------===//
+//===- unittests/ADT/Interleave.cpp - Interleave unit tests ---------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/raw_ostream.h"
 
 #include "gtest/gtest.h"

>From 791a0d3edbfaf6d6d49aa1e742d6750bf31c0daf Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Thu, 4 Apr 2024 16:30:49 -0700
Subject: [PATCH 8/8] anonymous namespace

---
 llvm/unittests/ADT/Interleave.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/unittests/ADT/Interleave.cpp b/llvm/unittests/ADT/Interleave.cpp
index 07fa66dba949fa..bc1ab1fae725e0 100644
--- a/llvm/unittests/ADT/Interleave.cpp
+++ b/llvm/unittests/ADT/Interleave.cpp
@@ -14,6 +14,8 @@
 
 using namespace llvm;
 
+namespace {
+
 TEST(InterleaveTest, Interleave) {
   std::string Str;
   raw_string_ostream OS(Str);
@@ -36,3 +38,5 @@ TEST(InterleaveTest, InterleaveComma) {
 
   EXPECT_EQ(OS.str(), "dachshund, doxie, dackel, teckel");
 }
+
+} // anonymous namespace



More information about the llvm-commits mailing list