From: Fariborz Jahanian Date: Thu, 1 Aug 2013 22:29:32 +0000 (+0000) Subject: ObjectiveC migrator. Migrate to instancetype return type X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=59944677b0576c19a08db92a6b45619077a00baa;p=clang ObjectiveC migrator. Migrate to instancetype return type for mehods with certain prefix selector matching their class names' suffix. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187626 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/IdentifierTable.h b/include/clang/Basic/IdentifierTable.h index 8e12b972ed..0bc13e6555 100644 --- a/include/clang/Basic/IdentifierTable.h +++ b/include/clang/Basic/IdentifierTable.h @@ -586,10 +586,7 @@ enum ObjCInstanceTypeFamily { OIT_None, OIT_Array, OIT_Dictionary, - OIT_MemManage, - OIT_NSString, - OIT_NSSet, - OIT_NSURL + OIT_MemManage }; /// \brief Smart pointer class that efficiently represents Objective-C method diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 2b2f9224e9..62f3c6b7e7 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -41,6 +41,8 @@ class ObjCMigrateASTConsumer : public ASTConsumer { void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl); void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, ObjCMethodDecl *OM); + void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl, + ObjCMethodDecl *OM); public: std::string MigrateDir; @@ -549,13 +551,34 @@ void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, Editor->commit(commit); } +static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC, + ObjCMethodDecl *OM) { + SourceRange R; + std::string ClassString; + if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { + TypeLoc TL = TSInfo->getTypeLoc(); + R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); + ClassString = "instancetype"; + } + else { + R = SourceRange(OM->getLocStart(), OM->getLocStart()); + ClassString = OM->isInstanceMethod() ? '-' : '+'; + ClassString += " (instancetype)"; + } + edit::Commit commit(*ASTC.Editor); + commit.replace(R, ClassString); + ASTC.Editor->commit(commit); +} + void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, ObjCMethodDecl *OM) { ObjCInstanceTypeFamily OIT_Family = Selector::getInstTypeMethodFamily(OM->getSelector()); - if (OIT_Family == OIT_None) + if (OIT_Family == OIT_None) { + migrateFactoryMethod(Ctx, CDecl, OM); return; + } std::string ClassName; switch (OIT_Family) { case OIT_Array: @@ -581,24 +604,11 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, IDecl = ImpDecl->getClassInterface(); } if (!IDecl || - !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) + !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { + migrateFactoryMethod(Ctx, CDecl, OM); return; - - SourceRange R; - std::string ClassString; - if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { - TypeLoc TL = TSInfo->getTypeLoc(); - R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); - ClassString = "instancetype"; - } - else { - R = SourceRange(OM->getLocStart(), OM->getLocStart()); - ClassString = OM->isInstanceMethod() ? '-' : '+'; - ClassString += " (instancetype)"; } - edit::Commit commit(*Editor); - commit.replace(R, ClassString); - Editor->commit(commit); + ReplaceWithInstancetype(*this, OM); } void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx, @@ -612,6 +622,42 @@ void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx, } } +void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, + ObjCContainerDecl *CDecl, + ObjCMethodDecl *OM) { + if (OM->isInstanceMethod() || !OM->getResultType()->isObjCIdType()) + return; + + // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class + // NSYYYNamE with matching names be at least 3 characters long. + ObjCInterfaceDecl *IDecl = dyn_cast(CDecl); + if (!IDecl) { + if (ObjCCategoryDecl *CatDecl = dyn_cast(CDecl)) + IDecl = CatDecl->getClassInterface(); + else if (ObjCImplDecl *ImpDecl = dyn_cast(CDecl)) + IDecl = ImpDecl->getClassInterface(); + } + if (!IDecl) + return; + + StringRef ClassName = IDecl->getName(); + if (!ClassName.startswith("NS")) + return; + + ClassName = ClassName.lower(); + IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); + StringRef MethodName = MethodIdName->getName(); + StringRef MethodNamePrefix = MethodName.substr(0, 3).lower(); + size_t Ix = ClassName.rfind(MethodNamePrefix); + if (Ix == StringRef::npos) + return; + StringRef ClassNamePostfix = ClassName.substr(Ix); + MethodName = MethodName.lower(); + if (!MethodName.startswith(ClassNamePostfix)) + return; + ReplaceWithInstancetype(*this, OM); +} + namespace { class RewritesReceiver : public edit::EditsReceiver { diff --git a/lib/Basic/IdentifierTable.cpp b/lib/Basic/IdentifierTable.cpp index 96d9e56b6c..3572930903 100644 --- a/lib/Basic/IdentifierTable.cpp +++ b/lib/Basic/IdentifierTable.cpp @@ -474,14 +474,6 @@ ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) { case 'r': if (startsWithWord(name, "retain")) return OIT_MemManage; break; - case 's': - if (startsWithWord(name, "string")) return OIT_NSString; - else - if (startsWithWord(name, "set")) return OIT_NSSet; - break; - case 'U': - if (startsWithWord(name, "URL")) return OIT_NSURL; - break; default: break; } diff --git a/test/ARCMT/objcmt-instancetype-2.m.result b/test/ARCMT/objcmt-instancetype-2.m.result new file mode 100644 index 0000000000..ba66480653 --- /dev/null +++ b/test/ARCMT/objcmt-instancetype-2.m.result @@ -0,0 +1,76 @@ +// 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 unsigned int NSUInteger; +typedef int NSInteger; +typedef char BOOL; +@class NSData, NSError, NSProtocolChecker, NSObject; +@class NSPortNameServer, NSTimeZone; + +@interface NSMutableString +@end + +@interface NSString @end + +@class NSString, NSURL; +@interface NSString (NSStringDeprecated) ++ (instancetype)stringWithContentsOfFile:(NSString *)path __attribute__((availability(macosx,introduced=10.0 ,deprecated=10.4,message="" ))); ++ (instancetype)stringWithContentsOfURL:(NSURL *)url __attribute__((availability(macosx,introduced=10.0 ,deprecated=10.4,message="" ))); ++ (instancetype)stringWithCString:(const char *)bytes length:(NSUInteger)length __attribute__((availability(macosx,introduced=10.0 ,deprecated=10.4,message="" ))); ++ (instancetype)stringWithCString:(const char *)bytes __attribute__((availability(macosx,introduced=10.0 ,deprecated=10.4,message="" ))); +@end + + +typedef enum NSURLBookmarkResolutionOptions { + Bookmark +} NSURLBookmarkResolutionOptions; + +@interface NSURL ++ (instancetype)URLWithString:(NSString *)URLString; ++ (instancetype)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL; ++ (instancetype)URLByResolvingBookmarkData:(NSData *)bookmarkData options:(NSURLBookmarkResolutionOptions)options relativeToURL:(NSURL *)relativeURL bookmarkDataIsStale:(BOOL *)isStale error:(NSError **)error __attribute__((availability(macosx,introduced=10.6))); +@end + +@class NSDictionary; +@interface NSError ++ (instancetype)errorWithDomain:(NSString *)domain code:(NSInteger)code userInfo:(NSDictionary *)dict; +@end + + +@interface NSMutableString (NSMutableStringExtensionMethods) ++ (instancetype)stringWithCapacity:(NSUInteger)capacity; +@end + +@interface NSMutableData ++ (instancetype)dataWithCapacity:(NSUInteger)aNumItems; ++ (instancetype)dataWithLength:(NSUInteger)length; +@end + +@interface NSMutableDictionary @end + +@interface NSMutableDictionary (NSSharedKeySetDictionary) ++ (instancetype )dictionaryWithSharedKeySet:(id)keyset __attribute__((availability(macosx,introduced=10.8))); +@end + +@interface NSProtocolChecker ++ (instancetype)protocolCheckerWithTarget:(NSObject *)anObject protocol:(Protocol *)aProtocol; +@end + +@interface NSConnection ++ (instancetype)connectionWithRegisteredName:(NSString *)name host:(NSString *)hostName; ++ (instancetype)connectionWithRegisteredName:(NSString *)name host:(NSString *)hostName usingNameServer:(NSPortNameServer *)server; +@end + +@interface NSDate ++ (instancetype)dateWithString:(NSString *)aString __attribute__((availability(macosx,introduced=10.4))); +@end + +@interface NSCalendarDate : NSDate ++ (instancetype)calendarDate __attribute__((availability(macosx,introduced=10.4))); ++ (instancetype)dateWithString:(NSString *)description calendarFormat:(NSString *)format locale:(id)locale __attribute__((availability(macosx,introduced=10.4))); ++ (instancetype)dateWithString:(NSString *)description calendarFormat:(NSString *)format __attribute__((availability(macosx,introduced=10.4))); ++ (instancetype)dateWithYear:(NSInteger)year month:(NSUInteger)month day:(NSUInteger)day hour:(NSUInteger)hour minute:(NSUInteger)minute second:(NSUInteger)second timeZone:(NSTimeZone *)aTimeZone __attribute__((availability(macosx,introduced=10.4))); +@end + diff --git a/test/ARCMT/objcmt-instancetype.m.result b/test/ARCMT/objcmt-instancetype.m.result index 7bc554faed..ad8fcaeb9b 100644 --- a/test/ARCMT/objcmt-instancetype.m.result +++ b/test/ARCMT/objcmt-instancetype.m.result @@ -11,7 +11,7 @@ typedef signed char BOOL; @end @interface NSString : NSObject -+ (id)stringWithString:(NSString *)string; ++ (instancetype)stringWithString:(NSString *)string; - (instancetype)initWithString:(NSString *)aString; @end