]> granicus.if.org Git - clang/commitdiff
Fix aligning of comments that are at the start of the line.
authorManuel Klimek <klimek@google.com>
Thu, 23 May 2013 19:54:43 +0000 (19:54 +0000)
committerManuel Klimek <klimek@google.com>
Thu, 23 May 2013 19:54:43 +0000 (19:54 +0000)
Now correctly leaves:
f(); // comment
// comment
g(); // comment
... alone if the middle comment was aligned with g() before formatting.

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

lib/Format/WhitespaceManager.cpp
unittests/Format/FormatTest.cpp

index d4be86e586134daf683bc7553d1b47b7eea8a7e2..e6c363b3732b2c94be7dd4cc10e6fd5accd82fe3 100644 (file)
@@ -170,9 +170,10 @@ void WhitespaceManager::alignTrailingComments() {
         MinColumn = std::max(MinColumn, ChangeMinColumn);
         MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
       }
-      BreakBeforeNext =
-          (i == 0) || (Changes[i].NewlinesBefore > 1) ||
-          (Changes[i].NewlinesBefore == 1 && !Changes[i - 1].IsTrailingComment);
+      BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) ||
+                        (Changes[i].NewlinesBefore == 1 &&
+                         !Changes[i - 1].IsTrailingComment) ||
+                        WasAlignedWithStartOfNextLine;
       Newlines = 0;
     }
   }
index 1215be17c4589f8546f544c59b03d5df8c4b26df..5346bdc7579f2d5cd7450d8d55b756b167c81e39 100644 (file)
@@ -650,6 +650,13 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
             format("lineWith();   // comment\n"
                    " // at start\n"
                    "otherLine();"));
+
+  EXPECT_EQ("lineWith(); // comment\n"
+            "// at start\n"
+            "otherLine(); // comment",
+            format("lineWith();   // comment\n"
+                   "// at start\n"
+                   "otherLine();   // comment"));
 }
 
 TEST_F(FormatTest, CanFormatCommentsLocally) {