]> granicus.if.org Git - clang/commitdiff
Fix aligning of comments.
authorManuel Klimek <klimek@google.com>
Thu, 23 May 2013 20:46:07 +0000 (20:46 +0000)
committerManuel Klimek <klimek@google.com>
Thu, 23 May 2013 20:46:07 +0000 (20:46 +0000)
Previously we started sequences to align for single line comments when
the previous line had a trailing comment, but the sequence was broken
for other reasons.

Now we re-format:
// a
 // b
f(); // c
to:
// a
// b
f(); // c

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

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

index e6c363b3732b2c94be7dd4cc10e6fd5accd82fe3..f7b65fc72cf2da362b333e3c0c80505cac003ef1 100644 (file)
@@ -170,10 +170,11 @@ 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) ||
-                        WasAlignedWithStartOfNextLine;
+      BreakBeforeNext =
+          (i == 0) || (Changes[i].NewlinesBefore > 1) ||
+          // Never start a sequence with a comment at the beginning of
+          // the line.
+          (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
       Newlines = 0;
     }
   }
index 5346bdc7579f2d5cd7450d8d55b756b167c81e39..ce551e9d1bc2d05be81f07f10d999de383890fb1 100644 (file)
@@ -657,6 +657,26 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
             format("lineWith();   // comment\n"
                    "// at start\n"
                    "otherLine();   // comment"));
+  EXPECT_EQ("lineWith();\n"
+            "// at start\n"
+            "otherLine(); // comment",
+            format("lineWith();\n"
+                   " // at start\n"
+                   "otherLine();   // comment"));
+  EXPECT_EQ("// first\n"
+            "// at start\n"
+            "otherLine(); // comment",
+            format("// first\n"
+                   " // at start\n"
+                   "otherLine();   // comment"));
+  EXPECT_EQ("f();\n"
+            "// first\n"
+            "// at start\n"
+            "otherLine(); // comment",
+            format("f();\n"
+                   "// first\n"
+                   " // at start\n"
+                   "otherLine();   // comment"));
 }
 
 TEST_F(FormatTest, CanFormatCommentsLocally) {