r189514 - For methods where clang automatically infers instancetype from the selector

Douglas Gregor dgregor at apple.com
Wed Aug 28 14:09:31 PDT 2013


On Aug 28, 2013, at 1:49 PM, Fariborz Jahanian <fjahanian at apple.com> wrote:

> Author: fjahanian
> Date: Wed Aug 28 15:49:58 2013
> New Revision: 189514
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=189514&view=rev
> Log:
> For methods where clang automatically infers instancetype from the selector 
> (e.g., all -init* methods), no need to suggest "instancetype" because it 
> is redundant.
> 
> Modified:
>    cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
>    cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result
>    cfe/trunk/test/ARCMT/objcmt-instancetype-2.m.result
>    cfe/trunk/test/ARCMT/objcmt-instancetype.m.result
> 
> Modified: cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ObjCMT.cpp?rev=189514&r1=189513&r2=189514&view=diff
> ==============================================================================
> --- cfe/trunk/lib/ARCMigrate/ObjCMT.cpp (original)
> +++ cfe/trunk/lib/ARCMigrate/ObjCMT.cpp Wed Aug 28 15:49:58 2013
> @@ -668,15 +668,13 @@ void ObjCMigrateASTConsumer::migrateMeth
>     case OIT_None:
>       migrateFactoryMethod(Ctx, CDecl, OM);
>       return;
> +    // For methods where Clang automatically infers instancetype from the selector 
> +    // (e.g., all -init* methods), we should not suggest "instancetype" because it 
> +    // is redundant,
>     case OIT_Array:
> -      ClassName = "NSArray";
> -      break;
>     case OIT_Dictionary:
> -      ClassName = "NSDictionary";
> -      break;
>     case OIT_MemManage:
> -      ClassName = "NSObject";
> -      break;
> +      return;
>     case OIT_Singleton:
>       migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
>       return;

This is just checking a few special cases. What we want is to avoid adding ‘instancetype’ when the method already has a related result type, which you can check directly with ObjCMethodDecl::hasRelatedResultType().

For example, migrateFactoryMethod() should replace the “== Ctx.getObjCInstanceType()” check with a call to hasRelatedResultType().

  if (OM->isInstanceMethod() ||
      OM->getResultType() == Ctx.getObjCInstanceType() ||
      !OM->getResultType()->isObjCIdType())
    return;

And migrateMethodInstanceType() should bail out early if it sees that the method already has a related result type.

	- Doug

