ObjectiveC migrator: For 'default' and 'shared' family of

methods, infer their self's type as their result type.
// rdar://15145218

llvm-svn: 192377
This commit is contained in:
Fariborz Jahanian 2013-10-10 18:23:13 +00:00
parent 9b971f9590
commit 7c87b43d28
5 changed files with 41 additions and 8 deletions

View File

@ -587,7 +587,8 @@ enum ObjCInstanceTypeFamily {
OIT_Array, OIT_Array,
OIT_Dictionary, OIT_Dictionary,
OIT_Singleton, OIT_Singleton,
OIT_Init OIT_Init,
OIT_ReturnsSelf
}; };
/// \brief Smart pointer class that efficiently represents Objective-C method /// \brief Smart pointer class that efficiently represents Objective-C method

View File

@ -696,6 +696,28 @@ static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC,
ASTC.Editor->commit(commit); ASTC.Editor->commit(commit);
} }
static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC,
ObjCMethodDecl *OM) {
ObjCInterfaceDecl *IDecl = OM->getClassInterface();
SourceRange R;
std::string ClassString;
if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) {
TypeLoc TL = TSInfo->getTypeLoc();
R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); {
ClassString = IDecl->getName();
ClassString += "*";
}
}
else {
R = SourceRange(OM->getLocStart(), OM->getLocStart());
ClassString = "+ (";
ClassString += IDecl->getName(); ClassString += "*)";
}
edit::Commit commit(*ASTC.Editor);
commit.replace(R, ClassString);
ASTC.Editor->commit(commit);
}
void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
ObjCContainerDecl *CDecl, ObjCContainerDecl *CDecl,
ObjCMethodDecl *OM) { ObjCMethodDecl *OM) {
@ -720,6 +742,9 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
if (OM->getResultType()->isObjCIdType()) if (OM->getResultType()->isObjCIdType())
ReplaceWithInstancetype(*this, OM); ReplaceWithInstancetype(*this, OM);
return; return;
case OIT_ReturnsSelf:
migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf);
return;
} }
if (!OM->getResultType()->isObjCIdType()) if (!OM->getResultType()->isObjCIdType())
return; return;
@ -965,7 +990,7 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
return; return;
std::string MethodName = MethodIdName->getName(); std::string MethodName = MethodIdName->getName();
if (OIT_Family == OIT_Singleton) { if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
StringRef STRefMethodName(MethodName); StringRef STRefMethodName(MethodName);
size_t len = 0; size_t len = 0;
if (STRefMethodName.startswith("standard")) if (STRefMethodName.startswith("standard"))
@ -991,6 +1016,9 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
LoweredMethodName = StringLoweredMethodName; LoweredMethodName = StringLoweredMethodName;
if (!LoweredMethodName.startswith(ClassNamePostfix)) if (!LoweredMethodName.startswith(ClassNamePostfix))
return; return;
if (OIT_Family == OIT_ReturnsSelf)
ReplaceWithClasstype(*this, OM);
else
ReplaceWithInstancetype(*this, OM); ReplaceWithInstancetype(*this, OM);
} }

View File

@ -464,12 +464,12 @@ ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
if (startsWithWord(name, "array")) return OIT_Array; if (startsWithWord(name, "array")) return OIT_Array;
break; break;
case 'd': case 'd':
if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
if (startsWithWord(name, "dictionary")) return OIT_Dictionary; if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
break; break;
case 's': case 's':
if (startsWithWord(name, "shared") || if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
startsWithWord(name, "standard")) if (startsWithWord(name, "standard")) return OIT_Singleton;
return OIT_Singleton;
case 'i': case 'i':
if (startsWithWord(name, "init")) return OIT_Init; if (startsWithWord(name, "init")) return OIT_Init;
default: default:

View File

@ -80,10 +80,12 @@ typedef enum NSURLBookmarkResolutionOptions {
@interface NSNotificationCenter @interface NSNotificationCenter
+ (id) defaultCenter; + (id) defaultCenter;
+ sharedCenter;
@end @end
@interface UIApplication @interface UIApplication
+ (id)sharedApplication; + (id)sharedApplication;
+ defaultApplication;
@end @end
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//

View File

@ -79,11 +79,13 @@ typedef enum NSURLBookmarkResolutionOptions {
@end @end
@interface NSNotificationCenter @interface NSNotificationCenter
+ (id) defaultCenter; + (NSNotificationCenter*) defaultCenter;
+ (NSNotificationCenter*) sharedCenter;
@end @end
@interface UIApplication @interface UIApplication
+ (instancetype)sharedApplication; + (UIApplication*)sharedApplication;
+ (UIApplication*) defaultApplication;
@end @end
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//