r191654 - clang-format: Improve alignment after 'return'.

Daniel Jasper djasper at google.com
Mon Sep 30 01:29:03 PDT 2013


Author: djasper
Date: Mon Sep 30 03:29:03 2013
New Revision: 191654

URL: http://llvm.org/viewvc/llvm-project?rev=191654&view=rev
Log:
clang-format: Improve alignment after 'return'.

Previously, comments, could totally confuse it.

Before:
  return
             // true if code is one of a or b.
             code == a ||
         code == b;

After:
  return
      // true if code is one of a or b.
      code == a || code == b;

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/TokenAnnotator.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=191654&r1=191653&r2=191654&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Mon Sep 30 03:29:03 2013
@@ -459,10 +459,6 @@ unsigned ContinuationIndenter::moveState
     State.Stack.back().BreakBeforeParameter = false;
   }
 
-  // If return returns a binary expression, align after it.
-  if (Current.is(tok::kw_return) && Current.StartsBinaryExpression)
-    State.Stack.back().LastSpace = State.Column + 7;
-
   // In ObjC method declaration we align on the ":" of parameters, but we need
   // to ensure that we indent parameters on subsequent lines by at least 4.
   if (Current.Type == TT_ObjCMethodSpecifier)
@@ -474,8 +470,7 @@ unsigned ContinuationIndenter::moveState
   // 'return', assignements or opening <({[. The indentation for these cases
   // is special cased.
   bool SkipFirstExtraIndent =
-      Current.is(tok::kw_return) ||
-      (Previous && (Previous->opensScope() ||
+      (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
                     Previous->getPrecedence() == prec::Assignment));
   for (SmallVectorImpl<prec::Level>::const_reverse_iterator
            I = Current.FakeLParens.rbegin(),
@@ -483,9 +478,15 @@ unsigned ContinuationIndenter::moveState
        I != E; ++I) {
     ParenState NewParenState = State.Stack.back();
     NewParenState.ContainsLineBreak = false;
-    NewParenState.Indent =
-        std::max(std::max(State.Column, NewParenState.Indent),
-                 State.Stack.back().LastSpace);
+
+    // 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)
+      NewParenState.Indent =
+          std::max(std::max(State.Column, NewParenState.Indent),
+                   State.Stack.back().LastSpace);
+
     // 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/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=191654&r1=191653&r2=191654&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Sep 30 03:29:03 2013
@@ -853,6 +853,10 @@ public:
 
   /// \brief Parse expressions with the given operatore precedence.
   void parse(int Precedence = 0) {
+    // Skip 'return' as it is not part of a binary expression.
+    while (Current && Current->is(tok::kw_return))
+      next();
+
     if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
       return;
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=191654&r1=191653&r2=191654&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Sep 30 03:29:03 2013
@@ -3103,6 +3103,9 @@ TEST_F(FormatTest, AlignsAfterReturn) {
   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+  verifyFormat("return\n"
+               "    // true if code is one of a or b.\n"
+               "    code == a || code == b;");
 }
 
 TEST_F(FormatTest, BreaksConditionalExpressions) {





More information about the cfe-commits mailing list