Skip to content

Commit acfeddb

Browse files
committed
Pull interleaved range out of Format.h
1 parent 7a12756 commit acfeddb

File tree

5 files changed

+106
-100
lines changed

5 files changed

+106
-100
lines changed

llvm/include/llvm/Support/Format.h

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -256,90 +256,6 @@ format_bytes_with_ascii(ArrayRef<uint8_t> Bytes,
256256
ByteGroupSize, Upper, true);
257257
}
258258

259-
/// The base class for printing interleaved ranges. Provides a virtual function
260-
/// to be used by the '<<' operator in raw_ostream. This class is not intended
261-
/// to be copied or used in polymorphic containers.
262-
class InterleavedRangeBase {
263-
protected:
264-
~InterleavedRangeBase() = default; // Disallow polymorphic deletion.
265-
InterleavedRangeBase() = default;
266-
InterleavedRangeBase(const InterleavedRangeBase &) = delete; // Disallow copy.
267-
virtual void home(); // Out of line virtual method to anchor the vtable.
268-
269-
/// Appends the interleaved range to the output stream `OS`.
270-
virtual void printInterleaved(raw_ostream &OS) const = 0;
271-
272-
friend class raw_ostream;
273-
};
274-
275-
/// The concrete class template for interleaved ranges. Supports specifying the
276-
/// separator and, optionally, the prefix and suffix to be printed surrounding
277-
/// the range.
278-
/// Uses the operator '<<' of the range element type for printing. The range
279-
/// type itself does not have to have an '<<' operator defined.
280-
template <typename Range>
281-
class InterleavedRange final : public InterleavedRangeBase {
282-
const Range &TheRange;
283-
StringRef Separator;
284-
StringRef Prefix;
285-
StringRef Suffix;
286-
287-
virtual void printInterleaved(raw_ostream &OS) const override {
288-
if (!Prefix.empty())
289-
OS << Prefix;
290-
llvm::interleave(TheRange, OS, Separator);
291-
if (!Suffix.empty())
292-
OS << Suffix;
293-
}
294-
295-
public:
296-
InterleavedRange(const Range &R, StringRef Separator, StringRef Prefix,
297-
StringRef Suffix)
298-
: TheRange(R), Separator(Separator), Prefix(Prefix), Suffix(Suffix) {}
299-
300-
std::string str() const {
301-
std::string Result;
302-
raw_string_ostream Stream(Result);
303-
Stream << *this;
304-
Stream.flush();
305-
return Result;
306-
}
307-
308-
operator std::string() const { return str(); }
309-
};
310-
311-
/// interleaved - Output range \p R as a sequence of interleaved elements.
312-
/// Requires the range element type to be printable using
313-
/// `raw_ostream& operator<<`. The \p Separator and \p Prefix / \p Suffix can be
314-
/// customized. Examples:
315-
/// ```c++
316-
/// SmallVector<int> Vals = {1, 2, 3};
317-
/// OS << interleaved(Vals); // ==> "1, 2, 3"
318-
/// OS << interleaved(Vals, ";"); // ==> "1;2;3"
319-
/// OS << interleaved(Vals, " ", "{", "}"); // ==> "{1 2 3}"
320-
/// ```
321-
template <typename Range>
322-
InterleavedRange<Range> interleaved(const Range &R, StringRef Separator = ", ",
323-
StringRef Prefix = "",
324-
StringRef Suffix = "") {
325-
return {R, Separator, Prefix, Suffix};
326-
}
327-
328-
/// interleaved - Output range \p R as an array of interleaved elements.
329-
/// Requires the range element type to be printable using
330-
/// `raw_ostream& operator<<`. The \p Separator can be customized. Examples:
331-
/// ```c++
332-
/// SmallVector<int> Vals = {1, 2, 3};
333-
/// OS << interleaved_array(Vals); // ==> "[1, 2, 3]"
334-
/// OS << interleaved_array(Vals, ";"); // ==> "[1;2;3]"
335-
/// OS << interleaved_array(Vals, " "); // ==> "[1 2 3]"
336-
/// ```
337-
template <typename Range>
338-
InterleavedRange<Range> interleaved_array(const Range &R,
339-
StringRef Separator = ", ") {
340-
return {R, Separator, "[", "]"};
341-
}
342-
343259
} // end namespace llvm
344260

