Skip to content
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

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

Merged
merged 1 commit into from
Mar 27, 2024

Conversation

rayroudc
Copy link
Contributor

@rayroudc rayroudc commented Mar 22, 2024

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}"

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 22, 2024

@llvm/pr-subscribers-clang-format

Author: None (rayroudc)

Changes

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}"

Full diff: https://github.com/llvm/llvm-project/pull/86254.diff

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+4)
  • (modified) clang/unittests/Format/FormatTest.cpp (+7)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 6577c19cdf7978..c4a7d54e5bc8ed 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -471,6 +471,10 @@ 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 a14b002c37c631..23169363542ec1 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;

Copy link

github-actions bot commented Mar 22, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@HazardyKnusperkeks HazardyKnusperkeks added the invalid-code-generation Tool (e.g. clang-format) produced invalid code that no longer compiles label Mar 23, 2024
Copy link

✅ With the latest revision this PR passed the Python code formatter.

@rayroudc rayroudc force-pushed the fix_anonym_ref_param branch 2 times, most recently from a1acaee to 9a4e613 Compare March 25, 2024 20:53
@rayroudc
Copy link
Contributor Author

@HazardyKnusperkeks, thank you for the review!
Could you please help with the merge?

@HazardyKnusperkeks HazardyKnusperkeks merged commit dd06b8e into llvm:main Mar 27, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format invalid-code-generation Tool (e.g. clang-format) produced invalid code that no longer compiles
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants