]> granicus.if.org Git - clang/commitdiff
clang-format: Correctly identify ObjC Block with return type.
authorDaniel Jasper <djasper@google.com>
Thu, 27 Mar 2014 09:43:54 +0000 (09:43 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 27 Mar 2014 09:43:54 +0000 (09:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204905 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index cb4e4f53b2f3c0994aa03a774801b5b62d430d7a..431be41e0a3f73b59bbfbecef92b0df488088cf3 100644 (file)
@@ -636,16 +636,11 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
           }
           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 {
index 04587244c37280bd03e67e5419c3785bc4cd6f60..42d4a8dc249a213e086a0b839e253f61660fcd56 100644 (file)
@@ -45,6 +45,7 @@ enum TokenType {
   TT_LambdaLSquare,
   TT_LineComment,
   TT_ObjCBlockLParen,
+  TT_ObjCBlockLBrace,
   TT_ObjCDecl,
   TT_ObjCForIn,
   TT_ObjCMethodExpr,
index 369a37b082ee5b2cd2a41b5c3d4bc649cb379399..19bc5a6a96b04a770e1cbef81102346a1829f2e9 100644 (file)
@@ -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;
 
index 3da478c0923bf245698f168034eea8b82046ea8f..fa8ed3828d40f42970ef8456f3fa55ef9dffd481 100644 (file)
@@ -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"