Skip to content

Commit dd06b8e

Browse files
authored
[clang-format] Fix anonymous reference parameter with default value (#86254)
When enabling alignment of consecutive declarations and reference right alignment, the needed space between `& ` and ` = ` is removed in the following use case. Problem (does not compile) ``` int a(const Test &= Test()); double b(); ``` Expected: ``` int a(const Test & = Test()); double b(); ``` Test command: ``` echo "int a(const Test& = Test()); double b();" | clang-format -style="{AlignConsecutiveDeclarations: true, ReferenceAlignment: Right}" ```
1 parent e36ec2f commit dd06b8e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,11 @@ 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;

clang/unittests/Format/FormatTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19066,6 +19066,11 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1906619066
verifyFormat("int a(int x);\n"
1906719067
"double b();",
1906819068
Alignment);
19069+
verifyFormat("int a(const Test & = Test());\n"
19070+
"int a1(int &foo, const Test & = Test());\n"
19071+
"int a2(int &foo, const Test &name = Test());\n"
19072+
"double b();",
19073+
Alignment);
1906919074
verifyFormat("struct Test {\n"
1907019075
" Test(const Test &) = default;\n"
1907119076
" ~Test() = default;\n"
@@ -19102,6 +19107,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1910219107
" int x,\n"
1910319108
" bool y);",
1910419109
Alignment);
19110+
// Set ColumnLimit low so that we break the argument list in multiple lines.
19111+
Alignment.ColumnLimit = 35;
19112+
verifyFormat("int a3(SomeTypeName1 &x,\n"
19113+
" SomeTypeName2 &y,\n"
19114+
" const Test & = Test());\n"
19115+
"double b();",
19116+
Alignment);
1910519117
Alignment.ColumnLimit = OldColumnLimit;
1910619118
// Ensure function pointers don't screw up recursive alignment
1910719119
verifyFormat("int a(int x, void (*fp)(int y));\n"
@@ -19287,6 +19299,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1928719299
"int foobar;",
1928819300
AlignmentLeft);
1928919301

19302+
verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
19303+
"double b();",
19304+
AlignmentLeft);
19305+
1929019306
// PAS_Middle
1929119307
FormatStyle AlignmentMiddle = Alignment;
1929219308
AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
@@ -19347,6 +19363,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1934719363
"int foobar;",
1934819364
AlignmentMiddle);
1934919365

19366+
verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
19367+
"double b();",
19368+
AlignmentMiddle);
19369+
1935019370
Alignment.AlignConsecutiveAssignments.Enabled = false;
1935119371
Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
1935219372
verifyFormat("#define A \\\n"

0 commit comments

Comments
 (0)