| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1 | //===--- ObjCMT.cpp - ObjC Migrate Tool -----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 10 | #include "Transforms.h" |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 11 | #include "clang/ARCMigrate/ARCMTActions.h" |
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/NSAPI.h" |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ParentMap.h" |
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecursiveASTVisitor.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Edit/Commit.h" |
| 19 | #include "clang/Edit/EditedSource.h" |
| 20 | #include "clang/Edit/EditsReceiver.h" |
| 21 | #include "clang/Edit/Rewriters.h" |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
| 23 | #include "clang/Frontend/MultiplexConsumer.h" |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 24 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
| 26 | #include "clang/Rewrite/Core/Rewriter.h" |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
| 28 | |
| 29 | using namespace clang; |
| 30 | using namespace arcmt; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class ObjCMigrateASTConsumer : public ASTConsumer { |
| 35 | void migrateDecl(Decl *D); |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 36 | void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D); |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 37 | void migrateProtocolConformance(ASTContext &Ctx, |
| 38 | const ObjCImplementationDecl *ImpDecl); |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 39 | void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, |
| 40 | const TypedefDecl *TypedefDcl); |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 41 | void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
| Fariborz Jahanian | 26cf046 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 42 | void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
| 43 | ObjCMethodDecl *OM); |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 44 | void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
| 45 | ObjCMethodDecl *OM); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | std::string MigrateDir; |
| 49 | bool MigrateLiterals; |
| 50 | bool MigrateSubscripting; |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 51 | bool MigrateProperty; |
| Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 52 | OwningPtr<NSAPI> NSAPIObj; |
| 53 | OwningPtr<edit::EditedSource> Editor; |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 54 | FileRemapper &Remapper; |
| 55 | FileManager &FileMgr; |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 56 | const PPConditionalDirectiveRecord *PPRec; |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 57 | Preprocessor &PP; |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 58 | bool IsOutputFile; |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 59 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls; |
| 60 | |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 61 | ObjCMigrateASTConsumer(StringRef migrateDir, |
| 62 | bool migrateLiterals, |
| 63 | bool migrateSubscripting, |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 64 | bool migrateProperty, |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 65 | FileRemapper &remapper, |
| 66 | FileManager &fileMgr, |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 67 | const PPConditionalDirectiveRecord *PPRec, |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 68 | Preprocessor &PP, |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 69 | bool isOutputFile = false) |
| 70 | : MigrateDir(migrateDir), |
| 71 | MigrateLiterals(migrateLiterals), |
| 72 | MigrateSubscripting(migrateSubscripting), |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 73 | MigrateProperty(migrateProperty), |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 74 | Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP), |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 75 | IsOutputFile(isOutputFile) { } |
| 76 | |
| 77 | protected: |
| 78 | virtual void Initialize(ASTContext &Context) { |
| 79 | NSAPIObj.reset(new NSAPI(Context)); |
| 80 | Editor.reset(new edit::EditedSource(Context.getSourceManager(), |
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 81 | Context.getLangOpts(), |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 82 | PPRec)); |
| 83 | } |
| 84 | |
| 85 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
| 86 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 87 | migrateDecl(*I); |
| 88 | return true; |
| 89 | } |
| 90 | virtual void HandleInterestingDecl(DeclGroupRef DG) { |
| 91 | // Ignore decls from the PCH. |
| 92 | } |
| 93 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 94 | ObjCMigrateASTConsumer::HandleTopLevelDecl(DG); |
| 95 | } |
| 96 | |
| 97 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, |
| 103 | StringRef migrateDir, |
| 104 | bool migrateLiterals, |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 105 | bool migrateSubscripting, |
| 106 | bool migrateProperty) |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 107 | : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), |
| 108 | MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting), |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 109 | MigrateProperty(migrateProperty), |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 110 | CompInst(0) { |
| 111 | if (MigrateDir.empty()) |
| 112 | MigrateDir = "."; // user current directory if none is given. |
| 113 | } |
| 114 | |
| 115 | ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, |
| 116 | StringRef InFile) { |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 117 | PPConditionalDirectiveRecord * |
| 118 | PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); |
| 119 | CompInst->getPreprocessor().addPPCallbacks(PPRec); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 120 | ASTConsumer * |
| 121 | WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 122 | ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir, |
| 123 | MigrateLiterals, |
| 124 | MigrateSubscripting, |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 125 | MigrateProperty, |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 126 | Remapper, |
| 127 | CompInst->getFileManager(), |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 128 | PPRec, |
| 129 | CompInst->getPreprocessor()); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 130 | ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer }; |
| 131 | return new MultiplexConsumer(Consumers); |
| 132 | } |
| 133 | |
| 134 | bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) { |
| 135 | Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(), |
| 136 | /*ignoreIfFilesChanges=*/true); |
| 137 | CompInst = &CI; |
| 138 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 139 | return true; |
| 140 | } |
| 141 | |
| 142 | namespace { |
| 143 | class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> { |
| 144 | ObjCMigrateASTConsumer &Consumer; |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 145 | ParentMap &PMap; |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 146 | |
| 147 | public: |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 148 | ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap) |
| 149 | : Consumer(consumer), PMap(PMap) { } |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 150 | |
| 151 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 152 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 153 | |
| 154 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 155 | if (Consumer.MigrateLiterals) { |
| 156 | edit::Commit commit(*Consumer.Editor); |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 157 | edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 158 | Consumer.Editor->commit(commit); |
| 159 | } |
| 160 | |
| 161 | if (Consumer.MigrateSubscripting) { |
| 162 | edit::Commit commit(*Consumer.Editor); |
| 163 | edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit); |
| 164 | Consumer.Editor->commit(commit); |
| 165 | } |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool TraverseObjCMessageExpr(ObjCMessageExpr *E) { |
| 171 | // Do depth first; we want to rewrite the subexpressions first so that if |
| 172 | // we have to move expressions we will move them already rewritten. |
| 173 | for (Stmt::child_range range = E->children(); range; ++range) |
| 174 | if (!TraverseStmt(*range)) |
| 175 | return false; |
| 176 | |
| 177 | return WalkUpFromObjCMessageExpr(E); |
| 178 | } |
| 179 | }; |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 180 | |
| 181 | class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> { |
| 182 | ObjCMigrateASTConsumer &Consumer; |
| 183 | OwningPtr<ParentMap> PMap; |
| 184 | |
| 185 | public: |
| 186 | BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { } |
| 187 | |
| 188 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 189 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 190 | |
| 191 | bool TraverseStmt(Stmt *S) { |
| 192 | PMap.reset(new ParentMap(S)); |
| 193 | ObjCMigrator(Consumer, *PMap).TraverseStmt(S); |
| 194 | return true; |
| 195 | } |
| 196 | }; |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void ObjCMigrateASTConsumer::migrateDecl(Decl *D) { |
| 200 | if (!D) |
| 201 | return; |
| 202 | if (isa<ObjCMethodDecl>(D)) |
| 203 | return; // Wait for the ObjC container declaration. |
| 204 | |
| Argyrios Kyrtzidis | a44b970 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 205 | BodyMigrator(*this).TraverseDecl(D); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 208 | static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter, |
| 209 | const ObjCMethodDecl *Setter, |
| 210 | const NSAPI &NS, edit::Commit &commit) { |
| 211 | ASTContext &Context = NS.getASTContext(); |
| 212 | std::string PropertyString = "@property"; |
| 213 | const ParmVarDecl *argDecl = *Setter->param_begin(); |
| 214 | QualType ArgType = Context.getCanonicalType(argDecl->getType()); |
| 215 | Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime(); |
| 216 | |
| 217 | if (ArgType->isObjCRetainableType() && |
| 218 | propertyLifetime == Qualifiers::OCL_Strong) { |
| 219 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 220 | ArgType->getAs<ObjCObjectPointerType>()) { |
| 221 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 222 | if (IDecl && |
| 223 | IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying"))) |
| 224 | PropertyString += "(copy)"; |
| 225 | } |
| 226 | } |
| 227 | else if (propertyLifetime == Qualifiers::OCL_Weak) |
| 228 | // TODO. More precise determination of 'weak' attribute requires |
| 229 | // looking into setter's implementation for backing weak ivar. |
| 230 | PropertyString += "(weak)"; |
| 231 | else |
| 232 | PropertyString += "(unsafe_unretained)"; |
| 233 | |
| 234 | // strip off any ARC lifetime qualifier. |
| 235 | QualType CanResultTy = Context.getCanonicalType(Getter->getResultType()); |
| 236 | if (CanResultTy.getQualifiers().hasObjCLifetime()) { |
| 237 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 238 | Qs.removeObjCLifetime(); |
| 239 | CanResultTy = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 240 | } |
| 241 | PropertyString += " "; |
| 242 | PropertyString += CanResultTy.getAsString(Context.getPrintingPolicy()); |
| 243 | PropertyString += " "; |
| 244 | PropertyString += Getter->getNameAsString(); |
| 245 | commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(), |
| 246 | Getter->getDeclaratorEndLoc()), |
| 247 | PropertyString); |
| 248 | SourceLocation EndLoc = Setter->getDeclaratorEndLoc(); |
| 249 | // Get location past ';' |
| 250 | EndLoc = EndLoc.getLocWithOffset(1); |
| 251 | commit.remove(CharSourceRange::getCharRange(Setter->getLocStart(), EndLoc)); |
| 252 | return true; |
| 253 | } |
| 254 | |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 255 | void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, |
| 256 | ObjCInterfaceDecl *D) { |
| 257 | for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end(); |
| 258 | M != MEnd; ++M) { |
| 259 | ObjCMethodDecl *Method = (*M); |
| Fariborz Jahanian | dca7289 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 260 | if (Method->isPropertyAccessor() || Method->param_size() != 0) |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 261 | continue; |
| 262 | // Is this method candidate to be a getter? |
| Fariborz Jahanian | dca7289 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 263 | QualType GRT = Method->getResultType(); |
| 264 | if (GRT->isVoidType()) |
| 265 | continue; |
| Fariborz Jahanian | 2a4ebcf | 2013-07-08 22:49:25 +0000 | [diff] [blame] | 266 | // FIXME. Don't know what todo with attributes, skip for now. |
| 267 | if (Method->hasAttrs()) |
| 268 | continue; |
| 269 | |
| Fariborz Jahanian | dca7289 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 270 | Selector GetterSelector = Method->getSelector(); |
| 271 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 272 | Selector SetterSelector = |
| 273 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 274 | PP.getSelectorTable(), |
| 275 | getterName); |
| 276 | if (ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true)) { |
| 277 | // Is this a valid setter, matching the target getter? |
| 278 | QualType SRT = SetterMethod->getResultType(); |
| 279 | if (!SRT->isVoidType()) |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 280 | continue; |
| Fariborz Jahanian | dca7289 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 281 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
| Fariborz Jahanian | 52477fb | 2013-07-04 00:24:32 +0000 | [diff] [blame] | 282 | QualType ArgType = argDecl->getType(); |
| Fariborz Jahanian | 2a4ebcf | 2013-07-08 22:49:25 +0000 | [diff] [blame] | 283 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) || |
| 284 | SetterMethod->hasAttrs()) |
| Fariborz Jahanian | 52477fb | 2013-07-04 00:24:32 +0000 | [diff] [blame] | 285 | continue; |
| Fariborz Jahanian | afcb16f | 2013-07-05 20:46:03 +0000 | [diff] [blame] | 286 | edit::Commit commit(*Editor); |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 287 | rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit); |
| Fariborz Jahanian | afcb16f | 2013-07-05 20:46:03 +0000 | [diff] [blame] | 288 | Editor->commit(commit); |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 289 | } |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 293 | static bool |
| Fariborz Jahanian | bf13df2 | 2013-07-13 17:16:41 +0000 | [diff] [blame] | 294 | ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 295 | const ObjCImplementationDecl *ImpDecl, |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 296 | const ObjCInterfaceDecl *IDecl, |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 297 | ObjCProtocolDecl *Protocol) { |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 298 | // In auto-synthesis, protocol properties are not synthesized. So, |
| 299 | // a conforming protocol must have its required properties declared |
| 300 | // in class interface. |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 301 | bool HasAtleastOneRequiredProperty = false; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 302 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) |
| 303 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 304 | E = PDecl->prop_end(); P != E; ++P) { |
| 305 | ObjCPropertyDecl *Property = *P; |
| 306 | if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 307 | continue; |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 308 | HasAtleastOneRequiredProperty = true; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 309 | DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); |
| Fariborz Jahanian | 8d8bfb3 | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 310 | if (R.size() == 0) { |
| 311 | // Relax the rule and look into class's implementation for a synthesize |
| 312 | // or dynamic declaration. Class is implementing a property coming from |
| 313 | // another protocol. This still makes the target protocol as conforming. |
| 314 | if (!ImpDecl->FindPropertyImplDecl( |
| 315 | Property->getDeclName().getAsIdentifierInfo())) |
| 316 | return false; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 317 | } |
| Fariborz Jahanian | 8d8bfb3 | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 318 | else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 319 | if ((ClassProperty->getPropertyAttributes() |
| 320 | != Property->getPropertyAttributes()) || |
| 321 | !Ctx.hasSameType(ClassProperty->getType(), Property->getType())) |
| 322 | return false; |
| 323 | } |
| 324 | else |
| 325 | return false; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 326 | } |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 327 | |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 328 | // At this point, all required properties in this protocol conform to those |
| 329 | // declared in the class. |
| 330 | // Check that class implements the required methods of the protocol too. |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 331 | bool HasAtleastOneRequiredMethod = false; |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 332 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) { |
| 333 | if (PDecl->meth_begin() == PDecl->meth_end()) |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 334 | return HasAtleastOneRequiredProperty; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 335 | for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(), |
| 336 | MEnd = PDecl->meth_end(); M != MEnd; ++M) { |
| 337 | ObjCMethodDecl *MD = (*M); |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 338 | if (MD->isImplicit()) |
| 339 | continue; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 340 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) |
| 341 | continue; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 342 | DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 343 | if (R.size() == 0) |
| 344 | return false; |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 345 | bool match = false; |
| 346 | HasAtleastOneRequiredMethod = true; |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 347 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 348 | if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0])) |
| 349 | if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) { |
| 350 | match = true; |
| 351 | break; |
| 352 | } |
| 353 | if (!match) |
| 354 | return false; |
| 355 | } |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 356 | } |
| Fariborz Jahanian | 34cb2a3 | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 357 | if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod) |
| 358 | return true; |
| 359 | return false; |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 362 | static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl, |
| 363 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols, |
| 364 | const NSAPI &NS, edit::Commit &commit) { |
| 365 | const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols(); |
| 366 | std::string ClassString; |
| 367 | SourceLocation EndLoc = |
| 368 | IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation(); |
| 369 | |
| 370 | if (Protocols.empty()) { |
| 371 | ClassString = '<'; |
| 372 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 373 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 374 | if (i != (e-1)) |
| 375 | ClassString += ", "; |
| 376 | } |
| 377 | ClassString += "> "; |
| 378 | } |
| 379 | else { |
| 380 | ClassString = ", "; |
| 381 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 382 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 383 | if (i != (e-1)) |
| 384 | ClassString += ", "; |
| 385 | } |
| 386 | ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1; |
| 387 | EndLoc = *PL; |
| 388 | } |
| 389 | |
| 390 | commit.insertAfterToken(EndLoc, ClassString); |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl, |
| 395 | const TypedefDecl *TypedefDcl, |
| Fariborz Jahanian | 9f9e543 | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 396 | const NSAPI &NS, edit::Commit &commit, |
| 397 | bool IsNSIntegerType) { |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 398 | std::string ClassString = |
| Fariborz Jahanian | 9f9e543 | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 399 | IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " : "typedef NS_OPTIONS(NSUInteger, "; |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 400 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 401 | ClassString += ')'; |
| 402 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 403 | commit.replace(R, ClassString); |
| 404 | SourceLocation EndOfTypedefLoc = TypedefDcl->getLocEnd(); |
| 405 | EndOfTypedefLoc = trans::findLocationAfterSemi(EndOfTypedefLoc, NS.getASTContext()); |
| 406 | if (!EndOfTypedefLoc.isInvalid()) { |
| 407 | commit.remove(SourceRange(TypedefDcl->getLocStart(), EndOfTypedefLoc)); |
| 408 | return true; |
| 409 | } |
| 410 | return false; |
| 411 | } |
| 412 | |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 413 | static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl, |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 414 | const TypedefDecl *TypedefDcl, |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 415 | const NSAPI &NS, edit::Commit &commit, |
| 416 | bool IsNSIntegerType) { |
| 417 | std::string ClassString = |
| 418 | IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, "; |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 419 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 420 | ClassString += ')'; |
| 421 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 422 | commit.replace(R, ClassString); |
| 423 | SourceLocation TypedefLoc = TypedefDcl->getLocEnd(); |
| 424 | commit.remove(SourceRange(TypedefLoc, TypedefLoc)); |
| 425 | return true; |
| 426 | } |
| 427 | |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 428 | static bool UseNSOptionsMacro(ASTContext &Ctx, |
| 429 | const EnumDecl *EnumDcl) { |
| 430 | bool PowerOfTwo = true; |
| 431 | for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(), |
| 432 | EE = EnumDcl->enumerator_end(); EI != EE; ++EI) { |
| 433 | EnumConstantDecl *Enumerator = (*EI); |
| 434 | const Expr *InitExpr = Enumerator->getInitExpr(); |
| 435 | if (!InitExpr) { |
| 436 | PowerOfTwo = false; |
| 437 | continue; |
| 438 | } |
| 439 | InitExpr = InitExpr->IgnoreImpCasts(); |
| 440 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) |
| 441 | if (BO->isShiftOp() || BO->isBitwiseOp()) |
| 442 | return true; |
| 443 | |
| 444 | uint64_t EnumVal = Enumerator->getInitVal().getZExtValue(); |
| 445 | if (PowerOfTwo && EnumVal && !llvm::isPowerOf2_64(EnumVal)) |
| 446 | PowerOfTwo = false; |
| 447 | } |
| 448 | return PowerOfTwo; |
| 449 | } |
| 450 | |
| Fariborz Jahanian | 7268563 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 451 | void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 452 | const ObjCImplementationDecl *ImpDecl) { |
| 453 | const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface(); |
| 454 | if (!IDecl || ObjCProtocolDecls.empty()) |
| 455 | return; |
| 456 | // Find all implicit conforming protocols for this class |
| 457 | // and make them explicit. |
| 458 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols; |
| 459 | Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols); |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 460 | llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols; |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 461 | |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 462 | for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I = |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 463 | ObjCProtocolDecls.begin(), |
| 464 | E = ObjCProtocolDecls.end(); I != E; ++I) |
| 465 | if (!ExplicitProtocols.count(*I)) |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 466 | PotentialImplicitProtocols.push_back(*I); |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 467 | |
| 468 | if (PotentialImplicitProtocols.empty()) |
| 469 | return; |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 470 | |
| 471 | // go through list of non-optional methods and properties in each protocol |
| 472 | // in the PotentialImplicitProtocols list. If class implements every one of the |
| 473 | // methods and properties, then this class conforms to this protocol. |
| 474 | llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols; |
| 475 | for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++) |
| Fariborz Jahanian | ad4aaf1 | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 476 | if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl, |
| Fariborz Jahanian | e99b37c | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 477 | PotentialImplicitProtocols[i])) |
| 478 | ConformingProtocols.push_back(PotentialImplicitProtocols[i]); |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 479 | |
| 480 | if (ConformingProtocols.empty()) |
| 481 | return; |
| Fariborz Jahanian | e828018 | 2013-07-17 00:02:22 +0000 | [diff] [blame] | 482 | |
| 483 | // Further reduce number of conforming protocols. If protocol P1 is in the list |
| 484 | // protocol P2 (P2<P1>), No need to include P1. |
| 485 | llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols; |
| 486 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 487 | bool DropIt = false; |
| 488 | ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i]; |
| 489 | for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) { |
| 490 | ObjCProtocolDecl *PDecl = ConformingProtocols[i1]; |
| 491 | if (PDecl == TargetPDecl) |
| 492 | continue; |
| 493 | if (PDecl->lookupProtocolNamed( |
| 494 | TargetPDecl->getDeclName().getAsIdentifierInfo())) { |
| 495 | DropIt = true; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | if (!DropIt) |
| 500 | MinimalConformingProtocols.push_back(TargetPDecl); |
| 501 | } |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 502 | edit::Commit commit(*Editor); |
| Fariborz Jahanian | 44b41b1 | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 503 | rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols, |
| 504 | *NSAPIObj, commit); |
| Fariborz Jahanian | 8c35583 | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 505 | Editor->commit(commit); |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 508 | void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, |
| 509 | const EnumDecl *EnumDcl, |
| 510 | const TypedefDecl *TypedefDcl) { |
| 511 | if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() || |
| 512 | !TypedefDcl->getIdentifier()) |
| 513 | return; |
| 514 | |
| 515 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
| Fariborz Jahanian | 9f9e543 | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 516 | bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt); |
| 517 | bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt); |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 518 | |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 519 | if (!IsNSIntegerType && !IsNSUIntegerType) { |
| 520 | // Also check for typedef enum {...} TD; |
| 521 | if (const EnumType *EnumTy = qt->getAs<EnumType>()) { |
| 522 | if (EnumTy->getDecl() == EnumDcl) { |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 523 | bool NSOptions = UseNSOptionsMacro(Ctx, EnumDcl); |
| 524 | if (NSOptions) { |
| 525 | if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
| 526 | return; |
| 527 | } |
| 528 | else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 529 | return; |
| 530 | edit::Commit commit(*Editor); |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 531 | rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions); |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 532 | Editor->commit(commit); |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 533 | } |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 534 | } |
| Fariborz Jahanian | e3069e2 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | if (IsNSIntegerType && UseNSOptionsMacro(Ctx, EnumDcl)) { |
| 538 | // We may still use NS_OPTIONS based on what we find in the enumertor list. |
| 539 | IsNSIntegerType = false; |
| 540 | IsNSUIntegerType = true; |
| Fariborz Jahanian | faae53d | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 541 | } |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 542 | |
| 543 | // NS_ENUM must be available. |
| Fariborz Jahanian | 9f9e543 | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 544 | if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
| 545 | return; |
| 546 | // NS_OPTIONS must be available. |
| 547 | if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 548 | return; |
| 549 | edit::Commit commit(*Editor); |
| Fariborz Jahanian | 9f9e543 | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 550 | rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, IsNSIntegerType); |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 551 | Editor->commit(commit); |
| 552 | } |
| 553 | |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 554 | static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC, |
| 555 | ObjCMethodDecl *OM) { |
| 556 | SourceRange R; |
| 557 | std::string ClassString; |
| 558 | if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { |
| 559 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 560 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); |
| 561 | ClassString = "instancetype"; |
| 562 | } |
| 563 | else { |
| 564 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 565 | ClassString = OM->isInstanceMethod() ? '-' : '+'; |
| 566 | ClassString += " (instancetype)"; |
| 567 | } |
| 568 | edit::Commit commit(*ASTC.Editor); |
| 569 | commit.replace(R, ClassString); |
| 570 | ASTC.Editor->commit(commit); |
| 571 | } |
| 572 | |
| Fariborz Jahanian | 26cf046 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 573 | void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, |
| 574 | ObjCContainerDecl *CDecl, |
| 575 | ObjCMethodDecl *OM) { |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 576 | ObjCInstanceTypeFamily OIT_Family = |
| 577 | Selector::getInstTypeMethodFamily(OM->getSelector()); |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 578 | if (OIT_Family == OIT_None) { |
| 579 | migrateFactoryMethod(Ctx, CDecl, OM); |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 580 | return; |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 581 | } |
| Fariborz Jahanian | 65f1a4c | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 582 | std::string ClassName; |
| Fariborz Jahanian | 211b4a2 | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 583 | switch (OIT_Family) { |
| 584 | case OIT_Array: |
| Fariborz Jahanian | 65f1a4c | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 585 | ClassName = "NSArray"; |
| Fariborz Jahanian | 211b4a2 | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 586 | break; |
| 587 | case OIT_Dictionary: |
| Fariborz Jahanian | 65f1a4c | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 588 | ClassName = "NSDictionary"; |
| 589 | break; |
| 590 | case OIT_MemManage: |
| 591 | ClassName = "NSObject"; |
| Fariborz Jahanian | 211b4a2 | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 592 | break; |
| 593 | default: |
| 594 | return; |
| 595 | } |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 596 | if (!OM->getResultType()->isObjCIdType()) |
| 597 | return; |
| 598 | |
| 599 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 600 | if (!IDecl) { |
| 601 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 602 | IDecl = CatDecl->getClassInterface(); |
| 603 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 604 | IDecl = ImpDecl->getClassInterface(); |
| 605 | } |
| Fariborz Jahanian | 65f1a4c | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 606 | if (!IDecl || |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 607 | !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { |
| 608 | migrateFactoryMethod(Ctx, CDecl, OM); |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 609 | return; |
| Fariborz Jahanian | 5e151c5 | 2013-07-24 18:31:42 +0000 | [diff] [blame] | 610 | } |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 611 | ReplaceWithInstancetype(*this, OM); |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx, |
| 615 | ObjCContainerDecl *CDecl) { |
| 616 | // migrate methods which can have instancetype as their result type. |
| 617 | for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), |
| 618 | MEnd = CDecl->meth_end(); |
| 619 | M != MEnd; ++M) { |
| 620 | ObjCMethodDecl *Method = (*M); |
| 621 | migrateMethodInstanceType(Ctx, CDecl, Method); |
| 622 | } |
| 623 | } |
| 624 | |
| Fariborz Jahanian | f647b69 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 625 | void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, |
| 626 | ObjCContainerDecl *CDecl, |
| 627 | ObjCMethodDecl *OM) { |
| 628 | if (OM->isInstanceMethod() || !OM->getResultType()->isObjCIdType()) |
| 629 | return; |
| 630 | |
| 631 | // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class |
| 632 | // NSYYYNamE with matching names be at least 3 characters long. |
| 633 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 634 | if (!IDecl) { |
| 635 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 636 | IDecl = CatDecl->getClassInterface(); |
| 637 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 638 | IDecl = ImpDecl->getClassInterface(); |
| 639 | } |
| 640 | if (!IDecl) |
| 641 | return; |
| 642 | |
| 643 | std::string StringClassName = IDecl->getName(); |
| 644 | StringRef LoweredClassName(StringClassName); |
| 645 | std::string StringLoweredClassName = LoweredClassName.lower(); |
| 646 | LoweredClassName = StringLoweredClassName; |
| 647 | |
| 648 | IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); |
| 649 | std::string MethodName = MethodIdName->getName(); |
| 650 | std::string MethodNameSubStr = MethodName.substr(0, 3); |
| 651 | StringRef MethodNamePrefix(MethodNameSubStr); |
| 652 | std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower(); |
| 653 | MethodNamePrefix = StringLoweredMethodNamePrefix; |
| 654 | size_t Ix = LoweredClassName.rfind(MethodNamePrefix); |
| 655 | if (Ix == StringRef::npos) |
| 656 | return; |
| 657 | std::string ClassNamePostfix = LoweredClassName.substr(Ix); |
| 658 | StringRef LoweredMethodName(MethodName); |
| 659 | std::string StringLoweredMethodName = LoweredMethodName.lower(); |
| 660 | LoweredMethodName = StringLoweredMethodName; |
| 661 | if (!LoweredMethodName.startswith(ClassNamePostfix)) |
| 662 | return; |
| 663 | ReplaceWithInstancetype(*this, OM); |
| 664 | } |
| 665 | |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 666 | namespace { |
| 667 | |
| 668 | class RewritesReceiver : public edit::EditsReceiver { |
| 669 | Rewriter &Rewrite; |
| 670 | |
| 671 | public: |
| 672 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 673 | |
| 674 | virtual void insert(SourceLocation loc, StringRef text) { |
| 675 | Rewrite.InsertText(loc, text); |
| 676 | } |
| 677 | virtual void replace(CharSourceRange range, StringRef text) { |
| 678 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 679 | } |
| 680 | }; |
| 681 | |
| 682 | } |
| 683 | |
| 684 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 685 | |
| 686 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 687 | if (MigrateProperty) |
| 688 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 689 | D != DEnd; ++D) { |
| 690 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 691 | migrateObjCInterfaceDecl(Ctx, CDecl); |
| Fariborz Jahanian | 7556853 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 692 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
| 693 | ObjCProtocolDecls.insert(PDecl); |
| 694 | else if (const ObjCImplementationDecl *ImpDecl = |
| 695 | dyn_cast<ObjCImplementationDecl>(*D)) |
| 696 | migrateProtocolConformance(Ctx, ImpDecl); |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 697 | else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { |
| 698 | DeclContext::decl_iterator N = D; |
| 699 | ++N; |
| Fariborz Jahanian | 7268563 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 700 | if (N != DEnd) |
| 701 | if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N)) |
| 702 | migrateNSEnumDecl(Ctx, ED, TD); |
| Fariborz Jahanian | bbbb0fe | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 703 | } |
| Fariborz Jahanian | 11638f7 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 704 | // migrate methods which can have instancetype as their result type. |
| 705 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) |
| 706 | migrateInstanceType(Ctx, CDecl); |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 707 | } |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 708 | |
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 709 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 710 | RewritesReceiver Rec(rewriter); |
| 711 | Editor->applyRewrites(Rec); |
| 712 | |
| 713 | for (Rewriter::buffer_iterator |
| 714 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 715 | FileID FID = I->first; |
| 716 | RewriteBuffer &buf = I->second; |
| 717 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 718 | assert(file); |
| Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 719 | SmallString<512> newText; |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 720 | llvm::raw_svector_ostream vecOS(newText); |
| 721 | buf.write(vecOS); |
| 722 | vecOS.flush(); |
| 723 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 724 | StringRef(newText.data(), newText.size()), file->getName()); |
| Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 725 | SmallString<64> filePath(file->getName()); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 726 | FileMgr.FixupRelativePath(filePath); |
| 727 | Remapper.remap(filePath.str(), memBuf); |
| 728 | } |
| 729 | |
| 730 | if (IsOutputFile) { |
| 731 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 732 | } else { |
| 733 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
| Argyrios Kyrtzidis | 013a254 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 738 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 739 | return true; |
| 740 | } |
| 741 | |
| 742 | ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, |
| 743 | StringRef InFile) { |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 744 | PPConditionalDirectiveRecord * |
| 745 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
| 746 | CI.getPreprocessor().addPPCallbacks(PPRec); |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 747 | return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile, |
| 748 | /*MigrateLiterals=*/true, |
| 749 | /*MigrateSubscripting=*/true, |
| Fariborz Jahanian | d412999 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 750 | /*MigrateProperty*/true, |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 751 | Remapper, |
| 752 | CI.getFileManager(), |
| Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 753 | PPRec, |
| Fariborz Jahanian | baf1557 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 754 | CI.getPreprocessor(), |
| Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 755 | /*isOutputFile=*/true); |
| 756 | } |