ObjectiveC migrator: Add another family of factory
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 2 Aug 2013 20:54:18 +0000 (20:54 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 2 Aug 2013 20:54:18 +0000 (20:54 +0000)
methods which can be migrated to instancetype.

llvm-svn: 187672

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

index 0bc13e6..4a54728 100644 (file)
@@ -586,7 +586,8 @@ enum ObjCInstanceTypeFamily {
   OIT_None,
   OIT_Array,
   OIT_Dictionary,
-  OIT_MemManage
+  OIT_MemManage,
+  OIT_Singleton
 };
 
 /// \brief Smart pointer class that efficiently represents Objective-C method
index bd1f57f..706a4ec 100644 (file)
@@ -42,7 +42,8 @@ class ObjCMigrateASTConsumer : public ASTConsumer {
   void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
                                  ObjCMethodDecl *OM);
   void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
-                            ObjCMethodDecl *OM);
+                            ObjCMethodDecl *OM,
+                            ObjCInstanceTypeFamily OIT_Family = OIT_None);
 
 public:
   std::string MigrateDir;
@@ -575,12 +576,12 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
                                                        ObjCMethodDecl *OM) {
   ObjCInstanceTypeFamily OIT_Family =
     Selector::getInstTypeMethodFamily(OM->getSelector());
-  if (OIT_Family == OIT_None) {
-    migrateFactoryMethod(Ctx, CDecl, OM);
-    return;
-  }
+  
   std::string ClassName;
   switch (OIT_Family) {
+    case OIT_None:
+      migrateFactoryMethod(Ctx, CDecl, OM);
+      return;
     case OIT_Array:
       ClassName = "NSArray";
       break;
@@ -590,7 +591,8 @@ void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
     case OIT_MemManage:
       ClassName = "NSObject";
       break;
-    default:
+    case OIT_Singleton:
+      migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
       return;
   }
   if (!OM->getResultType()->isObjCIdType())
@@ -624,7 +626,8 @@ void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx,
 
 void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
                                                   ObjCContainerDecl *CDecl,
-                                                  ObjCMethodDecl *OM) {
+                                                  ObjCMethodDecl *OM,
+                                                  ObjCInstanceTypeFamily OIT_Family) {
   if (OM->isInstanceMethod() || !OM->getResultType()->isObjCIdType())
     return;
   
@@ -647,6 +650,19 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
   
   IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0);
   std::string MethodName = MethodIdName->getName();
+  if (OIT_Family == OIT_Singleton) {
+    StringRef STRefMethodName(MethodName);
+    size_t len = 0;
+    if (STRefMethodName.startswith("standard"))
+      len = strlen("standard");
+    else if (STRefMethodName.startswith("shared"))
+      len = strlen("shared");
+    else if (STRefMethodName.startswith("default"))
+      len = strlen("default");
+    else
+      return;
+    MethodName = STRefMethodName.substr(len);
+  }
   std::string MethodNameSubStr = MethodName.substr(0, 3);
   StringRef MethodNamePrefix(MethodNameSubStr);
   std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower();
index 3572930..b1a22ee 100644 (file)
@@ -467,6 +467,7 @@ ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
       break;
     case 'd':
       if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
+      if (startsWithWord(name, "default")) return OIT_Singleton;
       break;
     case 'i':
       if (startsWithWord(name, "init")) return OIT_MemManage;
@@ -474,6 +475,10 @@ ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
     case 'r':
       if (startsWithWord(name, "retain")) return OIT_MemManage;
       break;
+    case 's':
+      if (startsWithWord(name, "shared") ||
+          startsWithWord(name, "standard"))
+        return OIT_Singleton;
     default:
       break;
   }
index aff9998..325f221 100644 (file)
@@ -74,3 +74,14 @@ typedef enum NSURLBookmarkResolutionOptions {
 + (id)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
 
+@interface NSUserDefaults
++ (id) standardUserDefaults;
+@end
+
+@interface NSNotificationCenter
++ (id) defaultCenter;
+@end
+
+@interface UIApplication
++ (id)sharedApplication;
+@end
index ba66480..7077f45 100644 (file)
@@ -74,3 +74,14 @@ typedef enum NSURLBookmarkResolutionOptions {
 + (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
 
+@interface NSUserDefaults
++ (instancetype) standardUserDefaults;
+@end
+
+@interface NSNotificationCenter
++ (instancetype) defaultCenter;
+@end
+
+@interface UIApplication
++ (instancetype)sharedApplication;
+@end