diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -445,8 +445,16 @@ unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; int LineLengthAfter = Changes[i].TokenLength; - for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) - LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength; + for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) { + LineLengthAfter += Changes[j].Spaces; + // Changes are generally 1:1 with the tokens, but a change could also be + // inside of a token, in which case it's counted more than once: once for + // the whitespace surrounding the token (!IsInsideToken) and once for + // each whitespace change within it (IsInsideToken). + // Therefore, changes inside of a token should only count the space. + if (!Changes[j].IsInsideToken) + LineLengthAfter += Changes[j].TokenLength; + } unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter; // If we are restricted by the maximum column width, end the sequence. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12010,6 +12010,16 @@ " x = 1;\n" "y = 1;\n", Alignment); + + Alignment.ReflowComments = true; + Alignment.ColumnLimit = 50; + EXPECT_EQ("int x = 0;\n" + "int yy = 1; /// specificlennospace\n" + "int zzz = 2;\n", + format("int x = 0;\n" + "int yy = 1; ///specificlennospace\n" + "int zzz = 2;\n", + Alignment)); } TEST_F(FormatTest, AlignConsecutiveDeclarations) {