r199411 - clang-format: Improve formatting of ObjC Blocks with return type.

Daniel Jasper djasper at google.com
Thu Jan 16 11:14:34 PST 2014


Author: djasper
Date: Thu Jan 16 13:14:34 2014
New Revision: 199411

URL: http://llvm.org/viewvc/llvm-project?rev=199411&view=rev
Log:
clang-format: Improve formatting of ObjC Blocks with return type.

Before:
  int a = [operation block:^int(int * i) { return 1; }];

After:
  int a = [operation block:^int(int *i) { return 1; }];

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    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=199411&r1=199410&r2=199411&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Jan 16 13:14:34 2014
@@ -75,6 +75,9 @@ private:
   bool parseParens(bool LookForDecls = false) {
     if (CurrentToken == NULL)
       return false;
+    bool AfterCaret = Contexts.back().CaretFound;
+    Contexts.back().CaretFound = false;
+
     ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
 
     // FIXME: This is a bit of a hack. Do better.
@@ -103,8 +106,7 @@ private:
                Left->Previous->MatchingParen->Type == TT_LambdaLSquare) {
       // This is a parameter list of a lambda expression.
       Contexts.back().IsExpression = false;
-    } else if (Left->Previous && Left->Previous->is(tok::caret) &&
-               Left->Previous->Type == TT_UnaryOperator) {
+    } else if (AfterCaret) {
       // This is the parameter list of an ObjC block.
       Contexts.back().IsExpression = false;
     }
@@ -581,7 +583,7 @@ private:
           ColonIsForRangeExpr(false), ColonIsDictLiteral(false),
           ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL),
           FirstStartOfName(NULL), IsExpression(IsExpression),
-          CanBeExpression(true), InCtorInitializer(false) {}
+          CanBeExpression(true), InCtorInitializer(false), CaretFound(false) {}
 
     tok::TokenKind ContextKind;
     unsigned BindingStrength;
@@ -595,6 +597,7 @@ private:
     bool IsExpression;
     bool CanBeExpression;
     bool InCtorInitializer;
+    bool CaretFound;
   };
 
   /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
@@ -673,8 +676,11 @@ private:
                                                Contexts.back().IsExpression);
       } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
         Current.Type = determinePlusMinusCaretUsage(Current);
-        if (Current.Type == TT_UnaryOperator)
+        if (Current.Type == TT_UnaryOperator) {
           ++Contexts.back().NumBlockParameters;
+          if (Current.is(tok::caret))
+            Contexts.back().CaretFound = true;
+        }
       } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
         Current.Type = determineIncrementUsage(Current);
       } else if (Current.is(tok::exclaim)) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=199411&r1=199410&r2=199411&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Jan 16 13:14:34 2014
@@ -7907,6 +7907,10 @@ TEST_F(FormatTest, FormatsBlocks) {
   verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
                "onOperationDone]; }] };");
   verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
+  verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");
+  verifyFormat("[myObject doSomethingWith:arg1\n"
+               "                      aaa:^int(int *a) { return 1; }\n"
+               "                      bbb:f(a * b)];");
 
   verifyFormat("[operation setCompletionBlock:^{\n"
                "    [self.delegate newDataAvailable];\n"





More information about the cfe-commits mailing list