]> granicus.if.org Git - clang/commitdiff
ObjectiveC migrator: In suggesting 'instancetype' use clang's
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 28 Aug 2013 21:23:00 +0000 (21:23 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 28 Aug 2013 21:23:00 +0000 (21:23 +0000)
hasRelatedResultType() as it knows of methods which have
related result type by default. Such methods do not need
a redundant 'instancetype'.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189520 91177308-0d34-0410-b5e6-96231b3b80d8

lib/ARCMigrate/ObjCMT.cpp
test/ARCMT/objcmt-arc-cf-annotations.m.result
test/ARCMT/objcmt-instancetype-2.m.result
test/ARCMT/objcmt-instancetype.m.result

index 6f230577df145e1cd9e015857ba413b8dca7a2a6..9b9d6aa10dbd668cefd7d5ed894466348fec86e9 100644 (file)
@@ -660,6 +660,11 @@ static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC,
 void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
                                                        ObjCContainerDecl *CDecl,
                                                        ObjCMethodDecl *OM) {
+  // bail out early and do not suggest 'instancetype' when the method already 
+  // has a related result type,
+  if (OM->hasRelatedResultType())
+    return;
+
   ObjCInstanceTypeFamily OIT_Family =
     Selector::getInstTypeMethodFamily(OM->getSelector());
   
@@ -668,11 +673,15 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
     case OIT_None:
       migrateFactoryMethod(Ctx, CDecl, OM);
       return;
+    case OIT_Array:
+      ClassName = "NSArray";
+      break;
+    case OIT_Dictionary:
+      ClassName = "NSDictionary";
+      break;
     // 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:
-    case OIT_Dictionary:
     case OIT_MemManage:
       return;
     case OIT_Singleton:
index 2c013301f5d34963de32e211341e5531c5d2be71..a0fab2c24eee34c25bdb404d3ab4d4954ca42554 100644 (file)
@@ -181,12 +181,12 @@ NSFastEnumerationState;
 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
 - (NSUInteger)count;
 - (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)));
++ (instancetype)arrayWithObject:(id)anObject;
++ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
++ (instancetype)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 NSArray (NSArrayCreation)  + (instancetype)array;
 @end       @interface NSAutoreleasePool : NSObject {
 }
 - (void)drain;
@@ -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;
-+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
-+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
++ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;
++ (instancetype)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;
-@end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (id)dictionaryWithCapacity:(NSUInteger)numItems;
+@end  @interface NSMutableDictionary (NSMutableDictionaryCreation)  + (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
 @end  typedef double CGFloat;
 struct CGSize {
 };
@@ -350,7 +350,7 @@ CF_IMPLICIT_BRIDGING_DISABLED
 
 @interface NSMutableArray : NSObject
 - (void)addObject:(id)object;
-+ (id)array;
++ (instancetype)array;
 @end
 
 // This is how NSMakeCollectable is declared in the OS X 10.8 headers.
index 434f026f55857422fc3f13c2532c438c9cf79c79..b6bd05adeb66735d19d87d3e9fae47c910380f33 100644 (file)
@@ -51,7 +51,7 @@ typedef enum NSURLBookmarkResolutionOptions {
 @interface NSMutableDictionary @end
 
 @interface NSMutableDictionary (NSSharedKeySetDictionary)
-+ (id )dictionaryWithSharedKeySet:(id)keyset __attribute__((availability(macosx,introduced=10.8)));
++ (instancetype )dictionaryWithSharedKeySet:(id)keyset __attribute__((availability(macosx,introduced=10.8)));
 @end
 
 @interface NSProtocolChecker
index b8c3fb6ec7a2c0da8b5f9a8c35daa4d0116cae56..6303232ba67604c8aa17f257552ee2510f345820 100644 (file)
@@ -21,11 +21,11 @@ typedef signed char BOOL;
 @end
 
 @interface NSArray (NSArrayCreation)
-+ (id)array;
-+ (id)arrayWithObject:(id)anObject;
-+ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
-+ (id)arrayWithObjects:(id)firstObj, ...;
-+ arrayWithArray:(NSArray *)array;
++ (instancetype)array;
++ (instancetype)arrayWithObject:(id)anObject;
++ (instancetype)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
++ (instancetype)arrayWithObjects:(id)firstObj, ...;
++ (instancetype) arrayWithArray:(NSArray *)array;
 
 - (id)initWithObjects:(const id [])objects count:(unsigned long)cnt;
 - (id)initWithObjects:(id)firstObj, ...;
@@ -44,12 +44,12 @@ typedef signed char BOOL;
 @end
 
 @interface NSDictionary (NSDictionaryCreation)
-+ (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;
++ (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;
 
 - (id)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(unsigned long)cnt;
 - (id)initWithObjectsAndKeys:(id)firstObject, ...;