From 051860ee770bf83c3e66ab893be3642bb8bc2680 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 10 Feb 2013 02:08:05 +0000 Subject: [PATCH] Formatter: Detect ObjC array literals. Use this to add a space after "@[" and before "]" for now. Later, I want to use this to format multi-line array literals nicer, too. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174822 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 15 +++++++++++---- lib/Format/TokenAnnotator.h | 3 ++- unittests/Format/FormatTest.cpp | 7 ++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index db059018c2..641ebd704a 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/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/lib/Format/TokenAnnotator.h b/lib/Format/TokenAnnotator.h index 85e41021c2..8815d799b0 100644 --- a/lib/Format/TokenAnnotator.h +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8d72df2754..4e44f5e650 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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("@{"); } -- 2.40.0