r189924 - Fix layout of lambda captures.

Manuel Klimek klimek at google.com
Wed Sep 4 01:20:48 PDT 2013


Author: klimek
Date: Wed Sep  4 03:20:47 2013
New Revision: 189924

URL: http://llvm.org/viewvc/llvm-project?rev=189924&view=rev
Log:
Fix layout of lambda captures.

Before:
 int c = [ &, &a, a]{
   [ =, c, &d]{
     return b++;
   }();
 }();

After:
 int c = [&, &a, a] {
   [=, c, &d] {
     return b++;
   }();
 }();

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

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=189924&r1=189923&r2=189924&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Sep  4 03:20:47 2013
@@ -624,7 +624,9 @@ private:
         Current.Type = determineIncrementUsage(Current);
       } else if (Current.is(tok::exclaim)) {
         Current.Type = TT_UnaryOperator;
-      } else if (Current.isBinaryOperator()) {
+      } else if (Current.isBinaryOperator() &&
+                 (!Current.Previous ||
+                  Current.Previous->isNot(tok::l_square))) {
         Current.Type = TT_BinaryOperator;
       } else if (Current.is(tok::comment)) {
         if (Current.TokenText.startswith("//"))
@@ -1188,6 +1190,8 @@ bool TokenAnnotator::spaceRequiredBetwee
     return false;
   if (Right.is(tok::ellipsis))
     return false;
+  if (Left.is(tok::l_square) && Right.is(tok::amp))
+    return false;
   if (Right.Type == TT_PointerOrReference)
     return Left.Tok.isLiteral() ||
            ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
@@ -1236,7 +1240,8 @@ bool TokenAnnotator::spaceRequiredBetwee
     return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
            (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
   if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
-      Right.is(tok::l_brace) && Right.getNextNonComment())
+      Right.is(tok::l_brace) && Right.getNextNonComment() &&
+      Right.BlockKind != BK_Block)
     return false;
   if (Left.is(tok::period) || Right.is(tok::period))
     return false;

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=189924&r1=189923&r2=189924&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Sep  4 03:20:47 2013
@@ -278,13 +278,11 @@ void UnwrappedLineParser::calculateBrace
       if (!LBraceStack.empty()) {
         if (LBraceStack.back()->BlockKind == BK_Unknown) {
           // If there is a comma, semicolon or right paren after the closing
-          // brace, we assume this is a braced initializer list.
-
-          // FIXME: Note that this currently works only because we do not
-          // use the brace information while inside a braced init list.
-          // Thus, if the parent is a braced init list, we consider all
-          // brace blocks inside it braced init list. That works good enough
-          // for now, but we will need to fix it to correctly handle lambdas.
+          // brace, we assume this is a braced initializer list.  Note that
+          // regardless how we mark inner braces here, we will overwrite the
+          // BlockKind later if we parse a braced list (where all blocks inside
+          // are by default braced lists), or when we explicitly detect blocks
+          // (for example while parsing lambdas).
           //
           // We exclude + and - as they can be ObjC visibility modifiers.
           if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren,
@@ -315,12 +313,13 @@ void UnwrappedLineParser::calculateBrace
     }
     Tok = NextTok;
     Position += ReadTokens;
-  } while (Tok->Tok.isNot(tok::eof));
+  } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
   // Assume other blocks for all unclosed opening braces.
   for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
     if (LBraceStack[i]->BlockKind == BK_Unknown)
       LBraceStack[i]->BlockKind = BK_Block;
   }
+
   FormatTok = Tokens->setPosition(StoredPosition);
 }
 
@@ -675,6 +674,7 @@ void UnwrappedLineParser::tryToParseLamb
         break;
     }
   }
+  FormatTok->BlockKind = BK_Block;
   nextToken();
   {
     ScopedLineState LineState(*this);
@@ -745,6 +745,9 @@ void UnwrappedLineParser::parseBracedLis
         tryToParseLambda();
         break;
     case tok::l_brace:
+      // Assume there are no blocks inside a braced init list apart
+      // from the ones we explicitly parse out (like lambdas).
+      FormatTok->BlockKind = BK_BracedInit;
       parseBracedList();
       break;
     case tok::r_brace:

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=189924&r1=189923&r2=189924&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Sep  4 03:20:47 2013
@@ -6262,37 +6262,37 @@ TEST_F(FormatTest, FormatsLambdas) {
   // parsing of the unwrapped lines doesn't regress.
   verifyFormat(
       "int c = [b]() mutable {\n"
-      "  return [&b]{\n"
+      "  return [&b] {\n"
       "    return b++;\n"
       "  }();\n"
       "}();\n");
   verifyFormat(
-      "int c = [&]{\n"
-      "  [ = ]{\n"
+      "int c = [&] {\n"
+      "  [=] {\n"
       "    return b++;\n"
       "  }();\n"
       "}();\n");
   verifyFormat(
-      "int c = [ &, &a, a]{\n"
-      "  [ =, c, &d]{\n"
+      "int c = [&, &a, a] {\n"
+      "  [=, c, &d] {\n"
       "    return b++;\n"
       "  }();\n"
       "}();\n");
   verifyFormat(
-      "int c = [&a, &a, a]{\n"
-      "  [ =, a, b, &c]{\n"
+      "int c = [&a, &a, a] {\n"
+      "  [=, a, b, &c] {\n"
       "    return b++;\n"
       "  }();\n"
       "}();\n");
   verifyFormat(
-      "auto c = {[&a, &a, a]{\n"
-      "  [ =, a, b, &c]{\n"
+      "auto c = {[&a, &a, a] {\n"
+      "  [=, a, b, &c] {\n"
       "    return b++;\n"
       "  }();\n"
       "} }\n");
   verifyFormat(
-      "auto c = {[&a, &a, a]{\n"
-      "  [ =, a, b, &c]{\n"
+      "auto c = {[&a, &a, a] {\n"
+      "  [=, a, b, &c] {\n"
       "  }();\n"
       "} }\n");
 }





More information about the cfe-commits mailing list