r187004 - ObjectiveC migrator: start migrating to

Fariborz Jahanian fjahanian at apple.com
Tue Jul 23 16:34:43 PDT 2013


Author: fjahanian
Date: Tue Jul 23 18:34:42 2013
New Revision: 187004

URL: http://llvm.org/viewvc/llvm-project?rev=187004&view=rev
Log:
ObjectiveC migrator: start migrating to
instancetype methods.

Added:
    cfe/trunk/test/ARCMT/objcmt-instancetype.m
    cfe/trunk/test/ARCMT/objcmt-instancetype.m.result
Modified:
    cfe/trunk/lib/ARCMigrate/ObjCMT.cpp

Modified: cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ObjCMT.cpp?rev=187004&r1=187003&r2=187004&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ObjCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ObjCMT.cpp Tue Jul 23 18:34:42 2013
@@ -39,6 +39,8 @@ class ObjCMigrateASTConsumer : public AS
   void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
                      const TypedefDecl *TypedefDcl);
   void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
+  void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
+                                 ObjCMethodDecl *OM);
 
 public:
   std::string MigrateDir;
@@ -547,10 +549,9 @@ void ObjCMigrateASTConsumer::migrateNSEn
   Editor->commit(commit);
 }
 
-static void
-migrateMethodInstanceType(ASTContext &Ctx,
-                          ObjCContainerDecl *CDecl,
-                          ObjCMethodDecl *OM) {
+void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
+                                                       ObjCContainerDecl *CDecl,
+                                                       ObjCMethodDecl *OM) {
   ObjCInstanceTypeFamily OIT_Family =
     Selector::getInstTypeMethodFamily(OM->getSelector());
   if (OIT_Family == OIT_None)
@@ -571,6 +572,13 @@ migrateMethodInstanceType(ASTContext &Ct
   if (!IDecl || !IDecl->lookupInheritedClass(&Ctx.Idents.get("NSArray")))
     return;
   
+  TypeSourceInfo *TSInfo =  OM->getResultTypeSourceInfo();
+  TypeLoc TL = TSInfo->getTypeLoc();
+  SourceRange R = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
+  edit::Commit commit(*Editor);
+  std::string ClassString = "instancetype";
+  commit.replace(R, ClassString);
+  Editor->commit(commit);
 }
 
 void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx,

Added: cfe/trunk/test/ARCMT/objcmt-instancetype.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-instancetype.m?rev=187004&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-instancetype.m (added)
+++ cfe/trunk/test/ARCMT/objcmt-instancetype.m Tue Jul 23 18:34:42 2013
@@ -0,0 +1,83 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -objcmt-migrate-property -mt-migrate-directory %t %s -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties -triple x86_64-apple-darwin11
+// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties %s.result
+
+typedef signed char BOOL;
+#define nil ((void*) 0)
+
+ at interface NSObject
++ (id)alloc;
+ at end
+
+ at interface NSString : NSObject
++ (id)stringWithString:(NSString *)string;
+- (id)initWithString:(NSString *)aString;
+ at end
+
+ at interface NSArray : NSObject
+- (id)objectAtIndex:(unsigned long)index;
+- (id)objectAtIndexedSubscript:(int)index;
+ at end
+
+ at interface NSArray (NSArrayCreation)
++ (id)array;
++ (id)arrayWithObject:(id)anObject;
++ (id)arrayWithObjects:(const id [])objects count:(unsigned long)cnt;
++ (id)arrayWithObjects:(id)firstObj, ...;
++ (id)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;
+ at end
+
+ at interface NSMutableArray : NSArray
+- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
+- (void)setObject:(id)object atIndexedSubscript:(int)index;
+ at end
+
+ at interface NSDictionary : NSObject
+- (id)objectForKeyedSubscript:(id)key;
+ at end
+
+ at 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;
++ (id)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;
+ at end
+
+ at interface NSMutableDictionary : NSDictionary
+- (void)setObject:(id)anObject forKey:(id)aKey;
+- (void)setObject:(id)object forKeyedSubscript:(id)key;
+ at end
+
+ at interface NSNumber : NSObject
+ at end
+
+ at interface NSNumber (NSNumberCreation)
++ (NSNumber *)numberWithInt:(int)value;
+ at end
+
+#define M(x) (x)
+#define PAIR(x) @#x, [NSNumber numberWithInt:(x)]
+#define TWO(x) ((x), (x))
+
+void foo() {
+  NSString *str = M([NSString stringWithString:@"foo"]); // expected-warning {{redundant}}
+  str = [[NSString alloc] initWithString:@"foo"]; // expected-warning {{redundant}}
+  NSArray *arr = [NSArray arrayWithArray:@[str]]; // expected-warning {{redundant}}
+  NSDictionary *dict = [NSDictionary dictionaryWithDictionary:@{str: arr}]; // expected-warning {{redundant}}
+}

Added: cfe/trunk/test/ARCMT/objcmt-instancetype.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/objcmt-instancetype.m.result?rev=187004&view=auto
==============================================================================
--- cfe/trunk/test/ARCMT/objcmt-instancetype.m.result (added)
+++ cfe/trunk/test/ARCMT/objcmt-instancetype.m.result Tue Jul 23 18:34:42 2013
@@ -0,0 +1,83 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -objcmt-migrate-property -mt-migrate-directory %t %s -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties -triple x86_64-apple-darwin11
+// RUN: c-arcmt-test -mt-migrate-directory %t | arcmt-test -verify-transformed-files %s.result
+// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fsyntax-only -x objective-c -fobjc-runtime-has-weak -fobjc-arc -fobjc-default-synthesize-properties %s.result
+
+typedef signed char BOOL;
+#define nil ((void*) 0)
+
+ at interface NSObject
++ (id)alloc;
+ at end
+
+ at interface NSString : NSObject
++ (id)stringWithString:(NSString *)string;
+- (id)initWithString:(NSString *)aString;
+ at end
+
+ at interface NSArray : NSObject
+- (id)objectAtIndex:(unsigned long)index;
+- (id)objectAtIndexedSubscript:(int)index;
+ at end
+
+ at 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)initWithObjects:(const id [])objects count:(unsigned long)cnt;
+- (id)initWithObjects:(id)firstObj, ...;
+- (id)initWithArray:(NSArray *)array;
+
+- (id)objectAtIndex:(unsigned long)index;
+ at end
+
+ at interface NSMutableArray : NSArray
+- (void)replaceObjectAtIndex:(unsigned long)index withObject:(id)anObject;
+- (void)setObject:(id)object atIndexedSubscript:(int)index;
+ at end
+
+ at interface NSDictionary : NSObject
+- (id)objectForKeyedSubscript:(id)key;
+ at end
+
+ at 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;
++ (id)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;
+ at end
+
+ at interface NSMutableDictionary : NSDictionary
+- (void)setObject:(id)anObject forKey:(id)aKey;
+- (void)setObject:(id)object forKeyedSubscript:(id)key;
+ at end
+
+ at interface NSNumber : NSObject
+ at end
+
+ at interface NSNumber (NSNumberCreation)
++ (NSNumber *)numberWithInt:(int)value;
+ at end
+
+#define M(x) (x)
+#define PAIR(x) @#x, [NSNumber numberWithInt:(x)]
+#define TWO(x) ((x), (x))
+
+void foo() {
+  NSString *str = M([NSString stringWithString:@"foo"]); // expected-warning {{redundant}}
+  str = [[NSString alloc] initWithString:@"foo"]; // expected-warning {{redundant}}
+  NSArray *arr = [NSArray arrayWithArray:@[str]]; // expected-warning {{redundant}}
+  NSDictionary *dict = [NSDictionary dictionaryWithDictionary:@{str: arr}]; // expected-warning {{redundant}}
+}





More information about the cfe-commits mailing list