r304687 - [clang-format] Don't align too long broken trailing comments

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 4 12:27:03 PDT 2017


Author: krasimir
Date: Sun Jun  4 14:27:02 2017
New Revision: 304687

URL: http://llvm.org/viewvc/llvm-project?rev=304687&view=rev
Log:
[clang-format] Don't align too long broken trailing comments

Summary:
This patch fixes a bug where clang-format will align newly broken trailing
comments even if this will make them exceed the line limit. The bug was caused
by a combination of unsigned arithmetic overflow and an imprecise computation
of the length of broken comment lines.

Reviewers: djasper, alexfh

Reviewed By: alexfh

Subscribers: klimek

Differential Revision: https://reviews.llvm.org/D33830

Modified:
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/unittests/Format/FormatTestComments.cpp

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=304687&r1=304686&r2=304687&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Sun Jun  4 14:27:02 2017
@@ -111,7 +111,7 @@ void WhitespaceManager::calculateLineBre
 
     // 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::alignTrailingCom
       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.

Modified: cfe/trunk/unittests/Format/FormatTestComments.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestComments.cpp?rev=304687&r1=304686&r2=304687&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestComments.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestComments.cpp Sun Jun  4 14:27:02 2017
@@ -2170,6 +2170,15 @@ TEST_F(FormatTestComments, AlignTrailing
                    "// 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"




More information about the cfe-commits mailing list