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" |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 28 | #include "clang/StaticAnalyzer/Checkers/ObjCRetainCount.h" |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 29 | #include "clang/AST/Attr.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
| 31 | |
| 32 | using namespace clang; |
| 33 | using namespace arcmt; |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 34 | using namespace ento::objc_retain; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | class ObjCMigrateASTConsumer : public ASTConsumer { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 39 | enum CF_BRIDGING_KIND { |
| 40 | CF_BRIDGING_NONE, |
| 41 | CF_BRIDGING_ENABLE, |
| 42 | CF_BRIDGING_MAY_INCLUDE |
| 43 | }; |
| 44 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 45 | void migrateDecl(Decl *D); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 46 | void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCContainerDecl *D); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame^] | 47 | void migregateDeprecatedAnnotation(ASTContext &Ctx, ObjCCategoryDecl *CatDecl); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 48 | void migrateProtocolConformance(ASTContext &Ctx, |
| 49 | const ObjCImplementationDecl *ImpDecl); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 50 | void CacheObjCNSIntegerTypedefed(const TypedefDecl *TypedefDcl); |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 51 | bool migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 52 | const TypedefDecl *TypedefDcl); |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 53 | void migrateAllMethodInstaceType(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 54 | void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
| 55 | ObjCMethodDecl *OM); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 56 | bool migrateProperty(ASTContext &Ctx, ObjCContainerDecl *D, ObjCMethodDecl *OM); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 57 | void migrateNsReturnsInnerPointer(ASTContext &Ctx, ObjCMethodDecl *OM); |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 58 | void migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, ObjCPropertyDecl *P); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 59 | void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 60 | ObjCMethodDecl *OM, |
| 61 | ObjCInstanceTypeFamily OIT_Family = OIT_None); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 62 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 63 | void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 64 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 65 | const FunctionDecl *FuncDecl, bool ResultAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 66 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 67 | const ObjCMethodDecl *MethodDecl, bool ResultAnnotated); |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 69 | void AnnotateImplicitBridging(ASTContext &Ctx); |
| 70 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 71 | CF_BRIDGING_KIND migrateAddFunctionAnnotation(ASTContext &Ctx, |
| 72 | const FunctionDecl *FuncDecl); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 73 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 74 | void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
| 75 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 76 | void migrateAddMethodAnnotation(ASTContext &Ctx, |
| 77 | const ObjCMethodDecl *MethodDecl); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 78 | public: |
| 79 | std::string MigrateDir; |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 80 | unsigned ASTMigrateActions; |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 81 | unsigned FileId; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 82 | const TypedefDecl *NSIntegerTypedefed; |
| 83 | const TypedefDecl *NSUIntegerTypedefed; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 84 | OwningPtr<NSAPI> NSAPIObj; |
| 85 | OwningPtr<edit::EditedSource> Editor; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 86 | FileRemapper &Remapper; |
| 87 | FileManager &FileMgr; |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 88 | const PPConditionalDirectiveRecord *PPRec; |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 89 | Preprocessor &PP; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 90 | bool IsOutputFile; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 91 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls; |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 92 | llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 94 | ObjCMigrateASTConsumer(StringRef migrateDir, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 95 | unsigned astMigrateActions, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 96 | FileRemapper &remapper, |
| 97 | FileManager &fileMgr, |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 98 | const PPConditionalDirectiveRecord *PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 99 | Preprocessor &PP, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 100 | bool isOutputFile = false) |
| 101 | : MigrateDir(migrateDir), |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 102 | ASTMigrateActions(astMigrateActions), |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 103 | FileId(0), NSIntegerTypedefed(0), NSUIntegerTypedefed(0), |
| 104 | Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 105 | IsOutputFile(isOutputFile) { } |
| 106 | |
| 107 | protected: |
| 108 | virtual void Initialize(ASTContext &Context) { |
| 109 | NSAPIObj.reset(new NSAPI(Context)); |
| 110 | Editor.reset(new edit::EditedSource(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 111 | Context.getLangOpts(), |
Fariborz Jahanian | 8f5225b | 2013-10-01 21:16:29 +0000 | [diff] [blame] | 112 | PPRec, false)); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
| 116 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 117 | migrateDecl(*I); |
| 118 | return true; |
| 119 | } |
| 120 | virtual void HandleInterestingDecl(DeclGroupRef DG) { |
| 121 | // Ignore decls from the PCH. |
| 122 | } |
| 123 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 124 | ObjCMigrateASTConsumer::HandleTopLevelDecl(DG); |
| 125 | } |
| 126 | |
| 127 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 128 | }; |
| 129 | |
| 130 | } |
| 131 | |
| 132 | ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 133 | StringRef migrateDir, |
| 134 | unsigned migrateAction) |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 135 | : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 136 | ObjCMigAction(migrateAction), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 137 | CompInst(0) { |
| 138 | if (MigrateDir.empty()) |
| 139 | MigrateDir = "."; // user current directory if none is given. |
| 140 | } |
| 141 | |
| 142 | ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, |
| 143 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 144 | PPConditionalDirectiveRecord * |
| 145 | PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); |
| 146 | CompInst->getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 147 | ASTConsumer * |
| 148 | WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 149 | ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 150 | ObjCMigAction, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 151 | Remapper, |
| 152 | CompInst->getFileManager(), |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 153 | PPRec, |
| 154 | CompInst->getPreprocessor()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 155 | ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer }; |
| 156 | return new MultiplexConsumer(Consumers); |
| 157 | } |
| 158 | |
| 159 | bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) { |
| 160 | Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(), |
| 161 | /*ignoreIfFilesChanges=*/true); |
| 162 | CompInst = &CI; |
| 163 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | namespace { |
| 168 | class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> { |
| 169 | ObjCMigrateASTConsumer &Consumer; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 170 | ParentMap &PMap; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 171 | |
| 172 | public: |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 173 | ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap) |
| 174 | : Consumer(consumer), PMap(PMap) { } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 175 | |
| 176 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 177 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 178 | |
| 179 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 180 | if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 181 | edit::Commit commit(*Consumer.Editor); |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 182 | edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 183 | Consumer.Editor->commit(commit); |
| 184 | } |
| 185 | |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 186 | if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 187 | edit::Commit commit(*Consumer.Editor); |
| 188 | edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit); |
| 189 | Consumer.Editor->commit(commit); |
| 190 | } |
| 191 | |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | bool TraverseObjCMessageExpr(ObjCMessageExpr *E) { |
| 196 | // Do depth first; we want to rewrite the subexpressions first so that if |
| 197 | // we have to move expressions we will move them already rewritten. |
| 198 | for (Stmt::child_range range = E->children(); range; ++range) |
| 199 | if (!TraverseStmt(*range)) |
| 200 | return false; |
| 201 | |
| 202 | return WalkUpFromObjCMessageExpr(E); |
| 203 | } |
| 204 | }; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 205 | |
| 206 | class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> { |
| 207 | ObjCMigrateASTConsumer &Consumer; |
| 208 | OwningPtr<ParentMap> PMap; |
| 209 | |
| 210 | public: |
| 211 | BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { } |
| 212 | |
| 213 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 214 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 215 | |
| 216 | bool TraverseStmt(Stmt *S) { |
| 217 | PMap.reset(new ParentMap(S)); |
| 218 | ObjCMigrator(Consumer, *PMap).TraverseStmt(S); |
| 219 | return true; |
| 220 | } |
| 221 | }; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void ObjCMigrateASTConsumer::migrateDecl(Decl *D) { |
| 225 | if (!D) |
| 226 | return; |
| 227 | if (isa<ObjCMethodDecl>(D)) |
| 228 | return; // Wait for the ObjC container declaration. |
| 229 | |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 230 | BodyMigrator(*this).TraverseDecl(D); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 233 | static void append_attr(std::string &PropertyString, const char *attr, |
| 234 | bool &LParenAdded) { |
| 235 | if (!LParenAdded) { |
| 236 | PropertyString += "("; |
| 237 | LParenAdded = true; |
| 238 | } |
| 239 | else |
| 240 | PropertyString += ", "; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 241 | PropertyString += attr; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 244 | static |
| 245 | void MigrateBlockOrFunctionPointerTypeVariable(std::string & PropertyString, |
| 246 | const std::string& TypeString, |
| 247 | const char *name) { |
| 248 | const char *argPtr = TypeString.c_str(); |
| 249 | int paren = 0; |
| 250 | while (*argPtr) { |
| 251 | switch (*argPtr) { |
| 252 | case '(': |
| 253 | PropertyString += *argPtr; |
| 254 | paren++; |
| 255 | break; |
| 256 | case ')': |
| 257 | PropertyString += *argPtr; |
| 258 | paren--; |
| 259 | break; |
| 260 | case '^': |
Fariborz Jahanian | 3c593d6 | 2013-10-08 21:32:16 +0000 | [diff] [blame] | 261 | case '*': |
| 262 | PropertyString += (*argPtr); |
| 263 | if (paren == 1) { |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 264 | PropertyString += name; |
Fariborz Jahanian | 3c593d6 | 2013-10-08 21:32:16 +0000 | [diff] [blame] | 265 | name = ""; |
| 266 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 267 | break; |
| 268 | default: |
| 269 | PropertyString += *argPtr; |
| 270 | break; |
| 271 | } |
| 272 | argPtr++; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
Fariborz Jahanian | 1b66787 | 2013-10-16 22:35:19 +0000 | [diff] [blame] | 277 | static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 278 | const ObjCMethodDecl *Setter, |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 279 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 280 | unsigned LengthOfPrefix, |
| 281 | bool Atomic) { |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 282 | ASTContext &Context = NS.getASTContext(); |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 283 | bool LParenAdded = false; |
| 284 | std::string PropertyString = "@property "; |
| 285 | if (!Atomic) { |
| 286 | PropertyString += "(nonatomic"; |
| 287 | LParenAdded = true; |
| 288 | } |
| 289 | |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 290 | std::string PropertyNameString = Getter->getNameAsString(); |
| 291 | StringRef PropertyName(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 292 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 293 | if (!LParenAdded) { |
| 294 | PropertyString += "(getter="; |
| 295 | LParenAdded = true; |
| 296 | } |
| 297 | else |
| 298 | PropertyString += ", getter="; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 299 | PropertyString += PropertyNameString; |
| 300 | } |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 301 | // Property with no setter may be suggested as a 'readonly' property. |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 302 | if (!Setter) { |
| 303 | if (!LParenAdded) { |
| 304 | PropertyString += "(readonly"; |
| 305 | LParenAdded = true; |
| 306 | } |
| 307 | else |
| 308 | append_attr(PropertyString, "readonly", LParenAdded); |
| 309 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 310 | |
Fariborz Jahanian | 33304e30 | 2013-10-16 18:52:17 +0000 | [diff] [blame] | 311 | // Short circuit 'delegate' properties that contain the name "delegate" or |
| 312 | // "dataSource", or have exact name "target" to have 'assign' attribute. |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 313 | if (PropertyName.equals("target") || |
| 314 | (PropertyName.find("delegate") != StringRef::npos) || |
Fariborz Jahanian | 78bff05 | 2013-10-16 20:44:26 +0000 | [diff] [blame] | 315 | (PropertyName.find("dataSource") != StringRef::npos)) { |
| 316 | QualType QT = Getter->getResultType(); |
| 317 | if (!QT->isRealType()) |
| 318 | append_attr(PropertyString, "assign", LParenAdded); |
| 319 | } |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 320 | else if (Setter) { |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 321 | const ParmVarDecl *argDecl = *Setter->param_begin(); |
| 322 | QualType ArgType = Context.getCanonicalType(argDecl->getType()); |
| 323 | Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime(); |
| 324 | bool RetainableObject = ArgType->isObjCRetainableType(); |
| 325 | if (RetainableObject && propertyLifetime == Qualifiers::OCL_Strong) { |
| 326 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 327 | ArgType->getAs<ObjCObjectPointerType>()) { |
| 328 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 329 | if (IDecl && |
| 330 | IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying"))) |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 331 | append_attr(PropertyString, "copy", LParenAdded); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 332 | else |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 333 | append_attr(PropertyString, "retain", LParenAdded); |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 334 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 335 | else if (ArgType->isBlockPointerType()) |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 336 | append_attr(PropertyString, "copy", LParenAdded); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 337 | } else if (propertyLifetime == Qualifiers::OCL_Weak) |
| 338 | // TODO. More precise determination of 'weak' attribute requires |
| 339 | // looking into setter's implementation for backing weak ivar. |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 340 | append_attr(PropertyString, "weak", LParenAdded); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 341 | else if (RetainableObject) |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 342 | append_attr(PropertyString, |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 343 | ArgType->isBlockPointerType() ? "copy" : "retain", LParenAdded); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 344 | } |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 345 | if (LParenAdded) |
| 346 | PropertyString += ')'; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 347 | QualType RT = Getter->getResultType(); |
| 348 | if (!isa<TypedefType>(RT)) { |
| 349 | // strip off any ARC lifetime qualifier. |
| 350 | QualType CanResultTy = Context.getCanonicalType(RT); |
| 351 | if (CanResultTy.getQualifiers().hasObjCLifetime()) { |
| 352 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 353 | Qs.removeObjCLifetime(); |
| 354 | RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 355 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 356 | } |
| 357 | PropertyString += " "; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 358 | PrintingPolicy SubPolicy(Context.getPrintingPolicy()); |
| 359 | SubPolicy.SuppressStrongLifetime = true; |
Fariborz Jahanian | de557f8 | 2013-10-09 17:37:28 +0000 | [diff] [blame] | 360 | SubPolicy.SuppressLifetimeQualifiers = true; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 361 | std::string TypeString = RT.getAsString(SubPolicy); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 362 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 363 | // property name must strip off "is" and lower case the first character |
| 364 | // after that; e.g. isContinuous will become continuous. |
| 365 | StringRef PropertyNameStringRef(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 366 | PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 367 | PropertyNameString = PropertyNameStringRef; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 368 | bool NoLowering = (isUppercase(PropertyNameString[0]) && |
| 369 | PropertyNameString.size() > 1 && |
| 370 | isUppercase(PropertyNameString[1])); |
Fariborz Jahanian | 34fea36 | 2013-09-11 18:27:16 +0000 | [diff] [blame] | 371 | if (!NoLowering) |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 372 | PropertyNameString[0] = toLowercase(PropertyNameString[0]); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 373 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 374 | if (RT->isBlockPointerType() || RT->isFunctionPointerType()) |
| 375 | MigrateBlockOrFunctionPointerTypeVariable(PropertyString, |
| 376 | TypeString, |
| 377 | PropertyNameString.c_str()); |
| 378 | else { |
| 379 | char LastChar = TypeString[TypeString.size()-1]; |
| 380 | PropertyString += TypeString; |
| 381 | if (LastChar != '*') |
| 382 | PropertyString += ' '; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 383 | PropertyString += PropertyNameString; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 384 | } |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 385 | SourceLocation StartGetterSelectorLoc = Getter->getSelectorStartLoc(); |
| 386 | Selector GetterSelector = Getter->getSelector(); |
| 387 | |
| 388 | SourceLocation EndGetterSelectorLoc = |
| 389 | StartGetterSelectorLoc.getLocWithOffset(GetterSelector.getNameForSlot(0).size()); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 390 | commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(), |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 391 | EndGetterSelectorLoc), |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 392 | PropertyString); |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 393 | if (Setter) { |
| 394 | SourceLocation EndLoc = Setter->getDeclaratorEndLoc(); |
| 395 | // Get location past ';' |
| 396 | EndLoc = EndLoc.getLocWithOffset(1); |
Fariborz Jahanian | 1b66787 | 2013-10-16 22:35:19 +0000 | [diff] [blame] | 397 | SourceLocation BeginOfSetterDclLoc = Setter->getLocStart(); |
| 398 | // FIXME. This assumes that setter decl; is immediately preceeded by eoln. |
| 399 | // It is trying to remove the setter method decl. line entirely. |
| 400 | BeginOfSetterDclLoc = BeginOfSetterDclLoc.getLocWithOffset(-1); |
| 401 | commit.remove(SourceRange(BeginOfSetterDclLoc, EndLoc)); |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 402 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 405 | void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 406 | ObjCContainerDecl *D) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 407 | if (D->isDeprecated()) |
| 408 | return; |
| 409 | |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 410 | for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end(); |
| 411 | M != MEnd; ++M) { |
| 412 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 413 | if (Method->isDeprecated()) |
| 414 | continue; |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 415 | migrateProperty(Ctx, D, Method); |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 416 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 417 | migrateNsReturnsInnerPointer(Ctx, Method); |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 418 | } |
| 419 | for (ObjCContainerDecl::prop_iterator P = D->prop_begin(), |
| 420 | E = D->prop_end(); P != E; ++P) { |
| 421 | ObjCPropertyDecl *Prop = *P; |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 422 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame^] | 423 | !Prop->isDeprecated()) |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 424 | migratePropertyNsReturnsInnerPointer(Ctx, Prop); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame^] | 428 | void ObjCMigrateASTConsumer::migregateDeprecatedAnnotation(ASTContext &Ctx, |
| 429 | ObjCCategoryDecl *CatDecl) { |
| 430 | StringRef Name = CatDecl->getName(); |
| 431 | if (!Name.startswith("NS") || !Name.endswith("Deprecated")) |
| 432 | return; |
| 433 | |
| 434 | if (!Ctx.Idents.get("DEPRECATED").hasMacroDefinition()) |
| 435 | return; |
| 436 | |
| 437 | ObjCContainerDecl *D = cast<ObjCContainerDecl>(CatDecl); |
| 438 | |
| 439 | for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end(); |
| 440 | M != MEnd; ++M) { |
| 441 | ObjCMethodDecl *Method = (*M); |
| 442 | if (Method->isDeprecated() || Method->isImplicit()) |
| 443 | continue; |
| 444 | // Annotate with DEPRECATED |
| 445 | edit::Commit commit(*Editor); |
| 446 | commit.insertBefore(Method->getLocEnd(), " DEPRECATED"); |
| 447 | Editor->commit(commit); |
| 448 | } |
| 449 | for (ObjCContainerDecl::prop_iterator P = D->prop_begin(), |
| 450 | E = D->prop_end(); P != E; ++P) { |
| 451 | ObjCPropertyDecl *Prop = *P; |
| 452 | if (Prop->isDeprecated()) |
| 453 | continue; |
| 454 | // Annotate with DEPRECATED |
| 455 | edit::Commit commit(*Editor); |
| 456 | commit.insertAfterToken(Prop->getLocEnd(), " DEPRECATED"); |
| 457 | Editor->commit(commit); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static bool |
Fariborz Jahanian | 9a3512b | 2013-07-13 17:16:41 +0000 | [diff] [blame] | 462 | ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 463 | const ObjCImplementationDecl *ImpDecl, |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 464 | const ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 465 | ObjCProtocolDecl *Protocol) { |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 466 | // In auto-synthesis, protocol properties are not synthesized. So, |
| 467 | // a conforming protocol must have its required properties declared |
| 468 | // in class interface. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 469 | bool HasAtleastOneRequiredProperty = false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 470 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) |
| 471 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 472 | E = PDecl->prop_end(); P != E; ++P) { |
| 473 | ObjCPropertyDecl *Property = *P; |
| 474 | if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 475 | continue; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 476 | HasAtleastOneRequiredProperty = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 477 | DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 478 | if (R.size() == 0) { |
| 479 | // Relax the rule and look into class's implementation for a synthesize |
| 480 | // or dynamic declaration. Class is implementing a property coming from |
| 481 | // another protocol. This still makes the target protocol as conforming. |
| 482 | if (!ImpDecl->FindPropertyImplDecl( |
| 483 | Property->getDeclName().getAsIdentifierInfo())) |
| 484 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 485 | } |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 486 | else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 487 | if ((ClassProperty->getPropertyAttributes() |
| 488 | != Property->getPropertyAttributes()) || |
| 489 | !Ctx.hasSameType(ClassProperty->getType(), Property->getType())) |
| 490 | return false; |
| 491 | } |
| 492 | else |
| 493 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 494 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 495 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 496 | // At this point, all required properties in this protocol conform to those |
| 497 | // declared in the class. |
| 498 | // Check that class implements the required methods of the protocol too. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 499 | bool HasAtleastOneRequiredMethod = false; |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 500 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) { |
| 501 | if (PDecl->meth_begin() == PDecl->meth_end()) |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 502 | return HasAtleastOneRequiredProperty; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 503 | for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(), |
| 504 | MEnd = PDecl->meth_end(); M != MEnd; ++M) { |
| 505 | ObjCMethodDecl *MD = (*M); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 506 | if (MD->isImplicit()) |
| 507 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 508 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) |
| 509 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 510 | DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 511 | if (R.size() == 0) |
| 512 | return false; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 513 | bool match = false; |
| 514 | HasAtleastOneRequiredMethod = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 515 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 516 | if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0])) |
| 517 | if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) { |
| 518 | match = true; |
| 519 | break; |
| 520 | } |
| 521 | if (!match) |
| 522 | return false; |
| 523 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 524 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 525 | if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod) |
| 526 | return true; |
| 527 | return false; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 530 | static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl, |
| 531 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols, |
| 532 | const NSAPI &NS, edit::Commit &commit) { |
| 533 | const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols(); |
| 534 | std::string ClassString; |
| 535 | SourceLocation EndLoc = |
| 536 | IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation(); |
| 537 | |
| 538 | if (Protocols.empty()) { |
| 539 | ClassString = '<'; |
| 540 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 541 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 542 | if (i != (e-1)) |
| 543 | ClassString += ", "; |
| 544 | } |
| 545 | ClassString += "> "; |
| 546 | } |
| 547 | else { |
| 548 | ClassString = ", "; |
| 549 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 550 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 551 | if (i != (e-1)) |
| 552 | ClassString += ", "; |
| 553 | } |
| 554 | ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1; |
| 555 | EndLoc = *PL; |
| 556 | } |
| 557 | |
| 558 | commit.insertAfterToken(EndLoc, ClassString); |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl, |
| 563 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 564 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 565 | bool IsNSIntegerType, |
| 566 | bool NSOptions) { |
| 567 | std::string ClassString; |
| 568 | if (NSOptions) |
| 569 | ClassString = "typedef NS_OPTIONS(NSUInteger, "; |
| 570 | else |
| 571 | ClassString = |
| 572 | IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " |
| 573 | : "typedef NS_ENUM(NSUInteger, "; |
| 574 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 575 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 576 | ClassString += ')'; |
| 577 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 578 | commit.replace(R, ClassString); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 579 | SourceLocation EndOfEnumDclLoc = EnumDcl->getLocEnd(); |
| 580 | EndOfEnumDclLoc = trans::findSemiAfterLocation(EndOfEnumDclLoc, |
| 581 | NS.getASTContext(), /*IsDecl*/true); |
| 582 | if (!EndOfEnumDclLoc.isInvalid()) { |
| 583 | SourceRange EnumDclRange(EnumDcl->getLocStart(), EndOfEnumDclLoc); |
| 584 | commit.insertFromRange(TypedefDcl->getLocStart(), EnumDclRange); |
| 585 | } |
| 586 | else |
| 587 | return false; |
| 588 | |
| 589 | SourceLocation EndTypedefDclLoc = TypedefDcl->getLocEnd(); |
| 590 | EndTypedefDclLoc = trans::findSemiAfterLocation(EndTypedefDclLoc, |
| 591 | NS.getASTContext(), /*IsDecl*/true); |
| 592 | if (!EndTypedefDclLoc.isInvalid()) { |
| 593 | SourceRange TDRange(TypedefDcl->getLocStart(), EndTypedefDclLoc); |
| 594 | commit.remove(TDRange); |
| 595 | } |
| 596 | else |
| 597 | return false; |
| 598 | |
| 599 | EndOfEnumDclLoc = trans::findLocationAfterSemi(EnumDcl->getLocEnd(), NS.getASTContext(), |
Fariborz Jahanian | 1d27ffd | 2013-10-11 17:35:22 +0000 | [diff] [blame] | 600 | /*IsDecl*/true); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 601 | if (!EndOfEnumDclLoc.isInvalid()) { |
| 602 | SourceLocation BeginOfEnumDclLoc = EnumDcl->getLocStart(); |
| 603 | // FIXME. This assumes that enum decl; is immediately preceeded by eoln. |
| 604 | // It is trying to remove the enum decl. lines entirely. |
| 605 | BeginOfEnumDclLoc = BeginOfEnumDclLoc.getLocWithOffset(-1); |
| 606 | commit.remove(SourceRange(BeginOfEnumDclLoc, EndOfEnumDclLoc)); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | return false; |
| 610 | } |
| 611 | |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 612 | static void rewriteToNSMacroDecl(const EnumDecl *EnumDcl, |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 613 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 614 | const NSAPI &NS, edit::Commit &commit, |
| 615 | bool IsNSIntegerType) { |
| 616 | std::string ClassString = |
| 617 | IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, "; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 618 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 619 | ClassString += ')'; |
| 620 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 621 | commit.replace(R, ClassString); |
| 622 | SourceLocation TypedefLoc = TypedefDcl->getLocEnd(); |
| 623 | commit.remove(SourceRange(TypedefLoc, TypedefLoc)); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 626 | static bool UseNSOptionsMacro(Preprocessor &PP, ASTContext &Ctx, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 627 | const EnumDecl *EnumDcl) { |
| 628 | bool PowerOfTwo = true; |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 629 | bool AllHexdecimalEnumerator = true; |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 630 | uint64_t MaxPowerOfTwoVal = 0; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 631 | for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(), |
| 632 | EE = EnumDcl->enumerator_end(); EI != EE; ++EI) { |
| 633 | EnumConstantDecl *Enumerator = (*EI); |
| 634 | const Expr *InitExpr = Enumerator->getInitExpr(); |
| 635 | if (!InitExpr) { |
| 636 | PowerOfTwo = false; |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 637 | AllHexdecimalEnumerator = false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 638 | continue; |
| 639 | } |
Fariborz Jahanian | 6e1798e | 2013-09-17 23:32:51 +0000 | [diff] [blame] | 640 | InitExpr = InitExpr->IgnoreParenCasts(); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 641 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) |
| 642 | if (BO->isShiftOp() || BO->isBitwiseOp()) |
| 643 | return true; |
| 644 | |
| 645 | uint64_t EnumVal = Enumerator->getInitVal().getZExtValue(); |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 646 | if (PowerOfTwo && EnumVal) { |
| 647 | if (!llvm::isPowerOf2_64(EnumVal)) |
| 648 | PowerOfTwo = false; |
| 649 | else if (EnumVal > MaxPowerOfTwoVal) |
| 650 | MaxPowerOfTwoVal = EnumVal; |
| 651 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 652 | if (AllHexdecimalEnumerator && EnumVal) { |
| 653 | bool FoundHexdecimalEnumerator = false; |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 654 | SourceLocation EndLoc = Enumerator->getLocEnd(); |
| 655 | Token Tok; |
| 656 | if (!PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true)) |
| 657 | if (Tok.isLiteral() && Tok.getLength() > 2) { |
| 658 | if (const char *StringLit = Tok.getLiteralData()) |
| 659 | FoundHexdecimalEnumerator = |
| 660 | (StringLit[0] == '0' && (toLowercase(StringLit[1]) == 'x')); |
| 661 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 662 | if (!FoundHexdecimalEnumerator) |
| 663 | AllHexdecimalEnumerator = false; |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 664 | } |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 665 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 666 | return AllHexdecimalEnumerator || (PowerOfTwo && (MaxPowerOfTwoVal > 2)); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Fariborz Jahanian | 008ef72 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 669 | void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 670 | const ObjCImplementationDecl *ImpDecl) { |
| 671 | const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface(); |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 672 | if (!IDecl || ObjCProtocolDecls.empty() || IDecl->isDeprecated()) |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 673 | return; |
| 674 | // Find all implicit conforming protocols for this class |
| 675 | // and make them explicit. |
| 676 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols; |
| 677 | Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols); |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 678 | llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 679 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 680 | for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I = |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 681 | ObjCProtocolDecls.begin(), |
| 682 | E = ObjCProtocolDecls.end(); I != E; ++I) |
| 683 | if (!ExplicitProtocols.count(*I)) |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 684 | PotentialImplicitProtocols.push_back(*I); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 685 | |
| 686 | if (PotentialImplicitProtocols.empty()) |
| 687 | return; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 688 | |
| 689 | // go through list of non-optional methods and properties in each protocol |
| 690 | // in the PotentialImplicitProtocols list. If class implements every one of the |
| 691 | // methods and properties, then this class conforms to this protocol. |
| 692 | llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols; |
| 693 | for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 694 | if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 695 | PotentialImplicitProtocols[i])) |
| 696 | ConformingProtocols.push_back(PotentialImplicitProtocols[i]); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 697 | |
| 698 | if (ConformingProtocols.empty()) |
| 699 | return; |
Fariborz Jahanian | cb7b8de | 2013-07-17 00:02:22 +0000 | [diff] [blame] | 700 | |
| 701 | // Further reduce number of conforming protocols. If protocol P1 is in the list |
| 702 | // protocol P2 (P2<P1>), No need to include P1. |
| 703 | llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols; |
| 704 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 705 | bool DropIt = false; |
| 706 | ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i]; |
| 707 | for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) { |
| 708 | ObjCProtocolDecl *PDecl = ConformingProtocols[i1]; |
| 709 | if (PDecl == TargetPDecl) |
| 710 | continue; |
| 711 | if (PDecl->lookupProtocolNamed( |
| 712 | TargetPDecl->getDeclName().getAsIdentifierInfo())) { |
| 713 | DropIt = true; |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | if (!DropIt) |
| 718 | MinimalConformingProtocols.push_back(TargetPDecl); |
| 719 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 720 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 721 | rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols, |
| 722 | *NSAPIObj, commit); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 723 | Editor->commit(commit); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 726 | void ObjCMigrateASTConsumer::CacheObjCNSIntegerTypedefed( |
| 727 | const TypedefDecl *TypedefDcl) { |
| 728 | |
| 729 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
| 730 | if (NSAPIObj->isObjCNSIntegerType(qt)) |
| 731 | NSIntegerTypedefed = TypedefDcl; |
| 732 | else if (NSAPIObj->isObjCNSUIntegerType(qt)) |
| 733 | NSUIntegerTypedefed = TypedefDcl; |
| 734 | } |
| 735 | |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 736 | bool ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 737 | const EnumDecl *EnumDcl, |
| 738 | const TypedefDecl *TypedefDcl) { |
| 739 | if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() || |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 740 | EnumDcl->isDeprecated()) |
| 741 | return false; |
| 742 | if (!TypedefDcl) { |
| 743 | if (NSIntegerTypedefed) { |
| 744 | TypedefDcl = NSIntegerTypedefed; |
| 745 | NSIntegerTypedefed = 0; |
| 746 | } |
| 747 | else if (NSUIntegerTypedefed) { |
| 748 | TypedefDcl = NSUIntegerTypedefed; |
| 749 | NSUIntegerTypedefed = 0; |
| 750 | } |
| 751 | else |
| 752 | return false; |
| 753 | unsigned FileIdOfTypedefDcl = |
| 754 | PP.getSourceManager().getFileID(TypedefDcl->getLocation()).getHashValue(); |
| 755 | unsigned FileIdOfEnumDcl = |
| 756 | PP.getSourceManager().getFileID(EnumDcl->getLocation()).getHashValue(); |
| 757 | if (FileIdOfTypedefDcl != FileIdOfEnumDcl) |
| 758 | return false; |
| 759 | } |
| 760 | if (TypedefDcl->isDeprecated()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 761 | return false; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 762 | |
| 763 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 764 | bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt); |
| 765 | bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 766 | |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 767 | if (!IsNSIntegerType && !IsNSUIntegerType) { |
| 768 | // Also check for typedef enum {...} TD; |
| 769 | if (const EnumType *EnumTy = qt->getAs<EnumType>()) { |
| 770 | if (EnumTy->getDecl() == EnumDcl) { |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 771 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 772 | if (NSOptions) { |
| 773 | if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 774 | return false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 775 | } |
| 776 | else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 777 | return false; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 778 | edit::Commit commit(*Editor); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 779 | rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 780 | Editor->commit(commit); |
Fariborz Jahanian | de79e81 | 2013-10-17 22:23:32 +0000 | [diff] [blame] | 781 | return true; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 782 | } |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 783 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 784 | return false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 785 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 786 | |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 787 | // We may still use NS_OPTIONS based on what we find in the enumertor list. |
| 788 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 789 | // NS_ENUM must be available. |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 790 | if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 791 | return false; |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 792 | // NS_OPTIONS must be available. |
| 793 | if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 794 | return false; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 795 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 796 | bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, |
| 797 | commit, IsNSIntegerType, NSOptions); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 798 | Editor->commit(commit); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 799 | return Res; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 802 | static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC, |
| 803 | ObjCMethodDecl *OM) { |
| 804 | SourceRange R; |
| 805 | std::string ClassString; |
| 806 | if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { |
| 807 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 808 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); |
| 809 | ClassString = "instancetype"; |
| 810 | } |
| 811 | else { |
| 812 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 813 | ClassString = OM->isInstanceMethod() ? '-' : '+'; |
| 814 | ClassString += " (instancetype)"; |
| 815 | } |
| 816 | edit::Commit commit(*ASTC.Editor); |
| 817 | commit.replace(R, ClassString); |
| 818 | ASTC.Editor->commit(commit); |
| 819 | } |
| 820 | |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 821 | static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC, |
| 822 | ObjCMethodDecl *OM) { |
| 823 | ObjCInterfaceDecl *IDecl = OM->getClassInterface(); |
| 824 | SourceRange R; |
| 825 | std::string ClassString; |
| 826 | if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { |
| 827 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 828 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); { |
| 829 | ClassString = IDecl->getName(); |
| 830 | ClassString += "*"; |
| 831 | } |
| 832 | } |
| 833 | else { |
| 834 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 835 | ClassString = "+ ("; |
| 836 | ClassString += IDecl->getName(); ClassString += "*)"; |
| 837 | } |
| 838 | edit::Commit commit(*ASTC.Editor); |
| 839 | commit.replace(R, ClassString); |
| 840 | ASTC.Editor->commit(commit); |
| 841 | } |
| 842 | |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 843 | void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, |
| 844 | ObjCContainerDecl *CDecl, |
| 845 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 846 | ObjCInstanceTypeFamily OIT_Family = |
| 847 | Selector::getInstTypeMethodFamily(OM->getSelector()); |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 848 | |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 849 | std::string ClassName; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 850 | switch (OIT_Family) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 851 | case OIT_None: |
| 852 | migrateFactoryMethod(Ctx, CDecl, OM); |
| 853 | return; |
Fariborz Jahanian | 7dd7143 | 2013-08-28 21:23:00 +0000 | [diff] [blame] | 854 | case OIT_Array: |
| 855 | ClassName = "NSArray"; |
| 856 | break; |
| 857 | case OIT_Dictionary: |
| 858 | ClassName = "NSDictionary"; |
| 859 | break; |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 860 | case OIT_Singleton: |
| 861 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton); |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 862 | return; |
Fariborz Jahanian | 1c900bc | 2013-09-18 20:35:47 +0000 | [diff] [blame] | 863 | case OIT_Init: |
| 864 | if (OM->getResultType()->isObjCIdType()) |
| 865 | ReplaceWithInstancetype(*this, OM); |
| 866 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 867 | case OIT_ReturnsSelf: |
| 868 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf); |
| 869 | return; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 870 | } |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 871 | if (!OM->getResultType()->isObjCIdType()) |
| 872 | return; |
| 873 | |
| 874 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 875 | if (!IDecl) { |
| 876 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 877 | IDecl = CatDecl->getClassInterface(); |
| 878 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 879 | IDecl = ImpDecl->getClassInterface(); |
| 880 | } |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 881 | if (!IDecl || |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 882 | !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { |
| 883 | migrateFactoryMethod(Ctx, CDecl, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 884 | return; |
Fariborz Jahanian | 280954a | 2013-07-24 18:31:42 +0000 | [diff] [blame] | 885 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 886 | ReplaceWithInstancetype(*this, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 889 | static bool TypeIsInnerPointer(QualType T) { |
| 890 | if (!T->isAnyPointerType()) |
| 891 | return false; |
| 892 | if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() || |
Fariborz Jahanian | 73466ca | 2013-09-26 21:43:47 +0000 | [diff] [blame] | 893 | T->isBlockPointerType() || T->isFunctionPointerType() || |
| 894 | ento::coreFoundation::isCFObjectRef(T)) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 895 | return false; |
Fariborz Jahanian | 9d5fffb | 2013-09-09 23:56:14 +0000 | [diff] [blame] | 896 | // Also, typedef-of-pointer-to-incomplete-struct is something that we assume |
| 897 | // is not an innter pointer type. |
| 898 | QualType OrigT = T; |
| 899 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) |
| 900 | T = TD->getDecl()->getUnderlyingType(); |
| 901 | if (OrigT == T || !T->isPointerType()) |
| 902 | return true; |
| 903 | const PointerType* PT = T->getAs<PointerType>(); |
| 904 | QualType UPointeeT = PT->getPointeeType().getUnqualifiedType(); |
| 905 | if (UPointeeT->isRecordType()) { |
| 906 | const RecordType *RecordTy = UPointeeT->getAs<RecordType>(); |
| 907 | if (!RecordTy->getDecl()->isCompleteDefinition()) |
| 908 | return false; |
| 909 | } |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 910 | return true; |
| 911 | } |
| 912 | |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 913 | static bool AttributesMatch(const Decl *Decl1, const Decl *Decl2) { |
| 914 | if (Decl1->hasAttrs() != Decl2->hasAttrs()) |
| 915 | return false; |
| 916 | |
| 917 | if (!Decl1->hasAttrs()) |
| 918 | return true; |
| 919 | |
| 920 | const AttrVec &Attrs1 = Decl1->getAttrs(); |
| 921 | const AttrVec &Attrs2 = Decl2->getAttrs(); |
| 922 | // This list is very small, so this need not be optimized. |
| 923 | for (unsigned i = 0, e = Attrs1.size(); i != e; i++) { |
| 924 | bool match = false; |
| 925 | for (unsigned j = 0, f = Attrs2.size(); j != f; j++) { |
| 926 | // Matching attribute kind only. We are not getting into |
| 927 | // details of the attributes. For all practical purposes |
| 928 | // this is sufficient. |
| 929 | if (Attrs1[i]->getKind() == Attrs2[j]->getKind()) { |
| 930 | match = true; |
| 931 | break; |
| 932 | } |
| 933 | } |
| 934 | if (!match) |
| 935 | return false; |
| 936 | } |
| 937 | return true; |
| 938 | } |
| 939 | |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 940 | static bool IsValidIdentifier(ASTContext &Ctx, |
| 941 | const char *Name) { |
| 942 | if (!isIdentifierHead(Name[0])) |
| 943 | return false; |
| 944 | std::string NameString = Name; |
| 945 | NameString[0] = toLowercase(NameString[0]); |
| 946 | IdentifierInfo *II = &Ctx.Idents.get(NameString); |
| 947 | return II->getTokenID() == tok::identifier; |
| 948 | } |
| 949 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 950 | bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 951 | ObjCContainerDecl *D, |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 952 | ObjCMethodDecl *Method) { |
| 953 | if (Method->isPropertyAccessor() || !Method->isInstanceMethod() || |
| 954 | Method->param_size() != 0) |
| 955 | return false; |
| 956 | // Is this method candidate to be a getter? |
| 957 | QualType GRT = Method->getResultType(); |
| 958 | if (GRT->isVoidType()) |
| 959 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 960 | |
| 961 | Selector GetterSelector = Method->getSelector(); |
Fariborz Jahanian | 7391a7b5a | 2013-09-25 00:17:07 +0000 | [diff] [blame] | 962 | ObjCInstanceTypeFamily OIT_Family = |
| 963 | Selector::getInstTypeMethodFamily(GetterSelector); |
| 964 | |
| 965 | if (OIT_Family != OIT_None) |
| 966 | return false; |
| 967 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 968 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 969 | Selector SetterSelector = |
| 970 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 971 | PP.getSelectorTable(), |
| 972 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 973 | ObjCMethodDecl *SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 974 | unsigned LengthOfPrefix = 0; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 975 | if (!SetterMethod) { |
| 976 | // try a different naming convention for getter: isXxxxx |
| 977 | StringRef getterNameString = getterName->getName(); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 978 | bool IsPrefix = getterNameString.startswith("is"); |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 979 | // Note that we don't want to change an isXXX method of retainable object |
| 980 | // type to property (readonly or otherwise). |
| 981 | if (IsPrefix && GRT->isObjCRetainableType()) |
| 982 | return false; |
| 983 | if (IsPrefix || getterNameString.startswith("get")) { |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 984 | LengthOfPrefix = (IsPrefix ? 2 : 3); |
| 985 | const char *CGetterName = getterNameString.data() + LengthOfPrefix; |
| 986 | // Make sure that first character after "is" or "get" prefix can |
| 987 | // start an identifier. |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 988 | if (!IsValidIdentifier(Ctx, CGetterName)) |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 989 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 990 | if (CGetterName[0] && isUppercase(CGetterName[0])) { |
| 991 | getterName = &Ctx.Idents.get(CGetterName); |
| 992 | SetterSelector = |
| 993 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 994 | PP.getSelectorTable(), |
| 995 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 996 | SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | } |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 1000 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1001 | if (SetterMethod) { |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1002 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_ReadwriteProperty) == 0) |
| 1003 | return false; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1004 | if (SetterMethod->isDeprecated() || |
| 1005 | !AttributesMatch(Method, SetterMethod)) |
Fariborz Jahanian | f6c6505 | 2013-09-17 22:41:25 +0000 | [diff] [blame] | 1006 | return false; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1007 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1008 | // Is this a valid setter, matching the target getter? |
| 1009 | QualType SRT = SetterMethod->getResultType(); |
| 1010 | if (!SRT->isVoidType()) |
| 1011 | return false; |
| 1012 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
| 1013 | QualType ArgType = argDecl->getType(); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1014 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT)) |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1015 | return false; |
| 1016 | edit::Commit commit(*Editor); |
| 1017 | rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1018 | LengthOfPrefix, |
| 1019 | (ASTMigrateActions & |
| 1020 | FrontendOptions::ObjCMT_AtomicProperty) != 0); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1021 | Editor->commit(commit); |
| 1022 | return true; |
| 1023 | } |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 1024 | else if (ASTMigrateActions & FrontendOptions::ObjCMT_ReadonlyProperty) { |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1025 | // Try a non-void method with no argument (and no setter or property of same name |
| 1026 | // as a 'readonly' property. |
| 1027 | edit::Commit commit(*Editor); |
| 1028 | rewriteToObjCProperty(Method, 0 /*SetterMethod*/, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1029 | LengthOfPrefix, |
| 1030 | (ASTMigrateActions & |
| 1031 | FrontendOptions::ObjCMT_AtomicProperty) != 0); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1032 | Editor->commit(commit); |
| 1033 | return true; |
| 1034 | } |
| 1035 | return false; |
| 1036 | } |
| 1037 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1038 | void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, |
| 1039 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1040 | if (OM->isImplicit() || |
Fariborz Jahanian | 7c1a445 | 2013-09-26 22:43:41 +0000 | [diff] [blame] | 1041 | !OM->isInstanceMethod() || |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1042 | OM->hasAttr<ObjCReturnsInnerPointerAttr>()) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1043 | return; |
| 1044 | |
| 1045 | QualType RT = OM->getResultType(); |
| 1046 | if (!TypeIsInnerPointer(RT) || |
| 1047 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1048 | return; |
| 1049 | |
| 1050 | edit::Commit commit(*Editor); |
| 1051 | commit.insertBefore(OM->getLocEnd(), " NS_RETURNS_INNER_POINTER"); |
| 1052 | Editor->commit(commit); |
| 1053 | } |
| 1054 | |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1055 | void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, |
| 1056 | ObjCPropertyDecl *P) { |
| 1057 | QualType T = P->getType(); |
| 1058 | |
| 1059 | if (!TypeIsInnerPointer(T) || |
| 1060 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1061 | return; |
| 1062 | edit::Commit commit(*Editor); |
| 1063 | commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER "); |
| 1064 | Editor->commit(commit); |
| 1065 | } |
| 1066 | |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1067 | void ObjCMigrateASTConsumer::migrateAllMethodInstaceType(ASTContext &Ctx, |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1068 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | e47a14a | 2013-09-18 15:43:52 +0000 | [diff] [blame] | 1069 | if (CDecl->isDeprecated()) |
| 1070 | return; |
| 1071 | |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1072 | // migrate methods which can have instancetype as their result type. |
| 1073 | for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), |
| 1074 | MEnd = CDecl->meth_end(); |
| 1075 | M != MEnd; ++M) { |
| 1076 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1077 | if (Method->isDeprecated()) |
| 1078 | continue; |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1079 | migrateMethodInstanceType(Ctx, CDecl, Method); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1083 | void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, |
| 1084 | ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1085 | ObjCMethodDecl *OM, |
| 1086 | ObjCInstanceTypeFamily OIT_Family) { |
Fariborz Jahanian | 3bfc35e | 2013-08-02 22:34:18 +0000 | [diff] [blame] | 1087 | if (OM->isInstanceMethod() || |
| 1088 | OM->getResultType() == Ctx.getObjCInstanceType() || |
| 1089 | !OM->getResultType()->isObjCIdType()) |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1090 | return; |
| 1091 | |
| 1092 | // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class |
| 1093 | // NSYYYNamE with matching names be at least 3 characters long. |
| 1094 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 1095 | if (!IDecl) { |
| 1096 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 1097 | IDecl = CatDecl->getClassInterface(); |
| 1098 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 1099 | IDecl = ImpDecl->getClassInterface(); |
| 1100 | } |
| 1101 | if (!IDecl) |
| 1102 | return; |
| 1103 | |
| 1104 | std::string StringClassName = IDecl->getName(); |
| 1105 | StringRef LoweredClassName(StringClassName); |
| 1106 | std::string StringLoweredClassName = LoweredClassName.lower(); |
| 1107 | LoweredClassName = StringLoweredClassName; |
| 1108 | |
| 1109 | IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1110 | // Handle method with no name at its first selector slot; e.g. + (id):(int)x. |
| 1111 | if (!MethodIdName) |
| 1112 | return; |
| 1113 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1114 | std::string MethodName = MethodIdName->getName(); |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1115 | if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1116 | StringRef STRefMethodName(MethodName); |
| 1117 | size_t len = 0; |
| 1118 | if (STRefMethodName.startswith("standard")) |
| 1119 | len = strlen("standard"); |
| 1120 | else if (STRefMethodName.startswith("shared")) |
| 1121 | len = strlen("shared"); |
| 1122 | else if (STRefMethodName.startswith("default")) |
| 1123 | len = strlen("default"); |
| 1124 | else |
| 1125 | return; |
| 1126 | MethodName = STRefMethodName.substr(len); |
| 1127 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1128 | std::string MethodNameSubStr = MethodName.substr(0, 3); |
| 1129 | StringRef MethodNamePrefix(MethodNameSubStr); |
| 1130 | std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower(); |
| 1131 | MethodNamePrefix = StringLoweredMethodNamePrefix; |
| 1132 | size_t Ix = LoweredClassName.rfind(MethodNamePrefix); |
| 1133 | if (Ix == StringRef::npos) |
| 1134 | return; |
| 1135 | std::string ClassNamePostfix = LoweredClassName.substr(Ix); |
| 1136 | StringRef LoweredMethodName(MethodName); |
| 1137 | std::string StringLoweredMethodName = LoweredMethodName.lower(); |
| 1138 | LoweredMethodName = StringLoweredMethodName; |
| 1139 | if (!LoweredMethodName.startswith(ClassNamePostfix)) |
| 1140 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1141 | if (OIT_Family == OIT_ReturnsSelf) |
| 1142 | ReplaceWithClasstype(*this, OM); |
| 1143 | else |
| 1144 | ReplaceWithInstancetype(*this, OM); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1147 | static bool IsVoidStarType(QualType Ty) { |
| 1148 | if (!Ty->isPointerType()) |
| 1149 | return false; |
| 1150 | |
| 1151 | while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr())) |
| 1152 | Ty = TD->getDecl()->getUnderlyingType(); |
| 1153 | |
| 1154 | // Is the type void*? |
| 1155 | const PointerType* PT = Ty->getAs<PointerType>(); |
| 1156 | if (PT->getPointeeType().getUnqualifiedType()->isVoidType()) |
| 1157 | return true; |
| 1158 | return IsVoidStarType(PT->getPointeeType()); |
| 1159 | } |
| 1160 | |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1161 | /// AuditedType - This routine audits the type AT and returns false if it is one of known |
| 1162 | /// CF object types or of the "void *" variety. It returns true if we don't care about the type |
| 1163 | /// such as a non-pointer or pointers which have no ownership issues (such as "int *"). |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1164 | static bool AuditedType (QualType AT) { |
| 1165 | if (!AT->isAnyPointerType() && !AT->isBlockPointerType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1166 | return true; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1167 | // FIXME. There isn't much we can say about CF pointer type; or is there? |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1168 | if (ento::coreFoundation::isCFObjectRef(AT) || |
| 1169 | IsVoidStarType(AT) || |
| 1170 | // If an ObjC object is type, assuming that it is not a CF function and |
| 1171 | // that it is an un-audited function. |
Fariborz Jahanian | 2e9c19c | 2013-08-22 22:27:36 +0000 | [diff] [blame] | 1172 | AT->isObjCObjectPointerType() || AT->isObjCBuiltinType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1173 | return false; |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1174 | // All other pointers are assumed audited as harmless. |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1175 | return true; |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1178 | void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1179 | if (CFFunctionIBCandidates.empty()) |
| 1180 | return; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1181 | if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { |
| 1182 | CFFunctionIBCandidates.clear(); |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 1183 | FileId = 0; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1187 | const Decl *FirstFD = CFFunctionIBCandidates[0]; |
| 1188 | const Decl *LastFD = |
| 1189 | CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1]; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1190 | const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n"; |
| 1191 | edit::Commit commit(*Editor); |
| 1192 | commit.insertBefore(FirstFD->getLocStart(), PragmaString); |
| 1193 | PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n"; |
| 1194 | SourceLocation EndLoc = LastFD->getLocEnd(); |
| 1195 | // get location just past end of function location. |
| 1196 | EndLoc = PP.getLocForEndOfToken(EndLoc); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1197 | if (isa<FunctionDecl>(LastFD)) { |
| 1198 | // For Methods, EndLoc points to the ending semcolon. So, |
| 1199 | // not of these extra work is needed. |
| 1200 | Token Tok; |
| 1201 | // get locaiton of token that comes after end of function. |
| 1202 | bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true); |
| 1203 | if (!Failed) |
| 1204 | EndLoc = Tok.getLocation(); |
| 1205 | } |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1206 | commit.insertAfterToken(EndLoc, PragmaString); |
| 1207 | Editor->commit(commit); |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 1208 | FileId = 0; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1209 | CFFunctionIBCandidates.clear(); |
| 1210 | } |
| 1211 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1212 | void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1213 | if (Decl->isDeprecated()) |
| 1214 | return; |
| 1215 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1216 | if (Decl->hasAttr<CFAuditedTransferAttr>()) { |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1217 | assert(CFFunctionIBCandidates.empty() && |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1218 | "Cannot have audited functions/methods inside user " |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1219 | "provided CF_IMPLICIT_BRIDGING_ENABLE"); |
| 1220 | return; |
| 1221 | } |
| 1222 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1223 | // Finction must be annotated first. |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1224 | if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) { |
| 1225 | CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl); |
| 1226 | if (AuditKind == CF_BRIDGING_ENABLE) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1227 | CFFunctionIBCandidates.push_back(Decl); |
| 1228 | if (!FileId) |
| 1229 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()).getHashValue(); |
| 1230 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1231 | else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) { |
| 1232 | if (!CFFunctionIBCandidates.empty()) { |
| 1233 | CFFunctionIBCandidates.push_back(Decl); |
| 1234 | if (!FileId) |
| 1235 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()).getHashValue(); |
| 1236 | } |
| 1237 | } |
| 1238 | else |
| 1239 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1240 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1241 | else { |
| 1242 | migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl)); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1243 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1244 | } |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1247 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1248 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1249 | const FunctionDecl *FuncDecl, |
| 1250 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1251 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1252 | if (!ResultAnnotated) { |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1253 | RetEffect Ret = CE.getReturnValue(); |
| 1254 | const char *AnnotationString = 0; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1255 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1256 | if (Ret.isOwned() && |
| 1257 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1258 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1259 | else if (Ret.notOwned() && |
| 1260 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1261 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1262 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1263 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
| 1264 | if (Ret.isOwned() && |
| 1265 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1266 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1269 | if (AnnotationString) { |
| 1270 | edit::Commit commit(*Editor); |
| 1271 | commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString); |
| 1272 | Editor->commit(commit); |
| 1273 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1274 | } |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1275 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1276 | unsigned i = 0; |
| 1277 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1278 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
Fariborz Jahanian | b918d7a | 2013-08-21 19:37:47 +0000 | [diff] [blame] | 1279 | const ParmVarDecl *pd = *pi; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1280 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1281 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() && |
| 1282 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1283 | edit::Commit commit(*Editor); |
| 1284 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1285 | Editor->commit(commit); |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1286 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1287 | else if (AE == DecRefMsg && !pd->getAttr<NSConsumedAttr>() && |
| 1288 | Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { |
| 1289 | edit::Commit commit(*Editor); |
| 1290 | commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); |
| 1291 | Editor->commit(commit); |
| 1292 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1293 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | ObjCMigrateASTConsumer::CF_BRIDGING_KIND |
| 1298 | ObjCMigrateASTConsumer::migrateAddFunctionAnnotation( |
| 1299 | ASTContext &Ctx, |
| 1300 | const FunctionDecl *FuncDecl) { |
| 1301 | if (FuncDecl->hasBody()) |
| 1302 | return CF_BRIDGING_NONE; |
| 1303 | |
| 1304 | CallEffects CE = CallEffects::getEffect(FuncDecl); |
| 1305 | bool FuncIsReturnAnnotated = (FuncDecl->getAttr<CFReturnsRetainedAttr>() || |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1306 | FuncDecl->getAttr<CFReturnsNotRetainedAttr>() || |
| 1307 | FuncDecl->getAttr<NSReturnsRetainedAttr>() || |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1308 | FuncDecl->getAttr<NSReturnsNotRetainedAttr>() || |
| 1309 | FuncDecl->getAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Trivial case of when funciton is annotated and has no argument. |
| 1312 | if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0) |
| 1313 | return CF_BRIDGING_NONE; |
| 1314 | |
| 1315 | bool ReturnCFAudited = false; |
| 1316 | if (!FuncIsReturnAnnotated) { |
| 1317 | RetEffect Ret = CE.getReturnValue(); |
| 1318 | if (Ret.getObjKind() == RetEffect::CF && |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1319 | (Ret.isOwned() || Ret.notOwned())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1320 | ReturnCFAudited = true; |
| 1321 | else if (!AuditedType(FuncDecl->getResultType())) |
| 1322 | return CF_BRIDGING_NONE; |
| 1323 | } |
| 1324 | |
| 1325 | // At this point result type is audited for potential inclusion. |
| 1326 | // Now, how about argument types. |
| 1327 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1328 | unsigned i = 0; |
| 1329 | bool ArgCFAudited = false; |
| 1330 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1331 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1332 | const ParmVarDecl *pd = *pi; |
| 1333 | ArgEffect AE = AEArgs[i]; |
| 1334 | if (AE == DecRef /*CFConsumed annotated*/ || AE == IncRef) { |
| 1335 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>()) |
| 1336 | ArgCFAudited = true; |
| 1337 | else if (AE == IncRef) |
| 1338 | ArgCFAudited = true; |
| 1339 | } |
| 1340 | else { |
| 1341 | QualType AT = pd->getType(); |
| 1342 | if (!AuditedType(AT)) { |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1343 | AddCFAnnotations(Ctx, CE, FuncDecl, FuncIsReturnAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1344 | return CF_BRIDGING_NONE; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | if (ReturnCFAudited || ArgCFAudited) |
| 1349 | return CF_BRIDGING_ENABLE; |
| 1350 | |
| 1351 | return CF_BRIDGING_MAY_INCLUDE; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1354 | void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx, |
| 1355 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1356 | if (!isa<ObjCInterfaceDecl>(CDecl) || CDecl->isDeprecated()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1357 | return; |
| 1358 | |
| 1359 | // migrate methods which can have instancetype as their result type. |
| 1360 | for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), |
| 1361 | MEnd = CDecl->meth_end(); |
| 1362 | M != MEnd; ++M) { |
| 1363 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1364 | migrateCFAnnotation(Ctx, Method); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1368 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1369 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1370 | const ObjCMethodDecl *MethodDecl, |
| 1371 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1372 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1373 | if (!ResultAnnotated) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1374 | RetEffect Ret = CE.getReturnValue(); |
| 1375 | const char *AnnotationString = 0; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1376 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1377 | if (Ret.isOwned() && |
| 1378 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1379 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1380 | else if (Ret.notOwned() && |
| 1381 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1382 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1383 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1384 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1385 | ObjCMethodFamily OMF = MethodDecl->getMethodFamily(); |
| 1386 | switch (OMF) { |
| 1387 | case clang::OMF_alloc: |
| 1388 | case clang::OMF_new: |
| 1389 | case clang::OMF_copy: |
| 1390 | case clang::OMF_init: |
| 1391 | case clang::OMF_mutableCopy: |
| 1392 | break; |
| 1393 | |
| 1394 | default: |
| 1395 | if (Ret.isOwned() && |
| 1396 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1397 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1402 | if (AnnotationString) { |
| 1403 | edit::Commit commit(*Editor); |
| 1404 | commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString); |
| 1405 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1406 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1407 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1408 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1409 | unsigned i = 0; |
| 1410 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1411 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1412 | const ParmVarDecl *pd = *pi; |
| 1413 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1414 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() && |
| 1415 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1416 | edit::Commit commit(*Editor); |
| 1417 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1418 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1419 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1420 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1423 | void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1424 | ASTContext &Ctx, |
| 1425 | const ObjCMethodDecl *MethodDecl) { |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1426 | if (MethodDecl->hasBody() || MethodDecl->isImplicit()) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1427 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1428 | |
| 1429 | CallEffects CE = CallEffects::getEffect(MethodDecl); |
| 1430 | bool MethodIsReturnAnnotated = (MethodDecl->getAttr<CFReturnsRetainedAttr>() || |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1431 | MethodDecl->getAttr<CFReturnsNotRetainedAttr>() || |
| 1432 | MethodDecl->getAttr<NSReturnsRetainedAttr>() || |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1433 | MethodDecl->getAttr<NSReturnsNotRetainedAttr>() || |
| 1434 | MethodDecl->getAttr<NSReturnsAutoreleasedAttr>()); |
| 1435 | |
| 1436 | if (CE.getReceiver() == DecRefMsg && |
| 1437 | !MethodDecl->getAttr<NSConsumesSelfAttr>() && |
| 1438 | MethodDecl->getMethodFamily() != OMF_init && |
| 1439 | MethodDecl->getMethodFamily() != OMF_release && |
| 1440 | Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { |
| 1441 | edit::Commit commit(*Editor); |
| 1442 | commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); |
| 1443 | Editor->commit(commit); |
| 1444 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Trivial case of when funciton is annotated and has no argument. |
| 1447 | if (MethodIsReturnAnnotated && |
| 1448 | (MethodDecl->param_begin() == MethodDecl->param_end())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1449 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1450 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1451 | if (!MethodIsReturnAnnotated) { |
| 1452 | RetEffect Ret = CE.getReturnValue(); |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1453 | if ((Ret.getObjKind() == RetEffect::CF || |
| 1454 | Ret.getObjKind() == RetEffect::ObjC) && |
| 1455 | (Ret.isOwned() || Ret.notOwned())) { |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1456 | AddCFAnnotations(Ctx, CE, MethodDecl, false); |
| 1457 | return; |
| 1458 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1459 | else if (!AuditedType(MethodDecl->getResultType())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1460 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | // At this point result type is either annotated or audited. |
| 1464 | // Now, how about argument types. |
| 1465 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1466 | unsigned i = 0; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1467 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1468 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1469 | const ParmVarDecl *pd = *pi; |
| 1470 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1471 | if ((AE == DecRef && !pd->getAttr<CFConsumedAttr>()) || AE == IncRef || |
| 1472 | !AuditedType(pd->getType())) { |
| 1473 | AddCFAnnotations(Ctx, CE, MethodDecl, MethodIsReturnAnnotated); |
| 1474 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1477 | return; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1480 | namespace { |
| 1481 | |
| 1482 | class RewritesReceiver : public edit::EditsReceiver { |
| 1483 | Rewriter &Rewrite; |
| 1484 | |
| 1485 | public: |
| 1486 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 1487 | |
| 1488 | virtual void insert(SourceLocation loc, StringRef text) { |
| 1489 | Rewrite.InsertText(loc, text); |
| 1490 | } |
| 1491 | virtual void replace(CharSourceRange range, StringRef text) { |
| 1492 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 1493 | } |
| 1494 | }; |
| 1495 | |
| 1496 | } |
| 1497 | |
Fariborz Jahanian | 8f5225b | 2013-10-01 21:16:29 +0000 | [diff] [blame] | 1498 | static bool |
| 1499 | IsReallyASystemHeader(ASTContext &Ctx, const FileEntry *file, FileID FID) { |
| 1500 | bool Invalid = false; |
| 1501 | const SrcMgr::SLocEntry &SEntry = |
| 1502 | Ctx.getSourceManager().getSLocEntry(FID, &Invalid); |
| 1503 | if (!Invalid && SEntry.isFile()) { |
| 1504 | const SrcMgr::FileInfo &FI = SEntry.getFile(); |
| 1505 | if (!FI.hasLineDirectives()) { |
| 1506 | if (FI.getFileCharacteristic() == SrcMgr::C_ExternCSystem) |
| 1507 | return true; |
| 1508 | if (FI.getFileCharacteristic() == SrcMgr::C_System) { |
| 1509 | // This file is in a system header directory. Continue with commiting change |
| 1510 | // only if it is a user specified system directory because user put a |
| 1511 | // .system_framework file in the framework directory. |
| 1512 | StringRef Directory(file->getDir()->getName()); |
| 1513 | size_t Ix = Directory.rfind(".framework"); |
| 1514 | if (Ix == StringRef::npos) |
| 1515 | return true; |
| 1516 | std::string PatchToSystemFramework = Directory.slice(0, Ix+sizeof(".framework")); |
| 1517 | PatchToSystemFramework += ".system_framework"; |
| 1518 | if (!llvm::sys::fs::exists(PatchToSystemFramework.data())) |
| 1519 | return true; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | return false; |
| 1524 | } |
| 1525 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1526 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1527 | |
| 1528 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1529 | if (ASTMigrateActions & FrontendOptions::ObjCMT_MigrateDecls) { |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1530 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 1531 | D != DEnd; ++D) { |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 1532 | if (unsigned FID = |
| 1533 | PP.getSourceManager().getFileID((*D)->getLocation()).getHashValue()) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1534 | if (FileId && FileId != FID) { |
| 1535 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1536 | AnnotateImplicitBridging(Ctx); |
| 1537 | } |
| 1538 | |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1539 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 1540 | migrateObjCInterfaceDecl(Ctx, CDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame^] | 1541 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) { |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1542 | migrateObjCInterfaceDecl(Ctx, CatDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame^] | 1543 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1544 | migregateDeprecatedAnnotation(Ctx, CatDecl); |
| 1545 | } |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1546 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
| 1547 | ObjCProtocolDecls.insert(PDecl); |
| 1548 | else if (const ObjCImplementationDecl *ImpDecl = |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1549 | dyn_cast<ObjCImplementationDecl>(*D)) { |
| 1550 | if (ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) |
| 1551 | migrateProtocolConformance(Ctx, ImpDecl); |
| 1552 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1553 | else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1554 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1555 | continue; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1556 | DeclContext::decl_iterator N = D; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1557 | if (++N != DEnd) { |
| 1558 | const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N); |
| 1559 | if (migrateNSEnumDecl(Ctx, ED, TD) && TD) |
| 1560 | D++; |
| 1561 | } |
| 1562 | else |
| 1563 | migrateNSEnumDecl(Ctx, ED, /*TypedefDecl */0); |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1564 | } |
| 1565 | else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*D)) { |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1566 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1567 | continue; |
| 1568 | DeclContext::decl_iterator N = D; |
| 1569 | if (++N == DEnd) |
| 1570 | continue; |
| 1571 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(*N)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1572 | if (++N != DEnd) |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1573 | if (const TypedefDecl *TDF = dyn_cast<TypedefDecl>(*N)) { |
| 1574 | // prefer typedef-follows-enum to enum-follows-typedef pattern. |
| 1575 | if (migrateNSEnumDecl(Ctx, ED, TDF)) { |
| 1576 | ++D; ++D; |
| 1577 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1578 | continue; |
| 1579 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1580 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1581 | if (migrateNSEnumDecl(Ctx, ED, TD)) { |
| 1582 | ++D; |
| 1583 | continue; |
| 1584 | } |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1585 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1586 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1587 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1588 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) { |
| 1589 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1590 | migrateCFAnnotation(Ctx, FD); |
| 1591 | } |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1592 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1593 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) { |
| 1594 | // migrate methods which can have instancetype as their result type. |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1595 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Instancetype) |
| 1596 | migrateAllMethodInstaceType(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1597 | // annotate methods with CF annotations. |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1598 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1599 | migrateARCSafeAnnotation(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1600 | } |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1601 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1602 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1603 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1604 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1605 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1606 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1607 | RewritesReceiver Rec(rewriter); |
| 1608 | Editor->applyRewrites(Rec); |
| 1609 | |
| 1610 | for (Rewriter::buffer_iterator |
| 1611 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 1612 | FileID FID = I->first; |
| 1613 | RewriteBuffer &buf = I->second; |
| 1614 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 1615 | assert(file); |
Fariborz Jahanian | 8f5225b | 2013-10-01 21:16:29 +0000 | [diff] [blame] | 1616 | if (IsReallyASystemHeader(Ctx, file, FID)) |
| 1617 | continue; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1618 | SmallString<512> newText; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1619 | llvm::raw_svector_ostream vecOS(newText); |
| 1620 | buf.write(vecOS); |
| 1621 | vecOS.flush(); |
| 1622 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 1623 | StringRef(newText.data(), newText.size()), file->getName()); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1624 | SmallString<64> filePath(file->getName()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1625 | FileMgr.FixupRelativePath(filePath); |
| 1626 | Remapper.remap(filePath.str(), memBuf); |
| 1627 | } |
| 1628 | |
| 1629 | if (IsOutputFile) { |
| 1630 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 1631 | } else { |
| 1632 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
Argyrios Kyrtzidis | b482260 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 1637 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
| 1641 | ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, |
| 1642 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1643 | PPConditionalDirectiveRecord * |
| 1644 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
| 1645 | CI.getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1646 | return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile, |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1647 | FrontendOptions::ObjCMT_MigrateAll, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1648 | Remapper, |
| 1649 | CI.getFileManager(), |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1650 | PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1651 | CI.getPreprocessor(), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1652 | /*isOutputFile=*/true); |
| 1653 | } |