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