r195954 - clang-format: Extends formatted ranges to subsequent lines comments.

Daniel Jasper djasper at google.com
Fri Nov 29 01:27:44 PST 2013


Author: djasper
Date: Fri Nov 29 03:27:43 2013
New Revision: 195954

URL: http://llvm.org/viewvc/llvm-project?rev=195954&view=rev
Log:
clang-format: Extends formatted ranges to subsequent lines comments.

Before:
  int aaaa;     // This line is formatted.
                // The comment continues ..
                // .. here.

Before:
  int aaaa; // This line is formatted.
            // The comment continues ..
            // .. here.

This fixes llvm.org/PR17914.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=195954&r1=195953&r2=195954&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Nov 29 03:27:43 2013
@@ -1275,7 +1275,7 @@ private:
   bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
                             SmallVectorImpl<AnnotatedLine *>::iterator E) {
     bool SomeLineAffected = false;
-    bool PreviousLineAffected = false;
+    const AnnotatedLine *PreviousLine = NULL;
     while (I != E) {
       AnnotatedLine *Line = *I;
       Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
@@ -1299,9 +1299,10 @@ private:
         continue;
       }
 
-      if (nonPPLineAffected(Line, &PreviousLineAffected))
+      if (nonPPLineAffected(Line, PreviousLine))
         SomeLineAffected = true;
 
+      PreviousLine = Line;
       ++I;
     }
     return SomeLineAffected;
@@ -1309,7 +1310,8 @@ private:
 
   // Determines whether 'Line' is affected by the SourceRanges given as input.
   // Returns \c true if line or one if its children is affected.
-  bool nonPPLineAffected(AnnotatedLine *Line, bool *PreviousLineAffected) {
+  bool nonPPLineAffected(AnnotatedLine *Line,
+                         const AnnotatedLine *PreviousLine) {
     bool SomeLineAffected = false;
     Line->ChildrenAffected =
         computeAffectedLines(Line->Children.begin(), Line->Children.end());
@@ -1340,14 +1342,18 @@ private:
 
     // Was this line moved, i.e. has it previously been on the same line as an
     // affected line?
-    bool LineMoved = *PreviousLineAffected && Line->First->NewlinesBefore == 0;
+    bool LineMoved = PreviousLine && PreviousLine->Affected &&
+                     Line->First->NewlinesBefore == 0;
 
-    if (SomeTokenAffected || SomeFirstChildAffected || LineMoved) {
+    bool IsContinuedComment = Line->First->is(tok::comment) &&
+                              Line->First->Next == NULL &&
+                              Line->First->NewlinesBefore < 2 && PreviousLine &&
+                              PreviousLine->Last->is(tok::comment);
+
+    if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
+        IsContinuedComment) {
       Line->Affected = true;
-      *PreviousLineAffected = true;
       SomeLineAffected = true;
-    } else {
-      *PreviousLineAffected = false;
     }
     return SomeLineAffected;
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=195954&r1=195953&r2=195954&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Nov 29 03:27:43 2013
@@ -829,6 +829,36 @@ TEST_F(FormatTest, CanFormatCommentsLoca
                    "int b;\n"
                    "int   c; // unrelated comment",
                    31, 0, getLLVMStyle()));
+
+  EXPECT_EQ("int a; // This\n"
+            "       // is\n"
+            "       // a",
+            format("int a;      // This\n"
+                   "            // is\n"
+                   "            // a",
+                   0, 0, getLLVMStyle()));
+  EXPECT_EQ("int a; // This\n"
+            "       // is\n"
+            "       // a\n"
+            "// This is b\n"
+            "int b;",
+            format("int a; // This\n"
+                   "     // is\n"
+                   "     // a\n"
+                   "// This is b\n"
+                   "int b;",
+                   0, 0, getLLVMStyle()));
+  EXPECT_EQ("int a; // This\n"
+            "       // is\n"
+            "       // a\n"
+            "\n"
+            "  // This is unrelated",
+            format("int a; // This\n"
+                   "     // is\n"
+                   "     // a\n"
+                   "\n"
+                   "  // This is unrelated",
+                   0, 0, getLLVMStyle()));
 }
 
 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
@@ -843,11 +873,11 @@ TEST_F(FormatTest, RemovesTrailingWhites
 TEST_F(FormatTest, UnderstandsBlockComments) {
   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
-  EXPECT_EQ(
-      "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
-      "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
-      format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n/* Trailing comment for aa... */\n"
-             "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
+  EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
+            "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
+            format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
+                   "/* Trailing comment for aa... */\n"
+                   "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
   EXPECT_EQ(
       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",





More information about the cfe-commits mailing list