From: Daniel Jasper Date: Thu, 28 May 2015 07:21:50 +0000 (+0000) Subject: clang-format: Lower binding strengths created by the [] created by ObjC X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=94d40371549f13678b1256874e9322747f71c7e9;p=clang clang-format: Lower binding strengths created by the [] created by ObjC method expressions and array literals. They should not bind stronger than regular parentheses or the braces of braced lists. Specific test case in JavaScript: Before: var aaaaa: List< SomeThing> = [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()]; After: var aaaaa: List = [ new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB() ]; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@238400 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 98f5709b90..2d8dbdbad5 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -260,6 +260,7 @@ private: Left->ParentBracket = Contexts.back().ContextKind; FormatToken *Parent = Left->getPreviousNonComment(); bool StartsObjCMethodExpr = + Style.Language == FormatStyle::LK_Cpp && Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && CurrentToken->isNot(tok::l_brace) && (!Parent || @@ -268,19 +269,24 @@ private: Parent->isUnaryOperator() || Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) || getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); - ScopedContextCreator ContextCreator(*this, tok::l_square, 10); - Contexts.back().IsExpression = true; bool ColonFound = false; - if (StartsObjCMethodExpr) { - Contexts.back().ColonIsObjCMethodExpr = true; - Left->Type = TT_ObjCMethodExpr; - } else if (Parent && Parent->is(tok::at)) { - Left->Type = TT_ArrayInitializerLSquare; - } else if (Left->is(TT_Unknown)) { - Left->Type = TT_ArraySubscriptLSquare; + unsigned BindingIncrease = 1; + if (Left->is(TT_Unknown)) { + if (StartsObjCMethodExpr) { + Left->Type = TT_ObjCMethodExpr; + } else if (Parent && Parent->isOneOf(tok::at, tok::equal, tok::comma)) { + Left->Type = TT_ArrayInitializerLSquare; + } else { + BindingIncrease = 10; + Left->Type = TT_ArraySubscriptLSquare; + } } + ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); + Contexts.back().IsExpression = true; + Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; + while (CurrentToken) { if (CurrentToken->is(tok::r_square)) { if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && @@ -321,10 +327,8 @@ private: } ColonFound = true; } - if (CurrentToken->is(tok::comma) && - Style.Language != FormatStyle::LK_Proto && - (Left->is(TT_ArraySubscriptLSquare) || - (Left->is(TT_ObjCMethodExpr) && !ColonFound))) + if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) && + !ColonFound) Left->Type = TT_ArrayInitializerLSquare; FormatToken *Tok = CurrentToken; if (!consumeToken()) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 1c100c3b9b..09e514e7cd 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -7585,9 +7585,9 @@ TEST_F(FormatTest, ObjCArrayLiterals) { " index:(NSUInteger)index\n" " nonDigitAttributes:\n" " (NSDictionary *)noDigitAttributes;"); - verifyFormat( - "[someFunction someLooooooooooooongParameter:\n" - " @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];"); + verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" + " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" + "]];"); } TEST_F(FormatTest, ReformatRegionAdjustsIndent) { diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index a06daac24e..a536926c73 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -239,6 +239,13 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) { "}"); } +TEST_F(FormatTestJS, ArrayLiterals) { + verifyFormat("var aaaaa: List = [\n" + " new SomeThingAAAAAAAAAAAA(),\n" + " new SomeThingBBBBBBBBB()\n" + "];"); +} + TEST_F(FormatTestJS, FunctionLiterals) { verifyFormat("doFoo(function() {});"); verifyFormat("doFoo(function() { return 1; });");