https://github.com/rayroudc updated https://github.com/llvm/llvm-project/pull/86254
>From 3d4d9d701ee39d0bd4d60e4d0cd4a8577ce085d7 Mon Sep 17 00:00:00 2001 From: "C. Rayroud" <rayro...@gmail.com> Date: Mon, 18 Mar 2024 06:39:26 +0000 Subject: [PATCH 1/2] [clang-format] Fix anonymous reference parameter with default value --- clang/lib/Format/WhitespaceManager.cpp | 3 +++ clang/unittests/Format/FormatTest.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index fef85abf79a38c..b2f7fb2643a67a 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -475,6 +475,9 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, Previous >= 0 && Changes[Previous].Tok->getType() == TT_PointerOrReference; --Previous) { + // Don't align function default argument using return type maximum size + if (Changes[Previous + 1].Tok->is(tok::equal)) + continue; assert(Changes[Previous].Tok->isPointerOrReference()); if (Changes[Previous].Tok->isNot(tok::star)) { if (ReferenceNotRightAligned) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bea989c8c306db..a59480e0d3c505 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -19056,6 +19056,9 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { verifyFormat("int a(int x);\n" "double b();", Alignment); + verifyFormat("int a(const Test & = Test());\n" + "double b();", + Alignment); verifyFormat("struct Test {\n" " Test(const Test &) = default;\n" " ~Test() = default;\n" @@ -19277,6 +19280,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { "int foobar;", AlignmentLeft); + verifyFormat("int a(const Test& = Test());\n" + "double b();", + AlignmentLeft); + // PAS_Middle FormatStyle AlignmentMiddle = Alignment; AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; >From a1acaeecd660f1489b6f87aa9775588c2eef5693 Mon Sep 17 00:00:00 2001 From: "C. Rayroud" <rayro...@gmail.com> Date: Mon, 25 Mar 2024 07:26:39 +0000 Subject: [PATCH 2/2] Simplify and add more tests --- clang/lib/Format/WhitespaceManager.cpp | 8 +++----- clang/unittests/Format/FormatTest.cpp | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index b2f7fb2643a67a..44b583a623b9d4 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; @@ -475,9 +476,6 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End, Previous >= 0 && Changes[Previous].Tok->getType() == TT_PointerOrReference; --Previous) { - // Don't align function default argument using return type maximum size - if (Changes[Previous + 1].Tok->is(tok::equal)) - continue; assert(Changes[Previous].Tok->isPointerOrReference()); if (Changes[Previous].Tok->isNot(tok::star)) { if (ReferenceNotRightAligned) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index a59480e0d3c505..fe979f63932bbb 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -19056,7 +19056,8 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { verifyFormat("int a(int x);\n" "double b();", Alignment); - verifyFormat("int a(const Test & = Test());\n" + verifyFormat("int a(const Test &name = Test());\n" + "int a2(int &foo, const Test &name = Test());\n" "double b();", Alignment); verifyFormat("struct Test {\n" @@ -19095,6 +19096,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" @@ -19280,7 +19288,7 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { "int foobar;", AlignmentLeft); - verifyFormat("int a(const Test& = Test());\n" + verifyFormat("int a(int& count, SomeType& foo, const Test& = Test());\n" "double b();", AlignmentLeft); @@ -19344,6 +19352,10 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { "int foobar;", AlignmentMiddle); + verifyFormat("int a(int & count, SomeType & foo, const Test & = Test());\n" + "double b();", + AlignmentMiddle); + Alignment.AlignConsecutiveAssignments.Enabled = false; Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; verifyFormat("#define A \\\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits