]> granicus.if.org Git - clang/commitdiff
clang-format: Improve clang-format's detection about comment binding.
authorDaniel Jasper <djasper@google.com>
Fri, 8 Nov 2013 23:31:14 +0000 (23:31 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 8 Nov 2013 23:31:14 +0000 (23:31 +0000)
Before, existing code in the form of:

  int a; // this is a.
  // This is
  // b.
  int b;

Got turned into:

  int a; // this is a.
         // This is
  // b.
  int b;

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

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

index c24ccdfee4d5d7a7d5e16f96af6998bab7fd8bd5..26a8d41e87417ff1f025104449a7014bfa142732 100644 (file)
@@ -141,19 +141,21 @@ void WhitespaceManager::alignTrailingComments() {
       bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
                                     Changes[i - 1].Kind == tok::r_brace &&
                                     Changes[i - 1].StartOfTokenColumn == 0;
-      bool WasAlignedWithStartOfNextLine =
-          // A comment on its own line.
-          Changes[i].NewlinesBefore == 1 &&
-          // Not the last line.
-          i + 1 != e &&
-          // The start of the next token was previously aligned with
-          // the start of this comment.
-          (SourceMgr.getSpellingColumnNumber(
-               Changes[i].OriginalWhitespaceRange.getEnd()) ==
-           SourceMgr.getSpellingColumnNumber(
-               Changes[i + 1].OriginalWhitespaceRange.getEnd())) &&
-          // Which is not a comment itself.
-          Changes[i + 1].Kind != tok::comment;
+      bool WasAlignedWithStartOfNextLine = false;
+      if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
+        for (unsigned j = i + 1; j != e; ++j) {
+          if (Changes[j].Kind != tok::comment) { // Skip over comments.
+            // The start of the next token was previously aligned with the
+            // start of this comment.
+            WasAlignedWithStartOfNextLine =
+                (SourceMgr.getSpellingColumnNumber(
+                     Changes[i].OriginalWhitespaceRange.getEnd()) ==
+                 SourceMgr.getSpellingColumnNumber(
+                     Changes[j].OriginalWhitespaceRange.getEnd()));
+            break;
+          }
+        }
+      }
       if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
         alignTrailingComments(StartOfSequence, i, MinColumn);
         MinColumn = ChangeMinColumn;
index 794fb1a64b08f125e41203ed216c39c26e239b26..e37f0cd4eb9008b3235d7678df23390b8ea3fb2b 100644 (file)
@@ -787,6 +787,26 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
                    "// first\n"
                    " // at start\n"
                    "otherLine();   // comment"));
+  verifyFormat("f(); // comment\n"
+               "// first\n"
+               "// at start\n"
+               "otherLine();");
+  EXPECT_EQ("f(); // comment\n"
+            "// first\n"
+            "// at start\n"
+            "otherLine();",
+            format("f();   // comment\n"
+                   "// first\n"
+                   " // at start\n"
+                   "otherLine();"));
+  EXPECT_EQ("f(); // comment\n"
+            "     // first\n"
+            "// at start\n"
+            "otherLine();",
+            format("f();   // comment\n"
+                   " // first\n"
+                   "// at start\n"
+                   "otherLine();"));
 }
 
 TEST_F(FormatTest, CanFormatCommentsLocally) {