]> granicus.if.org Git - clang/commitdiff
ObjectiveC migrator: For 'default' and 'shared' family of
authorFariborz Jahanian <fjahanian@apple.com>
Thu, 10 Oct 2013 18:23:13 +0000 (18:23 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Thu, 10 Oct 2013 18:23:13 +0000 (18:23 +0000)
methods, infer their self's type as their result type.
// rdar://15145218

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

include/clang/Basic/IdentifierTable.h
lib/ARCMigrate/ObjCMT.cpp
lib/Basic/IdentifierTable.cpp
test/ARCMT/objcmt-instancetype-2.m
test/ARCMT/objcmt-instancetype-2.m.result

index 01b8e1f36134d97a98b19c675bbedf6660e2ad55..304ff364bfc95f8d5aced749d03a1749fc24936e 100644 (file)
@@ -587,7 +587,8 @@ enum ObjCInstanceTypeFamily {
   OIT_Array,
   OIT_Dictionary,
   OIT_Singleton,
-  OIT_Init
+  OIT_Init,
+  OIT_ReturnsSelf
 };
 
 /// \brief Smart pointer class that efficiently represents Objective-C method
index a1d7a97842609d425c0ff28ab529ca40f5b64dbf..21ba06cc2cfc51fcf7c2c2d40dca16ab9352989c 100644 (file)
@@ -696,6 +696,28 @@ static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC,
   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,
                                                        ObjCContainerDecl *CDecl,
                                                        ObjCMethodDecl *OM) {
@@ -720,6 +742,9 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
       if (OM->getResultType()->isObjCIdType())
         ReplaceWithInstancetype(*this, OM);
       return;
+    case OIT_ReturnsSelf:
+      migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf);
+      return;
   }
   if (!OM->getResultType()->isObjCIdType())
     return;
@@ -965,7 +990,7 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
     return;
   
   std::string MethodName = MethodIdName->getName();
-  if (OIT_Family == OIT_Singleton) {
+  if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
     StringRef STRefMethodName(MethodName);
     size_t len = 0;
     if (STRefMethodName.startswith("standard"))
@@ -991,7 +1016,10 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
   LoweredMethodName = StringLoweredMethodName;
   if (!LoweredMethodName.startswith(ClassNamePostfix))
     return;
-  ReplaceWithInstancetype(*this, OM);
+  if (OIT_Family == OIT_ReturnsSelf)
+    ReplaceWithClasstype(*this, OM);
+  else
+    ReplaceWithInstancetype(*this, OM);
 }
 
 static bool IsVoidStarType(QualType Ty) {
index 2c70b9933a7dd03393411fc9b026fceb0fd92e0b..500e732eef3478ee6e4f4fead599bb7c7acb493b 100644 (file)
@@ -464,12 +464,12 @@ ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
       if (startsWithWord(name, "array")) return OIT_Array;
       break;
     case 'd':
+      if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
       if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
       break;
     case 's':
-      if (startsWithWord(name, "shared") ||
-          startsWithWord(name, "standard"))
-        return OIT_Singleton;
+      if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
+      if (startsWithWord(name, "standard")) return OIT_Singleton;
     case 'i':
       if (startsWithWord(name, "init")) return OIT_Init;
     default:
index 93db1fdec8d4fb7cf080424a2039b4407f3cfabf..fb59265c4be32b5ca290db346ec35cf661399392 100644 (file)
@@ -80,10 +80,12 @@ typedef enum NSURLBookmarkResolutionOptions {
 
 @interface NSNotificationCenter
 + (id) defaultCenter;
++  sharedCenter;
 @end
 
 @interface UIApplication
 + (id)sharedApplication;
++ defaultApplication;
 @end
 
 //===----------------------------------------------------------------------===//
index 199430b7555b1be74a45b97fea0ceb4eda49e7cf..8837e971ad270fe5ada9be88ff73b325f174b26f 100644 (file)
@@ -79,11 +79,13 @@ typedef enum NSURLBookmarkResolutionOptions {
 @end
 
 @interface NSNotificationCenter
-+ (id) defaultCenter;
++ (NSNotificationCenter*) defaultCenter;
++ (NSNotificationCenter*)  sharedCenter;
 @end
 
 @interface UIApplication
-+ (instancetype)sharedApplication;
++ (UIApplication*)sharedApplication;
++ (UIApplication*) defaultApplication;
 @end
 
 //===----------------------------------------------------------------------===//