yusuke-kadowaki created this revision.
Herald added a project: All.
yusuke-kadowaki edited the summary of this revision.
yusuke-kadowaki added reviewers: curdeius, MyDeveloperDay, HazardyKnusperkeks.
yusuke-kadowaki published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This patch addresses https://github.com/llvm/llvm-project/issues/19756
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D132131
Files:
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -20042,6 +20042,7 @@
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
CHECK_PARSE_BOOL(AlignTrailingComments);
+ CHECK_PARSE_BOOL(AlignTrailingCommentsIgnoreEmptyLine);
CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
@@ -26434,6 +26435,15 @@
"inline bool var = is_integral_v<T> && is_signed_v<T>;");
}
+TEST_F(FormatTest, AlignTrailingCommentsIgnoreEmptyLine) {
+ FormatStyle Style = getLLVMStyle();
+ Style.AlignTrailingCommentsIgnoreEmptyLine = true;
+ verifyFormat("#include \"a.h\" //\n"
+ "\n"
+ "#include \"aa.h\" //\n",
+ Style);
+}
+
} // namespace
} // namespace format
} // namespace clang
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -927,6 +927,7 @@
unsigned StartOfSequence = 0;
bool BreakBeforeNext = false;
unsigned Newlines = 0;
+ unsigned int NewLineThr = Style.AlignTrailingCommentsIgnoreEmptyLine ? 2 : 1;
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
if (Changes[i].StartOfBlockComment)
continue;
@@ -979,7 +980,7 @@
MinColumn = ChangeMinColumn;
MaxColumn = ChangeMinColumn;
StartOfSequence = i;
- } else if (BreakBeforeNext || Newlines > 1 ||
+ else if (BreakBeforeNext || Newlines > NewLineThr ||
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
// Break the comment sequence if the previous line did not end
// in a trailing comment.
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -647,6 +647,7 @@
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
+ IO.mapOptional("AlignTrailingCommentsIgnoreEmptyLine", Style.AlignTrailingCommentsIgnoreEmptyLine);
IO.mapOptional("AllowAllArgumentsOnNextLine",
Style.AllowAllArgumentsOnNextLine);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
@@ -1182,6 +1183,7 @@
LLVMStyle.AlignArrayOfStructures = FormatStyle::AIAS_None;
LLVMStyle.AlignOperands = FormatStyle::OAS_Align;
LLVMStyle.AlignTrailingComments = true;
+ LLVMStyle.AlignTrailingCommentsIgnoreEmptyLine = false;
LLVMStyle.AlignConsecutiveAssignments = {};
LLVMStyle.AlignConsecutiveAssignments.Enabled = false;
LLVMStyle.AlignConsecutiveAssignments.AcrossEmptyLines = false;
@@ -1448,6 +1450,7 @@
GoogleStyle.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
GoogleStyle.AlignOperands = FormatStyle::OAS_DontAlign;
GoogleStyle.AlignTrailingComments = false;
+ GoogleStyle.AlignTrailingCommentsIgnoreEmptyLine = false;
GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
GoogleStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
@@ -1596,6 +1599,7 @@
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
Style.AlignOperands = FormatStyle::OAS_DontAlign;
Style.AlignTrailingComments = false;
+ Style.AlignTrailingCommentsIgnoreEmptyLine = false;
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
Style.BreakBeforeBraces = FormatStyle::BS_WebKit;
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -378,6 +378,8 @@
/// \version 3.7
bool AlignTrailingComments;
+ bool AlignTrailingCommentsIgnoreEmptyLine;
+
/// \brief If a function call or braced initializer list doesn't fit on a
/// line, allow putting all arguments onto the next line, even if
/// ``BinPackArguments`` is ``false``.
@@ -3857,6 +3859,7 @@
AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
+ AlignTrailingCommentsIgnoreEmptyLine == R.AlignTrailingCommentsIgnoreEmptyLine &&
AllowAllArgumentsOnNextLine == R.AllowAllArgumentsOnNextLine &&
AllowAllParametersOfDeclarationOnNextLine ==
R.AllowAllParametersOfDeclarationOnNextLine &&
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits