[clang] dd06b8e - [clang-format] Fix anonymous reference parameter with default value (#86254)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 27 11:59:34 PDT 2024
Author: rayroudc
Date: 2024-03-27T19:59:31+01:00
New Revision: dd06b8e679fd28f51cd065401062041a40b87f9c
URL: https://github.com/llvm/llvm-project/commit/dd06b8e679fd28f51cd065401062041a40b87f9c
DIFF: https://github.com/llvm/llvm-project/commit/dd06b8e679fd28f51cd065401062041a40b87f9c.diff
LOG: [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}"
```
Added:
Modified:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 710bf8d8a8ec70..d06c42d5f4c5c5 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -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;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 03005384a6f667..d1e977dfa66af5 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
@@ -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"
@@ -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;
@@ -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"
More information about the cfe-commits
mailing list