From: Fariborz Jahanian Date: Thu, 14 Nov 2013 18:28:58 +0000 (+0000) Subject: ObjectiveC migrator: This patch sets access property X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=93d4b8cbc7d55fcd3326747d07c7a65f42e02d93;p=clang ObjectiveC migrator: This patch sets access property attributes on 'readonly' properties. // rdar://15460787 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194718 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index ff2ae40758..cac0fb0aed 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -291,6 +291,29 @@ void MigrateBlockOrFunctionPointerTypeVariable(std::string & PropertyString, } } +static const char *PropertyMemoryAttribute(ASTContext &Context, QualType ArgType) { + 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"))) + return "copy"; + else + return "retain"; + } + else if (ArgType->isBlockPointerType()) + return "copy"; + } else if (propertyLifetime == Qualifiers::OCL_Weak) + // TODO. More precise determination of 'weak' attribute requires + // looking into setter's implementation for backing weak ivar. + return "weak"; + else if (RetainableObject) + return ArgType->isBlockPointerType() ? "copy" : "retain"; + return 0; +} static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, const ObjCMethodDecl *Setter, @@ -322,12 +345,10 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, } // Property with no setter may be suggested as a 'readonly' property. if (!Setter) { - if (!LParenAdded) { - PropertyString += "(readonly"; - LParenAdded = true; - } - else - append_attr(PropertyString, "readonly", LParenAdded); + append_attr(PropertyString, "readonly", LParenAdded); + QualType ResType = Context.getCanonicalType(Getter->getResultType()); + if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ResType)) + append_attr(PropertyString, MemoryManagementAttr, LParenAdded); } // Short circuit 'delegate' properties that contain the name "delegate" or @@ -342,27 +363,8 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, else if (Setter) { 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"))) - append_attr(PropertyString, "copy", LParenAdded); - else - append_attr(PropertyString, "retain", LParenAdded); - } - else if (ArgType->isBlockPointerType()) - append_attr(PropertyString, "copy", LParenAdded); - } else if (propertyLifetime == Qualifiers::OCL_Weak) - // TODO. More precise determination of 'weak' attribute requires - // looking into setter's implementation for backing weak ivar. - append_attr(PropertyString, "weak", LParenAdded); - else if (RetainableObject) - append_attr(PropertyString, - ArgType->isBlockPointerType() ? "copy" : "retain", LParenAdded); + if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ArgType)) + append_attr(PropertyString, MemoryManagementAttr, LParenAdded); } if (LParenAdded) PropertyString += ')'; diff --git a/test/ARCMT/objcmt-atomic-property.m.result b/test/ARCMT/objcmt-atomic-property.m.result index 761a208a16..1650cd23e3 100644 --- a/test/ARCMT/objcmt-atomic-property.m.result +++ b/test/ARCMT/objcmt-atomic-property.m.result @@ -80,14 +80,14 @@ typedef char BOOL; - (void) Nothing; @property (readonly) int Length; -@property (readonly) id object; +@property (readonly, retain) id object; + (double) D; @property (readonly) void *JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); @property (getter=isIgnoringInteractionEvents, readonly) BOOL ignoringInteractionEvents; @property (getter=getStringValue, retain) NSString *stringValue; @property (getter=getCounterValue, readonly) BOOL counterValue; -@property (getter=getns_dixtionary, readonly) NSDictionary *ns_dixtionary; +@property (getter=getns_dixtionary, readonly, retain) NSDictionary *ns_dixtionary; - (BOOL)is3bar; // watch out - (NSString *)get3foo; // watch out @@ -124,7 +124,7 @@ typedef char BOOL; - (void) Nothing; @property (readonly) int Length; -@property (readonly) id object; +@property (readonly, retain) id object; + (double) D; - (BOOL)is3bar; // watch out @@ -170,7 +170,7 @@ DEPRECATED @property (retain) NSURL *appStoreReceiptURLY ; - (void) setAppStoreReceiptURLY : (NSURL *)object NS_AVAILABLE; -@property (readonly) id OkToInfer NS_AVAILABLE; +@property (readonly, retain) id OkToInfer NS_AVAILABLE; // Do not infer a property. @property (retain) NSURL *appStoreReceiptURLZ ; @@ -195,7 +195,7 @@ DEPRECATED @class NSMutableDictionary; @interface NSArray -@property (readonly) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); +@property (readonly, copy) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); @property (copy) id (^MyBlock)(id, NSArray *, NSMutableDictionary *); @property (readonly) id (*expressionFuncptr)(id, NSArray *, NSMutableDictionary *); @property id (*MyFuncptr)(id, NSArray *, NSMutableDictionary *); diff --git a/test/ARCMT/objcmt-ns-nonatomic-iosonly.m.result b/test/ARCMT/objcmt-ns-nonatomic-iosonly.m.result index 3eb648b25a..804142ee32 100644 --- a/test/ARCMT/objcmt-ns-nonatomic-iosonly.m.result +++ b/test/ARCMT/objcmt-ns-nonatomic-iosonly.m.result @@ -87,14 +87,14 @@ typedef char BOOL; - (void) Nothing; @property (NS_NONATOMIC_IOSONLY, readonly) int Length; -@property (NS_NONATOMIC_IOSONLY, readonly) id object; +@property (NS_NONATOMIC_IOSONLY, readonly, retain) id object; + (double) D; @property (NS_NONATOMIC_IOSONLY, readonly) void *JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); @property (NS_NONATOMIC_IOSONLY, getter=isIgnoringInteractionEvents, readonly) BOOL ignoringInteractionEvents; @property (NS_NONATOMIC_IOSONLY, getter=getStringValue, retain) NSString *stringValue; @property (NS_NONATOMIC_IOSONLY, getter=getCounterValue, readonly) BOOL counterValue; -@property (NS_NONATOMIC_IOSONLY, getter=getns_dixtionary, readonly) NSDictionary *ns_dixtionary; +@property (NS_NONATOMIC_IOSONLY, getter=getns_dixtionary, readonly, retain) NSDictionary *ns_dixtionary; - (BOOL)is3bar; // watch out - (NSString *)get3foo; // watch out @@ -131,7 +131,7 @@ typedef char BOOL; - (void) Nothing; @property (NS_NONATOMIC_IOSONLY, readonly) int Length; -@property (NS_NONATOMIC_IOSONLY, readonly) id object; +@property (NS_NONATOMIC_IOSONLY, readonly, retain) id object; + (double) D; - (BOOL)is3bar; // watch out @@ -177,7 +177,7 @@ DEPRECATED @property (NS_NONATOMIC_IOSONLY, retain) NSURL *appStoreReceiptURLY ; - (void) setAppStoreReceiptURLY : (NSURL *)object NS_AVAILABLE; -@property (NS_NONATOMIC_IOSONLY, readonly) id OkToInfer NS_AVAILABLE; +@property (NS_NONATOMIC_IOSONLY, readonly, retain) id OkToInfer NS_AVAILABLE; // Do not infer a property. @property (NS_NONATOMIC_IOSONLY, retain) NSURL *appStoreReceiptURLZ ; @@ -202,7 +202,7 @@ DEPRECATED @class NSMutableDictionary; @interface NSArray -@property (NS_NONATOMIC_IOSONLY, readonly) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); +@property (NS_NONATOMIC_IOSONLY, readonly, copy) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); @property (NS_NONATOMIC_IOSONLY, copy) id (^MyBlock)(id, NSArray *, NSMutableDictionary *); @property (NS_NONATOMIC_IOSONLY, readonly) id (*expressionFuncptr)(id, NSArray *, NSMutableDictionary *); @property (NS_NONATOMIC_IOSONLY) id (*MyFuncptr)(id, NSArray *, NSMutableDictionary *); diff --git a/test/ARCMT/objcmt-property.m.result b/test/ARCMT/objcmt-property.m.result index 2d19ddb680..c6380c8052 100644 --- a/test/ARCMT/objcmt-property.m.result +++ b/test/ARCMT/objcmt-property.m.result @@ -80,14 +80,14 @@ typedef char BOOL; - (void) Nothing; @property (nonatomic, readonly) int Length; -@property (nonatomic, readonly) id object; +@property (nonatomic, readonly, retain) id object; + (double) D; @property (nonatomic, readonly) void *JSObject WEBKIT_OBJC_METHOD_ANNOTATION(AVAILABLE_WEBKIT_VERSION_3_0_AND_LATER); @property (nonatomic, getter=isIgnoringInteractionEvents, readonly) BOOL ignoringInteractionEvents; @property (nonatomic, getter=getStringValue, retain) NSString *stringValue; @property (nonatomic, getter=getCounterValue, readonly) BOOL counterValue; -@property (nonatomic, getter=getns_dixtionary, readonly) NSDictionary *ns_dixtionary; +@property (nonatomic, getter=getns_dixtionary, readonly, retain) NSDictionary *ns_dixtionary; - (BOOL)is3bar; // watch out - (NSString *)get3foo; // watch out @@ -124,7 +124,7 @@ typedef char BOOL; - (void) Nothing; @property (nonatomic, readonly) int Length; -@property (nonatomic, readonly) id object; +@property (nonatomic, readonly, retain) id object; + (double) D; - (BOOL)is3bar; // watch out @@ -170,7 +170,7 @@ DEPRECATED @property (nonatomic, retain) NSURL *appStoreReceiptURLY ; - (void) setAppStoreReceiptURLY : (NSURL *)object NS_AVAILABLE; -@property (nonatomic, readonly) id OkToInfer NS_AVAILABLE; +@property (nonatomic, readonly, retain) id OkToInfer NS_AVAILABLE; // Do not infer a property. @property (nonatomic, retain) NSURL *appStoreReceiptURLZ ; @@ -195,7 +195,7 @@ DEPRECATED @class NSMutableDictionary; @interface NSArray -@property (nonatomic, readonly) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); +@property (nonatomic, readonly, copy) id (^expressionBlock)(id, NSArray *, NSMutableDictionary *); @property (nonatomic, copy) id (^MyBlock)(id, NSArray *, NSMutableDictionary *); @property (nonatomic, readonly) id (*expressionFuncptr)(id, NSArray *, NSMutableDictionary *); @property (nonatomic) id (*MyFuncptr)(id, NSArray *, NSMutableDictionary *);