diff options
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 15 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.h | 3 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 7 |
3 files changed, 19 insertions, 6 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index db059018c26..641ebd704a4 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -187,8 +187,8 @@ public: ScopedContextCreator ContextCreator(*this, 10); // A '[' could be an index subscript (after an indentifier or after - // ')' or ']'), or it could be the start of an Objective-C method - // expression. + // ')' or ']'), it could be the start of an Objective-C method + // expression, or it could the the start of an Objective-C array literal. AnnotatedToken *Left = CurrentToken->Parent; AnnotatedToken *Parent = getPreviousToken(*Left); bool StartsObjCMethodExpr = @@ -197,10 +197,13 @@ public: Parent->is(tok::kw_throw) || isUnaryOperator(*Parent) || getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) > prec::Unknown; + bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at); if (StartsObjCMethodExpr) { Contexts.back().ColonIsObjCMethodExpr = true; Left->Type = TT_ObjCMethodExpr; + } else if (StartsObjCArrayLiteral) { + Left->Type = TT_ObjCArrayLiteral; } while (CurrentToken != NULL) { @@ -221,6 +224,8 @@ public: (Parent->is(tok::star) || Parent->is(tok::amp)) && Parent->Type == TT_PointerOrReference) Parent->Type = TT_BinaryOperator; + } else if (StartsObjCArrayLiteral) { + CurrentToken->Type = TT_ObjCArrayLiteral; } Left->MatchingParen = CurrentToken; CurrentToken->MatchingParen = Left; @@ -926,8 +931,10 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, return Right.FormatTok.Tok.isLiteral() || Style.PointerBindsToType; if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; - if (Left.is(tok::l_square) || Right.is(tok::r_square)) - return false; + if (Left.is(tok::l_square)) + return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square); + if (Right.is(tok::r_square)) + return Right.Type == TT_ObjCArrayLiteral; if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr) return false; if (Left.is(tok::period) || Right.is(tok::period)) diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 85e41021c21..8815d799b09 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -35,10 +35,11 @@ enum TokenType { TT_CtorInitializerColon, TT_ImplicitStringLiteral, TT_LineComment, + TT_ObjCArrayLiteral, TT_ObjCBlockLParen, TT_ObjCDecl, - TT_ObjCMethodSpecifier, TT_ObjCMethodExpr, + TT_ObjCMethodSpecifier, TT_ObjCProperty, TT_ObjCSelectorName, TT_OverloadedOperator, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8d72df2754d..4e44f5e6509 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2540,8 +2540,13 @@ TEST_F(FormatTest, ObjCLiterals) { verifyFormat("NSNumber *favoriteColor = @(Green);"); verifyFormat("NSString *path = @(getenv(\"PATH\"));"); - // FIXME: Array and dictionary literals need more work. verifyFormat("@["); + verifyFormat("@[]"); + verifyFormat( + "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); + verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); + + // FIXME: Array and dictionary literals need more work. verifyFormat("@{"); } |