Skip to content

Conversation

sstwcw
Copy link
Contributor

@sstwcw sstwcw commented Oct 21, 2025

before

void foo(int   name, // name
         float name, // name
         int   name)   // name
{}

after

void foo(int   name, // name
         float name, // name
         int   name) // name
{}

Fixes #85123.

As the bug report explained, the procedure for aligning the function parameters previously failed to update StartOfTokenColumn.

@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-clang-format

Author: None (sstwcw)

Changes

before

void foo(int   name, // name
         float name, // name
         int   name)   // name
{}

after

void foo(int   name, // name
         float name, // name
         int   name) // name
{}

Fixes #85123.

As the bug report explained, the procedure for aligning the function parameters previously failed to update StartOfTokenColumn.


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

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+7)
  • (modified) clang/unittests/Format/FormatTest.cpp (+8)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 65fc65e79fdc3..b9a261c9e91d2 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -399,6 +399,13 @@ AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
       }
     }
   }
+
+  // The scope to be aligned may not occupy entire lines. The rest of the line
+  // needs some book-keeping.
+  for (unsigned i = End; i < Changes.size() && Changes[i].NewlinesBefore == 0;
+       ++i) {
+    Changes[i].StartOfTokenColumn += Shift;
+  }
 }
 
 // Walk through a subset of the changes, starting at StartAt, and find
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ce68f91bef02a..9133eeb137cb9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -19828,6 +19828,14 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
                "  Test &operator=(const Test &) = default;\n"
                "};",
                Alignment);
+
+  // The comment to the right should still align right.
+  verifyFormat("void foo(int   name, // name\n"
+               "         float name, // name\n"
+               "         int   name) // name\n"
+               "{}",
+               Alignment);
+
   unsigned OldColumnLimit = Alignment.ColumnLimit;
   // We need to set ColumnLimit to zero, in order to stress nested alignments,
   // otherwise the function parameters will be re-flowed onto a single line.

before

```C++
void foo(int   name, // name
         float name, // name
         int   name)   // name
{}
```

after

```C++
void foo(int   name, // name
         float name, // name
         int   name) // name
{}
```

Fixes llvm#85123.

As the bug report explained, the procedure for aligning the function
parameters previously failed to update `StartOfTokenColumn`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-format] Incorrect indentation after a closing paren with Align ConsecutiveDeclarations + TrailingComments

3 participants