[clang] [llvm] [Support] Add scaling support in `indent` (PR #109478)
Rahul Joshi via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 20 14:11:53 PDT 2024
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/109478
>From 31194fd2fc1dbc2918fe60e4de7198517ff821a3 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.
---
clang/unittests/Format/FormatTest.cpp | 2 +-
llvm/include/llvm/Support/raw_ostream.h | 25 ++++++++++++++-------
llvm/unittests/Support/raw_ostream_test.cpp | 10 +++++++++
3 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 53aa93a7a4fb01..1c9f568d24fd98 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25137,7 +25137,7 @@ TEST_F(FormatTest, SkipMacroDefinitionBody) {
"a",
Style);
- // Adjust indendations but don't change the definition.
+ // Adjust indentations but don't change the definition.
Style.IndentPPDirectives = FormatStyle::PPDIS_None;
verifyNoChange("#if A\n"
"#define A a\n"
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 34f91cbe9551f4..3be9f9d18a1717 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 indentation 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); }
+ // Indentation 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 cfe-commits
mailing list