]> granicus.if.org Git - clang/commitdiff
clang-format: Fix bug with comments between non-trival parameters.
authorDaniel Jasper <djasper@google.com>
Tue, 7 Oct 2014 14:45:34 +0000 (14:45 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 7 Oct 2014 14:45:34 +0000 (14:45 +0000)
Before:
  SomeFunction(a, a,
               // comment
                      b + x);

After:
  SomeFunction(a, a,
               // comment
               b + x);

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

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

index c48df870416bb2c055d213d87627af87c91f8bcd..9d72994c1bfbdbb9d88eb234434e3ca896ca2849 100644 (file)
@@ -487,7 +487,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
   if (!State.NextToken || !State.NextToken->Previous)
     return 0;
   FormatToken &Current = *State.NextToken;
-  const FormatToken &Previous = *State.NextToken->Previous;
+  const FormatToken &Previous = *Current.Previous;
   // If we are continuing an expression, we want to use the continuation indent.
   unsigned ContinuationIndent =
       std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
@@ -717,7 +717,8 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
     // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
     // builder type call after 'return'. If such a call is line-wrapped, we
     // commonly just want to indent from the start of the line.
-    if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
+    if (!Current.isTrailingComment() &&
+        (!Previous || Previous->isNot(tok::kw_return) || *I > 0))
       NewParenState.Indent =
           std::max(std::max(State.Column, NewParenState.Indent),
                    State.Stack.back().LastSpace);
@@ -756,7 +757,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
     // other expression, unless the indentation needs to be skipped.
     if (*I == prec::Conditional ||
         (!SkipFirstExtraIndent && *I > prec::Assignment &&
-         !Style.BreakBeforeBinaryOperators))
+         !Current.isTrailingComment() && !Style.BreakBeforeBinaryOperators))
       NewParenState.Indent += Style.ContinuationIndentWidth;
     if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
       NewParenState.BreakBeforeParameter = false;
index 0842711749d3768a461eb1e8b6b47c541f5bb7f7..29f494603c654d517c18dbbcb99119db508c6b09 100644 (file)
@@ -832,6 +832,12 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
                "    // Comment inside a statement.\n"
                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
+  verifyFormat("SomeFunction(a,\n"
+               "             // comment\n"
+               "             b + x);");
+  verifyFormat("SomeFunction(a, a,\n"
+               "             // comment\n"
+               "             b + x);");
   verifyFormat(
       "bool aaaaaaaaaaaaa = // comment\n"
       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"