llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-llvm-support Author: Rahul Joshi (jurahul) <details> <summary>Changes</summary> 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. --- Full diff: https://github.com/llvm/llvm-project/pull/109478.diff 3 Files Affected: - (modified) clang/unittests/Format/FormatTest.cpp (+1-1) - (modified) llvm/include/llvm/Support/raw_ostream.h (+17-8) - (modified) llvm/unittests/Support/raw_ostream_test.cpp (+10) ``````````diff 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) { `````````` </details> https://github.com/llvm/llvm-project/pull/109478 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits