Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions llvm/include/llvm/ADT/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -2151,15 +2151,15 @@ 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.
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>>
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/ADT/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_llvm_unittest(ADTTests
ImmutableMapTest.cpp
ImmutableSetTest.cpp
IntEqClassesTest.cpp
Interleave.cpp
IntervalMapTest.cpp
IntervalTreeTest.cpp
IntrusiveRefCntPtrTest.cpp
Expand Down
42 changes: 42 additions & 0 deletions llvm/unittests/ADT/Interleave.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surround TEST with namespace { ... }

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