]> granicus.if.org Git - clang/commitdiff
Formatter: Detect ObjC array literals.
authorNico Weber <nicolasweber@gmx.de>
Sun, 10 Feb 2013 02:08:05 +0000 (02:08 +0000)
committerNico Weber <nicolasweber@gmx.de>
Sun, 10 Feb 2013 02:08:05 +0000 (02:08 +0000)
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
lib/Format/TokenAnnotator.h
unittests/Format/FormatTest.cpp

index db059018c26fc1900b241520f9dd75dbb88eccf1..641ebd704a4cbb82765c99e3d4518fd6405366bd 100644 (file)
@@ -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))
index 85e41021c213823c2c93794ab92c213a3677bcbc..8815d799b09fe77ec3c30fdb9709fe244a83653e 100644 (file)
@@ -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,
index 8d72df2754d8e04ef3972ac50c9ae5677d6d28f0..4e44f5e65091dc209cc9f7debcb39b2242066896 100644 (file)
@@ -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("@{");
 
 }