[clang] [clang-format] Stop aligning the to continuation lines (PR #76378)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 25 19:42:53 PST 2023


https://github.com/sstwcw created https://github.com/llvm/llvm-project/pull/76378

Some unwrapped lines are marked as continuations of the previous lines, for example the ports in a Verilog module header.  Previously, if the first line following the ports lines was changed, and git-clang-format was run, the changed line would be indented by an extra continuation indentation.

>From 7a8939bcd41cdfafe0546502064a6378ba117d60 Mon Sep 17 00:00:00 2001
From: sstwcw <su3e8a96kzlver at posteo.net>
Date: Tue, 26 Dec 2023 03:07:58 +0000
Subject: [PATCH] [clang-format] Stop aligning the to continuation lines

Some unwrapped lines are marked as continuations of the previous lines,
for example the ports in a Verilog module header.  Previously, if the
first line following the ports lines was changed, and git-clang-format
was run, the changed line would be indented by an extra continuation
indentation.
---
 clang/lib/Format/UnwrappedLineFormatter.cpp  |  2 +-
 clang/unittests/Format/FormatTestCSharp.cpp  | 12 ++++++++++++
 clang/unittests/Format/FormatTestVerilog.cpp | 11 +++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 56077499c39d53..2fc15d8828e4be 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -95,7 +95,7 @@ class LevelIndentTracker {
   /// level to the same indent.
   /// Note that \c nextLine must have been called before this method.
   void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
-    if (Line.InPPDirective)
+    if (Line.InPPDirective || Line.IsContinuation)
       return;
     assert(Line.Level < IndentForLevel.size());
     if (Line.First->is(tok::comment) && IndentForLevel[Line.Level] != -1)
diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 4a0840d32341e8..c27e2b576adf73 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -1304,6 +1304,18 @@ TEST_F(FormatTestCSharp, CSharpGenericTypeConstraints) {
                "}",
                Style);
 
+  // When the where line is not to be formatted, following lines should not take
+  // on its indentation.
+  verifyFormat("class ItemFactory<T>\n"
+               "    where T : new() {\n"
+               "  int f() {}\n"
+               "}",
+               "class ItemFactory<T>\n"
+               "    where T : new() {\n"
+               "  int f() {}\n"
+               "}",
+               Style, {tooling::Range(43, 13)});
+
   verifyFormat("class Dictionary<TKey, TVal>\n"
                "    where TKey : IComparable<TKey>\n"
                "    where TVal : IMyInterface {\n"
diff --git a/clang/unittests/Format/FormatTestVerilog.cpp b/clang/unittests/Format/FormatTestVerilog.cpp
index fcda05df182687..abebf9f7d4c785 100644
--- a/clang/unittests/Format/FormatTestVerilog.cpp
+++ b/clang/unittests/Format/FormatTestVerilog.cpp
@@ -613,6 +613,17 @@ TEST_F(FormatTestVerilog, Headers) {
                "      (input var x aaaaaaaaaaaaaaa``x, \\\n"
                "                   b);",
                Style);
+  // When the ports line is not to be formatted, following lines should not take
+  // on its indentation.
+  verifyFormat("module x\n"
+               "    (output x);\n"
+               "  assign x = 0;\n"
+               "endmodule",
+               "module x\n"
+               "    (output x);\n"
+               "    assign x = 0;\n"
+               "endmodule",
+               getDefaultStyle(), {tooling::Range(25, 18)});
 }
 
 TEST_F(FormatTestVerilog, Hierarchy) {



More information about the cfe-commits mailing list