summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/WhitespaceManager.cpp6
-rw-r--r--clang/unittests/Format/FormatTestComments.cpp9
2 files changed, 13 insertions, 2 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 3b6311d1548..c48883351c7 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -111,7 +111,7 @@ void WhitespaceManager::calculateLineBreakInformation() {
// If there are multiple changes in this token, sum up all the changes until
// the end of the line.
- if (Changes[i - 1].IsInsideToken)
+ if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0)
LastOutsideTokenChange->TokenLength +=
Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
else
@@ -434,7 +434,9 @@ void WhitespaceManager::alignTrailingComments() {
continue;
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
- unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
+ unsigned ChangeMaxColumn = Style.ColumnLimit >= Changes[i].TokenLength
+ ? Style.ColumnLimit - Changes[i].TokenLength
+ : 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 a44a6845587..09ee4645f59 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -2170,6 +2170,15 @@ TEST_F(FormatTestComments, AlignTrailingComments) {
"// long",
getLLVMStyleWithColumns(15)));
+ // Don't align newly broken trailing comments if that would put them over the
+ // column limit.
+ EXPECT_EQ("int i, j; // line 1\n"
+ "int k; // line longg\n"
+ " // long",
+ format("int i, j; // line 1\n"
+ "int k; // line longg long",
+ getLLVMStyleWithColumns(20)));
+
// Align comment line sections aligned with the next token with the next
// token.
EXPECT_EQ("class A {\n"
OpenPOWER on IntegriCloud