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