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

Nico Weber thakis at chromium.org
Wed Sep 4 07:29:08 PDT 2013


\o/

On Wed, Sep 4, 2013 at 6:25 AM, Manuel Klimek <klimek at google.com> wrote:

> 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");
>

This looks good, but in some styles (e.g. Google), block contents should be
indented 4 spaces not 2.


> +  verifyFormat("int i = {[operation setCompletionBlock : ^{\n"
> +               "  [self onOperationDone];\n"
> +               "}] };\n");
>

There should be no spaces around ':' , and the surrounding {} looks like
invalid syntax (it's like `int i = { f() };`.

+}
> +
>  } // end namespace tooling
>  } // end namespace clang
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130904/b8409b8f/attachment.html>


More information about the cfe-commits mailing list