[clang] ad83bea - [clang-format] Don't move comments if AlignTrailingComments: Leave
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 1 16:07:32 PST 2022
Author: MaĆra Canal
Date: 2022-12-01T16:07:06-08:00
New Revision: ad83bead3d42082f858d810a5b9f43ed096a6e3c
URL: https://github.com/llvm/llvm-project/commit/ad83bead3d42082f858d810a5b9f43ed096a6e3c
DIFF: https://github.com/llvm/llvm-project/commit/ad83bead3d42082f858d810a5b9f43ed096a6e3c.diff
LOG: [clang-format] Don't move comments if AlignTrailingComments: Leave
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 #59203.
Differential Revision: https://reviews.llvm.org/D139029
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTestComments.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index ff7ef7edc984b..99261bdf4519e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -957,7 +957,8 @@ void WhitespaceManager::alignTrailingComments() {
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].Tok->NewlinesBefore;
unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
Changes[i].TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 524bf87f37ba5..3336ee4f39efc 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3062,6 +3062,61 @@ TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
"int d;// comment\n",
Style));
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment",
+ Style));
+
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "\n"
+ "// comment",
+ Style));
+
+ // Allow to keep 2 empty lines
+ Style.MaxEmptyLinesToKeep = 2;
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment",
+ Style));
+ Style.MaxEmptyLinesToKeep = 1;
+
// Just format comments normally when leaving exceeds the column limit
Style.ColumnLimit = 35;
EXPECT_EQ("int foo = 12345; // comment\n"
More information about the cfe-commits
mailing list