Skip to content

[Support] Add helper struct indent for adding indentation #108966

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 1 commit into from
Sep 18, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 17, 2024

Add helper struct indent() for adding indentation to raw_ostream.

@jurahul jurahul force-pushed the add_raw_ostream_indent branch 2 times, most recently from 9173e23 to 415923c Compare September 17, 2024 16:24
Add helper struct indent() for adding indentation to raw_ostream.
@jurahul jurahul force-pushed the add_raw_ostream_indent branch from 415923c to 9a3cacb Compare September 17, 2024 16:30
@jurahul
Copy link
Contributor Author

jurahul commented Sep 17, 2024

See e51d14b for a change to adopt it (which I will put out for review as a follow on).

@jurahul jurahul marked this pull request as ready for review September 17, 2024 20:10
@llvmbot
Copy link
Member

llvmbot commented Sep 17, 2024

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

Changes

Add helper struct indent() for adding indentation to raw_ostream.


Full diff: https://github.com/llvm/llvm-project/pull/108966.diff

3 Files Affected:

  • (modified) llvm/include/llvm/Support/raw_ostream.h (+19)
  • (modified) llvm/unittests/Support/raw_ostream_test.cpp (+13)
  • (modified) llvm/utils/yaml-bench/YAMLBench.cpp (-11)
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 2570c826502e7c..34f91cbe9551f4 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -769,6 +769,25 @@ class buffer_unique_ostream : public raw_svector_ostream {
   ~buffer_unique_ostream() override { *OS << str(); }
 };
 
+// Helper struct to add indentation to raw_ostream. Instead of
+// OS.indent(6) << "more stuff";
+// you can use
+// OS << indent(6) << "more stuff";
+// which has better ergonomics (and clang-formats better as well).
+struct indent {
+  unsigned NumSpaces;
+
+  explicit indent(unsigned NumSpaces) : NumSpaces(NumSpaces) {}
+  void operator+=(unsigned N) { NumSpaces += N; }
+  void operator-=(unsigned N) { NumSpaces -= N; }
+  indent operator+(unsigned N) const { return indent(NumSpaces + N); }
+  indent operator-(unsigned N) const { return indent(NumSpaces - N); }
+};
+
+inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
+  return OS.indent(Indent.NumSpaces);
+}
+
 class Error;
 
 /// This helper creates an output stream and then passes it to \p Write.
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 1c6dfb6260cc04..99aa350adad71d 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -177,6 +177,19 @@ TEST(raw_ostreamTest, Justify) {
   EXPECT_EQ("none",    printToString(center_justify("none", 1), 1));
 }
 
+TEST(raw_ostreamTest, Indent) {
+  indent Indent(4);
+  auto Spaces = [](int N) { return std::string(N, ' '); };
+  EXPECT_EQ(Spaces(4), printToString(Indent));
+  EXPECT_EQ("", printToString(indent(0)));
+  EXPECT_EQ(Spaces(5), printToString(Indent + 1));
+  EXPECT_EQ(Spaces(3), printToString(Indent - 1));
+  Indent += 1;
+  EXPECT_EQ(Spaces(5), printToString(Indent));
+  Indent -= 1;
+  EXPECT_EQ(Spaces(4), printToString(Indent));
+}
+
 TEST(raw_ostreamTest, FormatHex) {  
   EXPECT_EQ("0x1234",     printToString(format_hex(0x1234, 6), 6));
   EXPECT_EQ("0x001234",   printToString(format_hex(0x1234, 8), 8));
diff --git a/llvm/utils/yaml-bench/YAMLBench.cpp b/llvm/utils/yaml-bench/YAMLBench.cpp
index 50e55538a011cb..4dc6caeb6fdbfc 100644
--- a/llvm/utils/yaml-bench/YAMLBench.cpp
+++ b/llvm/utils/yaml-bench/YAMLBench.cpp
@@ -56,17 +56,6 @@ cl::opt<cl::boolOrDefault>
     UseColor("use-color", cl::desc("Emit colored output (default=autodetect)"),
              cl::init(cl::BOU_UNSET));
 
-struct indent {
-  unsigned distance;
-  indent(unsigned d) : distance(d) {}
-};
-
-static raw_ostream &operator <<(raw_ostream &os, const indent &in) {
-  for (unsigned i = 0; i < in.distance; ++i)
-    os << "  ";
-  return os;
-}
-
 /// Pretty print a tag by replacing tag:yaml.org,2002: with !!.
 static std::string prettyTag(yaml::Node *N) {
   std::string Tag = N->getVerbatimTag();

@jurahul jurahul merged commit 2731be7 into llvm:main Sep 18, 2024
11 checks passed
@jurahul jurahul deleted the add_raw_ostream_indent branch September 18, 2024 15:54
tmsri pushed a commit to tmsri/llvm-project that referenced this pull request Sep 19, 2024
)

Add helper struct indent() for adding indentation to raw_ostream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants