]> granicus.if.org Git - clang/commitdiff
[clang-format] Align trailing comments if ColumnLimit is 0
authorKrasimir Georgiev <krasimir@google.com>
Wed, 23 Aug 2017 07:18:36 +0000 (07:18 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Wed, 23 Aug 2017 07:18:36 +0000 (07:18 +0000)
Summary:
ColumnLimit = 0 means no limit, so comment should always be aligned if requested. This was broken with

  https://llvm.org/svn/llvm-project/cfe/trunk@304687

introduced via

  https://reviews.llvm.org/D33830

and is included in 5.0.0-rc2. This commit fixes it and adds a unittest for this property.

Should go into clang-5.0 IMHO.

Contributed by @pboettch!

Reviewers: djasper, krasimir

Reviewed By: djasper, krasimir

Subscribers: hans, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311532 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/WhitespaceManager.cpp
unittests/Format/FormatTestComments.cpp

index e41a0767f59a7e978cd2cb1e80dc183a7425d862..f7d08f871b6096ab5f5f292b1909088ace6189cf 100644 (file)
@@ -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.
index ffcfb3cfe8b3548de539e260169e262924a17003..79dc003ae85518968c9f14cd98d661c90a9a133a 100644 (file)
@@ -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"