Skip to content

Commit 2731be7

Browse files
authored
[Support] Add helper struct indent for adding indentation (#108966)
Add helper struct indent() for adding indentation to raw_ostream.
1 parent eda72fa commit 2731be7

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,25 @@ class buffer_unique_ostream : public raw_svector_ostream {
769769
~buffer_unique_ostream() override { *OS << str(); }
770770
};
771771

772+
// Helper struct to add indentation to raw_ostream. Instead of
773+
// OS.indent(6) << "more stuff";
774+
// you can use
775+
// OS << indent(6) << "more stuff";
776+
// which has better ergonomics (and clang-formats better as well).
777+
struct indent {
778+
unsigned NumSpaces;
779+
780+
explicit indent(unsigned NumSpaces) : NumSpaces(NumSpaces) {}
781+
void operator+=(unsigned N) { NumSpaces += N; }
782+
void operator-=(unsigned N) { NumSpaces -= N; }
783+
indent operator+(unsigned N) const { return indent(NumSpaces + N); }
784+
indent operator-(unsigned N) const { return indent(NumSpaces - N); }
785+
};
786+
787+
inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
788+
return OS.indent(Indent.NumSpaces);
789+
}
790+
772791
class Error;
773792

774793
/// This helper creates an output stream and then passes it to \p Write.

llvm/unittests/Support/raw_ostream_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ TEST(raw_ostreamTest, Justify) {
177177
EXPECT_EQ("none", printToString(center_justify("none", 1), 1));
178178
}
179179

180+
TEST(raw_ostreamTest, Indent) {
181+
indent Indent(4);
182+
auto Spaces = [](int N) { return std::string(N, ' '); };
183+
EXPECT_EQ(Spaces(4), printToString(Indent));
184+
EXPECT_EQ("", printToString(indent(0)));
185+
EXPECT_EQ(Spaces(5), printToString(Indent + 1));
186+
EXPECT_EQ(Spaces(3), printToString(Indent - 1));
187+
Indent += 1;
188+
EXPECT_EQ(Spaces(5), printToString(Indent));
189+
Indent -= 1;
190+
EXPECT_EQ(Spaces(4), printToString(Indent));
191+
}
192+
180193
TEST(raw_ostreamTest, FormatHex) {
181194
EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 6), 6));
182195
EXPECT_EQ("0x001234", printToString(format_hex(0x1234, 8), 8));

llvm/utils/yaml-bench/YAMLBench.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ cl::opt<cl::boolOrDefault>
5656
UseColor("use-color", cl::desc("Emit colored output (default=autodetect)"),
5757
cl::init(cl::BOU_UNSET));
5858

59-
struct indent {
60-
unsigned distance;
61-
indent(unsigned d) : distance(d) {}
62-
};
63-
64-
static raw_ostream &operator <<(raw_ostream &os, const indent &in) {
65-
for (unsigned i = 0; i < in.distance; ++i)
66-
os << " ";
67-
return os;
68-
}
69-
7059
/// Pretty print a tag by replacing tag:yaml.org,2002: with !!.
7160
static std::string prettyTag(yaml::Node *N) {
7261
std::string Tag = N->getVerbatimTag();

0 commit comments

Comments
 (0)