[clang-format] Align trailing comments for function parameters (#164458)

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 #85123.

As the bug report explained, the procedure for aligning the function
parameters previously failed to update `StartOfTokenColumn`.
This commit is contained in:
sstwcw 2025-11-13 23:30:28 +00:00 committed by GitHub
parent 388ef61250
commit 3a2de951a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 2 deletions

View File

@ -516,7 +516,8 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
};
unsigned I = StartAt;
for (unsigned E = Changes.size(); I != E; ++I) {
const auto E = Changes.size();
for (; I != E; ++I) {
auto &CurrentChange = Changes[I];
if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
break;
@ -660,8 +661,15 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
MatchedIndices.push_back(I);
}
EndOfSequence = I;
// Pass entire lines to the function so that it can update the state of all
// tokens that move.
for (EndOfSequence = I;
EndOfSequence < E && Changes[EndOfSequence].NewlinesBefore == 0;
++EndOfSequence) {
}
AlignCurrentSequence();
// The return value should still be where the level ends. The rest of the line
// may contain stuff to be aligned within an outer level.
return I;
}

View File

@ -19851,6 +19851,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.