<div dir="ltr">\o/<div class="gmail_extra"><br>On Wed, Sep 4, 2013 at 6:25 AM, Manuel Klimek <span dir="ltr"><<a href="mailto:klimek@google.com" target="_blank">klimek@google.com</a>></span> wrote:<br><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: klimek<br>
Date: Wed Sep  4 08:25:30 2013<br>
New Revision: 189932<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=189932&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=189932&view=rev</a><br>
Log:<br>
Implement parsing of blocks (^{ ... }) in the unwrapped line parser.<br>
<br>
This patch makes sure we produce the right number of unwrapped lines,<br>
a follow-up patch will make the whitespace formatting consistent.<br>
<br>
Before:<br>
 void f() {<br>
   int i = {[operation setCompletionBlock : ^{ [self onOperationDone];<br>
 }]<br>
 }<br>
 ;<br>
 }<br>
<br>
After:<br>
 void f() {<br>
   int i = {[operation setCompletionBlock : ^{<br>
     [self onOperationDone];<br>
   }] };<br>
 }<br>
<br>
Modified:<br>
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
    cfe/trunk/lib/Format/UnwrappedLineParser.h<br>
    cfe/trunk/unittests/Format/FormatTest.cpp<br>
<br>
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=189932&r1=189931&r2=189932&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=189932&r1=189931&r2=189932&view=diff</a><br>

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

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

==============================================================================<br>
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)<br>
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Sep  4 08:25:30 2013<br>
@@ -6311,5 +6311,16 @@ TEST_F(FormatTest, FormatsLambdas) {<br>
       "}\n");<br>
 }<br>
<br>
+TEST_F(FormatTest, FormatsBlocks) {<br>
+  // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how<br>
+  // it would ideally look.<br>
+  verifyFormat("[operation setCompletionBlock:^{\n"<br>
+               "  [self onOperationDone];\n"<br>
+               "}];\n");<br></blockquote><div><br></div><div>This looks good, but in some styles (e.g. Google), block contents should be indented 4 spaces not 2.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+  verifyFormat("int i = {[operation setCompletionBlock : ^{\n"<br>
+               "  [self onOperationDone];\n"<br>
+               "}] };\n");<br></blockquote><div><br></div><div>There should be no spaces around ':' , and the surrounding {} looks like invalid syntax (it's like `int i = { f() };`.</div><div><br></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br>
+<br>
 } // end namespace tooling<br>
 } // end namespace clang<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>