r204905 - clang-format: Correctly identify ObjC Block with return type.

Daniel Jasper djasper at google.com
Thu Mar 27 02:43:55 PDT 2014


Author: djasper
Date: Thu Mar 27 04:43:54 2014
New Revision: 204905

URL: http://llvm.org/viewvc/llvm-project?rev=204905&view=rev
Log:
clang-format: Correctly identify ObjC Block with return type.

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=204905&r1=204904&r2=204905&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Mar 27 04:43:54 2014
@@ -636,16 +636,11 @@ unsigned ContinuationIndenter::moveState
           }
           State.Stack.pop_back();
         }
-        bool IsObjCBlock =
-            Previous &&
-            (Previous->is(tok::caret) ||
-             (Previous->is(tok::r_paren) && Previous->MatchingParen &&
-              Previous->MatchingParen->Previous &&
-              Previous->MatchingParen->Previous->is(tok::caret)));
         // For some reason, ObjC blocks are indented like continuations.
         NewIndent =
-            State.Stack.back().LastSpace +
-            (IsObjCBlock ? Style.ContinuationIndentWidth : Style.IndentWidth);
+            State.Stack.back().LastSpace + (Current.Type == TT_ObjCBlockLBrace
+                                                ? Style.ContinuationIndentWidth
+                                                : Style.IndentWidth);
         ++NewIndentLevel;
         BreakBeforeParameter = true;
       } else {

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=204905&r1=204904&r2=204905&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Thu Mar 27 04:43:54 2014
@@ -45,6 +45,7 @@ enum TokenType {
   TT_LambdaLSquare,
   TT_LineComment,
   TT_ObjCBlockLParen,
+  TT_ObjCBlockLBrace,
   TT_ObjCDecl,
   TT_ObjCForIn,
   TT_ObjCMethodExpr,

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=204905&r1=204904&r2=204905&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Mar 27 04:43:54 2014
@@ -76,9 +76,6 @@ private:
   bool parseParens(bool LookForDecls = false) {
     if (CurrentToken == NULL)
       return false;
-    bool AfterCaret = Contexts.back().CaretFound;
-    Contexts.back().CaretFound = false;
-
     ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
 
     // FIXME: This is a bit of a hack. Do better.
@@ -109,7 +106,7 @@ private:
                Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
       // This is a parameter list of a lambda expression.
       Contexts.back().IsExpression = false;
-    } else if (AfterCaret) {
+    } else if (Contexts[Contexts.size() - 2].CaretFound) {
       // This is the parameter list of an ObjC block.
       Contexts.back().IsExpression = false;
     } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
@@ -273,6 +270,11 @@ private:
   bool parseBrace() {
     if (CurrentToken != NULL) {
       FormatToken *Left = CurrentToken->Previous;
+
+      if (Contexts.back().CaretFound)
+        Left->Type = TT_ObjCBlockLBrace;
+      Contexts.back().CaretFound = false;
+
       ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
       Contexts.back().ColonIsDictLiteral = true;
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=204905&r1=204904&r2=204905&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 27 04:43:54 2014
@@ -8167,6 +8167,10 @@ TEST_F(FormatTest, FormatsBlocks) {
                "    secondBlock:^(Bar *b) {\n"
                "        // ...\n"
                "        int i;\n"
+               "    }\n"
+               "    thirdBlock:^Foo(Bar *b) {\n"
+               "        // ...\n"
+               "        int i;\n"
                "    }];");
 
   verifyFormat("f(^{\n"





More information about the cfe-commits mailing list