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