r185591 - [ObjectiveC Migration]: Provide knobs for

Fariborz Jahanian fjahanian at apple.com
Wed Jul 3 16:05:00 PDT 2013


Author: fjahanian
Date: Wed Jul  3 18:05:00 2013
New Revision: 185591

URL: http://llvm.org/viewvc/llvm-project?rev=185591&view=rev
Log:
[ObjectiveC Migration]: Provide knobs for 
migrating setter/getter methods to an eventual
property declaraiton. This is wip.
// rdar://14345082

Modified:
    cfe/trunk/lib/ARCMigrate/ObjCMT.cpp

Modified: cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ObjCMT.cpp?rev=185591&r1=185590&r2=185591&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ObjCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ObjCMT.cpp Wed Jul  3 18:05:00 2013
@@ -32,6 +32,7 @@ namespace {
 
 class ObjCMigrateASTConsumer : public ASTConsumer {
   void migrateDecl(Decl *D);
+  void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D);
 
 public:
   std::string MigrateDir;
@@ -42,6 +43,7 @@ public:
   FileRemapper &Remapper;
   FileManager &FileMgr;
   const PPConditionalDirectiveRecord *PPRec;
+  Preprocessor &PP;
   bool IsOutputFile;
 
   ObjCMigrateASTConsumer(StringRef migrateDir,
@@ -50,11 +52,12 @@ public:
                          FileRemapper &remapper,
                          FileManager &fileMgr,
                          const PPConditionalDirectiveRecord *PPRec,
+                         Preprocessor &PP,
                          bool isOutputFile = false)
   : MigrateDir(migrateDir),
     MigrateLiterals(migrateLiterals),
     MigrateSubscripting(migrateSubscripting),
-    Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec),
+    Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
     IsOutputFile(isOutputFile) { }
 
 protected:
@@ -105,7 +108,8 @@ ASTConsumer *ObjCMigrateAction::CreateAS
                                                        MigrateSubscripting,
                                                        Remapper,
                                                     CompInst->getFileManager(),
-                                                       PPRec);
+                                                       PPRec,
+                                                       CompInst->getPreprocessor());
   ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer };
   return new MultiplexConsumer(Consumers);
 }
@@ -184,6 +188,40 @@ void ObjCMigrateASTConsumer::migrateDecl
   BodyMigrator(*this).TraverseDecl(D);
 }
 
+void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx,
+                                                      ObjCInterfaceDecl *D) {
+  for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end();
+       M != MEnd; ++M) {
+    ObjCMethodDecl *Method = (*M);
+    if (Method->isPropertyAccessor())
+      continue;
+    // Is this method candidate to be a getter?
+    if (Method->param_size() == 0) {
+      QualType GRT = Method->getResultType();
+      if (GRT->isVoidType())
+        continue;
+      Selector GetterSelector = Method->getSelector();
+      IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
+      Selector SetterSelector =
+        SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
+                                               PP.getSelectorTable(),
+                                               getterName);
+      if (ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true)) {
+        // Is this a valid setter, matching the target getter?
+        QualType SRT = SetterMethod->getResultType();
+        if (!SRT->isVoidType())
+          continue;
+        const ParmVarDecl *argDecl = *SetterMethod->param_begin();
+        // FIXME. Can relax rule for matching getter/setter type further.
+        if (!Ctx.hasSameType(argDecl->getType(), GRT))
+          continue;
+        // we have a matching setter/getter pair.
+        // TODO. synthesize a suitable property declaration here.
+      }
+    }
+  }
+}
+
 namespace {
 
 class RewritesReceiver : public edit::EditsReceiver {
@@ -203,6 +241,14 @@ public:
 }
 
 void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
+  
+  TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
+  for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end();
+       D != DEnd; ++D) {
+    if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D))
+      migrateObjCInterfaceDecl(Ctx, CDecl);
+  }
+  
   Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
   RewritesReceiver Rec(rewriter);
   Editor->applyRewrites(Rec);
@@ -247,5 +293,6 @@ ASTConsumer *MigrateSourceAction::Create
                                     Remapper,
                                     CI.getFileManager(),
                                     PPRec,
+                                    CI.getPreprocessor(),
                                     /*isOutputFile=*/true); 
 }





More information about the cfe-commits mailing list