Skip to content

Commit 3a2de95

Browse files
authored
[clang-format] Align trailing comments for function parameters (llvm#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 llvm#85123. As the bug report explained, the procedure for aligning the function parameters previously failed to update `StartOfTokenColumn`.
1 parent 388ef61 commit 3a2de95

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
516516
};
517517

518518
unsigned I = StartAt;
519-
for (unsigned E = Changes.size(); I != E; ++I) {
519+
const auto E = Changes.size();
520+
for (; I != E; ++I) {
520521
auto &CurrentChange = Changes[I];
521522
if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
522523
break;
@@ -660,8 +661,15 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
660661
MatchedIndices.push_back(I);
661662
}
662663

663-
EndOfSequence = I;
664+
// Pass entire lines to the function so that it can update the state of all
665+
// tokens that move.
666+
for (EndOfSequence = I;
667+
EndOfSequence < E && Changes[EndOfSequence].NewlinesBefore == 0;
668+
++EndOfSequence) {
669+
}
664670
AlignCurrentSequence();
671+
// The return value should still be where the level ends. The rest of the line
672+
// may contain stuff to be aligned within an outer level.
665673
return I;
666674
}
667675

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19851,6 +19851,14 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
1985119851
" Test &operator=(const Test &) = default;\n"
1985219852
"};",
1985319853
Alignment);
19854+
19855+
// The comment to the right should still align right.
19856+
verifyFormat("void foo(int name, // name\n"
19857+
" float name, // name\n"
19858+
" int name) // name\n"
19859+
"{}",
19860+
Alignment);
19861+
1985419862
unsigned OldColumnLimit = Alignment.ColumnLimit;
1985519863
// We need to set ColumnLimit to zero, in order to stress nested alignments,
1985619864
// otherwise the function parameters will be re-flowed onto a single line.

0 commit comments

Comments
 (0)