[cfe-commits] r89290 - in /cfe/trunk: include/clang/Parse/Action.h include/clang/Sema/CodeCompleteConsumer.h lib/Parse/ParseObjc.cpp lib/Sema/Sema.h lib/Sema/SemaCodeComplete.cpp test/Index/complete-objc-message.m

Douglas Gregor dgregor at apple.com
Wed Nov 18 17:08:35 PST 2009


Author: dgregor
Date: Wed Nov 18 19:08:35 2009
New Revision: 89290

URL: http://llvm.org/viewvc/llvm-project?rev=89290&view=rev
Log:
Improve code completion for Objective-C message sends, so that we
provide completion results before each keyword argument, e.g.,

  [foo Method:arg WithArg1:arg1 WithArg2:arg2]

We now complete before "WithArg1" and before "WithArg2", in addition
to completing before "Method".

Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
    cfe/trunk/lib/Parse/ParseObjc.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp
    cfe/trunk/test/Index/complete-objc-message.m

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Wed Nov 18 19:08:35 2009
@@ -2335,23 +2335,31 @@
   /// a class method.
   ///
   /// This code completion action is invoked when the code-completion token is
-  /// found after the class name.
+  /// found after the class name and after each argument.
   ///
   /// \param S the scope in which the message expression occurs. 
   /// \param FName the factory name. 
   /// \param FNameLoc the source location of the factory name.
+  /// \param SelIdents the identifiers that describe the selector (thus far).
+  /// \param NumSelIdents the number of identifiers in \p SelIdents.
   virtual void CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
-                                            SourceLocation FNameLoc){ }
+                                            SourceLocation FNameLoc,
+                                            IdentifierInfo **SelIdents,
+                                            unsigned NumSelIdents){ }
   
   /// \brief Code completion for an ObjC message expression that refers to
   /// an instance method.
   ///
   /// This code completion action is invoked when the code-completion token is
-  /// found after the receiver expression.
+  /// found after the receiver expression and after each argument.
   ///
   /// \param S the scope in which the operator keyword occurs.  
   /// \param Receiver an expression for the receiver of the message. 
-  virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) { }
+  /// \param SelIdents the identifiers that describe the selector (thus far).
+  /// \param NumSelIdents the number of identifiers in \p SelIdents.
+  virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
+                                               IdentifierInfo **SelIdents,
+                                               unsigned NumSelIdents) { }
 
   /// \brief Code completion for a list of protocol references in Objective-C,
   /// such as P1 and P2 in \c id<P1,P2>.

Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h (original)
+++ cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h Wed Nov 18 19:08:35 2009
@@ -257,6 +257,10 @@
     /// result and progressively higher numbers representing poorer results.
     unsigned Rank;
     
+    /// \brief Specifiers which parameter (of a function, Objective-C method,
+    /// macro, etc.) we should start with when formatting the result.
+    unsigned StartParameter;
+    
     /// \brief Whether this result is hidden by another name.
     bool Hidden : 1;
     
@@ -277,26 +281,27 @@
            NestedNameSpecifier *Qualifier = 0,
            bool QualifierIsInformative = false)
       : Kind(RK_Declaration), Declaration(Declaration), Rank(Rank), 
-        Hidden(false), QualifierIsInformative(QualifierIsInformative),
+        StartParameter(0), Hidden(false), 
+        QualifierIsInformative(QualifierIsInformative),
         StartsNestedNameSpecifier(false), Qualifier(Qualifier) { }
     
     /// \brief Build a result that refers to a keyword or symbol.
     Result(const char *Keyword, unsigned Rank)
-      : Kind(RK_Keyword), Keyword(Keyword), Rank(Rank), Hidden(false),
-        QualifierIsInformative(0), StartsNestedNameSpecifier(false), 
-        Qualifier(0) { }
+      : Kind(RK_Keyword), Keyword(Keyword), Rank(Rank), StartParameter(0),
+        Hidden(false), QualifierIsInformative(0), 
+        StartsNestedNameSpecifier(false), Qualifier(0) { }
     
     /// \brief Build a result that refers to a macro.
     Result(IdentifierInfo *Macro, unsigned Rank)
-     : Kind(RK_Macro), Macro(Macro), Rank(Rank), Hidden(false), 
-       QualifierIsInformative(0), StartsNestedNameSpecifier(false),
-       Qualifier(0) { }
+     : Kind(RK_Macro), Macro(Macro), Rank(Rank), StartParameter(0), 
+       Hidden(false), QualifierIsInformative(0), 
+       StartsNestedNameSpecifier(false), Qualifier(0) { }
 
     /// \brief Build a result that refers to a pattern.
     Result(CodeCompletionString *Pattern, unsigned Rank)
