mairacanal created this revision.
mairacanal added reviewers: MyDeveloperDay, owenpan, yusuke-kadowaki,
HazardyKnusperkeks.
Herald added a project: All.
mairacanal requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
For comments that start after a new line, currently, the comments are
being indented. This happens because the OriginalWhitespaceRange
considers newlines on the range. Therefore, when AlignTrailingComments:
Kind: Leave, deduct the number of newlines before the token to calculate
the number of spaces for trailing comments.
Fixes llvm#59203
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D139029
Files:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTestComments.cpp
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3062,6 +3062,18 @@
"int d;// comment\n",
Style));
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n\n"
+ " // comment\n"
+ "// comment\n\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n\n"
+ " // comment\n"
+ "// comment\n\n"
+ "// comment\n",
+ Style));
+
// Just format comments normally when leaving exceeds the column limit
Style.ColumnLimit = 35;
EXPECT_EQ("int foo = 12345; // comment\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -957,7 +957,8 @@
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
auto OriginalSpaces =
Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
- Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding();
+ Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
+ Changes[i].NewlinesBefore;
unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
Changes[i].TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3062,6 +3062,18 @@
"int d;// comment\n",
Style));
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n\n"
+ " // comment\n"
+ "// comment\n\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n\n"
+ " // comment\n"
+ "// comment\n\n"
+ "// comment\n",
+ Style));
+
// Just format comments normally when leaving exceeds the column limit
Style.ColumnLimit = 35;
EXPECT_EQ("int foo = 12345; // comment\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -957,7 +957,8 @@
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
auto OriginalSpaces =
Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
- Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding();
+ Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
+ Changes[i].NewlinesBefore;
unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
Changes[i].TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits