[cfe-commits] r172070 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Thu Jan 10 06:36:46 PST 2013


Author: klimek
Date: Thu Jan 10 08:36:46 2013
New Revision: 172070

URL: http://llvm.org/viewvc/llvm-project?rev=172070&view=rev
Log:
Fixes formatting of function calls etc inside an initializer list.

We're now formatting (column limit 25):
int x = {
  avariable,
  b(alongervariable) };

This also fixes:
Aaa({
  int i;
}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
                                    ccccccccccccccccc));
... where we would previously break after the '},'.

Putting the closing curly into an extra line when there's a break
directly after the first curly will be done in a subsequent patch.

Paired with djasper.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172070&r1=172069&r2=172070&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 08:36:46 2013
@@ -282,11 +282,6 @@
       unsigned WhitespaceStartColumn = State.Column;
       if (Current.is(tok::r_brace)) {
         State.Column = Line.Level * 2;
-      } else if (Previous.is(tok::l_brace)) {
-        // FIXME: This does not work with nested static initializers.
-        // Implement a better handling for static initializers and similar
-        // constructs.
-        State.Column = Line.Level * 2 + 2;
       } else if (Current.is(tok::string_literal) &&
                  Previous.is(tok::string_literal)) {
         State.Column = State.Column - Previous.FormatTok.TokenLength;
@@ -309,7 +304,9 @@
         State.Column = State.Indent[ParenLevel];
       }
 
-      State.StartOfLineLevel = ParenLevel + 1;
+      // A line starting with a closing brace is assumed to be correct for the
+      // same level as before the opening brace.
+      State.StartOfLineLevel = ParenLevel + (Current.is(tok::r_brace) ? 0 : 1);
 
       if (RootToken.is(tok::kw_for))
         State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
@@ -345,6 +342,7 @@
            Previous.is(tok::kw_return)))
         State.Indent[ParenLevel] = State.Column + Spaces;
       if (Previous.is(tok::l_paren) ||
+          Previous.is(tok::l_brace) ||
           State.NextToken->Parent->Type == TT_TemplateOpener)
         State.Indent[ParenLevel] = State.Column;
 
@@ -368,14 +366,19 @@
     if (Current.is(tok::lessless) && State.FirstLessLess[ParenLevel] == 0)
       State.FirstLessLess[ParenLevel] = State.Column;
 
-    State.Column += Current.FormatTok.TokenLength;
-
     // If we encounter an opening (, [, { or <, we add a level to our stacks to
     // prepare for the following tokens.
     if (Current.is(tok::l_paren) || Current.is(tok::l_square) ||
         Current.is(tok::l_brace) ||
         State.NextToken->Type == TT_TemplateOpener) {
-      State.Indent.push_back(4 + State.LastSpace.back());
+      if (Current.is(tok::l_brace)) {
+        // FIXME: This does not work with nested static initializers.
+        // Implement a better handling for static initializers and similar
+        // constructs.
+        State.Indent.push_back(Line.Level * 2 + 2);
+      } else {
+        State.Indent.push_back(4 + State.LastSpace.back());
+      }
       State.LastSpace.push_back(State.LastSpace.back());
       State.FirstLessLess.push_back(0);
     }
@@ -389,10 +392,13 @@
       State.LastSpace.pop_back();
       State.FirstLessLess.pop_back();
     }
+
     if (State.NextToken->Children.empty())
       State.NextToken = NULL;
     else
       State.NextToken = &State.NextToken->Children[0];
+
+    State.Column += Current.FormatTok.TokenLength;
   }
 
   /// \brief Calculate the penalty for splitting after the token at \p Index.
@@ -847,7 +853,6 @@
       }
     }
     Current.CanBreakBefore = Current.MustBreakBefore || canBreakBefore(Current);
-
     if (!Current.Children.empty())
       calculateExtraInformation(Current.Children[0]);
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172070&r1=172069&r2=172070&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 10 08:36:46 2013
@@ -1117,6 +1117,21 @@
 
 }
 
+TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
+  verifyFormat(
+      "int x = {\n"
+      "  avariable,\n"
+      "  b(alongervariable) };", getLLVMStyleWithColumns(25));
+}
+
+TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) {
+  verifyFormat(
+      "Aaa({\n"
+      "  int i;\n"
+      "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+      "                                    ccccccccccccccccc));");
+}
+
 //===----------------------------------------------------------------------===//
 // Objective-C tests.
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list