> Modified: cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result?rev=189514&r1=189513&r2=189514&view=diff
> ==============================================================================
> --- cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result (original)
> +++ cfe/trunk/test/ARCMT/objcmt-arc-cf-annotations.m.result Wed Aug 28 15:49:58 2013
> @@ -154,8 +154,8 @@ typedef struct _NSZone NSZone;
> @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder;
> @end
> @interface NSObject <NSObject> {}
> -+ (instancetype)allocWithZone:(NSZone *)zone;
> -+ (instancetype)alloc;
> ++ (id)allocWithZone:(NSZone *)zone;
> ++ (id)alloc;
> + (id)new;
> - (void)dealloc;
> @end
> @@ -174,19 +174,19 @@ NSFastEnumerationState;
> @end
> @interface NSNumber : NSValue
> - (char)charValue;
> -- (instancetype)initWithInt:(int)value;
> +- (id)initWithInt:(int)value;
> + (NSNumber *)numberWithInt:(int)value;
> @end
> @class NSString;
> @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
> - (NSUInteger)count;
> -- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
> -+ (instancetype)arrayWithObject:(id)anObject;
> -+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
> -+ (instancetype)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
> -- (instancetype)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
> -- (instancetype)initWithArray:(NSArray *)array;
> - at end  @interface NSArray (NSArrayCreation)  + (instancetype)array;
> +- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt;
> ++ (id)arrayWithObject:(id)anObject;
> ++ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
> ++ (id)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
> +- (id)initWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
> +- (id)initWithArray:(NSArray *)array;
> + at end  @interface NSArray (NSArrayCreation)  + (id)array;
> @end       @interface NSAutoreleasePool : NSObject {
> }
> - (void)drain;
> @@ -198,7 +198,7 @@ typedef double NSTimeInterval;
> - (NSUInteger)length;
> - (NSString *)stringByAppendingString:(NSString *)aString;
> - ( const char *)UTF8String;
> -- (instancetype)initWithUTF8String:(const char *)nullTerminatedCString;
> +- (id)initWithUTF8String:(const char *)nullTerminatedCString;
> + (instancetype)stringWithUTF8String:(const char *)nullTerminatedCString;
> @end        @class NSString, NSURL, NSError;
> @interface NSData : NSObject <NSCopying, NSMutableCopying, NSCoding>  - (NSUInteger)length;
> @@ -207,12 +207,12 @@ typedef double NSTimeInterval;
> @end   @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary;
> @interface NSDictionary : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
> - (NSUInteger)count;
> -+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> -+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
> ++ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> ++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
> @end
> @interface NSMutableDictionary : NSDictionary  - (void)removeObjectForKey:(id)aKey;
> - (void)setObject:(id)anObject forKey:(id)aKey;
> - at end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
> + at end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (id)dictionaryWithCapacity:(NSUInteger)numItems;
> @end  typedef double CGFloat;
> struct CGSize {
> };
> @@ -254,7 +254,7 @@ extern DADiskRef DADiskCreateFromBSDName
> extern DADiskRef DADiskCreateFromIOMedia( CFAllocatorRef allocator, DASessionRef session, io_service_t media ) CF_RETURNS_RETAINED;
> extern CFDictionaryRef DADiskCopyDescription( DADiskRef disk ) CF_RETURNS_RETAINED;
> extern DADiskRef DADiskCopyWholeDisk( DADiskRef disk ) CF_RETURNS_RETAINED;
> - at interface NSTask : NSObject - (instancetype)init;
> + at interface NSTask : NSObject - (id)init;
> @end                    typedef struct CGColorSpace *CGColorSpaceRef;
> typedef struct CGImage *CGImageRef;
> typedef struct CGLayer *CGLayerRef;
> @@ -350,7 +350,7 @@ CF_IMPLICIT_BRIDGING_DISABLED
> 
> @interface NSMutableArray : NSObject
> - (void)addObject:(id)object;
> -+ (instancetype)array;
> ++ (id)array;
> @end
> 
> // This is how NSMakeCollectable is declared in the OS X 10.8 headers.
> @@ -677,7 +677,7 @@ static void rdar_6659160(char *inkind, c
> //  conventions with respect to 'return'ing ownership.
> @interface PR3677: NSObject @end
> @implementation PR3677
> -+ (instancetype)allocWithZone:(NSZone *)inZone {
> ++ (id)allocWithZone:(NSZone *)inZone {
>   return [super allocWithZone:inZone];  // no-warning
> }
> @end
> @@ -846,16 +846,16 @@ CF_IMPLICIT_BRIDGING_DISABLED
> @interface RDar6320065 : NSObject {
>   NSString *_foo;
> }
> -- (instancetype)initReturningNewClass;
> +- (id)initReturningNewClass;
> - (id)_initReturningNewClassBad;
> -- (instancetype)initReturningNewClassBad2;
> +- (id)initReturningNewClassBad2;
> @end
> 
> @interface RDar6320065Subclass : RDar6320065
> @end
> 
> @implementation RDar6320065
> -- (instancetype)initReturningNewClass {
> +- (id)initReturningNewClass {
>   [self release];
>   self = [[RDar6320065Subclass alloc] init]; // no-warning
>   return self;
> @@ -865,7 +865,7 @@ CF_IMPLICIT_BRIDGING_DISABLED
>   [[RDar6320065Subclass alloc] init]; // expected-warning {{leak}}
>   return self;
> }
> -- (instancetype)initReturningNewClassBad2 {
> +- (id)initReturningNewClassBad2 {
>   [self release];
>   self = [[RDar6320065Subclass alloc] init];
>   return [self autorelease]; // expected-warning{{Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected}}
> @@ -1580,7 +1580,7 @@ void test_blocks_1_indirect_retain_via_c
> 
> @interface Rdar8015556 : NSObject {} @end
> @implementation Rdar8015556
> -- (instancetype)retain {
> +- (id)retain {
>   return [super retain];
> }
> @end
> @@ -1796,11 +1796,11 @@ void rdar9658496() {
> 
> // Support annotations with method families.
> @interface RDar10824732 : NSObject
> -- (instancetype)initWithObj:(id CF_CONSUMED)obj;
> +- (id)initWithObj:(id CF_CONSUMED)obj;
> @end
> 
> @implementation RDar10824732
> -- (instancetype)initWithObj:(id)obj {
> +- (id)initWithObj:(id)obj {
>   [obj release];
>   return [super init];
> }
> @@ -2068,12 +2068,12 @@ void testCustomReturnsNotRetained() {
> // Don't print variables which are out of the current scope.
> //===----------------------------------------------------------------------===//
> @interface MyObj12706177 : NSObject
> --(instancetype)initX;
> +-(id)initX;
> +(void)test12706177;
> @end
> static int Cond;
> @implementation MyObj12706177
> --(instancetype)initX {
> +-(id)initX {
>   if (Cond)
>     return 0;
>   self = [super init];
> 
> Modified: cfe/trunk/test/ARCMT/objcmt-instancetype-2.m.result
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-instancetype-2.m.result?rev=189514&r1=189513&r2=189514&view=diff
> ==============================================================================
> --- cfe/trunk/test/ARCMT/objcmt-instancetype-2.m.result (original)
> +++ cfe/trunk/test/ARCMT/objcmt-instancetype-2.m.result Wed Aug 28 15:49:58 2013
> @@ -51,7 +51,7 @@ typedef enum NSURLBookmarkResolutionOpti
> @interface NSMutableDictionary @end
> 
> @interface NSMutableDictionary (NSSharedKeySetDictionary)
> -+ (instancetype )dictionaryWithSharedKeySet:(id)keyset __attribute__((availability(macosx,introduced=10.8)));
> ++ (id )dictionaryWithSharedKeySet:(id)keyset __attribute__((availability(macosx,introduced=10.8)));
> @end
> 
> @interface NSProtocolChecker
> 
> Modified: cfe/trunk/test/ARCMT/objcmt-instancetype.m.result
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-instancetype.m.result?rev=189514&r1=189513&r2=189514&view=diff
> ==============================================================================
> --- cfe/trunk/test/ARCMT/objcmt-instancetype.m.result (original)
> +++ cfe/trunk/test/ARCMT/objcmt-instancetype.m.result Wed Aug 28 15:49:58 2013
> @@ -7,12 +7,12 @@ typedef signed char BOOL;
> #define nil ((void*) 0)
> 
> @interface NSObject
> -+ (instancetype)alloc;
> ++ (id)alloc;
> @end
> 
> @interface NSString : NSObject
> + (instancetype)stringWithString:(NSString *)string;
> -- (instancetype)initWithString:(NSString *)aString;
> +- (id)initWithString:(NSString *)aString;
> @end
> 
> @interface NSArray : NSObject
> @@ -21,15 +21,15 @@ typedef signed char BOOL;
> @end
> 
> @interface NSArray (NSArrayCreation)
> -+ (instancetype)array;
> -+ (instancetype)arrayWithObject:(id)anObject;
> -+ (instancetype)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
> -+ (instancetype)arrayWithObjects:(id)firstObj, ...;
> -+ (instancetype) arrayWithArray:(NSArray *)array;
> -
> -- (instancetype)initWithObjects:(const id [])objects count:(unsigned long)cnt;
> -- (instancetype)initWithObjects:(id)firstObj, ...;
> -- (instancetype)initWithArray:(NSArray *)array;
> ++ (id)array;
> ++ (id)arrayWithObject:(id)anObject;
> ++ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
> ++ (id)arrayWithObjects:(id)firstObj, ...;
> ++ arrayWithArray:(NSArray *)array;
> +
> +- (id)initWithObjects:(const id [])objects count:(unsigned long)cnt;
> +- (id)initWithObjects:(id)firstObj, ...;
> +- (id)initWithArray:(NSArray *)array;
> 
> - (id)objectAtIndex:(unsigned long)index;
> @end
> @@ -44,17 +44,17 @@ typedef signed char BOOL;
> @end
> 
> @interface NSDictionary (NSDictionaryCreation)
> -+ (instancetype)dictionary;
> -+ (instancetype)dictionaryWithObject:(id)object forKey:(id)key;
> -+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
> -+ (instancetype) dictionaryWithObjectsAndKeys:(id)firstObject, ...;
> -+ (instancetype)dictionaryWithDictionary:(NSDictionary *)dict;
> -+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> -
> -- (instancetype)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
> -- (instancetype)initWithObjectsAndKeys:(id)firstObject, ...;
> -- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;
> -- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> ++ (id)dictionary;
> ++ (id)dictionaryWithObject:(id)object forKey:(id)key;
> ++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
> ++ dictionaryWithObjectsAndKeys:(id)firstObject, ...;
> ++ (id)dictionaryWithDictionary:(NSDictionary *)dict;
> ++ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> +
> +- (id)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
> +- (id)initWithObjectsAndKeys:(id)firstObject, ...;
> +- (id)initWithDictionary:(NSDictionary *)otherDictionary;
> +- (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
> 
> - (id)objectForKey:(id)aKey;
> @end
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130828/64179be5/attachment.html>


More information about the cfe-commits mailing list