Skip to content

Commit e84a757

Browse files
authored
[llvm][ADT] Use ADL to find begin()/end() in interleave* (#87669)
1 parent 51a4ab2 commit e84a757

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,15 +2151,15 @@ template <typename Container, typename UnaryFunctor, typename NullaryFunctor,
21512151
!std::is_constructible<StringRef, NullaryFunctor>::value>>
21522152
inline void interleave(const Container &c, UnaryFunctor each_fn,
21532153
NullaryFunctor between_fn) {
2154-
interleave(c.begin(), c.end(), each_fn, between_fn);
2154+
interleave(adl_begin(c), adl_end(c), each_fn, between_fn);
21552155
}
21562156

21572157
/// Overload of interleave for the common case of string separator.
21582158
template <typename Container, typename UnaryFunctor, typename StreamT,
21592159
typename T = detail::ValueOfRange<Container>>
21602160
inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn,
21612161
const StringRef &separator) {
2162-
interleave(c.begin(), c.end(), each_fn, [&] { os << separator; });
2162+
interleave(adl_begin(c), adl_end(c), each_fn, [&] { os << separator; });
21632163
}
21642164
template <typename Container, typename StreamT,
21652165
typename T = detail::ValueOfRange<Container>>

llvm/unittests/ADT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_llvm_unittest(ADTTests
4444
ImmutableMapTest.cpp
4545
ImmutableSetTest.cpp
4646
IntEqClassesTest.cpp
47+
Interleave.cpp
4748
IntervalMapTest.cpp
4849
IntervalTreeTest.cpp
4950
IntrusiveRefCntPtrTest.cpp

llvm/unittests/ADT/Interleave.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- unittests/ADT/Interleave.cpp - Interleave unit tests ---------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ADT/STLExtras.h"
10+
#include "llvm/ADT/SmallVector.h"
11+
#include "llvm/Support/raw_ostream.h"
12+
13+
#include "gtest/gtest.h"
14+
15+
using namespace llvm;
16+
17+
namespace {
18+
19+
TEST(InterleaveTest, Interleave) {
20+
std::string Str;
21+
raw_string_ostream OS(Str);
22+
23+
// Check that interleave works on a SmallVector.
24+
SmallVector<const char *> Doodles = {"golden", "berna", "labra"};
25+
interleave(
26+
Doodles, OS, [&](const char *Name) { OS << Name << "doodle"; }, ", ");
27+
28+
EXPECT_EQ(OS.str(), "goldendoodle, bernadoodle, labradoodle");
29+
}
30+
31+
TEST(InterleaveTest, InterleaveComma) {
32+
std::string Str;
33+
raw_string_ostream OS(Str);
34+
35+
// Check that interleaveComma uses ADL to find begin/end on an array.
36+
const StringRef LongDogs[] = {"dachshund", "doxie", "dackel", "teckel"};
37+
interleaveComma(LongDogs, OS);
38+
39+
EXPECT_EQ(OS.str(), "dachshund, doxie, dackel, teckel");
40+
}
41+
42+
} // anonymous namespace

0 commit comments

Comments
 (0)