r205527 - clang-format: Prefer an additional line-break over hanging indent.

Daniel Jasper djasper at google.com
Thu Apr 3 05:00:34 PDT 2014


Author: djasper
Date: Thu Apr  3 07:00:33 2014
New Revision: 205527

URL: http://llvm.org/viewvc/llvm-project?rev=205527&view=rev
Log:
clang-format: Prefer an additional line-break over hanging indent.

Don't allow the RHS of an operator to be split over multiple
lines unless there is a line-break right after the operator.

Before:
  if (aaaa && bbbbb || // break
                  cccc) {
  }

After:
  if (aaaa &&
      bbbbb || // break
          cccc) {
  }

In most cases, this seems to increase readability.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=205527&r1=205526&r2=205527&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Apr  3 07:00:33 2014
@@ -609,6 +609,18 @@ unsigned ContinuationIndenter::moveState
           std::max(std::max(State.Column, NewParenState.Indent),
                    State.Stack.back().LastSpace);
 
+    // Don't allow the RHS of an operator to be split over multiple lines unless
+    // there is a line-break right after the operator.
+    // Exclude relational operators, as there, it is always more desirable to
+    // have the LHS 'left' of the RHS.
+    // FIXME: Implement this for '<<' and BreakBeforeBinaryOperators.
+    if (!Newline && Previous && Previous->Type == TT_BinaryOperator &&
+        !Previous->isOneOf(tok::lessless, tok::question, tok::colon) &&
+        Previous->getPrecedence() > prec::Assignment &&
+        Previous->getPrecedence() != prec::Relational &&
+        !Style.BreakBeforeBinaryOperators)
+      NewParenState.NoLineBreak = true;
+
     // Do not indent relative to the fake parentheses inserted for "." or "->".
     // This is a special case to make the following to statements consistent:
     //   OuterFunction(InnerFunctionCall( // break

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=205527&r1=205526&r2=205527&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Apr  3 07:00:33 2014
@@ -2889,8 +2889,9 @@ TEST_F(FormatTest, ExpressionIndentation
                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
   verifyFormat("if () {\n"
-               "} else if (aaaaa && bbbbb > // break\n"
-               "                        ccccc) {\n"
+               "} else if (aaaaa &&\n"
+               "           bbbbb > // break\n"
+               "               ccccc) {\n"
                "}");
 
   // Presence of a trailing comment used to change indentation of b.





More information about the cfe-commits mailing list