Skip to content

Commit

Permalink
[clang-format] Stop aligning the to continuation lines (#76378)
Browse files Browse the repository at this point in the history
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 line was changed, and git-clang-format
was run, the changed line would be indented by an extra continuation
indentation.
  • Loading branch information
sstwcw committed Jan 15, 2024
1 parent 48e8cd8 commit e3acfbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTestCSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTestVerilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e3acfbc

Please sign in to comment.