r174521 - Formatter: Correctly detect ObjC message expressions preceded by a comment.

Nico Weber nicolasweber at gmx.de
Wed Feb 6 08:54:36 PST 2013


Author: nico
Date: Wed Feb  6 10:54:35 2013
New Revision: 174521

URL: http://llvm.org/viewvc/llvm-project?rev=174521&view=rev
Log:
Formatter: Correctly detect ObjC message expressions preceded by a comment.


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=174521&r1=174520&r2=174521&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Feb  6 10:54:35 2013
@@ -42,12 +42,15 @@ static bool isBinaryOperator(const Annot
 }
 
 // Returns the previous token ignoring comments.
-static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
-  const AnnotatedToken *PrevToken = Tok.Parent;
+static AnnotatedToken *getPreviousToken(AnnotatedToken &Tok) {
+  AnnotatedToken *PrevToken = Tok.Parent;
   while (PrevToken != NULL && PrevToken->is(tok::comment))
     PrevToken = PrevToken->Parent;
   return PrevToken;
 }
+static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) {
+  return getPreviousToken(const_cast<AnnotatedToken &>(Tok));
+}
 
 // Returns the next token ignoring comments.
 static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) {
@@ -181,12 +184,12 @@ public:
     // ')' or ']'), or it could be the start of an Objective-C method
     // expression.
     AnnotatedToken *Left = CurrentToken->Parent;
+    AnnotatedToken *Parent = getPreviousToken(*Left);
     bool StartsObjCMethodExpr =
-        !Left->Parent || Left->Parent->is(tok::colon) ||
-        Left->Parent->is(tok::l_square) || Left->Parent->is(tok::l_paren) ||
-        Left->Parent->is(tok::kw_return) || Left->Parent->is(tok::kw_throw) ||
-        isUnaryOperator(*Left->Parent) ||
-        getBinOpPrecedence(Left->Parent->FormatTok.Tok.getKind(), true, true) >
+        !Parent || Parent->is(tok::colon) || Parent->is(tok::l_square) ||
+        Parent->is(tok::l_paren) || Parent->is(tok::kw_return) ||
+        Parent->is(tok::kw_throw) || isUnaryOperator(*Parent) ||
+        getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) >
         prec::Unknown;
 
     if (StartsObjCMethodExpr) {
@@ -208,10 +211,10 @@ public:
           // 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->Type == TT_PointerOrReference)
-            Left->Parent->Type = TT_BinaryOperator;
+          if (Parent != NULL &&
+              (Parent->is(tok::star) || Parent->is(tok::amp)) &&
+              Parent->Type == TT_PointerOrReference)
+            Parent->Type = TT_BinaryOperator;
         }
         Left->MatchingParen = CurrentToken;
         CurrentToken->MatchingParen = Left;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174521&r1=174520&r2=174521&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb  6 10:54:35 2013
@@ -2422,6 +2422,12 @@ TEST_F(FormatTest, FormatObjCMethodExpr)
                "             fraction:1.0\n"
                "       respectFlipped:NO\n"
                "                hints:nil];");
+
+  verifyFormat(
+      "scoped_nsobject<NSTextField> message(\n"
+      "    // The frame will be fixed up when |-setMessageText:| is called.\n"
+      "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
+  
 }
 
 TEST_F(FormatTest, ObjCAt) {





More information about the cfe-commits mailing list