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

This commit is contained in:
Jon Roelofs authored and GitHub committed 2024-04-05 08:43:37 -07:00
1 parent 51a4ab287f
commit e84a757222
3 files changed
+45 -2

No files matched your search

+2 -2
View File
@@ -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>>
+1
View File
@@ -44,6 +44,7 @@ add_llvm_unittest(ADTTests
ImmutableMapTest.cpp
ImmutableSetTest.cpp
IntEqClassesTest.cpp
Interleave.cpp
IntervalMapTest.cpp
IntervalTreeTest.cpp
IntrusiveRefCntPtrTest.cpp
+42
View File
@@ -0,0 +1,42 @@
//===- 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.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
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");
}
} // anonymous namespace