]> granicus.if.org Git - clang/commitdiff
Improve code completion for Objective-C message sends, so that we
authorDouglas Gregor <dgregor@apple.com>
Thu, 19 Nov 2009 01:08:35 +0000 (01:08 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 19 Nov 2009 01:08:35 +0000 (01:08 +0000)
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".

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89290 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2a6171c6aa6d4260c56373f2dda1cf25b644d260..6ebdf0795885e01b4ac5efcc9bb383d10db15c37 100644 (file)
@@ -2335,23 +2335,31 @@ public:
   /// 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>.
index f8c4c5281c63da1465a9a64b8f9b82e9c631528e..3c5428bdb9f3932b938f945a2dfba243fd493928 100644 (file)
@@ -257,6 +257,10 @@ public:
     /// 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 @@ public:
            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 {
index fa14bb9a52e3df4704c3f3ecef485732403587de..9943428bc52203561a80539f01128a5995b7a525 100644 (file)
@@ -1632,11 +1632,14 @@ Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
                                        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 @@ Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
       // 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))
index 96f671535cf22f1cad8c1a0f3539a0f44f4a2b2d..0da822ec2e0445df3e05698c3f211144bd918de2 100644 (file)
@@ -3648,8 +3648,12 @@ public:
   
   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);
index 64c795eecb9ad8eca0cd7b284fa223dcad84669f..59060336fd6856018c57fb014971baaa692065ca 100644 (file)
@@ -944,19 +944,40 @@ CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) {
       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 @@ static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
 void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) { 
   if (!CodeCompleter)
     return;
+  
   unsigned Attributes = ODS.getPropertyAttributes();
   
   typedef CodeCompleteConsumer::Result Result;
@@ -1718,14 +1740,33 @@ void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
 /// \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 @@ static void AddObjCMethods(ObjCContainerDecl *Container,
   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 @@ static void AddObjCMethods(ObjCContainerDecl *Container,
     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 @@ void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
         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 @@ void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
     // 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 @@ void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) {
       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 @@ void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver) {
     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();
index 8f235d39bec2fa524cae907f56ce7d1754dca170..b692986cfda9106791ee0f0dffd1d52884cbac4b 100644 (file)
@@ -82,6 +82,19 @@ void test_qual_id(id<FooTestProtocol,FooTestProtocol2> ptr) {
   [ptr protocolInstanceMethod:1];
 }
 
+@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;
+@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 @@ void test_qual_id(id<FooTestProtocol,FooTestProtocol2> ptr) {
 // 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}