]> granicus.if.org Git - clang/commitdiff
Improve formatting of code with comments.
authorDaniel Jasper <djasper@google.com>
Tue, 29 Jan 2013 19:41:55 +0000 (19:41 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 29 Jan 2013 19:41:55 +0000 (19:41 +0000)
Before:
aaaaaaa(aaaaaa( // comment
                aaaaaaa));
<big mess>

After:
aaaaaaa(aaaaaa( // comment
    aaaaaaaa));
function(/* parameter 1 */ aaaaaaa,
         /* parameter 2 */ aaaaaaa,
         /* parameter 3 */ aaaaaaa,
         /* parameter 4 */ aaaaaaa);

(the latter example was only wrong in the one-arg-per-line mode, e.g. in
Google style).

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

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

index 81c8309ef9d13fe9cd5557ba573362fbe85bd972..ecd6f5d3854bf3d9ec63094ae4e5a01ab12a14bb 100644 (file)
@@ -617,12 +617,11 @@ private:
           (getPrecedence(Previous) == prec::Assignment ||
            Previous.is(tok::kw_return)))
         State.Stack.back().AssignmentColumn = State.Column + Spaces;
-      if (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
-          State.NextToken->Parent->Type == TT_TemplateOpener)
+      if (Current.Type != TT_LineComment &&
+          (Previous.is(tok::l_paren) || Previous.is(tok::l_brace) ||
+           State.NextToken->Parent->Type == TT_TemplateOpener))
         State.Stack[ParenLevel].Indent = State.Column + Spaces;
-      if (Current.getPreviousNoneComment() != NULL &&
-          Current.getPreviousNoneComment()->is(tok::comma) &&
-          Current.isNot(tok::comment))
+      if (Previous.is(tok::comma) && Current.Type != TT_LineComment)
         State.Stack[ParenLevel].HasMultiParameterLine = true;
 
       State.Column += Spaces;
@@ -746,7 +745,7 @@ private:
         State.LineContainsContinuedForLoopSection)
       return UINT_MAX;
     if (!NewLine && State.NextToken->Parent->is(tok::comma) &&
-        State.NextToken->isNot(tok::comment) &&
+        State.NextToken->Type != TT_LineComment &&
         State.Stack.back().BreakAfterComma)
       return UINT_MAX;
     // Trying to insert a parameter on a new line if there are already more than
index 219d73c04d65136d5f412f5ab953080a410e9ad2..32b2ff7f750ae3cfad05e40538e0be8527267b38 100644 (file)
@@ -447,6 +447,9 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
             format("int i;\\\n// single line trailing comment"));
 
   verifyGoogleFormat("int a;  // Trailing comment.");
+
+  verifyFormat("someFunction(anotherFunction( // Force break.\n"
+               "    parameter));");
 }
 
 TEST_F(FormatTest, UnderstandsMultiLineComments) {
@@ -461,6 +464,11 @@ TEST_F(FormatTest, UnderstandsMultiLineComments) {
       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
+
+  verifyGoogleFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
+                     "         /* parameter 2 */ aaaaaa,\n"
+                     "         /* parameter 3 */ aaaaaa,\n"
+                     "         /* parameter 4 */ aaaaaa);");
 }
 
 TEST_F(FormatTest, CommentsInStaticInitializers) {