]> granicus.if.org Git - clang/commitdiff
ObjectiveC migration: tweak setting of lifetime attribute
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 6 Aug 2013 18:06:23 +0000 (18:06 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 6 Aug 2013 18:06:23 +0000 (18:06 +0000)
on @property migration. Don't set unsafe_unretained
on non-object properties. Set 'retain' on strong
properties. Makecertain properties with specific
names unsafe_unretained as well.

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

lib/ARCMigrate/ObjCMT.cpp
test/ARCMT/objcmt-property.m
test/ARCMT/objcmt-property.m.result

index c8fc312e6a2b67dc7fe05bb14af6350bce0bf39b..f676149389ec77e4b25312f90bc75ef9cc279f88 100644 (file)
@@ -211,26 +211,37 @@ static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter,
                                   const NSAPI &NS, edit::Commit &commit) {
   ASTContext &Context = NS.getASTContext();
   std::string PropertyString = "@property";
-  const ParmVarDecl *argDecl = *Setter->param_begin();
-  QualType ArgType = Context.getCanonicalType(argDecl->getType());
-  Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
   
-  if (ArgType->isObjCRetainableType() &&
-      propertyLifetime == Qualifiers::OCL_Strong) {
-    if (const ObjCObjectPointerType *ObjPtrTy =
-        ArgType->getAs<ObjCObjectPointerType>()) {
-      ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
-      if (IDecl &&
-          IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
-        PropertyString += "(copy)";
-    }
-  }
-  else if (propertyLifetime == Qualifiers::OCL_Weak)
-    // TODO. More precise determination of 'weak' attribute requires
-    // looking into setter's implementation for backing weak ivar.
-    PropertyString += "(weak)";
-  else
+  std::string PropertyNameString = Getter->getNameAsString();
+  StringRef PropertyName(PropertyNameString);
+  // Short circuit properties that contain the name "delegate" or "dataSource",
+  // or have exact name "target" to have unsafe_unretained attribute.
+  if (PropertyName.equals("target") ||
+      (PropertyName.find("delegate") != StringRef::npos) ||
+      (PropertyName.find("dataSource") != StringRef::npos))
     PropertyString += "(unsafe_unretained)";
+  else {
+    const ParmVarDecl *argDecl = *Setter->param_begin();
+    QualType ArgType = Context.getCanonicalType(argDecl->getType());
+    Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
+    bool RetainableObject = ArgType->isObjCRetainableType();
+    if (RetainableObject && propertyLifetime == Qualifiers::OCL_Strong) {
+      if (const ObjCObjectPointerType *ObjPtrTy =
+          ArgType->getAs<ObjCObjectPointerType>()) {
+        ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
+        if (IDecl &&
+            IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
+          PropertyString += "(copy)";
+        else
+          PropertyString += "(retain)";
+      }
+    } else if (propertyLifetime == Qualifiers::OCL_Weak)
+      // TODO. More precise determination of 'weak' attribute requires
+      // looking into setter's implementation for backing weak ivar.
+      PropertyString += "(weak)";
+    else if (RetainableObject)
+      PropertyString += "(retain)";
+  }
   
   // strip off any ARC lifetime qualifier.
   QualType CanResultTy = Context.getCanonicalType(Getter->getResultType());
@@ -242,7 +253,7 @@ static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter,
   PropertyString += " ";
   PropertyString += CanResultTy.getAsString(Context.getPrintingPolicy());
   PropertyString += " ";
-  PropertyString += Getter->getNameAsString();
+  PropertyString += PropertyNameString;
   commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),
                                                Getter->getDeclaratorEndLoc()),
                  PropertyString);
index ca1b504dcbdb6b8d99017464cb25eed5e23aa99d..6511acd72c53f9157e402c07baa408a55734c0fe 100644 (file)
 - (__strong NSArray *)names4;
 - (NSArray *) names1;
 @end
+
+// Properties that contain the name "delegate" or "dataSource",
+// or have exact name "target" have unsafe_unretained attribute.
+@interface NSInvocation 
+- (id)target;
+- (void)setTarget:(id)target;
+
+- (id) dataSource;
+
+- (id)xxxdelegateYYY;
+- (void)setXxxdelegateYYY:(id)delegate;
+
+- (void)setDataSource:(id)source;
+
+- (id)MYtarget;
+- (void)setMYtarget: (id)target;
+
+- (id)targetX;
+- (void)setTargetX: (id)t;
+- (int)value;
+- (void)setValue: (int)val;
+@end
index 2bcae864a0cb6fb92df8e4ddb1419b548844fae8..3c9a539eb7d6abf8c38824226610a86c850e0e8c 100644 (file)
@@ -18,7 +18,7 @@
 
 @property(weak) NSString * WeakProp;
 
-@property NSString * StrongProp;
+@property(retain) NSString * StrongProp;
 
 
 - (NSString *) UnavailProp  __attribute__((unavailable));
 
 
 
-@property NSArray * names2;
-@property NSArray * names3;
-@property NSArray * names4;
-@property NSArray * names1;
+@property(retain) NSArray * names2;
+@property(retain) NSArray * names3;
+@property(retain) NSArray * names4;
+@property(retain) NSArray * names1;
+@end
+
+// Properties that contain the name "delegate" or "dataSource",
+// or have exact name "target" have unsafe_unretained attribute.
+@interface NSInvocation 
+@property(unsafe_unretained) id target;
+
+
+@property(unsafe_unretained) id dataSource;
+
+@property(unsafe_unretained) id xxxdelegateYYY;
+
+
+
+
+@property(retain) id MYtarget;
+
+
+@property(retain) id targetX;
+
+@property int value;
+
 @end