From c0d1b6aee0046ef5ca87e82dae889589b69dd232 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 28 Aug 2013 20:49:58 +0000 Subject: [PATCH] For methods where clang automatically infers instancetype from the selector (e.g., all -init* methods), no need to suggest "instancetype" because it is redundant. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189514 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ARCMigrate/ObjCMT.cpp | 10 ++-- test/ARCMT/objcmt-arc-cf-annotations.m.result | 52 +++++++++---------- test/ARCMT/objcmt-instancetype-2.m.result | 2 +- test/ARCMT/objcmt-instancetype.m.result | 42 +++++++-------- 4 files changed, 52 insertions(+), 54 deletions(-) diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 56a042f7ff..6f230577df 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -668,15 +668,13 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, 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; diff --git a/test/ARCMT/objcmt-arc-cf-annotations.m.result b/test/ARCMT/objcmt-arc-cf-annotations.m.result index cd152cd230..2c013301f5 100644 --- a/test/ARCMT/objcmt-arc-cf-annotations.m.result +++ b/test/ARCMT/objcmt-arc-cf-annotations.m.result @@ -154,8 +154,8 @@ typedef struct _NSZone NSZone; @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end @interface 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 - (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; -@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; +@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 - (NSUInteger)length; @@ -207,12 +207,12 @@ typedef double NSTimeInterval; @end @class NSLocale, NSDate, NSCalendar, NSTimeZone, NSError, NSArray, NSMutableDictionary; @interface NSDictionary : NSObject - (NSUInteger)count; -+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; -+ (instancetype)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt; ++ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys; ++ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt; @end @interface NSMutableDictionary : NSDictionary - (void)removeObjectForKey:(id)aKey; - (void)setObject:(id)anObject forKey:(id)aKey; -@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems; +@end @interface NSMutableDictionary (NSMutableDictionaryCreation) + (id)dictionaryWithCapacity:(NSUInteger)numItems; @end typedef double CGFloat; struct CGSize { }; @@ -254,7 +254,7 @@ extern DADiskRef DADiskCreateFromBSDName( CFAllocatorRef allocator, DASessionRef 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; -@interface NSTask : NSObject - (instancetype)init; +@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, char *inname) // 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_call(void) { @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]; diff --git a/test/ARCMT/objcmt-instancetype-2.m.result b/test/ARCMT/objcmt-instancetype-2.m.result index b6bd05adeb..434f026f55 100644 --- a/test/ARCMT/objcmt-instancetype-2.m.result +++ b/test/ARCMT/objcmt-instancetype-2.m.result @@ -51,7 +51,7 @@ typedef enum NSURLBookmarkResolutionOptions { @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 diff --git a/test/ARCMT/objcmt-instancetype.m.result b/test/ARCMT/objcmt-instancetype.m.result index ad8fcaeb9b..b8c3fb6ec7 100644 --- a/test/ARCMT/objcmt-instancetype.m.result +++ b/test/ARCMT/objcmt-instancetype.m.result @@ -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; ++ (id)array; ++ (id)arrayWithObject:(id)anObject; ++ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt; ++ (id)arrayWithObjects:(id)firstObj, ...; ++ arrayWithArray:(NSArray *)array; -- (instancetype)initWithObjects:(const id [])objects count:(unsigned long)cnt; -- (instancetype)initWithObjects:(id)firstObj, ...; -- (instancetype)initWithArray:(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 -- 2.40.0