r189932 - Implement parsing of blocks (^{ ... }) in the unwrapped line parser.

Manuel Klimek klimek at google.com
Wed Sep 4 06:25:30 PDT 2013


Author: klimek
Date: Wed Sep  4 08:25:30 2013
New Revision: 189932

URL: http://llvm.org/viewvc/llvm-project?rev=189932&view=rev
Log:
Implement parsing of blocks (^{ ... }) in the unwrapped line parser.

This patch makes sure we produce the right number of unwrapped lines,
a follow-up patch will make the whitespace formatting consistent.

Before:
 void f() {
   int i = {[operation setCompletionBlock : ^{ [self onOperationDone];
 }]
 }
 ;
 }

After:
 void f() {
   int i = {[operation setCompletionBlock : ^{
     [self onOperationDone];
   }] };
 }

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

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=189932&r1=189931&r2=189932&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Sep  4 08:25:30 2013
@@ -346,6 +346,20 @@ void UnwrappedLineParser::parseBlock(boo
   Line->Level = InitialLevel;
 }
 
+void UnwrappedLineParser::parseChildBlock() {
+  FormatTok->BlockKind = BK_Block;
+  nextToken();
+  {
+    ScopedLineState LineState(*this);
+    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
+                                            /*MustBeDeclaration=*/false);
+    Line->Level += 1;
+    parseLevel(/*HasOpeningBrace=*/true);
+    Line->Level -= 1;
+  }
+  nextToken();
+}
+
 void UnwrappedLineParser::parsePPDirective() {
   assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
   ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
@@ -591,6 +605,12 @@ void UnwrappedLineParser::parseStructura
     case tok::l_paren:
       parseParens();
       break;
+    case tok::caret:
+      nextToken();
+      if (FormatTok->is(tok::l_brace)) {
+        parseChildBlock();
+      }
+      break;
     case tok::l_brace:
       if (!tryToParseBracedList()) {
         // A block outside of parentheses must be the last part of a
@@ -674,17 +694,7 @@ void UnwrappedLineParser::tryToParseLamb
         break;
     }
   }
-  FormatTok->BlockKind = BK_Block;
-  nextToken();
-  {
-    ScopedLineState LineState(*this);
-    ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
-                                            /*MustBeDeclaration=*/false);
-    Line->Level += 1;
-    parseLevel(/*HasOpeningBrace=*/true);
-    Line->Level -= 1;
-  }
-  nextToken();
+  parseChildBlock();
 }
 
 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
@@ -741,9 +751,15 @@ void UnwrappedLineParser::parseBracedLis
     // here, otherwise our bail-out scenarios below break. The better solution
     // might be to just implement a more or less complete expression parser.
     switch (FormatTok->Tok.getKind()) {
-      case tok::l_square:
-        tryToParseLambda();
-        break;
+    case tok::caret:
+      nextToken();
+      if (FormatTok->is(tok::l_brace)) {
+        parseChildBlock();
+      }
+      break;
+    case tok::l_square:
+      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).

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=189932&r1=189931&r2=189932&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Wed Sep  4 08:25:30 2013
@@ -66,6 +66,7 @@ private:
   void parseFile();
   void parseLevel(bool HasOpeningBrace);
   void parseBlock(bool MustBeDeclaration, bool AddLevel = true);
+  void parseChildBlock();
   void parsePPDirective();
   void parsePPDefine();
   void parsePPIf();

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=189932&r1=189931&r2=189932&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Sep  4 08:25:30 2013
@@ -6311,5 +6311,16 @@ TEST_F(FormatTest, FormatsLambdas) {
       "}\n");
 }
 
+TEST_F(FormatTest, FormatsBlocks) {
+  // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
+  // it would ideally look.
+  verifyFormat("[operation setCompletionBlock:^{\n"
+               "  [self onOperationDone];\n"
+               "}];\n");
+  verifyFormat("int i = {[operation setCompletionBlock : ^{\n"
+               "  [self onOperationDone];\n"
+               "}] };\n");
+}
+
 } // end namespace tooling
 } // end namespace clang





More information about the cfe-commits mailing list