clang-format: [ObjC] Fix method expression detection.
authorDaniel Jasper <djasper@google.com>
Thu, 16 Oct 2014 08:38:51 +0000 (08:38 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 16 Oct 2014 08:38:51 +0000 (08:38 +0000)
Before:
  return (a)[foo bar : baz];

After:
  return (a)[foo bar:baz];

llvm-svn: 219919

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index 1f8ac5e..a8e2c55 100644 (file)
@@ -273,8 +273,16 @@ private:
       }
       if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
         return false;
-      if (CurrentToken->is(tok::colon))
+      if (CurrentToken->is(tok::colon)) {
+        if (Left->Type == TT_ArraySubscriptLSquare) {
+          Left->Type = TT_ObjCMethodExpr;
+          StartsObjCMethodExpr = true;
+          Contexts.back().ColonIsObjCMethodExpr = true;
+          if (Parent && Parent->is(tok::r_paren))
+            Parent->Type = TT_CastRParen;
+        }
         ColonFound = true;
+      }
       if (CurrentToken->is(tok::comma) &&
           Style.Language != FormatStyle::LK_Proto &&
           (Left->Type == TT_ArraySubscriptLSquare ||
index 758e504..b195df4 100644 (file)
@@ -6461,6 +6461,7 @@ TEST_F(FormatTest, FormatObjCMethodDeclarations) {
 TEST_F(FormatTest, FormatObjCMethodExpr) {
   verifyFormat("[foo bar:baz];");
   verifyFormat("return [foo bar:baz];");
+  verifyFormat("return (a)[foo bar:baz];");
   verifyFormat("f([foo bar:baz]);");
   verifyFormat("f(2, [foo bar:baz]);");
   verifyFormat("f(2, a ? b : c);");
@@ -9137,10 +9138,8 @@ TEST_F(FormatTest, FormatsBlocks) {
   verifyFormat("foo(^{ bar(); });");
   verifyFormat("foo(a, ^{ bar(); });");
 
-  // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
-  // it would ideally look.
   verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
-  verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
+  verifyFormat("int i = {[operation setCompletionBlock:^{ [self "
                "onOperationDone]; }]};");
   verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
   verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");