]> granicus.if.org Git - clang/commitdiff
Improve handling of trailing block comments.
authorDaniel Jasper <djasper@google.com>
Mon, 4 Feb 2013 07:32:14 +0000 (07:32 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 4 Feb 2013 07:32:14 +0000 (07:32 +0000)
We can now (even in non-bin-packing modes) format:
someFunction(1, /* comment 1 */
             2, /* comment 2 */
             3, /* comment 3 */
             aaa);

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

lib/Format/Format.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 2f14fc0ce15bdedce9d70748bde47139d5900d8b..91cbc2efcd4a459062b3260cd969bb0a69c6922f 100644 (file)
@@ -690,7 +690,8 @@ private:
       return true;
     if (State.NextToken->Parent->is(tok::comma) &&
         State.Stack.back().BreakAfterComma &&
-        State.NextToken->Type != TT_LineComment)
+        (State.NextToken->isNot(tok::comment) ||
+         !State.NextToken->Children[0].MustBreakBefore))
       return true;
     if ((State.NextToken->Type == TT_CtorInitializerColon ||
          (State.NextToken->Parent->ClosesTemplateDeclaration &&
index 0676df6c0e3922e900f22cf754a2fb2130617e92..0cfd9903cb886b1ab165fbd5570a60aa16b77b83 100644 (file)
@@ -662,17 +662,15 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedToken &Current) {
 
   if (Current.FormatTok.MustBreakBefore) {
     Current.MustBreakBefore = true;
+  } else if (Current.Type == TT_LineComment) {
+    Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0;
+  } else if ((Current.Parent->is(tok::comment) &&
+              Current.FormatTok.NewlinesBefore > 0) ||
+             (Current.is(tok::string_literal) &&
+              Current.Parent->is(tok::string_literal))) {
+    Current.MustBreakBefore = true;
   } else {
-    if (Current.Type == TT_LineComment) {
-      Current.MustBreakBefore = Current.FormatTok.NewlinesBefore > 0;
-    } else if ((Current.Parent->is(tok::comment) &&
-                Current.FormatTok.NewlinesBefore > 0) ||
-               (Current.is(tok::string_literal) &&
-                Current.Parent->is(tok::string_literal))) {
-      Current.MustBreakBefore = true;
-    } else {
-      Current.MustBreakBefore = false;
-    }
+    Current.MustBreakBefore = false;
   }
   Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current);
   if (Current.MustBreakBefore)
index 28d19d5ac51708bb76f046dfe8f9908d4f2dacde..17a8dc48abb9013de18cd1a718a32b09bc6025d9 100644 (file)
@@ -1908,6 +1908,15 @@ TEST_F(FormatTest, BlockComments) {
             "    parameter);",
             format("#define A\n"
                    "/* */someCall(parameter);", getLLVMStyleWithColumns(15)));
+
+  EXPECT_EQ("someFunction(1, /* comment 1 */\n"
+            "             2, /* comment 2 */\n"
+            "             3, /* comment 3 */\n"
+            "             aaaa);",
+            format("someFunction (1,   /* comment 1 */\n"
+                   "                2, /* comment 2 */  \n"
+                   "               3,   /* comment 3 */\n"
+                   "aaaa );", getGoogleStyle()));
 }
 
 TEST_F(FormatTest, FormatStarDependingOnContext) {