Skip to content

Commit 9a4e613

Browse files
committed
Simplify and add more tests
1 parent 3d4d9d7 commit 9a4e613

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,20 +464,18 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
464464
if (i + 1 != Changes.size())
465465
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
466466

467-
// If PointerAlignment is PAS_Right, keep *s or &s next to the token
467+
// If PointerAlignment is PAS_Right, keep *s or &s next to the token,
468+
// except if the token is equal, then a space is needed.
468469
if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
469470
Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
470-
CurrentChange.Spaces != 0) {
471+
CurrentChange.Spaces != 0 && CurrentChange.Tok->isNot(tok::equal)) {
471472
const bool ReferenceNotRightAligned =
472473
Style.ReferenceAlignment != FormatStyle::RAS_Right &&
473474
Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
474475
for (int Previous = i - 1;
475476
Previous >= 0 &&
476477
Changes[Previous].Tok->getType() == TT_PointerOrReference;
477478
--Previous) {
478-
// Don't align function default argument using return type maximum size
479-
if (Changes[Previous + 1].Tok->is(tok::equal))
480-
continue;
481479
assert(Changes[Previous].Tok->isPointerOrReference());
482480
if (Changes[Previous].Tok->isNot(tok::star)) {
483481
if (ReferenceNotRightAligned)

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19057,6 +19057,8 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1905719057
"double b();",
1905819058
Alignment);
1905919059
verifyFormat("int a(const Test & = Test());\n"
19060+
"int a1(int &foo, const Test & = Test());\n"
19061+
"int a2(int &foo, const Test &name = Test());\n"
1906019062
"double b();",
1906119063
Alignment);
1906219064
verifyFormat("struct Test {\n"
@@ -19095,6 +19097,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1909519097
" int x,\n"
1909619098
" bool y);",
1909719099
Alignment);
19100+
// Set ColumnLimit low so that we break the argument list in multiple lines.
19101+
Alignment.ColumnLimit = 35;
19102+
verifyFormat("int a3(SomeTypeName1 &x,\n"
19103+
" SomeTypeName2 &y,\n"
19104+
" const Test & = Test());\n"
19105+
"double b();",
19106+
Alignment);
1909819107
Alignment.ColumnLimit = OldColumnLimit;
1909919108
// Ensure function pointers don't screw up recursive alignment
1910019109
verifyFormat("int a(int x, void (*fp)(int y));\n"
@@ -19280,7 +19289,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1928019289
"int foobar;",
1928119290
AlignmentLeft);
1928219291

19283-
verifyFormat("int a(const Test& = Test());\n"
19292+
verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
1928419293
"double b();",
1928519294
AlignmentLeft);
1928619295

@@ -19344,6 +19353,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1934419353
"int foobar;",
1934519354
AlignmentMiddle);
1934619355

19356+
verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19357+
"double b();",
19358+
AlignmentMiddle);
19359+
1934719360
Alignment.AlignConsecutiveAssignments.Enabled = false;
1934819361
Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
1934919362
verifyFormat("#define A \\\n"

0 commit comments

Comments
 (0)