Skip to content

[clang-format] Fix anonymous reference parameter with default value #86254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,11 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;

// If PointerAlignment is PAS_Right, keep *s or &s next to the token
// If PointerAlignment is PAS_Right, keep *s or &s next to the token,
// except if the token is equal, then a space is needed.
if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
CurrentChange.Spaces != 0) {
CurrentChange.Spaces != 0 && CurrentChange.Tok->isNot(tok::equal)) {
const bool ReferenceNotRightAligned =
Style.ReferenceAlignment != FormatStyle::RAS_Right &&
Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19066,6 +19066,11 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
verifyFormat("int a(int x);\n"
"double b();",
Alignment);
verifyFormat("int a(const Test & = Test());\n"
"int a1(int &foo, const Test & = Test());\n"
"int a2(int &foo, const Test &name = Test());\n"
"double b();",
Alignment);
verifyFormat("struct Test {\n"
" Test(const Test &) = default;\n"
" ~Test() = default;\n"
Expand Down Expand Up @@ -19102,6 +19107,13 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
" int x,\n"
" bool y);",
Alignment);
// Set ColumnLimit low so that we break the argument list in multiple lines.
Alignment.ColumnLimit = 35;
verifyFormat("int a3(SomeTypeName1 &x,\n"
" SomeTypeName2 &y,\n"
" const Test & = Test());\n"
"double b();",
Alignment);
Alignment.ColumnLimit = OldColumnLimit;
// Ensure function pointers don't screw up recursive alignment
verifyFormat("int a(int x, void (*fp)(int y));\n"
Expand Down Expand Up @@ -19287,6 +19299,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
"int foobar;",
AlignmentLeft);

verifyFormat("int a(SomeType& foo, const Test& = Test());\n"
"double b();",
AlignmentLeft);

// PAS_Middle
FormatStyle AlignmentMiddle = Alignment;
AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle;
Expand Down Expand Up @@ -19347,6 +19363,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
"int foobar;",
AlignmentMiddle);

verifyFormat("int a(SomeType & foo, const Test & = Test());\n"
"double b();",
AlignmentMiddle);

Alignment.AlignConsecutiveAssignments.Enabled = false;
Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
verifyFormat("#define A \\\n"
Expand Down