Ted Kremenek | f7639e1 | 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 | |
| 10 | #include "clang/ARCMigrate/ARCMTActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTConsumer.h" |
| 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/AST/NSAPI.h" |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 14 | #include "clang/AST/ParentMap.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/RecursiveASTVisitor.h" |
| 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Edit/Commit.h" |
| 18 | #include "clang/Edit/EditedSource.h" |
| 19 | #include "clang/Edit/EditsReceiver.h" |
| 20 | #include "clang/Edit/Rewriters.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
| 22 | #include "clang/Frontend/MultiplexConsumer.h" |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 23 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
| 25 | #include "clang/Rewrite/Core/Rewriter.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
| 27 | |
| 28 | using namespace clang; |
| 29 | using namespace arcmt; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | class ObjCMigrateASTConsumer : public ASTConsumer { |
| 34 | void migrateDecl(Decl *D); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 35 | void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 36 | void migrateProtocolConformance(ASTContext &Ctx, |
| 37 | const ObjCImplementationDecl *ImpDecl); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 38 | |
| 39 | public: |
| 40 | std::string MigrateDir; |
| 41 | bool MigrateLiterals; |
| 42 | bool MigrateSubscripting; |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 43 | bool MigrateProperty; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 44 | OwningPtr<NSAPI> NSAPIObj; |
| 45 | OwningPtr<edit::EditedSource> Editor; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 46 | FileRemapper &Remapper; |
| 47 | FileManager &FileMgr; |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 48 | const PPConditionalDirectiveRecord *PPRec; |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 49 | Preprocessor &PP; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 50 | bool IsOutputFile; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 51 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls; |
| 52 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 53 | ObjCMigrateASTConsumer(StringRef migrateDir, |
| 54 | bool migrateLiterals, |
| 55 | bool migrateSubscripting, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 56 | bool migrateProperty, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 57 | FileRemapper &remapper, |
| 58 | FileManager &fileMgr, |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 59 | const PPConditionalDirectiveRecord *PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 60 | Preprocessor &PP, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 61 | bool isOutputFile = false) |
| 62 | : MigrateDir(migrateDir), |
| 63 | MigrateLiterals(migrateLiterals), |
| 64 | MigrateSubscripting(migrateSubscripting), |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 65 | MigrateProperty(migrateProperty), |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 66 | Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 67 | IsOutputFile(isOutputFile) { } |
| 68 | |
| 69 | protected: |
| 70 | virtual void Initialize(ASTContext &Context) { |
| 71 | NSAPIObj.reset(new NSAPI(Context)); |
| 72 | Editor.reset(new edit::EditedSource(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 73 | Context.getLangOpts(), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 74 | PPRec)); |
| 75 | } |
| 76 | |
| 77 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
| 78 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 79 | migrateDecl(*I); |
| 80 | return true; |
| 81 | } |
| 82 | virtual void HandleInterestingDecl(DeclGroupRef DG) { |
| 83 | // Ignore decls from the PCH. |
| 84 | } |
| 85 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 86 | ObjCMigrateASTConsumer::HandleTopLevelDecl(DG); |
| 87 | } |
| 88 | |
| 89 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 90 | }; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, |
| 95 | StringRef migrateDir, |
| 96 | bool migrateLiterals, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 97 | bool migrateSubscripting, |
| 98 | bool migrateProperty) |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 99 | : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), |
| 100 | MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting), |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 101 | MigrateProperty(migrateProperty), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 102 | CompInst(0) { |
| 103 | if (MigrateDir.empty()) |
| 104 | MigrateDir = "."; // user current directory if none is given. |
| 105 | } |
| 106 | |
| 107 | ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, |
| 108 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 109 | PPConditionalDirectiveRecord * |
| 110 | PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); |
| 111 | CompInst->getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 112 | ASTConsumer * |
| 113 | WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 114 | ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir, |
| 115 | MigrateLiterals, |
| 116 | MigrateSubscripting, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 117 | MigrateProperty, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 118 | Remapper, |
| 119 | CompInst->getFileManager(), |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 120 | PPRec, |
| 121 | CompInst->getPreprocessor()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 122 | ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer }; |
| 123 | return new MultiplexConsumer(Consumers); |
| 124 | } |
| 125 | |
| 126 | bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) { |
| 127 | Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(), |
| 128 | /*ignoreIfFilesChanges=*/true); |
| 129 | CompInst = &CI; |
| 130 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
| 134 | namespace { |
| 135 | class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> { |
| 136 | ObjCMigrateASTConsumer &Consumer; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 137 | ParentMap &PMap; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 138 | |
| 139 | public: |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 140 | ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap) |
| 141 | : Consumer(consumer), PMap(PMap) { } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 142 | |
| 143 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 144 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 145 | |
| 146 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 147 | if (Consumer.MigrateLiterals) { |
| 148 | edit::Commit commit(*Consumer.Editor); |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 149 | edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 150 | Consumer.Editor->commit(commit); |
| 151 | } |
| 152 | |
| 153 | if (Consumer.MigrateSubscripting) { |
| 154 | edit::Commit commit(*Consumer.Editor); |
| 155 | edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit); |
| 156 | Consumer.Editor->commit(commit); |
| 157 | } |
| 158 | |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | bool TraverseObjCMessageExpr(ObjCMessageExpr *E) { |
| 163 | // Do depth first; we want to rewrite the subexpressions first so that if |
| 164 | // we have to move expressions we will move them already rewritten. |
| 165 | for (Stmt::child_range range = E->children(); range; ++range) |
| 166 | if (!TraverseStmt(*range)) |
| 167 | return false; |
| 168 | |
| 169 | return WalkUpFromObjCMessageExpr(E); |
| 170 | } |
| 171 | }; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 172 | |
| 173 | class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> { |
| 174 | ObjCMigrateASTConsumer &Consumer; |
| 175 | OwningPtr<ParentMap> PMap; |
| 176 | |
| 177 | public: |
| 178 | BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { } |
| 179 | |
| 180 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 181 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 182 | |
| 183 | bool TraverseStmt(Stmt *S) { |
| 184 | PMap.reset(new ParentMap(S)); |
| 185 | ObjCMigrator(Consumer, *PMap).TraverseStmt(S); |
| 186 | return true; |
| 187 | } |
| 188 | }; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void ObjCMigrateASTConsumer::migrateDecl(Decl *D) { |
| 192 | if (!D) |
| 193 | return; |
| 194 | if (isa<ObjCMethodDecl>(D)) |
| 195 | return; // Wait for the ObjC container declaration. |
| 196 | |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 197 | BodyMigrator(*this).TraverseDecl(D); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 200 | void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, |
| 201 | ObjCInterfaceDecl *D) { |
| 202 | for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end(); |
| 203 | M != MEnd; ++M) { |
| 204 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | de66100 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 205 | if (Method->isPropertyAccessor() || Method->param_size() != 0) |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 206 | continue; |
| 207 | // Is this method candidate to be a getter? |
Fariborz Jahanian | de66100 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 208 | QualType GRT = Method->getResultType(); |
| 209 | if (GRT->isVoidType()) |
| 210 | continue; |
Fariborz Jahanian | 7ac20e1 | 2013-07-08 22:49:25 +0000 | [diff] [blame] | 211 | // FIXME. Don't know what todo with attributes, skip for now. |
| 212 | if (Method->hasAttrs()) |
| 213 | continue; |
| 214 | |
Fariborz Jahanian | de66100 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 215 | Selector GetterSelector = Method->getSelector(); |
| 216 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 217 | Selector SetterSelector = |
| 218 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 219 | PP.getSelectorTable(), |
| 220 | getterName); |
| 221 | if (ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true)) { |
| 222 | // Is this a valid setter, matching the target getter? |
| 223 | QualType SRT = SetterMethod->getResultType(); |
| 224 | if (!SRT->isVoidType()) |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 225 | continue; |
Fariborz Jahanian | de66100 | 2013-07-03 23:44:11 +0000 | [diff] [blame] | 226 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
Fariborz Jahanian | 43bbaac | 2013-07-04 00:24:32 +0000 | [diff] [blame] | 227 | QualType ArgType = argDecl->getType(); |
Fariborz Jahanian | 7ac20e1 | 2013-07-08 22:49:25 +0000 | [diff] [blame] | 228 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) || |
| 229 | SetterMethod->hasAttrs()) |
Fariborz Jahanian | 43bbaac | 2013-07-04 00:24:32 +0000 | [diff] [blame] | 230 | continue; |
Fariborz Jahanian | 266926d | 2013-07-05 20:46:03 +0000 | [diff] [blame] | 231 | edit::Commit commit(*Editor); |
| 232 | edit::rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit); |
| 233 | Editor->commit(commit); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 234 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 238 | static bool |
Fariborz Jahanian | 9a3512b | 2013-07-13 17:16:41 +0000 | [diff] [blame] | 239 | ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 240 | const ObjCImplementationDecl *ImpDecl, |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 241 | const ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 242 | ObjCProtocolDecl *Protocol) { |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 243 | // In auto-synthesis, protocol properties are not synthesized. So, |
| 244 | // a conforming protocol must have its required properties declared |
| 245 | // in class interface. |
| 246 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) |
| 247 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 248 | E = PDecl->prop_end(); P != E; ++P) { |
| 249 | ObjCPropertyDecl *Property = *P; |
| 250 | if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 251 | continue; |
| 252 | DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame^] | 253 | if (R.size() == 0) { |
| 254 | // Relax the rule and look into class's implementation for a synthesize |
| 255 | // or dynamic declaration. Class is implementing a property coming from |
| 256 | // another protocol. This still makes the target protocol as conforming. |
| 257 | if (!ImpDecl->FindPropertyImplDecl( |
| 258 | Property->getDeclName().getAsIdentifierInfo())) |
| 259 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 260 | } |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame^] | 261 | else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 262 | if ((ClassProperty->getPropertyAttributes() |
| 263 | != Property->getPropertyAttributes()) || |
| 264 | !Ctx.hasSameType(ClassProperty->getType(), Property->getType())) |
| 265 | return false; |
| 266 | } |
| 267 | else |
| 268 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 269 | } |
| 270 | // At this point, all required properties in this protocol conform to those |
| 271 | // declared in the class. |
| 272 | // Check that class implements the required methods of the protocol too. |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 273 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) { |
| 274 | if (PDecl->meth_begin() == PDecl->meth_end()) |
| 275 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 276 | for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(), |
| 277 | MEnd = PDecl->meth_end(); M != MEnd; ++M) { |
| 278 | ObjCMethodDecl *MD = (*M); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 279 | if (MD->isImplicit()) |
| 280 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 281 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) |
| 282 | continue; |
| 283 | bool match = false; |
| 284 | DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 285 | if (R.size() == 0) |
| 286 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 287 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 288 | if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0])) |
| 289 | if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) { |
| 290 | match = true; |
| 291 | break; |
| 292 | } |
| 293 | if (!match) |
| 294 | return false; |
| 295 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 296 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 297 | |
| 298 | return true; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 301 | void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, |
| 302 | const ObjCImplementationDecl *ImpDecl) { |
| 303 | const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface(); |
| 304 | if (!IDecl || ObjCProtocolDecls.empty()) |
| 305 | return; |
| 306 | // Find all implicit conforming protocols for this class |
| 307 | // and make them explicit. |
| 308 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols; |
| 309 | Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols); |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 310 | llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 311 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 312 | for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I = |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 313 | ObjCProtocolDecls.begin(), |
| 314 | E = ObjCProtocolDecls.end(); I != E; ++I) |
| 315 | if (!ExplicitProtocols.count(*I)) |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 316 | PotentialImplicitProtocols.push_back(*I); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 317 | |
| 318 | if (PotentialImplicitProtocols.empty()) |
| 319 | return; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 320 | |
| 321 | // go through list of non-optional methods and properties in each protocol |
| 322 | // in the PotentialImplicitProtocols list. If class implements every one of the |
| 323 | // methods and properties, then this class conforms to this protocol. |
| 324 | llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols; |
| 325 | for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 326 | if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 327 | PotentialImplicitProtocols[i])) |
| 328 | ConformingProtocols.push_back(PotentialImplicitProtocols[i]); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 329 | |
| 330 | if (ConformingProtocols.empty()) |
| 331 | return; |
| 332 | edit::Commit commit(*Editor); |
| 333 | edit::rewriteToObjCInterfaceDecl(IDecl, ConformingProtocols, *NSAPIObj, commit); |
| 334 | Editor->commit(commit); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 337 | namespace { |
| 338 | |
| 339 | class RewritesReceiver : public edit::EditsReceiver { |
| 340 | Rewriter &Rewrite; |
| 341 | |
| 342 | public: |
| 343 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 344 | |
| 345 | virtual void insert(SourceLocation loc, StringRef text) { |
| 346 | Rewrite.InsertText(loc, text); |
| 347 | } |
| 348 | virtual void replace(CharSourceRange range, StringRef text) { |
| 349 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 350 | } |
| 351 | }; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 356 | |
| 357 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 358 | if (MigrateProperty) |
| 359 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 360 | D != DEnd; ++D) { |
| 361 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 362 | migrateObjCInterfaceDecl(Ctx, CDecl); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 363 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
| 364 | ObjCProtocolDecls.insert(PDecl); |
| 365 | else if (const ObjCImplementationDecl *ImpDecl = |
| 366 | dyn_cast<ObjCImplementationDecl>(*D)) |
| 367 | migrateProtocolConformance(Ctx, ImpDecl); |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 368 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 369 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 370 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 371 | RewritesReceiver Rec(rewriter); |
| 372 | Editor->applyRewrites(Rec); |
| 373 | |
| 374 | for (Rewriter::buffer_iterator |
| 375 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 376 | FileID FID = I->first; |
| 377 | RewriteBuffer &buf = I->second; |
| 378 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 379 | assert(file); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 380 | SmallString<512> newText; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 381 | llvm::raw_svector_ostream vecOS(newText); |
| 382 | buf.write(vecOS); |
| 383 | vecOS.flush(); |
| 384 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 385 | StringRef(newText.data(), newText.size()), file->getName()); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 386 | SmallString<64> filePath(file->getName()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 387 | FileMgr.FixupRelativePath(filePath); |
| 388 | Remapper.remap(filePath.str(), memBuf); |
| 389 | } |
| 390 | |
| 391 | if (IsOutputFile) { |
| 392 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 393 | } else { |
| 394 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
Argyrios Kyrtzidis | b482260 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 399 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
| 403 | ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, |
| 404 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 405 | PPConditionalDirectiveRecord * |
| 406 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
| 407 | CI.getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 408 | return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile, |
| 409 | /*MigrateLiterals=*/true, |
| 410 | /*MigrateSubscripting=*/true, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 411 | /*MigrateProperty*/true, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 412 | Remapper, |
| 413 | CI.getFileManager(), |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 414 | PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 415 | CI.getPreprocessor(), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 416 | /*isOutputFile=*/true); |
| 417 | } |