[cfe-commits] r129699 - in /cfe/trunk: lib/Sema/SemaCodeComplete.cpp test/Index/complete-properties.m

Douglas Gregor dgregor at apple.com
Mon Apr 18 07:40:47 PDT 2011


Author: dgregor
Date: Mon Apr 18 09:40:46 2011
New Revision: 129699

URL: http://llvm.org/viewvc/llvm-project?rev=129699&view=rev
Log:
When providing code completions of ivar names for a property
implementation such as

  @synthesize Prop1 = 

Give priority to ivars whose type matches or closely matches the
property type (as we do for several other kinds of
results). Additionally, if there is an ivar with the same name as the
property, or differs only due to a _ prefix or suffix, give that ivar
a priority bump. Finally, verify that this search is properly
returning ivars within class extensions and implementations
(<rdar://problem/8488854>).



Modified:
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp
    cfe/trunk/test/Index/complete-properties.m

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=129699&r1=129698&r2=129699&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Mon Apr 18 09:40:46 2011
@@ -5370,6 +5370,19 @@
     Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
                                                           ->getClassInterface();
 
+  // Determine the type of the property we're synthesizing.
+  QualType PropertyType = Context.getObjCIdType();
+  if (Class) {
+    if (ObjCPropertyDecl *Property
+                              = Class->FindPropertyDeclaration(PropertyName)) {
+      PropertyType 
+        = Property->getType().getNonReferenceType().getUnqualifiedType();
+      
+      // Give preference to ivars 
+      Results.setPreferredType(PropertyType);
+    }
+  }
+
   // Add all of the instance variables in this class and its superclasses.
   Results.EnterNewScope();
   bool SawSimilarlyNamedIvar = false;
@@ -5379,39 +5392,38 @@
   std::string NameWithSuffix = PropertyName->getName().str();
   NameWithSuffix += '_';
   for(; Class; Class = Class->getSuperClass()) {
-    // FIXME: We could screen the type of each ivar for compatibility with
-    // the property, but is that being too paternal?    
     for (ObjCIvarDecl *Ivar = Class->all_declared_ivar_begin(); Ivar; 
          Ivar = Ivar->getNextIvar()) {
+      Results.AddResult(Result(Ivar, 0), CurContext, 0, false);
+      
       // Determine whether we've seen an ivar with a name similar to the 
       // property.
-      if (!SawSimilarlyNamedIvar &&
-          (PropertyName->getName() == Ivar->getName() ||
+      if ((PropertyName == Ivar->getIdentifier() ||
            NameWithPrefix == Ivar->getName() ||
-           NameWithSuffix == Ivar->getName()))
+           NameWithSuffix == Ivar->getName())) {
         SawSimilarlyNamedIvar = true;
-      
-      Results.AddResult(Result(Ivar, 0), CurContext, 0, false);
+       
+        // Reduce the priority of this result by one, to give it a slight
+        // advantage over other results whose names don't match so closely.
+        if (Results.size() && 
+            Results.data()[Results.size() - 1].Kind 
+                                      == CodeCompletionResult::RK_Declaration &&
+            Results.data()[Results.size() - 1].Declaration == Ivar)
+          Results.data()[Results.size() - 1].Priority--;
+      }
     }
   }
   
   if (!SawSimilarlyNamedIvar) {
     // Create ivar result _propName, that the user can use to synthesize
-    // an ivar of the appropriate type.
-    QualType T = Context.getObjCIdType();
-    
-    if (Class) {
-      if (ObjCPropertyDecl *Property
-                                = Class->FindPropertyDeclaration(PropertyName))
-        T = Property->getType().getNonReferenceType().getUnqualifiedType();
-    }
-    
-    unsigned Priority = CCP_MemberDeclaration;
+    // an ivar of the appropriate type.    
+    unsigned Priority = CCP_MemberDeclaration + 1;
     typedef CodeCompletionResult Result;
     CodeCompletionAllocator &Allocator = Results.getAllocator();
     CodeCompletionBuilder Builder(Allocator, Priority,CXAvailability_Available);
 
-    Builder.AddResultTypeChunk(GetCompletionTypeString(T, Context, Allocator));
+    Builder.AddResultTypeChunk(GetCompletionTypeString(PropertyType, Context,
+                                                       Allocator));
     Builder.AddTypedTextChunk(Allocator.CopyString(NameWithPrefix));
     Results.AddResult(Result(Builder.TakeString(), Priority, 
                              CXCursor_ObjCIvarDecl));

Modified: cfe/trunk/test/Index/complete-properties.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-properties.m?rev=129699&r1=129698&r2=129699&view=diff
==============================================================================
--- cfe/trunk/test/Index/complete-properties.m (original)
+++ cfe/trunk/test/Index/complete-properties.m Mon Apr 18 09:40:46 2011
@@ -29,6 +29,22 @@
   return i3.Prop3;
 }
 
+ at interface I4
+ at property id Prop2;
+ at end
+
+ at interface I4 () {
+  I4 *Prop1;
+}
+ at end
+
+ at implementation I4 {
+  id Prop2_;
+}
+
+ at synthesize Prop2 = Prop2_;
+ at end
+
 // RUN: c-index-test -code-completion-at=%s:20:13 %s | FileCheck -check-prefix=CHECK-CC1 %s
 // CHECK-CC1: ObjCPropertyDecl:{ResultType int}{TypedText Prop0}
 // CHECK-CC1: ObjCPropertyDecl:{ResultType int}{TypedText Prop1}
@@ -41,9 +57,9 @@
 // CHECK-CC2-NEXT: ObjCPropertyDecl:{ResultType id}{TypedText Prop3}
 // CHECK-CC2: ObjCPropertyDecl:{ResultType id}{TypedText Prop4}
 // RUN: c-index-test -code-completion-at=%s:20:35 %s | FileCheck -check-prefix=CHECK-CC3 %s
-// CHECK-CC3: ObjCIvarDecl:{ResultType id}{TypedText _Prop3} (35)
+// CHECK-CC3: ObjCIvarDecl:{ResultType id}{TypedText _Prop3} (36)
 // CHECK-CC3: ObjCIvarDecl:{ResultType int}{TypedText RandomIVar} (35)
-// CHECK-CC3: ObjCIvarDecl:{ResultType id}{TypedText StoredProp3} (35)
+// CHECK-CC3: ObjCIvarDecl:{ResultType id}{TypedText StoredProp3} (8)
 
 // RUN: c-index-test -code-completion-at=%s:21:10 %s | FileCheck -check-prefix=CHECK-CC4 %s
 // CHECK-CC4: ObjCPropertyDecl:{ResultType int}{TypedText Prop0}
@@ -59,3 +75,8 @@
 // RUN: c-index-test -code-completion-at=%s:9:11 %s | FileCheck -check-prefix=CHECK-CC6 %s
 // CHECK-CC6: ObjCInterfaceDecl:{TypedText MyClass} (50)
 
+
+// RUN: c-index-test -code-completion-at=%s:45:21 -fobjc-nonfragile-abi %s | FileCheck -check-prefix=CHECK-CC7 %s
+// CHECK-CC7-NOT: ObjCIvarDecl:{ResultType id}{TypedText _Prop2}
+// CHECK-CC7: ObjCIvarDecl:{ResultType I4 *}{TypedText Prop1} (17)
+// CHECK-CC7: ObjCIvarDecl:{ResultType id}{TypedText Prop2_} (7)





More information about the cfe-commits mailing list