-      : Kind(RK_Pattern), Pattern(Pattern), Rank(Rank), Hidden(false), 
-        QualifierIsInformative(0), StartsNestedNameSpecifier(false),
-        Qualifier(0) { }
+      : Kind(RK_Pattern), Pattern(Pattern), Rank(Rank), StartParameter(0), 
+        Hidden(false), QualifierIsInformative(0), 
+        StartsNestedNameSpecifier(false), Qualifier(0) { }
     
     /// \brief Retrieve the declaration stored in this result.
     NamedDecl *getDeclaration() const {

Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/lib/Parse/ParseObjc.cpp Wed Nov 18 19:08:35 2009
@@ -1632,11 +1632,14 @@
                                        ExprArg ReceiverExpr) {
   if (Tok.is(tok::code_completion)) {
     if (ReceiverName)
-      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
+      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc, 
+                                           0, 0);
     else
-      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
+      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(), 
+                                              0, 0);
     ConsumeToken();
   }
+  
   // Parse objc-selector
   SourceLocation Loc;
   IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
@@ -1674,6 +1677,19 @@
       // We have a valid expression.
       KeyExprs.push_back(Res.release());
 
+      // Code completion after each argument.
+      if (Tok.is(tok::code_completion)) {
+        if (ReceiverName)
+          Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
+                                               KeyIdents.data(), 
+                                               KeyIdents.size());
+        else
+          Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
+                                                  KeyIdents.data(), 
+                                                  KeyIdents.size());
+        ConsumeToken();
+      }
+            
       // Check for another keyword selector.
       selIdent = ParseObjCSelectorPiece(Loc);
       if (!selIdent && Tok.isNot(tok::colon))

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Nov 18 19:08:35 2009
@@ -3648,8 +3648,12 @@
   
   virtual void CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS);
   virtual void CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
-                                            SourceLocation FNameLoc);
-  virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver);
+                                            SourceLocation FNameLoc,
+                                            IdentifierInfo **SelIdents, 
+                                            unsigned NumSelIdents);
+  virtual void CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
+                                               IdentifierInfo **SelIdents,
+                                               unsigned NumSelIdents);
   virtual void CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
                                                   unsigned NumProtocols);
   virtual void CodeCompleteObjCProtocolDecl(Scope *S);

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed Nov 18 19:08:35 2009
@@ -944,19 +944,40 @@
       return Result;
     }
 
-    Result->AddTypedTextChunk(
-          Sel.getIdentifierInfoForSlot(0)->getName().str() + std::string(":"));
+    std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str();
+    SelName += ':';
+    if (StartParameter == 0)
+      Result->AddTypedTextChunk(SelName);
+    else {
+      Result->AddInformativeChunk(SelName);
+      
+      // If there is only one parameter, and we're past it, add an empty
+      // typed-text chunk since there is nothing to type.
+      if (Method->param_size() == 1)
+        Result->AddTypedTextChunk("");
+    }
     unsigned Idx = 0;
     for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
                                      PEnd = Method->param_end();
          P != PEnd; (void)++P, ++Idx) {
       if (Idx > 0) {
-        std::string Keyword = " ";
+        std::string Keyword;
+        if (Idx > StartParameter)
+          Keyword = " ";
         if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
           Keyword += II->getName().str();
         Keyword += ":";
-        Result->AddTextChunk(Keyword);
+        if (Idx < StartParameter) {
+          Result->AddInformativeChunk(Keyword);
+        } else if (Idx == StartParameter)
+          Result->AddTypedTextChunk(Keyword);
+        else
+          Result->AddTextChunk(Keyword);
       }
+      
+      // If we're before the starting parameter, skip the placeholder.
+      if (Idx < StartParameter)
+        continue;
 
       std::string Arg;
       (*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy);
@@ -1664,6 +1685,7 @@
 void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { 
   if (!CodeCompleter)
     return;
+  
   unsigned Attributes = ODS.getPropertyAttributes();
   
   typedef CodeCompleteConsumer::Result Result;
@@ -1718,14 +1740,33 @@
 /// \param Results the structure into which we'll add results.
 static void AddObjCMethods(ObjCContainerDecl *Container, 
                            bool WantInstanceMethods,
+                           IdentifierInfo **SelIdents,
+                           unsigned NumSelIdents,
                            DeclContext *CurContext,
                            ResultBuilder &Results) {
   typedef CodeCompleteConsumer::Result Result;
   for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
                                        MEnd = Container->meth_end();
        M != MEnd; ++M) {
-    if ((*M)->isInstanceMethod() == WantInstanceMethods)
-      Results.MaybeAddResult(Result(*M, 0), CurContext);
+    if ((*M)->isInstanceMethod() == WantInstanceMethods) {
+      // Check whether the selector identifiers we've been given are a 
+      // subset of the identifiers for this particular method.
+      Selector Sel = (*M)->getSelector();
+      if (NumSelIdents > Sel.getNumArgs())
+        continue;
+      
+      bool Failed = false;
+      for (unsigned I = 0; I != NumSelIdents && !Failed; ++I)
+        if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
+          Failed = true;
+      
+      if (Failed)
+        continue;
+      
+      Result R = Result(*M, 0);
+      R.StartParameter = NumSelIdents;
+      Results.MaybeAddResult(R, CurContext);
+    }
   }
   
   ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
@@ -1737,12 +1778,14 @@
   for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
                                             E = Protocols.end(); 
        I != E; ++I)
-    AddObjCMethods(*I, WantInstanceMethods, CurContext, Results);
+    AddObjCMethods(*I, WantInstanceMethods, SelIdents, NumSelIdents, 
+                   CurContext, Results);
   
   // Add methods in categories.
   for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
        CatDecl = CatDecl->getNextClassCategory()) {
-    AddObjCMethods(CatDecl, WantInstanceMethods, CurContext, Results);
+    AddObjCMethods(CatDecl, WantInstanceMethods, SelIdents, NumSelIdents,
+                   CurContext, Results);
     
     // Add a categories protocol methods.
     const ObjCList<ObjCProtocolDecl> &Protocols 
@@ -1750,25 +1793,30 @@
     for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
                                               E = Protocols.end();
          I != E; ++I)
