]> granicus.if.org Git - clang/commitdiff
clang-format: Improve alignment after 'return'.
authorDaniel Jasper <djasper@google.com>
Mon, 30 Sep 2013 08:29:03 +0000 (08:29 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 30 Sep 2013 08:29:03 +0000 (08:29 +0000)
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;

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

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

index b3888c1c1ae5b4cd37d681a89cd69ec6a0da493a..cc12fc9d81349fba497775b7e0cc510cf5acd1c8 100644 (file)
@@ -459,10 +459,6 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
     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::moveStateToNextToken(LineState &State,
   // '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::moveStateToNextToken(LineState &State,
        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
index 6772c894dc0278b7f62867088f22ca9cc6ba6a32..220da707b156521c90579131a711b1dc937c3ab7 100644 (file)
@@ -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;
 
index 833a87c4fd03055187938f70098ff77f82c43d73..dfdeae1a1e06da6c8f77acc584f83de0ea83f345 100644 (file)
@@ -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) {