r174489 - Formatter: No space after & and * in front of ObjC message expressions.

Nico Weber nicolasweber at gmx.de
Tue Feb 5 22:20:11 PST 2013


Author: nico
Date: Wed Feb  6 00:20:11 2013
New Revision: 174489

URL: http://llvm.org/viewvc/llvm-project?rev=174489&view=rev
Log:
Formatter: No space after & and * in front of ObjC message expressions.

1. let determineStarAmp() check of unary operators before checking for
   "is next '['". That check was added in r173150, and the test from that
   revision passes either way.

2. change determineStarAmp() to categorize '*' and '&' after '=' as unary
   operator.

3. don't let parseSquare() overwrite the type of a '*' or '&' before the start
   of an objc message expression if has the role of unary operator.


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=174489&r1=174488&r2=174489&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Feb  6 00:20:11 2013
@@ -224,15 +224,19 @@ public:
       if (CurrentToken->is(tok::r_square)) {
         if (!CurrentToken->Children.empty() &&
             CurrentToken->Children[0].is(tok::l_paren)) {
-          // An ObjC method call can't be followed by an open parenthesis.
+          // An ObjC method call is rarely followed by an open parenthesis.
           // FIXME: Do we incorrectly label ":" with this?
           StartsObjCMethodExpr = false;
           Left->Type = TT_Unknown;
         }
         if (StartsObjCMethodExpr) {
           objCSelector.markEnd(*CurrentToken);
+          // determineStarAmpUsage() thinks that '*' '[' is allocating an
+          // array of pointers, but if '[' starts a selector then '*' is a
+          // binary operator.
           if (Left->Parent != NULL &&
-              (Left->Parent->is(tok::star) || Left->Parent->is(tok::amp)))
+              (Left->Parent->is(tok::star) || Left->Parent->is(tok::amp)) &&
+              Left->Parent->Type == TT_PointerOrReference)
             Left->Parent->Type = TT_BinaryOperator;
         }
         Left->MatchingParen = CurrentToken;
@@ -607,16 +611,16 @@ private:
     if (NextToken == NULL)
       return TT_Unknown;
 
-    if (NextToken->is(tok::l_square))
-      return TT_PointerOrReference;
-
     if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) ||
         PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) ||
         PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) ||
-        PrevToken->Type == TT_BinaryOperator ||
+        PrevToken->is(tok::equal) || PrevToken->Type == TT_BinaryOperator ||
         PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
       return TT_UnaryOperator;
 
+    if (NextToken->is(tok::l_square))
+      return TT_PointerOrReference;
+
     if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) ||
         PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() ||
         isUnaryOperator(*NextToken) || NextToken->is(tok::l_paren) ||

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174489&r1=174488&r2=174489&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb  6 00:20:11 2013
@@ -2245,9 +2245,8 @@ TEST_F(FormatTest, FormatObjCMethodExpr)
   verifyFormat("int a = --[foo bar:baz];");
   verifyFormat("int a = sizeof [foo bar:baz];");
   verifyFormat("int a = alignof [foo bar:baz];");
-  // FIXME: no space after & and *.
-  verifyFormat("int a = & [foo bar:baz];");
-  verifyFormat("int a = * [foo bar:baz];");
+  verifyFormat("int a = &[foo bar:baz];");
+  verifyFormat("int a = *[foo bar:baz];");
   // FIXME: Make casts work, without breaking f()[4].
   //verifyFormat("int a = (int) [foo bar:baz];");
 





More information about the cfe-commits mailing list