]> granicus.if.org Git - clang/commitdiff
clang-format: Improve indentation of comments in expressions.
authorDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 12:31:14 +0000 (12:31 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 12:31:14 +0000 (12:31 +0000)
Before:
  int i = (a)
              // comment
          + b;
  return aaaa == bbbb
                 // comment
             ? aaaa
             : bbbb;
After:
  int i = (a)
          // comment
          + b;
  return aaaa == bbbb
             // comment
             ? aaaa
             : bbbb;

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

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

index 9e6014a6837c736563b6de68e03fe744adb738b3..7fdc1af09b7e2b5c4bc268d949998fb85f4689a1 100644 (file)
@@ -594,7 +594,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
   if (NextNonComment->Type == TT_CtorInitializerComma)
     return State.Stack.back().Indent;
   if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
-      Current.isNot(tok::colon))
+      !Current.isOneOf(tok::colon, tok::comment))
     return ContinuationIndent;
   if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
       PreviousNonComment->isNot(tok::r_brace))
index 48ce19e1323bf39073bfffbfc1db7dd564d1c571..cb945c6161b5f6029d0629835be37aa30dba9b96 100644 (file)
@@ -1186,9 +1186,12 @@ private:
     if (Precedence > prec::Unknown)
       Start->StartsBinaryExpression = true;
     if (Current) {
-      ++Current->Previous->FakeRParens;
+      FormatToken *Previous = Current->Previous;
+      if (Previous->is(tok::comment) && Previous->Previous)
+        Previous = Previous->Previous;
+      ++Previous->FakeRParens;
       if (Precedence > prec::Unknown)
-        Current->Previous->EndsBinaryExpression = true;
+        Previous->EndsBinaryExpression = true;
     }
   }
 
index b8ab1df139b5e5dd562de208418046e170fb1aae..d2e577aacbd023f2c0f2d90d6bbf9a73d0c4f213 100644 (file)
@@ -3266,6 +3266,10 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
                "              > ccccc) {\n"
                "}",
                Style);
+  verifyFormat("return (a)\n"
+               "       // comment\n"
+               "       + b;",
+               Style);
 
   // Forced by comments.
   verifyFormat(
@@ -4070,6 +4074,10 @@ TEST_F(FormatTest, BreaksConditionalExpressions) {
                "          aaaaaaaaa\n"
                "      ? b\n"
                "      : c);");
+  verifyFormat("return aaaa == bbbb\n"
+               "           // comment\n"
+               "           ? aaaa\n"
+               "           : bbbb;");
   verifyFormat(
       "unsigned Indent =\n"
       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"