-      AddObjCMethods(*I, WantInstanceMethods, CurContext, Results);
+      AddObjCMethods(*I, WantInstanceMethods, SelIdents, NumSelIdents,
+                     CurContext, Results);
     
     // Add methods in category implementations.
     if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
-      AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results);
+      AddObjCMethods(Impl, WantInstanceMethods, SelIdents, NumSelIdents,
+                     CurContext, Results);
   }
   
   // Add methods in superclass.
   if (IFace->getSuperClass())
-    AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, CurContext,
-                   Results);
+    AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, SelIdents, 
+                   NumSelIdents, CurContext,Results);
 
   // Add methods in our implementation, if any.
   if (ObjCImplementationDecl *Impl = IFace->getImplementation())
-    AddObjCMethods(Impl, WantInstanceMethods, CurContext, Results);
+    AddObjCMethods(Impl, WantInstanceMethods, SelIdents, NumSelIdents, 
+                   CurContext, Results);
 }
 
 void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
-                                        SourceLocation FNameLoc) {
+                                        SourceLocation FNameLoc,
+                                        IdentifierInfo **SelIdents,
+                                        unsigned NumSelIdents) {
   typedef CodeCompleteConsumer::Result Result;
   ObjCInterfaceDecl *CDecl = 0;
 
@@ -1794,7 +1842,8 @@
         SuperTy = Context.getObjCObjectPointerType(SuperTy);
         OwningExprResult Super
           = Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy));
-        return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get());
+        return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
+                                               SelIdents, NumSelIdents);
       }
 
       // Okay, we're calling a factory method in our superclass.
@@ -1816,21 +1865,24 @@
     // probably calling an instance method.
     OwningExprResult Super = ActOnDeclarationNameExpr(S, FNameLoc, FName,
                                                       false, 0, false);
-    return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get());
+    return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
+                                           SelIdents, NumSelIdents);
   }
 
   // Add all of the factory methods in this Objective-C class, its protocols,
   // superclasses, categories, implementation, etc.
   ResultBuilder Results(*this);
   Results.EnterNewScope();
-  AddObjCMethods(CDecl, false, CurContext, Results);  
+  AddObjCMethods(CDecl, false, SelIdents, NumSelIdents, CurContext, Results);  
   Results.ExitScope();
   
   // This also suppresses remaining diagnostics.
   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
 }
 
-void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) {
+void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
+                                           IdentifierInfo **SelIdents,
+                                           unsigned NumSelIdents) {
   typedef CodeCompleteConsumer::Result Result;
   
   Expr *RecExpr = static_cast<Expr *>(Receiver);
@@ -1858,7 +1910,8 @@
       ReceiverType->isObjCQualifiedClassType()) {
     if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
       if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
-        AddObjCMethods(ClassDecl, false, CurContext, Results);
+        AddObjCMethods(ClassDecl, false, SelIdents, NumSelIdents, CurContext, 
+                       Results);
     }
   } 
   // Handle messages to a qualified ID ("id<foo>").
@@ -1868,19 +1921,20 @@
     for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
                                               E = QualID->qual_end(); 
          I != E; ++I)
