diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 65fc65e79fdc3..e960a8f10c60d 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -650,8 +650,16 @@ static unsigned AlignTokens(const FormatStyle &Style, F &&Matches, MatchedIndices.push_back(I); } + // Pass to the function entire lines, so it can update the state of all tokens + // that move. EndOfSequence = I; + while (EndOfSequence < Changes.size() && + 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 the parent level. return I; } 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.