r194294 - clang-format: Improve clang-format's detection about comment binding.

Daniel Jasper djasper at google.com
Fri Nov 8 15:31:15 PST 2013


Author: djasper
Date: Fri Nov  8 17:31:14 2013
New Revision: 194294

URL: http://llvm.org/viewvc/llvm-project?rev=194294&view=rev
Log:
clang-format: Improve clang-format's detection about comment binding.

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;

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

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=194294&r1=194293&r2=194294&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Fri Nov  8 17:31:14 2013
@@ -141,19 +141,21 @@ void WhitespaceManager::alignTrailingCom
       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;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=194294&r1=194293&r2=194294&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Nov  8 17:31:14 2013
@@ -787,6 +787,26 @@ TEST_F(FormatTest, UnderstandsSingleLine
                    "// 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) {





More information about the cfe-commits mailing list