-      AddObjCMethods(*I, true, CurContext, Results);
+      AddObjCMethods(*I, true, SelIdents, NumSelIdents, CurContext, Results);
   }
   // Handle messages to a pointer to interface type.
   else if (const ObjCObjectPointerType *IFacePtr
                               = ReceiverType->getAsObjCInterfacePointerType()) {
     // Search the class, its superclasses, etc., for instance methods.
-    AddObjCMethods(IFacePtr->getInterfaceDecl(), true, CurContext, Results);
+    AddObjCMethods(IFacePtr->getInterfaceDecl(), true, SelIdents, NumSelIdents,
+                   CurContext, Results);
     
     // Search protocols for instance methods.
     for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
          E = IFacePtr->qual_end(); 
          I != E; ++I)
-      AddObjCMethods(*I, true, CurContext, Results);
+      AddObjCMethods(*I, true, SelIdents, NumSelIdents, CurContext, Results);
   }
   
   Results.ExitScope();

Modified: cfe/trunk/test/Index/complete-objc-message.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-objc-message.m?rev=89290&r1=89289&r2=89290&view=diff

==============================================================================
--- cfe/trunk/test/Index/complete-objc-message.m (original)
+++ cfe/trunk/test/Index/complete-objc-message.m Wed Nov 18 19:08:35 2009
@@ -82,6 +82,19 @@
   [ptr protocolInstanceMethod:1];
 }
 
+ at interface Overload
+- (int)Method:(int)i;
+- (int)Method;
+- (int)Method:(float)f Arg1:(int)i1 Arg2:(int)i2;
+- (int)Method:(float)f Arg1:(int)i1 OtherArg:(id)obj;
+- (int)Method:(float)f SomeArg:(int)i1 OtherArg:(id)obj;
+- (int)OtherMethod:(float)f Arg1:(int)i1 Arg2:(int)i2;
+ at end
+
+void test_overload(Overload *ovl) {
+  [ovl Method:1 Arg1:1 OtherArg:ovl];
+}
+
 // RUN: c-index-test -code-completion-at=%s:23:19 %s | FileCheck -check-prefix=CHECK-CC1 %s
 // CHECK-CC1: {TypedText categoryClassMethod}
 // CHECK-CC1: {TypedText classMethod1:}{Placeholder (id)a}{Text  withKeyword:}{Placeholder (int)b}
@@ -104,4 +117,18 @@
 // RUN: c-index-test -code-completion-at=%s:82:8 %s | FileCheck -check-prefix=CHECK-CC6 %s
 // CHECK-CC6: ObjCInstanceMethodDecl:{TypedText protocolInstanceMethod:}{Placeholder (int)value}
 // CHECK-CC6: ObjCInstanceMethodDecl:{TypedText secondProtocolInstanceMethod}
-
+// RUN: c-index-test -code-completion-at=%s:95:8 %s | FileCheck -check-prefix=CHECK-CC7 %s
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method}
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (int)i}
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text  Arg1:}{Placeholder (int)i1}{Text  Arg2:}{Placeholder (int)i2}
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text  Arg1:}{Placeholder (int)i1}{Text  OtherArg:}{Placeholder (id)obj}
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText Method:}{Placeholder (float)f}{Text  SomeArg:}{Placeholder (int)i1}{Text  OtherArg:}{Placeholder (id)obj}
+// CHECK-CC7: ObjCInstanceMethodDecl:{TypedText OtherMethod:}{Placeholder (float)f}{Text  Arg1:}{Placeholder (int)i1}{Text  Arg2:}{Placeholder (int)i2}
+// RUN: c-index-test -code-completion-at=%s:95:17 %s | FileCheck -check-prefix=CHECK-CC8 %s
+// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText }
+// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText Arg1:}{Placeholder (int)i1}{Text  Arg2:}{Placeholder (int)i2}
+// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText Arg1:}{Placeholder (int)i1}{Text  OtherArg:}{Placeholder (id)obj}
+// CHECK-CC8: ObjCInstanceMethodDecl:{Informative Method:}{TypedText SomeArg:}{Placeholder (int)i1}{Text  OtherArg:}{Placeholder (id)obj}
+// RUN: c-index-test -code-completion-at=%s:95:24 %s | FileCheck -check-prefix=CHECK-CC9 %s
+// CHECK-CC9: ObjCInstanceMethodDecl:{Informative Method:}{Informative Arg1:}{TypedText Arg2:}{Placeholder (int)i2}
+// CHECK-CC9: ObjCInstanceMethodDecl:{Informative Method:}{Informative Arg1:}{TypedText OtherArg:}{Placeholder (id)obj}





More information about the cfe-commits mailing list