From fa45cb3bf95a10d6c3b1ab55d60dae28e8a57cf4 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Tue, 6 Aug 2013 18:06:23 +0000 Subject: [PATCH] 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 --- lib/ARCMigrate/ObjCMT.cpp | 49 ++++++++++++++++++----------- test/ARCMT/objcmt-property.m | 23 ++++++++++++++ test/ARCMT/objcmt-property.m.result | 33 ++++++++++++++++--- 3 files changed, 81 insertions(+), 24 deletions(-) diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index c8fc312e6a..f676149389 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -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()) { - 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()) { + 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); diff --git a/test/ARCMT/objcmt-property.m b/test/ARCMT/objcmt-property.m index ca1b504dcb..6511acd72c 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 2bcae864a0..3c9a539eb7 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 -- 2.40.0