345261
#endif
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//===- InterleavedRange.h - Output stream formatting for ranges -----------===//
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+
// Implements format objects for printing ranges to output streams.
10+
// For example:
11+
// ```c++
12+
// ArrayRef<Type> Types = ...;
13+
// OS << "Types: " << interleaved(Types); // ==> "Types: i32, f16, i8"
14+
// ArrayRef<int> Values = ...;
15+
// OS << "Values: " << interleaved_array(Values); // ==> "Values: [1, 2, 3]"
16+
// ```
17+
//
18+
//===----------------------------------------------------------------------===//
19+
20+
#ifndef LLVM_SUPPORT_INTERLEAVED_RANGE_H
21+
#define LLVM_SUPPORT_INTERLEAVED_RANGE_H
22+
23+
#include "llvm/ADT/STLExtras.h"
24+
#include "llvm/ADT/StringRef.h"
25+
#include "llvm/Support/raw_ostream.h"
26+
#include <cassert>
27+
#include <cstdio>
28+
29+
namespace llvm {
30+
31+
/// Format object class for interleaved ranges. Supports specifying the
32+
/// separator and, optionally, the prefix and suffix to be printed surrounding
33+
/// the range.
34+
/// Uses the operator '<<' of the range element type for printing. The range
35+
/// type itself does not have to have an '<<' operator defined.
36+
template <typename Range> class InterleavedRange {
37+
const Range &TheRange;
38+
StringRef Separator;
39+
StringRef Prefix;
40+
StringRef Suffix;
41+
42+
public:
43+
InterleavedRange(const Range &R, StringRef Separator, StringRef Prefix,
44+
StringRef Suffix)
45+
: TheRange(R), Separator(Separator), Prefix(Prefix), Suffix(Suffix) {}
46+
47+
friend raw_ostream &operator<<(raw_ostream &OS,
48+
const InterleavedRange &Interleaved) {
49+
if (!Interleaved.Prefix.empty())
50+
OS << Interleaved.Prefix;
51+
llvm::interleave(Interleaved.TheRange, OS, Interleaved.Separator);
52+
if (!Interleaved.Suffix.empty())
53+
OS << Interleaved.Suffix;
54+
return OS;
55+
}
56+
57+
std::string str() const {
58+
std::string Result;
59+
raw_string_ostream Stream(Result);
60+
Stream << *this;
61+
Stream.flush();
62+
return Result;
63+
}
64+
65+
operator std::string() const { return str(); }
66+
};
67+
68+
/// interleaved - Output range `R` as a sequence of interleaved elements.
69+
/// Requires the range element type to be printable using
70+
/// `raw_ostream& operator<<`. The `Separator` and `Prefix` / `Suffix` can be
71+
/// customized. Examples:
72+
/// ```c++
73+
/// SmallVector<int> Vals = {1, 2, 3};
74+
/// OS << interleaved(Vals); // ==> "1, 2, 3"
75+
/// OS << interleaved(Vals, ";"); // ==> "1;2;3"
76+
/// OS << interleaved(Vals, " ", "{", "}"); // ==> "{1 2 3}"
77+
/// ```
78+
template <typename Range>
79+
InterleavedRange<Range> interleaved(const Range &R, StringRef Separator = ", ",
80+
StringRef Prefix = "",
81+
StringRef Suffix = "") {
82+
return {R, Separator, Prefix, Suffix};
83+
}
84+
85+
/// interleaved - Output range `R` as an array of interleaved elements.
86+
/// Requires the range element type to be printable using
87+
/// `raw_ostream& operator<<`. The `Separator` can be customized. Examples:
88+
/// ```c++
89+
/// SmallVector<int> Vals = {1, 2, 3};
90+
/// OS << interleaved_array(Vals); // ==> "[1, 2, 3]"
91+
/// OS << interleaved_array(Vals, ";"); // ==> "[1;2;3]"
92+
/// OS << interleaved_array(Vals, " "); // ==> "[1 2 3]"
93+
/// ```
94+
template <typename Range>
95+
InterleavedRange<Range> interleaved_array(const Range &R,
96+
StringRef Separator = ", ") {
97+
return {R, Separator, "[", "]"};
98+
}
99+
100+
} // end namespace llvm
101+
102+
#endif // LLVM_SUPPORT_INTERLEAVED_RANGE_H

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
namespace llvm {
3030

3131
class Duration;
32-
template <class T> class [[nodiscard]] Expected;
3332
class formatv_object_base;
3433
class format_object_base;
3534
class FormattedString;
3635
class FormattedNumber;
3736
class FormattedBytes;
38-
class InterleavedRangeBase;
37+
template <class T> class [[nodiscard]] Expected;
3938

4039
namespace sys {
4140
namespace fs {
@@ -319,9 +318,6 @@ class raw_ostream {
319318
// Formatted output, see the format_bytes() function in Support/Format.h.
320319
raw_ostream &operator<<(const FormattedBytes &);
321320

322-
// Formatted output, see the interleaved() function in Support/Format.h.
323-
raw_ostream &operator<<(const InterleavedRangeBase &);
324-
325321
/// indent - Insert 'NumSpaces' spaces.
326322
raw_ostream &indent(unsigned NumSpaces);
327323

llvm/lib/Support/raw_ostream.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,6 @@ raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
471471
return *this;
472472
}
473473

474-
raw_ostream &
475-
raw_ostream::operator<<(const InterleavedRangeBase &InterleavedRange) {
476-
InterleavedRange.printInterleaved(*this);
477-
return *this;
478-
}
479-
480474
template <char C>
481475
static raw_ostream &write_padding(raw_ostream &OS, unsigned NumChars) {
482476
static const char Chars[] = {C, C, C, C, C, C, C, C, C, C, C, C, C, C, C, C,
@@ -561,10 +555,8 @@ void raw_ostream::anchor() {}
561555
//===----------------------------------------------------------------------===//
562556

563557
// Out of line virtual method.
564-
void format_object_base::home() {}
565-
566-
// Out of line virtual method.
567-
void InterleavedRangeBase::home() {}
558+
void format_object_base::home() {
559+
}
568560

569561
//===----------------------------------------------------------------------===//
570562
// raw_fd_ostream

llvm/unittests/Support/InterleavedRangeTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/ADT/SmallVector.h"
10-
#include "llvm/Support/Format.h"
10+
#include "llvm/Support/InterleavedRange.h"
1111
#include "llvm/Support/raw_ostream.h"
1212
#include "gmock/gmock.h"
1313
#include "gtest/gtest.h"

0 commit comments

Comments
 (0)