From 11638f7b922aa0182ab2028ec819001ed2fe8085 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Tue, 23 Jul 2013 22:42:28 +0000 Subject: [PATCH] ObjC migrator: more knobs toward doing instancetype migration. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187000 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/IdentifierTable.h | 4 +-- lib/ARCMigrate/ObjCMT.cpp | 41 +++++++++++++++++++++++++++ lib/Basic/IdentifierTable.cpp | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/include/clang/Basic/IdentifierTable.h b/include/clang/Basic/IdentifierTable.h index 86f9d9b61e..8e12b972ed 100644 --- a/include/clang/Basic/IdentifierTable.h +++ b/include/clang/Basic/IdentifierTable.h @@ -637,8 +637,6 @@ class Selector { } static ObjCMethodFamily getMethodFamilyImpl(Selector sel); - - static ObjCInstanceTypeFamily getInstTypeMethodFamilyImpl(Selector sel); public: friend class SelectorTable; // only the SelectorTable can create these @@ -714,6 +712,8 @@ public: static Selector getTombstoneMarker() { return Selector(uintptr_t(-2)); } + + static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel); }; /// \brief This table allows us to fully hide how we implement diff --git a/lib/ARCMigrate/ObjCMT.cpp b/lib/ARCMigrate/ObjCMT.cpp index 153ebbcca1..b5aaa3b196 100644 --- a/lib/ARCMigrate/ObjCMT.cpp +++ b/lib/ARCMigrate/ObjCMT.cpp @@ -38,6 +38,7 @@ class ObjCMigrateASTConsumer : public ASTConsumer { const ObjCImplementationDecl *ImpDecl); void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, const TypedefDecl *TypedefDcl); + void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl); public: std::string MigrateDir; @@ -546,6 +547,43 @@ void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, Editor->commit(commit); } +static void +migrateMethodInstanceType(ASTContext &Ctx, + ObjCContainerDecl *CDecl, + ObjCMethodDecl *OM) { + ObjCInstanceTypeFamily OIT_Family = + Selector::getInstTypeMethodFamily(OM->getSelector()); + if (OIT_Family == OIT_None) + return; + // TODO. Many more to come + if (OIT_Family != OIT_Array) + return; + if (!OM->getResultType()->isObjCIdType()) + return; + + 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 || !IDecl->lookupInheritedClass(&Ctx.Idents.get("NSArray"))) + return; + +} + +void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx, + ObjCContainerDecl *CDecl) { + // migrate methods which can have instancetype as their result type. + for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), + MEnd = CDecl->meth_end(); + M != MEnd; ++M) { + ObjCMethodDecl *Method = (*M); + migrateMethodInstanceType(Ctx, CDecl, Method); + } +} + namespace { class RewritesReceiver : public edit::EditsReceiver { @@ -584,6 +622,9 @@ void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { if (const TypedefDecl *TD = dyn_cast(*N)) migrateNSEnumDecl(Ctx, ED, TD); } + // migrate methods which can have instancetype as their result type. + if (ObjCContainerDecl *CDecl = dyn_cast(*D)) + migrateInstanceType(Ctx, CDecl); } Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); diff --git a/lib/Basic/IdentifierTable.cpp b/lib/Basic/IdentifierTable.cpp index faff4d8863..fa021f0501 100644 --- a/lib/Basic/IdentifierTable.cpp +++ b/lib/Basic/IdentifierTable.cpp @@ -452,7 +452,7 @@ ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) { return OMF_None; } -ObjCInstanceTypeFamily Selector::getInstTypeMethodFamilyImpl(Selector sel) { +ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) { IdentifierInfo *first = sel.getIdentifierInfoForSlot(0); if (!first) return OIT_None; -- 2.40.0