r174822 - Formatter: Detect ObjC array literals.

Nico Weber nicolasweber at gmx.de
Sat Feb 9 18:08:05 PST 2013


Author: nico
Date: Sat Feb  9 20:08:05 2013
New Revision: 174822

URL: http://llvm.org/viewvc/llvm-project?rev=174822&view=rev
Log:
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.


Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/lib/Format/TokenAnnotator.h
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=174822&r1=174821&r2=174822&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sat Feb  9 20:08:05 2013
@@ -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::spaceRequiredBetwee
     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))

Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=174822&r1=174821&r2=174822&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Sat Feb  9 20:08:05 2013
@@ -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,

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174822&r1=174821&r2=174822&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sat Feb  9 20:08:05 2013
@@ -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("@{");
 
 }





More information about the cfe-commits mailing list