From 1146e7b61d505d216ffe937388b0837d39bf4b56 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 27 Mar 2014 09:43:54 +0000 Subject: [PATCH] clang-format: Correctly identify ObjC Block with return type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204905 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 11 +++-------- lib/Format/FormatToken.h | 1 + lib/Format/TokenAnnotator.cpp | 10 ++++++---- unittests/Format/FormatTest.cpp | 4 ++++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index cb4e4f53b2..431be41e0a 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -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 { diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 04587244c3..42d4a8dc24 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -45,6 +45,7 @@ enum TokenType { TT_LambdaLSquare, TT_LineComment, TT_ObjCBlockLParen, + TT_ObjCBlockLBrace, TT_ObjCDecl, TT_ObjCForIn, TT_ObjCMethodExpr, diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 369a37b082..19bc5a6a96 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3da478c092..fa8ed3828d 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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" -- 2.40.0