diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index e41a0767f59a..f7d08f871b60 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -472,9 +472,14 @@ void WhitespaceManager::alignTrailingComments() { continue; unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; - unsigned ChangeMaxColumn = Style.ColumnLimit >= Changes[i].TokenLength - ? Style.ColumnLimit - Changes[i].TokenLength - : ChangeMinColumn; + unsigned ChangeMaxColumn; + + if (Style.ColumnLimit == 0) + ChangeMaxColumn = UINT_MAX; + else if (Style.ColumnLimit >= Changes[i].TokenLength) + ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; + else + ChangeMaxColumn = ChangeMinColumn; // If we don't create a replacement for this change, we have to consider // it to be immovable. diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index ffcfb3cfe8b3..79dc003ae855 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -2476,6 +2476,13 @@ TEST_F(FormatTestComments, AlignTrailingComments) { "int k; // line longg long", getLLVMStyleWithColumns(20))); + // Always align if ColumnLimit = 0 + EXPECT_EQ("int i, j; // line 1\n" + "int k; // line longg long", + format("int i, j; // line 1\n" + "int k; // line longg long", + getLLVMStyleWithColumns(0))); + // Align comment line sections aligned with the next token with the next // token. EXPECT_EQ("class A {\n"