r174309 - Improve handling of trailing block comments.

Daniel Jasper djasper at google.com
Sun Feb 3 23:32:15 PST 2013


Author: djasper
Date: Mon Feb  4 01:32:14 2013
New Revision: 174309

URL: http://llvm.org/viewvc/llvm-project?rev=174309&view=rev
Log:
Improve handling of trailing block comments.

We can now (even in non-bin-packing modes) format:
someFunction(1, /* comment 1 */
             2, /* comment 2 */
             3, /* comment 3 */
             aaa);

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.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=174309&r1=174308&r2=174309&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Feb  4 01:32:14 2013
@@ -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 &&

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=174309&r1=174308&r2=174309&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Feb  4 01:32:14 2013
@@ -662,17 +662,15 @@ void TokenAnnotator::calculateFormatting
 
   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)

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174309&r1=174308&r2=174309&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb  4 01:32:14 2013
@@ -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) {





More information about the cfe-commits mailing list