[llvm] [Support] Add scaling support in `indent` (PR #109478)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 20 14:07:14 PDT 2024


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/109478

Scaled indent is useful when indentation is always in steps of a fixed number (the Scale) and still allow using the +/- operators to adjust indentation.

>From d07c7c218471f0f2ce5b84d5e6043d0c958fb995 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Fri, 20 Sep 2024 14:04:25 -0700
Subject: [PATCH] [Support] Add scaling support in `indent`

Scaled indent is useful when indentation is always in steps of a
fixed number (the Scale) and still allow using the +/- operators
to adjust indentation.
---
 llvm/include/llvm/Support/raw_ostream.h     | 25 ++++++++++++++-------
 llvm/unittests/Support/raw_ostream_test.cpp | 10 +++++++++
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 34f91cbe9551f4..ce0cb88d55821b 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -774,18 +774,27 @@ class buffer_unique_ostream : public raw_svector_ostream {
 // you can use
 // OS << indent(6) << "more stuff";
 // which has better ergonomics (and clang-formats better as well).
+//
+// If indendation is always in increments of a fixed value, you can use Scale
+// to set that value once. So indent(1, 2) will add 2 spaces and
+// indent(1,2) + 1 will add 4 spaces.
 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); }
+  // Indendation is represented as `NumIndents` steps of size `Scale` each.
+  unsigned NumIndents;
+  unsigned Scale;
+
+  explicit indent(unsigned NumIndents, unsigned Scale = 1)
+      : NumIndents(NumIndents), Scale(Scale) {}
+
+  // These arithmeric operators preserve scale.
+  void operator+=(unsigned N) { NumIndents += N; }
+  void operator-=(unsigned N) { NumIndents -= N; }
+  indent operator+(unsigned N) const { return indent(NumIndents + N, Scale); }
+  indent operator-(unsigned N) const { return indent(NumIndents - N, Scale); }
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
-  return OS.indent(Indent.NumSpaces);
+  return OS.indent(Indent.NumIndents * Indent.Scale);
 }
 
 class Error;
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index 99aa350adad71d..a35edd61685296 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -188,6 +188,16 @@ TEST(raw_ostreamTest, Indent) {
   EXPECT_EQ(Spaces(5), printToString(Indent));
   Indent -= 1;
   EXPECT_EQ(Spaces(4), printToString(Indent));
+
+  // Scaled indent.
+  indent Scaled(4, 2);
+  EXPECT_EQ(Spaces(8), printToString(Scaled));
+  EXPECT_EQ(Spaces(10), printToString(Scaled + 1));
+  EXPECT_EQ(Spaces(6), printToString(Scaled - 1));
+  Scaled += 1;
+  EXPECT_EQ(Spaces(10), printToString(Scaled));
+  Scaled -= 1;
+  EXPECT_EQ(Spaces(8), printToString(Scaled));
 }
 
 TEST(raw_ostreamTest, FormatHex) {  



More information about the llvm-commits mailing list