[clang] [clang-format] Option to Ignore Trailing Comment Length to AlignConsecutive (PR #190668)
Ben Dunkin via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 7 09:59:23 PDT 2026
https://github.com/bdunkin updated https://github.com/llvm/llvm-project/pull/190668
>From 50ecc069c535f2a035b4d981b40a9df11cf78060 Mon Sep 17 00:00:00 2001
From: Ben Dunkin <bdunkin at arena.net>
Date: Fri, 3 Apr 2026 15:49:18 -0700
Subject: [PATCH 1/2] Add option to ignore trailing comment length when
checking that alignining consecutive statements will fit on their lines.
---
clang/docs/ClangFormatStyleOptions.rst | 126 ++++++++++++
clang/include/clang/Format/Format.h | 20 +-
clang/lib/Format/Format.cpp | 17 +-
clang/lib/Format/WhitespaceManager.cpp | 19 +-
clang/unittests/Format/AlignmentTest.cpp | 223 +++++++++++++++++++++
clang/unittests/Format/ConfigParseTest.cpp | 71 +++----
6 files changed, 431 insertions(+), 45 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 54c085dff2840..02c716e0bfa4e 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -422,6 +422,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveBitFields:
@@ -580,6 +598,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveDeclarations:
@@ -738,6 +774,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveMacros:
@@ -897,6 +951,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveShortCaseStatements:
@@ -1175,6 +1247,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveTableGenCondOperatorColons:
@@ -1331,6 +1421,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignConsecutiveTableGenDefinitionColons:
@@ -1487,6 +1595,24 @@ the configuration (without a prefix: ``Auto``).
a = 2;
bbb >>= 2;
+ * ``bool IgnoreTrailingCommentLength`` Whether to consider trailing comments going over the column limit a
+ cause to separate alignment groups.
+
+ .. code-block:: c++
+
+ true:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+ short comment 2
+
+ false:
+ /* column limit -> | */
+ int a = 3; // a long trailing comment
+ int b = 4; // short comment 1
+ AReallyLongTypeName c = 5; // short comment 2
+
.. _AlignEscapedNewlines:
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 48ce5aa2bdfa1..98e29d1feb814 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -255,13 +255,31 @@ struct FormatStyle {
/// bbb >>= 2;
/// \endcode
bool PadOperators;
+ /// Whether to consider trailing comments going over the column limit a
+ /// cause to separate alignment groups.
+ /// \code
+ /// true:
+ /// /* column limit -> | */
+ /// int a = 3; // a long trailing comment
+ /// int b = 4; // short comment 1
+ /// AReallyLongTypeName c = 5; // short comment 2
+ /// short comment 2
+ ///
+ /// false:
+ /// /* column limit -> | */
+ /// int a = 3; // a long trailing comment
+ /// int b = 4; // short comment 1
+ /// AReallyLongTypeName c = 5; // short comment 2
+ /// \endcode
+ bool IgnoreTrailingCommentLength;
bool operator==(const AlignConsecutiveStyle &R) const {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignCompound == R.AlignCompound &&
AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
AlignFunctionPointers == R.AlignFunctionPointers &&
- PadOperators == R.PadOperators;
+ PadOperators == R.PadOperators &&
+ IgnoreTrailingCommentLength == R.IgnoreTrailingCommentLength;
}
bool operator!=(const AlignConsecutiveStyle &R) const {
return !(*this == R);
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 42190604b3881..e2d7ee939dc65 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -62,25 +62,29 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
// For backward compatibility.
IO.enumCase(Value, "true",
@@ -88,7 +92,8 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
IO.enumCase(Value, "false", FormatStyle::AlignConsecutiveStyle{});
}
@@ -101,6 +106,8 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
Value.AlignFunctionDeclarations);
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
IO.mapOptional("PadOperators", Value.PadOperators);
+ IO.mapOptional("IgnoreTrailingCommentLength",
+ Value.IgnoreTrailingCommentLength);
}
};
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 93f354a9f7256..459fe44d2ed6a 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -326,10 +326,12 @@ IncrementChangeSpaces(unsigned Start, int Delta,
// Column - The tokens indexed in Matches are moved to this column.
// RightJustify - Whether it is the token's right end or left end that gets
// moved to that column.
+// ForceAlignTrailingComments - whether trailing comments should be aligned
+// even if they have newlines before them
static void
AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
unsigned Column, bool RightJustify,
- ArrayRef<unsigned> Matches,
+ bool ForceAlignTrailingComments, ArrayRef<unsigned> Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
unsigned OriginalMatchColumn = 0;
int Shift = 0;
@@ -382,8 +384,12 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
(CurrentChange.indentAndNestingLevel() == ScopeStack[0] &&
CurrentChange.IndentedFromColumn >= OriginalMatchColumn));
- if (CurrentChange.NewlinesBefore > 0 && !InsideNestedScope)
+ bool IsForceAlignedTrailingComment =
+ ForceAlignTrailingComments && CurrentChange.IsTrailingComment;
+ if (CurrentChange.NewlinesBefore > 0 && !InsideNestedScope &&
+ !IsForceAlignedTrailingComment) {
Shift = 0;
+ }
// If this is the first matching token to be aligned, remember by how many
// spaces it has to be shifted, so the rest of the changes on the line are
@@ -403,7 +409,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
// not in a scope that should not move.
if ((!Matches.empty() && Matches[0] == i) ||
(ScopeStack.size() == 1u && CurrentChange.NewlinesBefore > 0 &&
- InsideNestedScope)) {
+ (InsideNestedScope || IsForceAlignedTrailingComment))) {
CurrentChange.IndentedFromColumn += Shift;
IncrementChangeSpaces(i, Shift, Changes);
}
@@ -545,7 +551,8 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
auto AlignCurrentSequence = [&] {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
AlignTokenSequence(Style, StartOfSequence, EndOfSequence,
- WidthLeft + WidthAnchor, RightJustify, MatchedIndices,
+ WidthLeft + WidthAnchor, RightJustify,
+ ACS.IgnoreTrailingCommentLength, MatchedIndices,
Changes);
}
WidthLeft = 0;
@@ -681,6 +688,10 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
MatchingParenToEncounter || Changes[J].IsAligned);
++J) {
const auto &Change = Changes[J];
+
+ if (ACS.IgnoreTrailingCommentLength && Change.IsTrailingComment)
+ continue;
+
const auto *Tok = Change.Tok;
if (Tok->MatchingParen) {
diff --git a/clang/unittests/Format/AlignmentTest.cpp b/clang/unittests/Format/AlignmentTest.cpp
index e3a3435424914..d7bb4d8a13a79 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -1913,6 +1913,229 @@ TEST_F(AlignmentTest, ConsecutiveDeclarations) {
"void f2(void);\n"
"size_t f3(void);",
Alignment);
+
+ Style = getLLVMStyleWithColumns(47);
+ Style.AlignConsecutiveDeclarations.Enabled = true;
+ verifyFormat("int a = 3; // a long trailing comment\n"
+ "int b = 4; // short comment 1\n"
+ "AReallyLongTypeName c = 5; // short comment 2\n",
+ Style);
+
+ Style.AlignConsecutiveDeclarations.IgnoreTrailingCommentLength = true;
+ verifyFormat("int a = 3; // a long trailing comment\n"
+ "int b = 4; // short comment 1\n"
+ "AReallyLongTypeName c = 5; // short comment 2\n",
+ Style);
+
+ Style = getLLVMStyleWithColumns(120);
+ Style.IndentWidth = 4;
+ Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
+ Style.AlignConsecutiveDeclarations.Enabled = true;
+ Style.AlignConsecutiveDeclarations.AcrossComments = true;
+ verifyFormat(
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo in\n"
+ " // lobortis. In ac fermentum enim. Duis "
+ "ullamcorper ac lectus suscipit interdum. Phasellus\n"
+ " // id condimentum lorem. Ut ac convallis "
+ "ligula. Maecenas quis interdum nisl. In vel\n"
+ " // molestie eros. Curabitur lobortis dui "
+ "vitae purus interdum\n"
+ " uint8 member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum "
+ "dolor sit amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ " uint8 aaa;\n"
+ " int8 bbbbb;\n"
+ " uint8 member7ABCDEFGHIJKLMNO;\n"
+ " ALongTypeNameV1ABCDE *member8ABCDEFGHIJKLMNO;\n"
+ " int8 member9ABCDEF; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo\n"
+ " uint8 member10ABCDEFGHIJKLMNO; // Lorem ipsum dolor sit amet, "
+ "consectetur elit\n"
+ " uint8 member11ABC;\n"
+ " uint8 member12ABCDEF;\n"
+ " uint8 member13ABC;\n"
+ " int8 member14ABCDEFGH;\n"
+ " AnotherLongTypeNameV1ABCDEFGH *member15ABCDEFGH;\n"
+ " int8 member16ABCDEF;\n"
+ " int8 member17;\n"
+ " int8 member18;\n"
+ " AThirdReallyLongTypeNameV1ABCDEFGHIJK member19ABCDEFGHIJK; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member20ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " uint8 member21ABCDEFGHIJKLM; // "
+ "Lorem ipsum dolor sit amet, "
+ "consectetur\n"
+ "};\n",
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo in lobortis. In ac "
+ "fermentum enim. Duis ullamcorper ac lectus suscipit interdum. Phasellus "
+ "id condimentum lorem. Ut ac convallis ligula. Maecenas quis interdum "
+ "nisl. In vel molestie eros. Curabitur lobortis dui vitae purus "
+ "interdum\n"
+ " uint8 member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum "
+ "dolor sit amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ " uint8 aaa;\n"
+ " int8 bbbbb;\n"
+ " uint8 member7ABCDEFGHIJKLMNO;\n"
+ " ALongTypeNameV1ABCDE * member8ABCDEFGHIJKLMNO;\n"
+ " int8 member9ABCDEF; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo\n"
+ " uint8 member10ABCDEFGHIJKLMNO; // Lorem ipsum dolor sit amet, "
+ "consectetur elit\n"
+ " uint8 member11ABC;\n"
+ " uint8 member12ABCDEF;\n"
+ " uint8 member13ABC;\n"
+ " int8 member14ABCDEFGH;\n"
+ " AnotherLongTypeNameV1ABCDEFGH * member15ABCDEFGH;\n"
+ " int8 member16ABCDEF;\n"
+ " int8 member17;\n"
+ " int8 member18;\n"
+ " AThirdReallyLongTypeNameV1ABCDEFGHIJK member19ABCDEFGHIJK; // Lorem "
+ "ipsum dolor sit amet, consectetur elit\n"
+ " TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member20ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " uint8 member21ABCDEFGHIJKLM; // Lorem ipsum dolor sit amet, "
+ "consectetur\n"
+ "};\n",
+ Style);
+
+ Style.AlignConsecutiveDeclarations.IgnoreTrailingCommentLength = true;
+ // This one tests proper alignment of the reflowed comment
+ verifyFormat(
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem "
+ "ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper "
+ "in leo in\n"
+ " // "
+ "lobortis. In ac fermentum enim. Duis ullamcorper ac lectus suscipit "
+ "interdum. Phasellus\n"
+ " // id "
+ "condimentum lorem. Ut ac convallis ligula. Maecenas quis interdum nisl. "
+ "In vel\n"
+ " // "
+ "molestie eros. Curabitur lobortis dui vitae purus interdum\n"
+ " uint8 "
+ "member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum dolor sit "
+ "amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 "
+ "member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ " uint8 aaa;\n"
+ " int8 bbbbb;\n"
+ " uint8 member7ABCDEFGHIJKLMNO;\n"
+ " ALongTypeNameV1ABCDE *member8ABCDEFGHIJKLMNO;\n"
+ " int8 member9ABCDEF; // Lorem "
+ "ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper "
+ "in leo\n"
+ " uint8 member10ABCDEFGHIJKLMNO; "
+ "// Lorem ipsum dolor sit amet, consectetur elit\n"
+ " uint8 member11ABC;\n"
+ " uint8 member12ABCDEF;\n"
+ " uint8 member13ABC;\n"
+ " int8 member14ABCDEFGH;\n"
+ " AnotherLongTypeNameV1ABCDEFGH *member15ABCDEFGH;\n"
+ " int8 member16ABCDEF;\n"
+ " int8 member17;\n"
+ " int8 member18;\n"
+ " AThirdReallyLongTypeNameV1ABCDEFGHIJK member19ABCDEFGHIJK; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member20ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " uint8 member21ABCDEFGHIJKLM; // "
+ "Lorem ipsum dolor sit amet, consectetur\n"
+ "};\n",
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo in lobortis. In ac "
+ "fermentum enim. Duis ullamcorper ac lectus suscipit interdum. Phasellus "
+ "id condimentum lorem. Ut ac convallis ligula. Maecenas quis interdum "
+ "nisl. In vel molestie eros. Curabitur lobortis dui vitae purus "
+ "interdum\n"
+ " uint8 member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum "
+ "dolor sit amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ " uint8 aaa;\n"
+ " int8 bbbbb;\n"
+ " uint8 member7ABCDEFGHIJKLMNO;\n"
+ " ALongTypeNameV1ABCDE * member8ABCDEFGHIJKLMNO;\n"
+ " int8 member9ABCDEF; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo\n"
+ " uint8 member10ABCDEFGHIJKLMNO; // Lorem ipsum dolor sit amet, "
+ "consectetur elit\n"
+ " uint8 member11ABC;\n"
+ " uint8 member12ABCDEF;\n"
+ " uint8 member13ABC;\n"
+ " int8 member14ABCDEFGH;\n"
+ " AnotherLongTypeNameV1ABCDEFGH * member15ABCDEFGH;\n"
+ " int8 member16ABCDEF;\n"
+ " int8 member17;\n"
+ " int8 member18;\n"
+ " AThirdReallyLongTypeNameV1ABCDEFGHIJK member19ABCDEFGHIJK; // Lorem "
+ "ipsum dolor sit amet, consectetur elit\n"
+ " TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member20ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ " uint8 member21ABCDEFGHIJKLM; // Lorem ipsum dolor sit amet, "
+ "consectetur\n"
+ "};\n",
+ Style);
+
+ // This one tests proper alignment of the trailing comments after the first
+ // member
+ verifyFormat(
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem "
+ "ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper "
+ "in leo in\n"
+ " // "
+ "lobortis. In ac fermentum enim. Duis ullamcorper ac lectus suscipit "
+ "interdum. Phasellus\n"
+ " // id "
+ "condimentum lorem. Ut ac convallis ligula. Maecenas quis interdum nisl. "
+ "In vel\n"
+ " // "
+ "molestie eros. Curabitur lobortis dui vitae purus interdum\n"
+ " uint8 "
+ "member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum dolor sit "
+ "amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 "
+ "member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ " TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member5ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ "};\n",
+ "struct ATestingTypeABCDEF {\n"
+ " uint8 member1ABCDEFGHI; // Lorem ipsum dolor sit amet, consectetur "
+ "adipiscing elit. Aliquam ullamcorper in leo in\n"
+ " // lobortis. In ac fermentum enim. Duis "
+ "ullamcorper ac lectus suscipit interdum. Phasellus\n"
+ " // id condimentum lorem. Ut ac convallis "
+ "ligula. Maecenas quis interdum nisl. In vel\n"
+ " // molestie eros. Curabitur lobortis dui "
+ "vitae purus interdum\n"
+ " uint8 "
+ "member2ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ; // Lorem ipsum dolor sit "
+ "amet, consectetur adipiscing\n"
+ " int8 member3ABCDEFGHIJ;\n"
+ " int8 "
+ "member4ABCDEFGHIJKLMNOPQRSTU; // Lorem ipsum dolor sit amet, "
+ "consectetur amet\n"
+ "TheLastReallyLongTypeNameV1ABCDEFGHIJKLM member5ABCDEFGHIJKLMN; // "
+ "Lorem ipsum dolor sit amet, consectetur elit\n"
+ "};\n",
+ Style);
}
TEST_F(AlignmentTest, ConsecutiveDeclarationsAcrossEmptyLinesAndComments) {
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 3cff71608cba4..199cb5db6a763 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -339,44 +339,44 @@ TEST(ConfigParseTest, ParsesConfiguration) {
do { \
Style.FIELD.Enabled = true; \
CHECK_PARSE(#FIELD ": None", FIELD, FormatStyle::AlignConsecutiveStyle{}); \
- CHECK_PARSE( \
- #FIELD ": Consecutive", FIELD, \
- FormatStyle::AlignConsecutiveStyle( \
- {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
- /*AcrossComments=*/false, /*AlignCompound=*/false, \
- /*AlignFunctionDeclarations=*/true, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
- CHECK_PARSE( \
- #FIELD ": AcrossEmptyLines", FIELD, \
- FormatStyle::AlignConsecutiveStyle( \
- {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
- /*AcrossComments=*/false, /*AlignCompound=*/false, \
- /*AlignFunctionDeclarations=*/true, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
- CHECK_PARSE( \
- #FIELD ": AcrossComments", FIELD, \
- FormatStyle::AlignConsecutiveStyle( \
- {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
- /*AcrossComments=*/true, /*AlignCompound=*/false, \
- /*AlignFunctionDeclarations=*/true, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
- CHECK_PARSE( \
- #FIELD ": AcrossEmptyLinesAndComments", FIELD, \
- FormatStyle::AlignConsecutiveStyle( \
- {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
- /*AcrossComments=*/true, /*AlignCompound=*/false, \
- /*AlignFunctionDeclarations=*/true, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
+ CHECK_PARSE(#FIELD ": Consecutive", FIELD, \
+ FormatStyle::AlignConsecutiveStyle( \
+ {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
+ /*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true, \
+ /*IgnoreTrailingCommentLength=*/false})); \
+ CHECK_PARSE(#FIELD ": AcrossEmptyLines", FIELD, \
+ FormatStyle::AlignConsecutiveStyle( \
+ {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
+ /*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true, \
+ /*IgnoreTrailingCommentLength=*/false})); \
+ CHECK_PARSE(#FIELD ": AcrossComments", FIELD, \
+ FormatStyle::AlignConsecutiveStyle( \
+ {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
+ /*AcrossComments=*/true, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true, \
+ /*IgnoreTrailingCommentLength=*/false})); \
+ CHECK_PARSE(#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
+ FormatStyle::AlignConsecutiveStyle( \
+ {/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
+ /*AcrossComments=*/true, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true, \
+ /*IgnoreTrailingCommentLength=*/false})); \
/* For backwards compability, false / true should still parse */ \
CHECK_PARSE(#FIELD ": false", FIELD, \
FormatStyle::AlignConsecutiveStyle{}); \
- CHECK_PARSE( \
- #FIELD ": true", FIELD, \
- FormatStyle::AlignConsecutiveStyle( \
- {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
- /*AcrossComments=*/false, /*AlignCompound=*/false, \
- /*AlignFunctionDeclarations=*/true, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
+ CHECK_PARSE(#FIELD ": true", FIELD, \
+ FormatStyle::AlignConsecutiveStyle( \
+ {/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
+ /*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
+ /*AlignFunctionPointers=*/false, /*PadOperators=*/true, \
+ /*IgnoreTrailingCommentLength=*/false})); \
\
CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \
@@ -385,6 +385,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE_NESTED_BOOL(FIELD, AlignFunctionDeclarations); \
CHECK_PARSE_NESTED_BOOL(FIELD, AlignFunctionPointers); \
CHECK_PARSE_NESTED_BOOL(FIELD, PadOperators); \
+ CHECK_PARSE_NESTED_BOOL(FIELD, IgnoreTrailingCommentLength); \
} while (false)
CHECK_ALIGN_CONSECUTIVE(AlignConsecutiveAssignments);
>From 02ed4e3ec1e208d922339b5c9a8ef0c9c1ac9a04 Mon Sep 17 00:00:00 2001
From: Ben Dunkin <bdunkin at arena.net>
Date: Tue, 7 Apr 2026 09:59:05 -0700
Subject: [PATCH 2/2] Fix build error on linux.
---
clang/unittests/Format/AlignmentTest.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/unittests/Format/AlignmentTest.cpp b/clang/unittests/Format/AlignmentTest.cpp
index d7bb4d8a13a79..8f325011efcfb 100644
--- a/clang/unittests/Format/AlignmentTest.cpp
+++ b/clang/unittests/Format/AlignmentTest.cpp
@@ -2586,14 +2586,16 @@ TEST_F(AlignmentTest, AlignWithLineBreaks) {
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/false,
/*AlignFunctionPointers=*/false,
- /*PadOperators=*/true}));
+ /*PadOperators=*/true,
+ /*IgnoreTrailingCommentLength=*/false}));
EXPECT_EQ(Style.AlignConsecutiveDeclarations,
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionDeclarations=*/true,
/*AlignFunctionPointers=*/false,
- /*PadOperators=*/false}));
+ /*PadOperators=*/false,
+ /*IgnoreTrailingCommentLength=*/false}));
verifyFormat("void foo() {\n"
" int myVar = 5;\n"
" double x = 3.14;\n"
More information about the cfe-commits
mailing list