ObjectiveC migration: tweak setting of lifetime attribute
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
diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp
index c8fc312..f676149 100644
--- a/lib/ARCMigrate/ObjCMT.cpp
+++ b/lib/ARCMigrate/ObjCMT.cpp
@@ -211,26 +211,37 @@
                                   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 @@
   PropertyString += " ";
   PropertyString += CanResultTy.getAsString(Context.getPrintingPolicy());
   PropertyString += " ";
-  PropertyString += Getter->getNameAsString();
+  PropertyString += PropertyNameString;
   commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),
                                                Getter->getDeclaratorEndLoc()),
                  PropertyString);
diff --git a/test/ARCMT/objcmt-property.m b/test/ARCMT/objcmt-property.m
index ca1b504..6511acd 100644
--- a/test/ARCMT/objcmt-property.m
+++ b/test/ARCMT/objcmt-property.m
@@ -55,3 +55,26 @@
 - (__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
diff --git a/test/ARCMT/objcmt-property.m.result b/test/ARCMT/objcmt-property.m.result
index 2bcae86..3c9a539 100644
--- a/test/ARCMT/objcmt-property.m.result
+++ b/test/ARCMT/objcmt-property.m.result
@@ -18,7 +18,7 @@
 
 @property(weak) NSString * WeakProp;
 
-@property NSString * StrongProp;
+@property(retain) NSString * StrongProp;
 
 
 - (NSString *) UnavailProp  __attribute__((unavailable));
@@ -50,8 +50,31 @@
 
 
 
-@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