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" |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 11 | #include "clang/ARCMigrate/ARCMT.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 12 | #include "clang/ARCMigrate/ARCMTActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
| 14 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 15 | #include "clang/AST/Attr.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/NSAPI.h" |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 17 | #include "clang/AST/ParentMap.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Edit/Commit.h" |
| 22 | #include "clang/Edit/EditedSource.h" |
| 23 | #include "clang/Edit/EditsReceiver.h" |
| 24 | #include "clang/Edit/Rewriters.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CompilerInstance.h" |
| 26 | #include "clang/Frontend/MultiplexConsumer.h" |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 27 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | #include "clang/Rewrite/Core/Rewriter.h" |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 30 | #include "clang/StaticAnalyzer/Checkers/ObjCRetainCount.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallString.h" |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Path.h" |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SourceMgr.h" |
| 34 | #include "llvm/Support/YAMLParser.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
| 37 | using namespace arcmt; |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 38 | using namespace ento::objc_retain; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
| 42 | class ObjCMigrateASTConsumer : public ASTConsumer { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 43 | enum CF_BRIDGING_KIND { |
| 44 | CF_BRIDGING_NONE, |
| 45 | CF_BRIDGING_ENABLE, |
| 46 | CF_BRIDGING_MAY_INCLUDE |
| 47 | }; |
| 48 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 49 | void migrateDecl(Decl *D); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 50 | void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCContainerDecl *D); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 51 | void migrateProtocolConformance(ASTContext &Ctx, |
| 52 | const ObjCImplementationDecl *ImpDecl); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 53 | void CacheObjCNSIntegerTypedefed(const TypedefDecl *TypedefDcl); |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 54 | bool migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 55 | const TypedefDecl *TypedefDcl); |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 56 | void migrateAllMethodInstaceType(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 57 | void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
| 58 | ObjCMethodDecl *OM); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 59 | bool migrateProperty(ASTContext &Ctx, ObjCContainerDecl *D, ObjCMethodDecl *OM); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 60 | void migrateNsReturnsInnerPointer(ASTContext &Ctx, ObjCMethodDecl *OM); |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 61 | void migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, ObjCPropertyDecl *P); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 62 | void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 63 | ObjCMethodDecl *OM, |
| 64 | ObjCInstanceTypeFamily OIT_Family = OIT_None); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 65 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 66 | void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 67 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 68 | const FunctionDecl *FuncDecl, bool ResultAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 69 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 70 | const ObjCMethodDecl *MethodDecl, bool ResultAnnotated); |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 71 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 72 | void AnnotateImplicitBridging(ASTContext &Ctx); |
| 73 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 74 | CF_BRIDGING_KIND migrateAddFunctionAnnotation(ASTContext &Ctx, |
| 75 | const FunctionDecl *FuncDecl); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 76 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 77 | void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
| 78 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 79 | void migrateAddMethodAnnotation(ASTContext &Ctx, |
| 80 | const ObjCMethodDecl *MethodDecl); |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 81 | |
| 82 | void inferDesignatedInitializers(ASTContext &Ctx, |
| 83 | const ObjCImplementationDecl *ImplD); |
| 84 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 85 | public: |
| 86 | std::string MigrateDir; |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 87 | unsigned ASTMigrateActions; |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 88 | FileID FileId; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 89 | const TypedefDecl *NSIntegerTypedefed; |
| 90 | const TypedefDecl *NSUIntegerTypedefed; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 91 | std::unique_ptr<NSAPI> NSAPIObj; |
| 92 | std::unique_ptr<edit::EditedSource> Editor; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 93 | FileRemapper &Remapper; |
| 94 | FileManager &FileMgr; |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 95 | const PPConditionalDirectiveRecord *PPRec; |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 96 | Preprocessor &PP; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 97 | bool IsOutputFile; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 98 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls; |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 99 | llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates; |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 100 | llvm::StringMap<char> WhiteListFilenames; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 102 | ObjCMigrateASTConsumer(StringRef migrateDir, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 103 | unsigned astMigrateActions, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 104 | FileRemapper &remapper, |
| 105 | FileManager &fileMgr, |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 106 | const PPConditionalDirectiveRecord *PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 107 | Preprocessor &PP, |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 108 | bool isOutputFile, |
| 109 | ArrayRef<std::string> WhiteList) |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 110 | : MigrateDir(migrateDir), |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 111 | ASTMigrateActions(astMigrateActions), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 112 | NSIntegerTypedefed(nullptr), NSUIntegerTypedefed(nullptr), |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 113 | Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP), |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 114 | IsOutputFile(isOutputFile) { |
| 115 | |
| 116 | for (ArrayRef<std::string>::iterator |
| 117 | I = WhiteList.begin(), E = WhiteList.end(); I != E; ++I) { |
| 118 | WhiteListFilenames.GetOrCreateValue(*I); |
| 119 | } |
| 120 | } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 121 | |
| 122 | protected: |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 123 | void Initialize(ASTContext &Context) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 124 | NSAPIObj.reset(new NSAPI(Context)); |
| 125 | Editor.reset(new edit::EditedSource(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 126 | Context.getLangOpts(), |
Argyrios Kyrtzidis | 7dd3133 | 2014-04-28 02:38:51 +0000 | [diff] [blame] | 127 | PPRec)); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 130 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 131 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 132 | migrateDecl(*I); |
| 133 | return true; |
| 134 | } |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 135 | void HandleInterestingDecl(DeclGroupRef DG) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 136 | // Ignore decls from the PCH. |
| 137 | } |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 138 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 139 | ObjCMigrateASTConsumer::HandleTopLevelDecl(DG); |
| 140 | } |
| 141 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 142 | void HandleTranslationUnit(ASTContext &Ctx) override; |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 143 | |
| 144 | bool canModifyFile(StringRef Path) { |
| 145 | if (WhiteListFilenames.empty()) |
| 146 | return true; |
| 147 | return WhiteListFilenames.find(llvm::sys::path::filename(Path)) |
| 148 | != WhiteListFilenames.end(); |
| 149 | } |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 150 | bool canModifyFile(const FileEntry *FE) { |
| 151 | if (!FE) |
| 152 | return false; |
| 153 | return canModifyFile(FE->getName()); |
| 154 | } |
| 155 | bool canModifyFile(FileID FID) { |
| 156 | if (FID.isInvalid()) |
| 157 | return false; |
| 158 | return canModifyFile(PP.getSourceManager().getFileEntryForID(FID)); |
| 159 | } |
| 160 | |
| 161 | bool canModify(const Decl *D) { |
| 162 | if (!D) |
| 163 | return false; |
| 164 | if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(D)) |
| 165 | return canModify(CatImpl->getCategoryDecl()); |
| 166 | if (const ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(D)) |
| 167 | return canModify(Impl->getClassInterface()); |
| 168 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 169 | return canModify(cast<Decl>(MD->getDeclContext())); |
| 170 | |
| 171 | FileID FID = PP.getSourceManager().getFileID(D->getLocation()); |
| 172 | return canModifyFile(FID); |
| 173 | } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 179 | StringRef migrateDir, |
| 180 | unsigned migrateAction) |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 181 | : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 182 | ObjCMigAction(migrateAction), |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 183 | CompInst(nullptr) { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 184 | if (MigrateDir.empty()) |
| 185 | MigrateDir = "."; // user current directory if none is given. |
| 186 | } |
| 187 | |
| 188 | ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, |
| 189 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 190 | PPConditionalDirectiveRecord * |
| 191 | PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); |
| 192 | CompInst->getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 193 | ASTConsumer * |
| 194 | WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 195 | ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir, |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 196 | ObjCMigAction, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 197 | Remapper, |
| 198 | CompInst->getFileManager(), |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 199 | PPRec, |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 200 | CompInst->getPreprocessor(), |
| 201 | false, |
| 202 | ArrayRef<std::string>()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 203 | ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer }; |
| 204 | return new MultiplexConsumer(Consumers); |
| 205 | } |
| 206 | |
| 207 | bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) { |
| 208 | Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(), |
| 209 | /*ignoreIfFilesChanges=*/true); |
| 210 | CompInst = &CI; |
| 211 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 212 | return true; |
| 213 | } |
| 214 | |
| 215 | namespace { |
| 216 | class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> { |
| 217 | ObjCMigrateASTConsumer &Consumer; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 218 | ParentMap &PMap; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 219 | |
| 220 | public: |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 221 | ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap) |
| 222 | : Consumer(consumer), PMap(PMap) { } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 223 | |
| 224 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 225 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 226 | |
| 227 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 228 | if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Literals) { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 229 | edit::Commit commit(*Consumer.Editor); |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 230 | edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 231 | Consumer.Editor->commit(commit); |
| 232 | } |
| 233 | |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 234 | if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Subscripting) { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 235 | edit::Commit commit(*Consumer.Editor); |
| 236 | edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit); |
| 237 | Consumer.Editor->commit(commit); |
| 238 | } |
| 239 | |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | bool TraverseObjCMessageExpr(ObjCMessageExpr *E) { |
| 244 | // Do depth first; we want to rewrite the subexpressions first so that if |
| 245 | // we have to move expressions we will move them already rewritten. |
| 246 | for (Stmt::child_range range = E->children(); range; ++range) |
| 247 | if (!TraverseStmt(*range)) |
| 248 | return false; |
| 249 | |
| 250 | return WalkUpFromObjCMessageExpr(E); |
| 251 | } |
| 252 | }; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 253 | |
| 254 | class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> { |
| 255 | ObjCMigrateASTConsumer &Consumer; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 256 | std::unique_ptr<ParentMap> PMap; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 257 | |
| 258 | public: |
| 259 | BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { } |
| 260 | |
| 261 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 262 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 263 | |
| 264 | bool TraverseStmt(Stmt *S) { |
| 265 | PMap.reset(new ParentMap(S)); |
| 266 | ObjCMigrator(Consumer, *PMap).TraverseStmt(S); |
| 267 | return true; |
| 268 | } |
| 269 | }; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void ObjCMigrateASTConsumer::migrateDecl(Decl *D) { |
| 273 | if (!D) |
| 274 | return; |
| 275 | if (isa<ObjCMethodDecl>(D)) |
| 276 | return; // Wait for the ObjC container declaration. |
| 277 | |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 278 | BodyMigrator(*this).TraverseDecl(D); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 281 | static void append_attr(std::string &PropertyString, const char *attr, |
| 282 | bool &LParenAdded) { |
| 283 | if (!LParenAdded) { |
| 284 | PropertyString += "("; |
| 285 | LParenAdded = true; |
| 286 | } |
| 287 | else |
| 288 | PropertyString += ", "; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 289 | PropertyString += attr; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 292 | static |
| 293 | void MigrateBlockOrFunctionPointerTypeVariable(std::string & PropertyString, |
| 294 | const std::string& TypeString, |
| 295 | const char *name) { |
| 296 | const char *argPtr = TypeString.c_str(); |
| 297 | int paren = 0; |
| 298 | while (*argPtr) { |
| 299 | switch (*argPtr) { |
| 300 | case '(': |
| 301 | PropertyString += *argPtr; |
| 302 | paren++; |
| 303 | break; |
| 304 | case ')': |
| 305 | PropertyString += *argPtr; |
| 306 | paren--; |
| 307 | break; |
| 308 | case '^': |
Fariborz Jahanian | 3c593d6 | 2013-10-08 21:32:16 +0000 | [diff] [blame] | 309 | case '*': |
| 310 | PropertyString += (*argPtr); |
| 311 | if (paren == 1) { |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 312 | PropertyString += name; |
Fariborz Jahanian | 3c593d6 | 2013-10-08 21:32:16 +0000 | [diff] [blame] | 313 | name = ""; |
| 314 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 315 | break; |
| 316 | default: |
| 317 | PropertyString += *argPtr; |
| 318 | break; |
| 319 | } |
| 320 | argPtr++; |
| 321 | } |
| 322 | } |
| 323 | |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 324 | static const char *PropertyMemoryAttribute(ASTContext &Context, QualType ArgType) { |
| 325 | Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime(); |
| 326 | bool RetainableObject = ArgType->isObjCRetainableType(); |
Fariborz Jahanian | 49e69ee | 2013-12-12 01:02:00 +0000 | [diff] [blame] | 327 | if (RetainableObject && |
| 328 | (propertyLifetime == Qualifiers::OCL_Strong |
| 329 | || propertyLifetime == Qualifiers::OCL_None)) { |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 330 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 331 | ArgType->getAs<ObjCObjectPointerType>()) { |
| 332 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 333 | if (IDecl && |
| 334 | IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying"))) |
| 335 | return "copy"; |
| 336 | else |
Fariborz Jahanian | 54c8eaa | 2013-11-21 00:58:17 +0000 | [diff] [blame] | 337 | return "strong"; |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 338 | } |
| 339 | else if (ArgType->isBlockPointerType()) |
| 340 | return "copy"; |
| 341 | } else if (propertyLifetime == Qualifiers::OCL_Weak) |
| 342 | // TODO. More precise determination of 'weak' attribute requires |
| 343 | // looking into setter's implementation for backing weak ivar. |
| 344 | return "weak"; |
| 345 | else if (RetainableObject) |
Fariborz Jahanian | 54c8eaa | 2013-11-21 00:58:17 +0000 | [diff] [blame] | 346 | return ArgType->isBlockPointerType() ? "copy" : "strong"; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 347 | return nullptr; |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 348 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 349 | |
Fariborz Jahanian | 1b66787 | 2013-10-16 22:35:19 +0000 | [diff] [blame] | 350 | static void rewriteToObjCProperty(const ObjCMethodDecl *Getter, |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 351 | const ObjCMethodDecl *Setter, |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 352 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 353 | unsigned LengthOfPrefix, |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 354 | bool Atomic, bool UseNsIosOnlyMacro, |
| 355 | bool AvailabilityArgsMatch) { |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 356 | ASTContext &Context = NS.getASTContext(); |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 357 | bool LParenAdded = false; |
| 358 | std::string PropertyString = "@property "; |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 359 | if (UseNsIosOnlyMacro && Context.Idents.get("NS_NONATOMIC_IOSONLY").hasMacroDefinition()) { |
Fariborz Jahanian | bed1be9 | 2013-11-12 19:25:50 +0000 | [diff] [blame] | 360 | PropertyString += "(NS_NONATOMIC_IOSONLY"; |
| 361 | LParenAdded = true; |
| 362 | } else if (!Atomic) { |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 363 | PropertyString += "(nonatomic"; |
| 364 | LParenAdded = true; |
| 365 | } |
| 366 | |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 367 | std::string PropertyNameString = Getter->getNameAsString(); |
| 368 | StringRef PropertyName(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 369 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 370 | if (!LParenAdded) { |
| 371 | PropertyString += "(getter="; |
| 372 | LParenAdded = true; |
| 373 | } |
| 374 | else |
| 375 | PropertyString += ", getter="; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 376 | PropertyString += PropertyNameString; |
| 377 | } |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 378 | // Property with no setter may be suggested as a 'readonly' property. |
Fariborz Jahanian | d9aef788 | 2013-11-21 17:49:34 +0000 | [diff] [blame] | 379 | if (!Setter) |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 380 | append_attr(PropertyString, "readonly", LParenAdded); |
Fariborz Jahanian | d9aef788 | 2013-11-21 17:49:34 +0000 | [diff] [blame] | 381 | |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 382 | |
Fariborz Jahanian | 33304e30 | 2013-10-16 18:52:17 +0000 | [diff] [blame] | 383 | // Short circuit 'delegate' properties that contain the name "delegate" or |
| 384 | // "dataSource", or have exact name "target" to have 'assign' attribute. |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 385 | if (PropertyName.equals("target") || |
| 386 | (PropertyName.find("delegate") != StringRef::npos) || |
Fariborz Jahanian | 78bff05 | 2013-10-16 20:44:26 +0000 | [diff] [blame] | 387 | (PropertyName.find("dataSource") != StringRef::npos)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 388 | QualType QT = Getter->getReturnType(); |
Fariborz Jahanian | 78bff05 | 2013-10-16 20:44:26 +0000 | [diff] [blame] | 389 | if (!QT->isRealType()) |
| 390 | append_attr(PropertyString, "assign", LParenAdded); |
Fariborz Jahanian | d9aef788 | 2013-11-21 17:49:34 +0000 | [diff] [blame] | 391 | } else if (!Setter) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 392 | QualType ResType = Context.getCanonicalType(Getter->getReturnType()); |
Fariborz Jahanian | d9aef788 | 2013-11-21 17:49:34 +0000 | [diff] [blame] | 393 | if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ResType)) |
| 394 | append_attr(PropertyString, MemoryManagementAttr, LParenAdded); |
| 395 | } else { |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 396 | const ParmVarDecl *argDecl = *Setter->param_begin(); |
| 397 | QualType ArgType = Context.getCanonicalType(argDecl->getType()); |
Fariborz Jahanian | da267d0 | 2013-11-14 18:28:58 +0000 | [diff] [blame] | 398 | if (const char *MemoryManagementAttr = PropertyMemoryAttribute(Context, ArgType)) |
| 399 | append_attr(PropertyString, MemoryManagementAttr, LParenAdded); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 400 | } |
Fariborz Jahanian | 4a8865b | 2013-10-16 19:48:23 +0000 | [diff] [blame] | 401 | if (LParenAdded) |
| 402 | PropertyString += ')'; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 403 | QualType RT = Getter->getReturnType(); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 404 | if (!isa<TypedefType>(RT)) { |
| 405 | // strip off any ARC lifetime qualifier. |
| 406 | QualType CanResultTy = Context.getCanonicalType(RT); |
| 407 | if (CanResultTy.getQualifiers().hasObjCLifetime()) { |
| 408 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 409 | Qs.removeObjCLifetime(); |
| 410 | RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 411 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 412 | } |
| 413 | PropertyString += " "; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 414 | PrintingPolicy SubPolicy(Context.getPrintingPolicy()); |
| 415 | SubPolicy.SuppressStrongLifetime = true; |
Fariborz Jahanian | de557f8 | 2013-10-09 17:37:28 +0000 | [diff] [blame] | 416 | SubPolicy.SuppressLifetimeQualifiers = true; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 417 | std::string TypeString = RT.getAsString(SubPolicy); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 418 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 419 | // property name must strip off "is" and lower case the first character |
| 420 | // after that; e.g. isContinuous will become continuous. |
| 421 | StringRef PropertyNameStringRef(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 422 | PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 423 | PropertyNameString = PropertyNameStringRef; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 424 | bool NoLowering = (isUppercase(PropertyNameString[0]) && |
| 425 | PropertyNameString.size() > 1 && |
| 426 | isUppercase(PropertyNameString[1])); |
Fariborz Jahanian | 34fea36 | 2013-09-11 18:27:16 +0000 | [diff] [blame] | 427 | if (!NoLowering) |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 428 | PropertyNameString[0] = toLowercase(PropertyNameString[0]); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 429 | } |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 430 | if (RT->isBlockPointerType() || RT->isFunctionPointerType()) |
| 431 | MigrateBlockOrFunctionPointerTypeVariable(PropertyString, |
| 432 | TypeString, |
| 433 | PropertyNameString.c_str()); |
| 434 | else { |
| 435 | char LastChar = TypeString[TypeString.size()-1]; |
| 436 | PropertyString += TypeString; |
| 437 | if (LastChar != '*') |
| 438 | PropertyString += ' '; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 439 | PropertyString += PropertyNameString; |
Fariborz Jahanian | 02461d0 | 2013-10-08 20:14:24 +0000 | [diff] [blame] | 440 | } |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 441 | SourceLocation StartGetterSelectorLoc = Getter->getSelectorStartLoc(); |
| 442 | Selector GetterSelector = Getter->getSelector(); |
| 443 | |
| 444 | SourceLocation EndGetterSelectorLoc = |
| 445 | StartGetterSelectorLoc.getLocWithOffset(GetterSelector.getNameForSlot(0).size()); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 446 | commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(), |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 447 | EndGetterSelectorLoc), |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 448 | PropertyString); |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 449 | if (Setter && AvailabilityArgsMatch) { |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 450 | SourceLocation EndLoc = Setter->getDeclaratorEndLoc(); |
| 451 | // Get location past ';' |
| 452 | EndLoc = EndLoc.getLocWithOffset(1); |
Fariborz Jahanian | 1b66787 | 2013-10-16 22:35:19 +0000 | [diff] [blame] | 453 | SourceLocation BeginOfSetterDclLoc = Setter->getLocStart(); |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 454 | // FIXME. This assumes that setter decl; is immediately preceded by eoln. |
Fariborz Jahanian | 1b66787 | 2013-10-16 22:35:19 +0000 | [diff] [blame] | 455 | // It is trying to remove the setter method decl. line entirely. |
| 456 | BeginOfSetterDclLoc = BeginOfSetterDclLoc.getLocWithOffset(-1); |
| 457 | commit.remove(SourceRange(BeginOfSetterDclLoc, EndLoc)); |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 458 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Fariborz Jahanian | 48a44d4 | 2013-11-19 18:17:31 +0000 | [diff] [blame] | 461 | static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) { |
| 462 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D)) { |
| 463 | StringRef Name = CatDecl->getName(); |
| 464 | return Name.endswith("Deprecated"); |
| 465 | } |
| 466 | return false; |
| 467 | } |
| 468 | |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 469 | void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 470 | ObjCContainerDecl *D) { |
Fariborz Jahanian | 48a44d4 | 2013-11-19 18:17:31 +0000 | [diff] [blame] | 471 | if (D->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(D)) |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 472 | return; |
Fariborz Jahanian | 48a44d4 | 2013-11-19 18:17:31 +0000 | [diff] [blame] | 473 | |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 474 | for (auto *Method : D->methods()) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 475 | if (Method->isDeprecated()) |
| 476 | continue; |
Fariborz Jahanian | ec7cea9 | 2013-11-08 01:15:17 +0000 | [diff] [blame] | 477 | bool PropertyInferred = migrateProperty(Ctx, D, Method); |
| 478 | // If a property is inferred, do not attempt to attach NS_RETURNS_INNER_POINTER to |
| 479 | // the getter method as it ends up on the property itself which we don't want |
| 480 | // to do unless -objcmt-returns-innerpointer-property option is on. |
| 481 | if (!PropertyInferred || |
| 482 | (ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty)) |
Fariborz Jahanian | 22626e7 | 2013-11-08 02:00:22 +0000 | [diff] [blame] | 483 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 484 | migrateNsReturnsInnerPointer(Ctx, Method); |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 485 | } |
Fariborz Jahanian | 2341707 | 2013-11-05 22:28:30 +0000 | [diff] [blame] | 486 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_ReturnsInnerPointerProperty)) |
| 487 | return; |
| 488 | |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 489 | for (auto *Prop : D->properties()) { |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 490 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 491 | !Prop->isDeprecated()) |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 492 | migratePropertyNsReturnsInnerPointer(Ctx, Prop); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 496 | static bool |
Fariborz Jahanian | 9a3512b | 2013-07-13 17:16:41 +0000 | [diff] [blame] | 497 | ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 498 | const ObjCImplementationDecl *ImpDecl, |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 499 | const ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 500 | ObjCProtocolDecl *Protocol) { |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 501 | // In auto-synthesis, protocol properties are not synthesized. So, |
| 502 | // a conforming protocol must have its required properties declared |
| 503 | // in class interface. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 504 | bool HasAtleastOneRequiredProperty = false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 505 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) |
Aaron Ballman | d174edf | 2014-03-13 19:11:50 +0000 | [diff] [blame] | 506 | for (const auto *Property : PDecl->properties()) { |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 507 | if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 508 | continue; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 509 | HasAtleastOneRequiredProperty = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 510 | DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 511 | if (R.size() == 0) { |
| 512 | // Relax the rule and look into class's implementation for a synthesize |
| 513 | // or dynamic declaration. Class is implementing a property coming from |
| 514 | // another protocol. This still makes the target protocol as conforming. |
| 515 | if (!ImpDecl->FindPropertyImplDecl( |
| 516 | Property->getDeclName().getAsIdentifierInfo())) |
| 517 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 518 | } |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 519 | else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 520 | if ((ClassProperty->getPropertyAttributes() |
| 521 | != Property->getPropertyAttributes()) || |
| 522 | !Ctx.hasSameType(ClassProperty->getType(), Property->getType())) |
| 523 | return false; |
| 524 | } |
| 525 | else |
| 526 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 527 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 528 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 529 | // At this point, all required properties in this protocol conform to those |
| 530 | // declared in the class. |
| 531 | // Check that class implements the required methods of the protocol too. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 532 | bool HasAtleastOneRequiredMethod = false; |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 533 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) { |
| 534 | if (PDecl->meth_begin() == PDecl->meth_end()) |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 535 | return HasAtleastOneRequiredProperty; |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 536 | for (const auto *MD : PDecl->methods()) { |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 537 | if (MD->isImplicit()) |
| 538 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 539 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) |
| 540 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 541 | DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 542 | if (R.size() == 0) |
| 543 | return false; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 544 | bool match = false; |
| 545 | HasAtleastOneRequiredMethod = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 546 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 547 | if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0])) |
| 548 | if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) { |
| 549 | match = true; |
| 550 | break; |
| 551 | } |
| 552 | if (!match) |
| 553 | return false; |
| 554 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 555 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 556 | if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod) |
| 557 | return true; |
| 558 | return false; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 561 | static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl, |
| 562 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols, |
| 563 | const NSAPI &NS, edit::Commit &commit) { |
| 564 | const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols(); |
| 565 | std::string ClassString; |
| 566 | SourceLocation EndLoc = |
| 567 | IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation(); |
| 568 | |
| 569 | if (Protocols.empty()) { |
| 570 | ClassString = '<'; |
| 571 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 572 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 573 | if (i != (e-1)) |
| 574 | ClassString += ", "; |
| 575 | } |
| 576 | ClassString += "> "; |
| 577 | } |
| 578 | else { |
| 579 | ClassString = ", "; |
| 580 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 581 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 582 | if (i != (e-1)) |
| 583 | ClassString += ", "; |
| 584 | } |
| 585 | ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1; |
| 586 | EndLoc = *PL; |
| 587 | } |
| 588 | |
| 589 | commit.insertAfterToken(EndLoc, ClassString); |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl, |
| 594 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 595 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 596 | bool IsNSIntegerType, |
| 597 | bool NSOptions) { |
| 598 | std::string ClassString; |
| 599 | if (NSOptions) |
| 600 | ClassString = "typedef NS_OPTIONS(NSUInteger, "; |
| 601 | else |
| 602 | ClassString = |
| 603 | IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " |
| 604 | : "typedef NS_ENUM(NSUInteger, "; |
| 605 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 606 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 607 | ClassString += ')'; |
| 608 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 609 | commit.replace(R, ClassString); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 610 | SourceLocation EndOfEnumDclLoc = EnumDcl->getLocEnd(); |
| 611 | EndOfEnumDclLoc = trans::findSemiAfterLocation(EndOfEnumDclLoc, |
| 612 | NS.getASTContext(), /*IsDecl*/true); |
| 613 | if (!EndOfEnumDclLoc.isInvalid()) { |
| 614 | SourceRange EnumDclRange(EnumDcl->getLocStart(), EndOfEnumDclLoc); |
| 615 | commit.insertFromRange(TypedefDcl->getLocStart(), EnumDclRange); |
| 616 | } |
| 617 | else |
| 618 | return false; |
| 619 | |
| 620 | SourceLocation EndTypedefDclLoc = TypedefDcl->getLocEnd(); |
| 621 | EndTypedefDclLoc = trans::findSemiAfterLocation(EndTypedefDclLoc, |
| 622 | NS.getASTContext(), /*IsDecl*/true); |
| 623 | if (!EndTypedefDclLoc.isInvalid()) { |
| 624 | SourceRange TDRange(TypedefDcl->getLocStart(), EndTypedefDclLoc); |
| 625 | commit.remove(TDRange); |
| 626 | } |
| 627 | else |
| 628 | return false; |
| 629 | |
| 630 | EndOfEnumDclLoc = trans::findLocationAfterSemi(EnumDcl->getLocEnd(), NS.getASTContext(), |
Fariborz Jahanian | 1d27ffd | 2013-10-11 17:35:22 +0000 | [diff] [blame] | 631 | /*IsDecl*/true); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 632 | if (!EndOfEnumDclLoc.isInvalid()) { |
| 633 | SourceLocation BeginOfEnumDclLoc = EnumDcl->getLocStart(); |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 634 | // FIXME. This assumes that enum decl; is immediately preceded by eoln. |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 635 | // It is trying to remove the enum decl. lines entirely. |
| 636 | BeginOfEnumDclLoc = BeginOfEnumDclLoc.getLocWithOffset(-1); |
| 637 | commit.remove(SourceRange(BeginOfEnumDclLoc, EndOfEnumDclLoc)); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | return false; |
| 641 | } |
| 642 | |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 643 | static void rewriteToNSMacroDecl(const EnumDecl *EnumDcl, |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 644 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 645 | const NSAPI &NS, edit::Commit &commit, |
| 646 | bool IsNSIntegerType) { |
| 647 | std::string ClassString = |
| 648 | IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, "; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 649 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 650 | ClassString += ')'; |
| 651 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 652 | commit.replace(R, ClassString); |
| 653 | SourceLocation TypedefLoc = TypedefDcl->getLocEnd(); |
| 654 | commit.remove(SourceRange(TypedefLoc, TypedefLoc)); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 657 | static bool UseNSOptionsMacro(Preprocessor &PP, ASTContext &Ctx, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 658 | const EnumDecl *EnumDcl) { |
| 659 | bool PowerOfTwo = true; |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 660 | bool AllHexdecimalEnumerator = true; |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 661 | uint64_t MaxPowerOfTwoVal = 0; |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 662 | for (auto Enumerator : EnumDcl->enumerators()) { |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 663 | const Expr *InitExpr = Enumerator->getInitExpr(); |
| 664 | if (!InitExpr) { |
| 665 | PowerOfTwo = false; |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 666 | AllHexdecimalEnumerator = false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 667 | continue; |
| 668 | } |
Fariborz Jahanian | 6e1798e | 2013-09-17 23:32:51 +0000 | [diff] [blame] | 669 | InitExpr = InitExpr->IgnoreParenCasts(); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 670 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) |
| 671 | if (BO->isShiftOp() || BO->isBitwiseOp()) |
| 672 | return true; |
| 673 | |
| 674 | uint64_t EnumVal = Enumerator->getInitVal().getZExtValue(); |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 675 | if (PowerOfTwo && EnumVal) { |
| 676 | if (!llvm::isPowerOf2_64(EnumVal)) |
| 677 | PowerOfTwo = false; |
| 678 | else if (EnumVal > MaxPowerOfTwoVal) |
| 679 | MaxPowerOfTwoVal = EnumVal; |
| 680 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 681 | if (AllHexdecimalEnumerator && EnumVal) { |
| 682 | bool FoundHexdecimalEnumerator = false; |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 683 | SourceLocation EndLoc = Enumerator->getLocEnd(); |
| 684 | Token Tok; |
| 685 | if (!PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true)) |
| 686 | if (Tok.isLiteral() && Tok.getLength() > 2) { |
| 687 | if (const char *StringLit = Tok.getLiteralData()) |
| 688 | FoundHexdecimalEnumerator = |
| 689 | (StringLit[0] == '0' && (toLowercase(StringLit[1]) == 'x')); |
| 690 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 691 | if (!FoundHexdecimalEnumerator) |
| 692 | AllHexdecimalEnumerator = false; |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 693 | } |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 694 | } |
Fariborz Jahanian | 02bdb16 | 2013-09-23 20:27:06 +0000 | [diff] [blame] | 695 | return AllHexdecimalEnumerator || (PowerOfTwo && (MaxPowerOfTwoVal > 2)); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Fariborz Jahanian | 008ef72 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 698 | void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 699 | const ObjCImplementationDecl *ImpDecl) { |
| 700 | const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface(); |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 701 | if (!IDecl || ObjCProtocolDecls.empty() || IDecl->isDeprecated()) |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 702 | return; |
| 703 | // Find all implicit conforming protocols for this class |
| 704 | // and make them explicit. |
| 705 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols; |
| 706 | Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols); |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 707 | llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 708 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 709 | for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I = |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 710 | ObjCProtocolDecls.begin(), |
| 711 | E = ObjCProtocolDecls.end(); I != E; ++I) |
| 712 | if (!ExplicitProtocols.count(*I)) |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 713 | PotentialImplicitProtocols.push_back(*I); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 714 | |
| 715 | if (PotentialImplicitProtocols.empty()) |
| 716 | return; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 717 | |
| 718 | // go through list of non-optional methods and properties in each protocol |
| 719 | // in the PotentialImplicitProtocols list. If class implements every one of the |
| 720 | // methods and properties, then this class conforms to this protocol. |
| 721 | llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols; |
| 722 | for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 723 | if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 724 | PotentialImplicitProtocols[i])) |
| 725 | ConformingProtocols.push_back(PotentialImplicitProtocols[i]); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 726 | |
| 727 | if (ConformingProtocols.empty()) |
| 728 | return; |
Fariborz Jahanian | cb7b8de | 2013-07-17 00:02:22 +0000 | [diff] [blame] | 729 | |
| 730 | // Further reduce number of conforming protocols. If protocol P1 is in the list |
| 731 | // protocol P2 (P2<P1>), No need to include P1. |
| 732 | llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols; |
| 733 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 734 | bool DropIt = false; |
| 735 | ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i]; |
| 736 | for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) { |
| 737 | ObjCProtocolDecl *PDecl = ConformingProtocols[i1]; |
| 738 | if (PDecl == TargetPDecl) |
| 739 | continue; |
| 740 | if (PDecl->lookupProtocolNamed( |
| 741 | TargetPDecl->getDeclName().getAsIdentifierInfo())) { |
| 742 | DropIt = true; |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | if (!DropIt) |
| 747 | MinimalConformingProtocols.push_back(TargetPDecl); |
| 748 | } |
Fariborz Jahanian | 769c04e | 2013-12-17 01:01:33 +0000 | [diff] [blame] | 749 | if (MinimalConformingProtocols.empty()) |
| 750 | return; |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 751 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 752 | rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols, |
| 753 | *NSAPIObj, commit); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 754 | Editor->commit(commit); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 757 | void ObjCMigrateASTConsumer::CacheObjCNSIntegerTypedefed( |
| 758 | const TypedefDecl *TypedefDcl) { |
| 759 | |
| 760 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
| 761 | if (NSAPIObj->isObjCNSIntegerType(qt)) |
| 762 | NSIntegerTypedefed = TypedefDcl; |
| 763 | else if (NSAPIObj->isObjCNSUIntegerType(qt)) |
| 764 | NSUIntegerTypedefed = TypedefDcl; |
| 765 | } |
| 766 | |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 767 | bool ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 768 | const EnumDecl *EnumDcl, |
| 769 | const TypedefDecl *TypedefDcl) { |
| 770 | if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() || |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 771 | EnumDcl->isDeprecated()) |
| 772 | return false; |
| 773 | if (!TypedefDcl) { |
| 774 | if (NSIntegerTypedefed) { |
| 775 | TypedefDcl = NSIntegerTypedefed; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 776 | NSIntegerTypedefed = nullptr; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 777 | } |
| 778 | else if (NSUIntegerTypedefed) { |
| 779 | TypedefDcl = NSUIntegerTypedefed; |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 780 | NSUIntegerTypedefed = nullptr; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 781 | } |
| 782 | else |
| 783 | return false; |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 784 | FileID FileIdOfTypedefDcl = |
| 785 | PP.getSourceManager().getFileID(TypedefDcl->getLocation()); |
| 786 | FileID FileIdOfEnumDcl = |
| 787 | PP.getSourceManager().getFileID(EnumDcl->getLocation()); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 788 | if (FileIdOfTypedefDcl != FileIdOfEnumDcl) |
| 789 | return false; |
| 790 | } |
| 791 | if (TypedefDcl->isDeprecated()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 792 | return false; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 793 | |
| 794 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 795 | bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt); |
| 796 | bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 797 | |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 798 | if (!IsNSIntegerType && !IsNSUIntegerType) { |
| 799 | // Also check for typedef enum {...} TD; |
| 800 | if (const EnumType *EnumTy = qt->getAs<EnumType>()) { |
| 801 | if (EnumTy->getDecl() == EnumDcl) { |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 802 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 803 | if (NSOptions) { |
| 804 | if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 805 | return false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 806 | } |
| 807 | else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 808 | return false; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 809 | edit::Commit commit(*Editor); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 810 | rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 811 | Editor->commit(commit); |
Fariborz Jahanian | de79e81 | 2013-10-17 22:23:32 +0000 | [diff] [blame] | 812 | return true; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 813 | } |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 814 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 815 | return false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 816 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 817 | |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 818 | // We may still use NS_OPTIONS based on what we find in the enumertor list. |
| 819 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 820 | // NS_ENUM must be available. |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 821 | if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 822 | return false; |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 823 | // NS_OPTIONS must be available. |
| 824 | if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 825 | return false; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 826 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 827 | bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, |
| 828 | commit, IsNSIntegerType, NSOptions); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 829 | Editor->commit(commit); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 830 | return Res; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame^] | 833 | static void ReplaceWithInstancetype(ASTContext &Ctx, |
| 834 | const ObjCMigrateASTConsumer &ASTC, |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 835 | ObjCMethodDecl *OM) { |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame^] | 836 | if (OM->getReturnType() == Ctx.getObjCInstanceType()) |
| 837 | return; // already has instancetype. |
| 838 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 839 | SourceRange R; |
| 840 | std::string ClassString; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 841 | if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) { |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 842 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 843 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); |
| 844 | ClassString = "instancetype"; |
| 845 | } |
| 846 | else { |
| 847 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 848 | ClassString = OM->isInstanceMethod() ? '-' : '+'; |
| 849 | ClassString += " (instancetype)"; |
| 850 | } |
| 851 | edit::Commit commit(*ASTC.Editor); |
| 852 | commit.replace(R, ClassString); |
| 853 | ASTC.Editor->commit(commit); |
| 854 | } |
| 855 | |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 856 | static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC, |
| 857 | ObjCMethodDecl *OM) { |
| 858 | ObjCInterfaceDecl *IDecl = OM->getClassInterface(); |
| 859 | SourceRange R; |
| 860 | std::string ClassString; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 861 | if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) { |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 862 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 863 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); { |
| 864 | ClassString = IDecl->getName(); |
| 865 | ClassString += "*"; |
| 866 | } |
| 867 | } |
| 868 | else { |
| 869 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 870 | ClassString = "+ ("; |
| 871 | ClassString += IDecl->getName(); ClassString += "*)"; |
| 872 | } |
| 873 | edit::Commit commit(*ASTC.Editor); |
| 874 | commit.replace(R, ClassString); |
| 875 | ASTC.Editor->commit(commit); |
| 876 | } |
| 877 | |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 878 | void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, |
| 879 | ObjCContainerDecl *CDecl, |
| 880 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 881 | ObjCInstanceTypeFamily OIT_Family = |
| 882 | Selector::getInstTypeMethodFamily(OM->getSelector()); |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 883 | |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 884 | std::string ClassName; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 885 | switch (OIT_Family) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 886 | case OIT_None: |
| 887 | migrateFactoryMethod(Ctx, CDecl, OM); |
| 888 | return; |
Fariborz Jahanian | 7dd7143 | 2013-08-28 21:23:00 +0000 | [diff] [blame] | 889 | case OIT_Array: |
| 890 | ClassName = "NSArray"; |
| 891 | break; |
| 892 | case OIT_Dictionary: |
| 893 | ClassName = "NSDictionary"; |
| 894 | break; |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 895 | case OIT_Singleton: |
| 896 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton); |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 897 | return; |
Fariborz Jahanian | 1c900bc | 2013-09-18 20:35:47 +0000 | [diff] [blame] | 898 | case OIT_Init: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 899 | if (OM->getReturnType()->isObjCIdType()) |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame^] | 900 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | 1c900bc | 2013-09-18 20:35:47 +0000 | [diff] [blame] | 901 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 902 | case OIT_ReturnsSelf: |
| 903 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf); |
| 904 | return; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 905 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 906 | if (!OM->getReturnType()->isObjCIdType()) |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 907 | return; |
| 908 | |
| 909 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 910 | if (!IDecl) { |
| 911 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 912 | IDecl = CatDecl->getClassInterface(); |
| 913 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 914 | IDecl = ImpDecl->getClassInterface(); |
| 915 | } |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 916 | if (!IDecl || |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 917 | !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { |
| 918 | migrateFactoryMethod(Ctx, CDecl, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 919 | return; |
Fariborz Jahanian | 280954a | 2013-07-24 18:31:42 +0000 | [diff] [blame] | 920 | } |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame^] | 921 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 924 | static bool TypeIsInnerPointer(QualType T) { |
| 925 | if (!T->isAnyPointerType()) |
| 926 | return false; |
| 927 | if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() || |
Fariborz Jahanian | 73466ca | 2013-09-26 21:43:47 +0000 | [diff] [blame] | 928 | T->isBlockPointerType() || T->isFunctionPointerType() || |
| 929 | ento::coreFoundation::isCFObjectRef(T)) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 930 | return false; |
Fariborz Jahanian | 9d5fffb | 2013-09-09 23:56:14 +0000 | [diff] [blame] | 931 | // Also, typedef-of-pointer-to-incomplete-struct is something that we assume |
| 932 | // is not an innter pointer type. |
| 933 | QualType OrigT = T; |
| 934 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) |
| 935 | T = TD->getDecl()->getUnderlyingType(); |
| 936 | if (OrigT == T || !T->isPointerType()) |
| 937 | return true; |
| 938 | const PointerType* PT = T->getAs<PointerType>(); |
| 939 | QualType UPointeeT = PT->getPointeeType().getUnqualifiedType(); |
| 940 | if (UPointeeT->isRecordType()) { |
| 941 | const RecordType *RecordTy = UPointeeT->getAs<RecordType>(); |
| 942 | if (!RecordTy->getDecl()->isCompleteDefinition()) |
| 943 | return false; |
| 944 | } |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 945 | return true; |
| 946 | } |
| 947 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 948 | /// \brief Check whether the two versions match. |
| 949 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y) { |
| 950 | return (X == Y); |
| 951 | } |
| 952 | |
| 953 | /// AvailabilityAttrsMatch - This routine checks that if comparing two |
| 954 | /// availability attributes, all their components match. It returns |
| 955 | /// true, if not dealing with availability or when all components of |
| 956 | /// availability attributes match. This routine is only called when |
| 957 | /// the attributes are of the same kind. |
| 958 | static bool AvailabilityAttrsMatch(Attr *At1, Attr *At2) { |
| 959 | const AvailabilityAttr *AA1 = dyn_cast<AvailabilityAttr>(At1); |
| 960 | if (!AA1) |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 961 | return true; |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 962 | const AvailabilityAttr *AA2 = dyn_cast<AvailabilityAttr>(At2); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 963 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 964 | VersionTuple Introduced1 = AA1->getIntroduced(); |
| 965 | VersionTuple Deprecated1 = AA1->getDeprecated(); |
| 966 | VersionTuple Obsoleted1 = AA1->getObsoleted(); |
| 967 | bool IsUnavailable1 = AA1->getUnavailable(); |
| 968 | VersionTuple Introduced2 = AA2->getIntroduced(); |
| 969 | VersionTuple Deprecated2 = AA2->getDeprecated(); |
| 970 | VersionTuple Obsoleted2 = AA2->getObsoleted(); |
| 971 | bool IsUnavailable2 = AA2->getUnavailable(); |
| 972 | return (versionsMatch(Introduced1, Introduced2) && |
| 973 | versionsMatch(Deprecated1, Deprecated2) && |
| 974 | versionsMatch(Obsoleted1, Obsoleted2) && |
| 975 | IsUnavailable1 == IsUnavailable2); |
| 976 | |
| 977 | } |
| 978 | |
| 979 | static bool MatchTwoAttributeLists(const AttrVec &Attrs1, const AttrVec &Attrs2, |
| 980 | bool &AvailabilityArgsMatch) { |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 981 | // This list is very small, so this need not be optimized. |
| 982 | for (unsigned i = 0, e = Attrs1.size(); i != e; i++) { |
| 983 | bool match = false; |
| 984 | for (unsigned j = 0, f = Attrs2.size(); j != f; j++) { |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 985 | // Matching attribute kind only. Except for Availabilty attributes, |
| 986 | // we are not getting into details of the attributes. For all practical purposes |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 987 | // this is sufficient. |
| 988 | if (Attrs1[i]->getKind() == Attrs2[j]->getKind()) { |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 989 | if (AvailabilityArgsMatch) |
| 990 | AvailabilityArgsMatch = AvailabilityAttrsMatch(Attrs1[i], Attrs2[j]); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 991 | match = true; |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | if (!match) |
| 996 | return false; |
| 997 | } |
| 998 | return true; |
| 999 | } |
| 1000 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1001 | /// AttributesMatch - This routine checks list of attributes for two |
| 1002 | /// decls. It returns false, if there is a mismatch in kind of |
| 1003 | /// attributes seen in the decls. It returns true if the two decls |
| 1004 | /// have list of same kind of attributes. Furthermore, when there |
| 1005 | /// are availability attributes in the two decls, it sets the |
| 1006 | /// AvailabilityArgsMatch to false if availability attributes have |
| 1007 | /// different versions, etc. |
| 1008 | static bool AttributesMatch(const Decl *Decl1, const Decl *Decl2, |
| 1009 | bool &AvailabilityArgsMatch) { |
| 1010 | if (!Decl1->hasAttrs() || !Decl2->hasAttrs()) { |
| 1011 | AvailabilityArgsMatch = (Decl1->hasAttrs() == Decl2->hasAttrs()); |
| 1012 | return true; |
| 1013 | } |
| 1014 | AvailabilityArgsMatch = true; |
| 1015 | const AttrVec &Attrs1 = Decl1->getAttrs(); |
| 1016 | const AttrVec &Attrs2 = Decl2->getAttrs(); |
| 1017 | bool match = MatchTwoAttributeLists(Attrs1, Attrs2, AvailabilityArgsMatch); |
| 1018 | if (match && (Attrs2.size() > Attrs1.size())) |
| 1019 | return MatchTwoAttributeLists(Attrs2, Attrs1, AvailabilityArgsMatch); |
| 1020 | return match; |
| 1021 | } |
| 1022 | |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 1023 | static bool IsValidIdentifier(ASTContext &Ctx, |
| 1024 | const char *Name) { |
| 1025 | if (!isIdentifierHead(Name[0])) |
| 1026 | return false; |
| 1027 | std::string NameString = Name; |
| 1028 | NameString[0] = toLowercase(NameString[0]); |
| 1029 | IdentifierInfo *II = &Ctx.Idents.get(NameString); |
| 1030 | return II->getTokenID() == tok::identifier; |
| 1031 | } |
| 1032 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1033 | bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1034 | ObjCContainerDecl *D, |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1035 | ObjCMethodDecl *Method) { |
| 1036 | if (Method->isPropertyAccessor() || !Method->isInstanceMethod() || |
| 1037 | Method->param_size() != 0) |
| 1038 | return false; |
| 1039 | // Is this method candidate to be a getter? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1040 | QualType GRT = Method->getReturnType(); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1041 | if (GRT->isVoidType()) |
| 1042 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1043 | |
| 1044 | Selector GetterSelector = Method->getSelector(); |
Fariborz Jahanian | 7391a7b5a | 2013-09-25 00:17:07 +0000 | [diff] [blame] | 1045 | ObjCInstanceTypeFamily OIT_Family = |
| 1046 | Selector::getInstTypeMethodFamily(GetterSelector); |
| 1047 | |
| 1048 | if (OIT_Family != OIT_None) |
| 1049 | return false; |
| 1050 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1051 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 1052 | Selector SetterSelector = |
| 1053 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1054 | PP.getSelectorTable(), |
| 1055 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1056 | ObjCMethodDecl *SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1057 | unsigned LengthOfPrefix = 0; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1058 | if (!SetterMethod) { |
| 1059 | // try a different naming convention for getter: isXxxxx |
| 1060 | StringRef getterNameString = getterName->getName(); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1061 | bool IsPrefix = getterNameString.startswith("is"); |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 1062 | // Note that we don't want to change an isXXX method of retainable object |
| 1063 | // type to property (readonly or otherwise). |
| 1064 | if (IsPrefix && GRT->isObjCRetainableType()) |
| 1065 | return false; |
| 1066 | if (IsPrefix || getterNameString.startswith("get")) { |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1067 | LengthOfPrefix = (IsPrefix ? 2 : 3); |
| 1068 | const char *CGetterName = getterNameString.data() + LengthOfPrefix; |
| 1069 | // Make sure that first character after "is" or "get" prefix can |
| 1070 | // start an identifier. |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 1071 | if (!IsValidIdentifier(Ctx, CGetterName)) |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1072 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1073 | if (CGetterName[0] && isUppercase(CGetterName[0])) { |
| 1074 | getterName = &Ctx.Idents.get(CGetterName); |
| 1075 | SetterSelector = |
| 1076 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1077 | PP.getSelectorTable(), |
| 1078 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1079 | SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | } |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 1083 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1084 | if (SetterMethod) { |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1085 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_ReadwriteProperty) == 0) |
| 1086 | return false; |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1087 | bool AvailabilityArgsMatch; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1088 | if (SetterMethod->isDeprecated() || |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1089 | !AttributesMatch(Method, SetterMethod, AvailabilityArgsMatch)) |
Fariborz Jahanian | f6c6505 | 2013-09-17 22:41:25 +0000 | [diff] [blame] | 1090 | return false; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1091 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1092 | // Is this a valid setter, matching the target getter? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1093 | QualType SRT = SetterMethod->getReturnType(); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1094 | if (!SRT->isVoidType()) |
| 1095 | return false; |
| 1096 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
| 1097 | QualType ArgType = argDecl->getType(); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1098 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT)) |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1099 | return false; |
| 1100 | edit::Commit commit(*Editor); |
| 1101 | rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1102 | LengthOfPrefix, |
| 1103 | (ASTMigrateActions & |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1104 | FrontendOptions::ObjCMT_AtomicProperty) != 0, |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 1105 | (ASTMigrateActions & |
| 1106 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0, |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1107 | AvailabilityArgsMatch); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1108 | Editor->commit(commit); |
| 1109 | return true; |
| 1110 | } |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 1111 | else if (ASTMigrateActions & FrontendOptions::ObjCMT_ReadonlyProperty) { |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1112 | // Try a non-void method with no argument (and no setter or property of same name |
| 1113 | // as a 'readonly' property. |
| 1114 | edit::Commit commit(*Editor); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1115 | rewriteToObjCProperty(Method, nullptr /*SetterMethod*/, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1116 | LengthOfPrefix, |
| 1117 | (ASTMigrateActions & |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1118 | FrontendOptions::ObjCMT_AtomicProperty) != 0, |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 1119 | (ASTMigrateActions & |
| 1120 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0, |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1121 | /*AvailabilityArgsMatch*/false); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1122 | Editor->commit(commit); |
| 1123 | return true; |
| 1124 | } |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1128 | void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, |
| 1129 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1130 | if (OM->isImplicit() || |
Fariborz Jahanian | 7c1a445 | 2013-09-26 22:43:41 +0000 | [diff] [blame] | 1131 | !OM->isInstanceMethod() || |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1132 | OM->hasAttr<ObjCReturnsInnerPointerAttr>()) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1133 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1134 | |
| 1135 | QualType RT = OM->getReturnType(); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1136 | if (!TypeIsInnerPointer(RT) || |
| 1137 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1138 | return; |
| 1139 | |
| 1140 | edit::Commit commit(*Editor); |
| 1141 | commit.insertBefore(OM->getLocEnd(), " NS_RETURNS_INNER_POINTER"); |
| 1142 | Editor->commit(commit); |
| 1143 | } |
| 1144 | |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1145 | void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, |
| 1146 | ObjCPropertyDecl *P) { |
| 1147 | QualType T = P->getType(); |
| 1148 | |
| 1149 | if (!TypeIsInnerPointer(T) || |
| 1150 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1151 | return; |
| 1152 | edit::Commit commit(*Editor); |
| 1153 | commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER "); |
| 1154 | Editor->commit(commit); |
| 1155 | } |
| 1156 | |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1157 | void ObjCMigrateASTConsumer::migrateAllMethodInstaceType(ASTContext &Ctx, |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1158 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 48a44d4 | 2013-11-19 18:17:31 +0000 | [diff] [blame] | 1159 | if (CDecl->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(CDecl)) |
Fariborz Jahanian | e47a14a | 2013-09-18 15:43:52 +0000 | [diff] [blame] | 1160 | return; |
| 1161 | |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1162 | // migrate methods which can have instancetype as their result type. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1163 | for (auto *Method : CDecl->methods()) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1164 | if (Method->isDeprecated()) |
| 1165 | continue; |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1166 | migrateMethodInstanceType(Ctx, CDecl, Method); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1170 | void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, |
| 1171 | ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1172 | ObjCMethodDecl *OM, |
| 1173 | ObjCInstanceTypeFamily OIT_Family) { |
Fariborz Jahanian | 3bfc35e | 2013-08-02 22:34:18 +0000 | [diff] [blame] | 1174 | if (OM->isInstanceMethod() || |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1175 | OM->getReturnType() == Ctx.getObjCInstanceType() || |
| 1176 | !OM->getReturnType()->isObjCIdType()) |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | |
| 1179 | // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class |
| 1180 | // NSYYYNamE with matching names be at least 3 characters long. |
| 1181 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 1182 | if (!IDecl) { |
| 1183 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 1184 | IDecl = CatDecl->getClassInterface(); |
| 1185 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 1186 | IDecl = ImpDecl->getClassInterface(); |
| 1187 | } |
| 1188 | if (!IDecl) |
| 1189 | return; |
| 1190 | |
| 1191 | std::string StringClassName = IDecl->getName(); |
| 1192 | StringRef LoweredClassName(StringClassName); |
| 1193 | std::string StringLoweredClassName = LoweredClassName.lower(); |
| 1194 | LoweredClassName = StringLoweredClassName; |
| 1195 | |
| 1196 | IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1197 | // Handle method with no name at its first selector slot; e.g. + (id):(int)x. |
| 1198 | if (!MethodIdName) |
| 1199 | return; |
| 1200 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1201 | std::string MethodName = MethodIdName->getName(); |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1202 | if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1203 | StringRef STRefMethodName(MethodName); |
| 1204 | size_t len = 0; |
| 1205 | if (STRefMethodName.startswith("standard")) |
| 1206 | len = strlen("standard"); |
| 1207 | else if (STRefMethodName.startswith("shared")) |
| 1208 | len = strlen("shared"); |
| 1209 | else if (STRefMethodName.startswith("default")) |
| 1210 | len = strlen("default"); |
| 1211 | else |
| 1212 | return; |
| 1213 | MethodName = STRefMethodName.substr(len); |
| 1214 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1215 | std::string MethodNameSubStr = MethodName.substr(0, 3); |
| 1216 | StringRef MethodNamePrefix(MethodNameSubStr); |
| 1217 | std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower(); |
| 1218 | MethodNamePrefix = StringLoweredMethodNamePrefix; |
| 1219 | size_t Ix = LoweredClassName.rfind(MethodNamePrefix); |
| 1220 | if (Ix == StringRef::npos) |
| 1221 | return; |
| 1222 | std::string ClassNamePostfix = LoweredClassName.substr(Ix); |
| 1223 | StringRef LoweredMethodName(MethodName); |
| 1224 | std::string StringLoweredMethodName = LoweredMethodName.lower(); |
| 1225 | LoweredMethodName = StringLoweredMethodName; |
| 1226 | if (!LoweredMethodName.startswith(ClassNamePostfix)) |
| 1227 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1228 | if (OIT_Family == OIT_ReturnsSelf) |
| 1229 | ReplaceWithClasstype(*this, OM); |
| 1230 | else |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame^] | 1231 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1234 | static bool IsVoidStarType(QualType Ty) { |
| 1235 | if (!Ty->isPointerType()) |
| 1236 | return false; |
| 1237 | |
| 1238 | while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr())) |
| 1239 | Ty = TD->getDecl()->getUnderlyingType(); |
| 1240 | |
| 1241 | // Is the type void*? |
| 1242 | const PointerType* PT = Ty->getAs<PointerType>(); |
| 1243 | if (PT->getPointeeType().getUnqualifiedType()->isVoidType()) |
| 1244 | return true; |
| 1245 | return IsVoidStarType(PT->getPointeeType()); |
| 1246 | } |
| 1247 | |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1248 | /// AuditedType - This routine audits the type AT and returns false if it is one of known |
| 1249 | /// CF object types or of the "void *" variety. It returns true if we don't care about the type |
| 1250 | /// 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] | 1251 | static bool AuditedType (QualType AT) { |
| 1252 | if (!AT->isAnyPointerType() && !AT->isBlockPointerType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1253 | return true; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1254 | // 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] | 1255 | if (ento::coreFoundation::isCFObjectRef(AT) || |
| 1256 | IsVoidStarType(AT) || |
| 1257 | // If an ObjC object is type, assuming that it is not a CF function and |
| 1258 | // that it is an un-audited function. |
Fariborz Jahanian | 2e9c19c | 2013-08-22 22:27:36 +0000 | [diff] [blame] | 1259 | AT->isObjCObjectPointerType() || AT->isObjCBuiltinType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1260 | return false; |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1261 | // All other pointers are assumed audited as harmless. |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1262 | return true; |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1265 | void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1266 | if (CFFunctionIBCandidates.empty()) |
| 1267 | return; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1268 | if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { |
| 1269 | CFFunctionIBCandidates.clear(); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1270 | FileId = FileID(); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1271 | return; |
| 1272 | } |
| 1273 | // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1274 | const Decl *FirstFD = CFFunctionIBCandidates[0]; |
| 1275 | const Decl *LastFD = |
| 1276 | CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1]; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1277 | const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n"; |
| 1278 | edit::Commit commit(*Editor); |
| 1279 | commit.insertBefore(FirstFD->getLocStart(), PragmaString); |
| 1280 | PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n"; |
| 1281 | SourceLocation EndLoc = LastFD->getLocEnd(); |
| 1282 | // get location just past end of function location. |
| 1283 | EndLoc = PP.getLocForEndOfToken(EndLoc); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1284 | if (isa<FunctionDecl>(LastFD)) { |
| 1285 | // For Methods, EndLoc points to the ending semcolon. So, |
| 1286 | // not of these extra work is needed. |
| 1287 | Token Tok; |
| 1288 | // get locaiton of token that comes after end of function. |
| 1289 | bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true); |
| 1290 | if (!Failed) |
| 1291 | EndLoc = Tok.getLocation(); |
| 1292 | } |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1293 | commit.insertAfterToken(EndLoc, PragmaString); |
| 1294 | Editor->commit(commit); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1295 | FileId = FileID(); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1296 | CFFunctionIBCandidates.clear(); |
| 1297 | } |
| 1298 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1299 | void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1300 | if (Decl->isDeprecated()) |
| 1301 | return; |
| 1302 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1303 | if (Decl->hasAttr<CFAuditedTransferAttr>()) { |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1304 | assert(CFFunctionIBCandidates.empty() && |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1305 | "Cannot have audited functions/methods inside user " |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1306 | "provided CF_IMPLICIT_BRIDGING_ENABLE"); |
| 1307 | return; |
| 1308 | } |
| 1309 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1310 | // Finction must be annotated first. |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1311 | if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) { |
| 1312 | CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl); |
| 1313 | if (AuditKind == CF_BRIDGING_ENABLE) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1314 | CFFunctionIBCandidates.push_back(Decl); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1315 | if (FileId.isInvalid()) |
| 1316 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1317 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1318 | else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) { |
| 1319 | if (!CFFunctionIBCandidates.empty()) { |
| 1320 | CFFunctionIBCandidates.push_back(Decl); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1321 | if (FileId.isInvalid()) |
| 1322 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1323 | } |
| 1324 | } |
| 1325 | else |
| 1326 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1327 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1328 | else { |
| 1329 | migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl)); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1330 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1331 | } |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1334 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1335 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1336 | const FunctionDecl *FuncDecl, |
| 1337 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1338 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1339 | if (!ResultAnnotated) { |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1340 | RetEffect Ret = CE.getReturnValue(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1341 | const char *AnnotationString = nullptr; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1342 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1343 | if (Ret.isOwned() && |
| 1344 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1345 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1346 | else if (Ret.notOwned() && |
| 1347 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1348 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1349 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1350 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
| 1351 | if (Ret.isOwned() && |
| 1352 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1353 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1356 | if (AnnotationString) { |
| 1357 | edit::Commit commit(*Editor); |
| 1358 | commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString); |
| 1359 | Editor->commit(commit); |
| 1360 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1361 | } |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1362 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1363 | unsigned i = 0; |
| 1364 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1365 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
Fariborz Jahanian | b918d7a | 2013-08-21 19:37:47 +0000 | [diff] [blame] | 1366 | const ParmVarDecl *pd = *pi; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1367 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1368 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1369 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1370 | edit::Commit commit(*Editor); |
| 1371 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1372 | Editor->commit(commit); |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1373 | } |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1374 | else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() && |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1375 | Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { |
| 1376 | edit::Commit commit(*Editor); |
| 1377 | commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); |
| 1378 | Editor->commit(commit); |
| 1379 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1380 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | ObjCMigrateASTConsumer::CF_BRIDGING_KIND |
| 1385 | ObjCMigrateASTConsumer::migrateAddFunctionAnnotation( |
| 1386 | ASTContext &Ctx, |
| 1387 | const FunctionDecl *FuncDecl) { |
| 1388 | if (FuncDecl->hasBody()) |
| 1389 | return CF_BRIDGING_NONE; |
| 1390 | |
| 1391 | CallEffects CE = CallEffects::getEffect(FuncDecl); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1392 | bool FuncIsReturnAnnotated = (FuncDecl->hasAttr<CFReturnsRetainedAttr>() || |
| 1393 | FuncDecl->hasAttr<CFReturnsNotRetainedAttr>() || |
| 1394 | FuncDecl->hasAttr<NSReturnsRetainedAttr>() || |
| 1395 | FuncDecl->hasAttr<NSReturnsNotRetainedAttr>() || |
| 1396 | FuncDecl->hasAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Trivial case of when funciton is annotated and has no argument. |
| 1399 | if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0) |
| 1400 | return CF_BRIDGING_NONE; |
| 1401 | |
| 1402 | bool ReturnCFAudited = false; |
| 1403 | if (!FuncIsReturnAnnotated) { |
| 1404 | RetEffect Ret = CE.getReturnValue(); |
| 1405 | if (Ret.getObjKind() == RetEffect::CF && |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1406 | (Ret.isOwned() || Ret.notOwned())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1407 | ReturnCFAudited = true; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1408 | else if (!AuditedType(FuncDecl->getReturnType())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1409 | return CF_BRIDGING_NONE; |
| 1410 | } |
| 1411 | |
| 1412 | // At this point result type is audited for potential inclusion. |
| 1413 | // Now, how about argument types. |
| 1414 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1415 | unsigned i = 0; |
| 1416 | bool ArgCFAudited = false; |
| 1417 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1418 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1419 | const ParmVarDecl *pd = *pi; |
| 1420 | ArgEffect AE = AEArgs[i]; |
| 1421 | if (AE == DecRef /*CFConsumed annotated*/ || AE == IncRef) { |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1422 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>()) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1423 | ArgCFAudited = true; |
| 1424 | else if (AE == IncRef) |
| 1425 | ArgCFAudited = true; |
| 1426 | } |
| 1427 | else { |
| 1428 | QualType AT = pd->getType(); |
| 1429 | if (!AuditedType(AT)) { |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1430 | AddCFAnnotations(Ctx, CE, FuncDecl, FuncIsReturnAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1431 | return CF_BRIDGING_NONE; |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | if (ReturnCFAudited || ArgCFAudited) |
| 1436 | return CF_BRIDGING_ENABLE; |
| 1437 | |
| 1438 | return CF_BRIDGING_MAY_INCLUDE; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1441 | void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx, |
| 1442 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1443 | if (!isa<ObjCInterfaceDecl>(CDecl) || CDecl->isDeprecated()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | |
| 1446 | // migrate methods which can have instancetype as their result type. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1447 | for (const auto *Method : CDecl->methods()) |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1448 | migrateCFAnnotation(Ctx, Method); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1451 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1452 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1453 | const ObjCMethodDecl *MethodDecl, |
| 1454 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1455 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1456 | if (!ResultAnnotated) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1457 | RetEffect Ret = CE.getReturnValue(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1458 | const char *AnnotationString = nullptr; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1459 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1460 | if (Ret.isOwned() && |
| 1461 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1462 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1463 | else if (Ret.notOwned() && |
| 1464 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1465 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1466 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1467 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1468 | ObjCMethodFamily OMF = MethodDecl->getMethodFamily(); |
| 1469 | switch (OMF) { |
| 1470 | case clang::OMF_alloc: |
| 1471 | case clang::OMF_new: |
| 1472 | case clang::OMF_copy: |
| 1473 | case clang::OMF_init: |
| 1474 | case clang::OMF_mutableCopy: |
| 1475 | break; |
| 1476 | |
| 1477 | default: |
| 1478 | if (Ret.isOwned() && |
| 1479 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1480 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1485 | if (AnnotationString) { |
| 1486 | edit::Commit commit(*Editor); |
| 1487 | commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString); |
| 1488 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1489 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1490 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1491 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1492 | unsigned i = 0; |
| 1493 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1494 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1495 | const ParmVarDecl *pd = *pi; |
| 1496 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1497 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1498 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1499 | edit::Commit commit(*Editor); |
| 1500 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1501 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1502 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1503 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1506 | void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1507 | ASTContext &Ctx, |
| 1508 | const ObjCMethodDecl *MethodDecl) { |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1509 | if (MethodDecl->hasBody() || MethodDecl->isImplicit()) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1510 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1511 | |
| 1512 | CallEffects CE = CallEffects::getEffect(MethodDecl); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1513 | bool MethodIsReturnAnnotated = (MethodDecl->hasAttr<CFReturnsRetainedAttr>() || |
| 1514 | MethodDecl->hasAttr<CFReturnsNotRetainedAttr>() || |
| 1515 | MethodDecl->hasAttr<NSReturnsRetainedAttr>() || |
| 1516 | MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() || |
| 1517 | MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1518 | |
| 1519 | if (CE.getReceiver() == DecRefMsg && |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1520 | !MethodDecl->hasAttr<NSConsumesSelfAttr>() && |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1521 | MethodDecl->getMethodFamily() != OMF_init && |
| 1522 | MethodDecl->getMethodFamily() != OMF_release && |
| 1523 | Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { |
| 1524 | edit::Commit commit(*Editor); |
| 1525 | commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); |
| 1526 | Editor->commit(commit); |
| 1527 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1528 | |
| 1529 | // Trivial case of when funciton is annotated and has no argument. |
| 1530 | if (MethodIsReturnAnnotated && |
| 1531 | (MethodDecl->param_begin() == MethodDecl->param_end())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1532 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1533 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1534 | if (!MethodIsReturnAnnotated) { |
| 1535 | RetEffect Ret = CE.getReturnValue(); |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1536 | if ((Ret.getObjKind() == RetEffect::CF || |
| 1537 | Ret.getObjKind() == RetEffect::ObjC) && |
| 1538 | (Ret.isOwned() || Ret.notOwned())) { |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1539 | AddCFAnnotations(Ctx, CE, MethodDecl, false); |
| 1540 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1541 | } else if (!AuditedType(MethodDecl->getReturnType())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1542 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | // At this point result type is either annotated or audited. |
| 1546 | // Now, how about argument types. |
| 1547 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1548 | unsigned i = 0; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1549 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1550 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1551 | const ParmVarDecl *pd = *pi; |
| 1552 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1553 | if ((AE == DecRef && !pd->hasAttr<CFConsumedAttr>()) || AE == IncRef || |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1554 | !AuditedType(pd->getType())) { |
| 1555 | AddCFAnnotations(Ctx, CE, MethodDecl, MethodIsReturnAnnotated); |
| 1556 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1559 | return; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1562 | namespace { |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1563 | class SuperInitChecker : public RecursiveASTVisitor<SuperInitChecker> { |
| 1564 | public: |
| 1565 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 1566 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 1567 | |
| 1568 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 1569 | if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance) { |
| 1570 | if (E->getMethodFamily() == OMF_init) |
| 1571 | return false; |
| 1572 | } |
| 1573 | return true; |
| 1574 | } |
| 1575 | }; |
| 1576 | } // anonymous namespace |
| 1577 | |
| 1578 | static bool hasSuperInitCall(const ObjCMethodDecl *MD) { |
| 1579 | return !SuperInitChecker().TraverseStmt(MD->getBody()); |
| 1580 | } |
| 1581 | |
| 1582 | void ObjCMigrateASTConsumer::inferDesignatedInitializers( |
| 1583 | ASTContext &Ctx, |
| 1584 | const ObjCImplementationDecl *ImplD) { |
| 1585 | |
| 1586 | const ObjCInterfaceDecl *IFace = ImplD->getClassInterface(); |
| 1587 | if (!IFace || IFace->hasDesignatedInitializers()) |
| 1588 | return; |
| 1589 | if (!Ctx.Idents.get("NS_DESIGNATED_INITIALIZER").hasMacroDefinition()) |
| 1590 | return; |
| 1591 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1592 | for (const auto *MD : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1593 | if (MD->isDeprecated() || |
| 1594 | MD->getMethodFamily() != OMF_init || |
| 1595 | MD->isDesignatedInitializerForTheInterface()) |
| 1596 | continue; |
| 1597 | const ObjCMethodDecl *IFaceM = IFace->getMethod(MD->getSelector(), |
| 1598 | /*isInstance=*/true); |
| 1599 | if (!IFaceM) |
| 1600 | continue; |
| 1601 | if (hasSuperInitCall(MD)) { |
| 1602 | edit::Commit commit(*Editor); |
| 1603 | commit.insert(IFaceM->getLocEnd(), " NS_DESIGNATED_INITIALIZER"); |
| 1604 | Editor->commit(commit); |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | namespace { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1610 | |
| 1611 | class RewritesReceiver : public edit::EditsReceiver { |
| 1612 | Rewriter &Rewrite; |
| 1613 | |
| 1614 | public: |
| 1615 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 1616 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1617 | void insert(SourceLocation loc, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1618 | Rewrite.InsertText(loc, text); |
| 1619 | } |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1620 | void replace(CharSourceRange range, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1621 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 1622 | } |
| 1623 | }; |
| 1624 | |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1625 | class JSONEditWriter : public edit::EditsReceiver { |
| 1626 | SourceManager &SourceMgr; |
| 1627 | llvm::raw_ostream &OS; |
| 1628 | |
| 1629 | public: |
| 1630 | JSONEditWriter(SourceManager &SM, llvm::raw_ostream &OS) |
| 1631 | : SourceMgr(SM), OS(OS) { |
| 1632 | OS << "[\n"; |
| 1633 | } |
| 1634 | ~JSONEditWriter() { |
| 1635 | OS << "]\n"; |
| 1636 | } |
| 1637 | |
| 1638 | private: |
| 1639 | struct EntryWriter { |
| 1640 | SourceManager &SourceMgr; |
| 1641 | llvm::raw_ostream &OS; |
| 1642 | |
| 1643 | EntryWriter(SourceManager &SM, llvm::raw_ostream &OS) |
| 1644 | : SourceMgr(SM), OS(OS) { |
| 1645 | OS << " {\n"; |
| 1646 | } |
| 1647 | ~EntryWriter() { |
| 1648 | OS << " },\n"; |
| 1649 | } |
| 1650 | |
| 1651 | void writeLoc(SourceLocation Loc) { |
| 1652 | FileID FID; |
| 1653 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1654 | std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1655 | assert(!FID.isInvalid()); |
| 1656 | SmallString<200> Path = |
| 1657 | StringRef(SourceMgr.getFileEntryForID(FID)->getName()); |
| 1658 | llvm::sys::fs::make_absolute(Path); |
| 1659 | OS << " \"file\": \""; |
| 1660 | OS.write_escaped(Path.str()) << "\",\n"; |
| 1661 | OS << " \"offset\": " << Offset << ",\n"; |
| 1662 | } |
| 1663 | |
| 1664 | void writeRemove(CharSourceRange Range) { |
| 1665 | assert(Range.isCharRange()); |
| 1666 | std::pair<FileID, unsigned> Begin = |
| 1667 | SourceMgr.getDecomposedLoc(Range.getBegin()); |
| 1668 | std::pair<FileID, unsigned> End = |
| 1669 | SourceMgr.getDecomposedLoc(Range.getEnd()); |
| 1670 | assert(Begin.first == End.first); |
| 1671 | assert(Begin.second <= End.second); |
| 1672 | unsigned Length = End.second - Begin.second; |
| 1673 | |
| 1674 | OS << " \"remove\": " << Length << ",\n"; |
| 1675 | } |
| 1676 | |
| 1677 | void writeText(StringRef Text) { |
| 1678 | OS << " \"text\": \""; |
| 1679 | OS.write_escaped(Text) << "\",\n"; |
| 1680 | } |
| 1681 | }; |
| 1682 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1683 | void insert(SourceLocation Loc, StringRef Text) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1684 | EntryWriter Writer(SourceMgr, OS); |
| 1685 | Writer.writeLoc(Loc); |
| 1686 | Writer.writeText(Text); |
| 1687 | } |
| 1688 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1689 | void replace(CharSourceRange Range, StringRef Text) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1690 | EntryWriter Writer(SourceMgr, OS); |
| 1691 | Writer.writeLoc(Range.getBegin()); |
| 1692 | Writer.writeRemove(Range); |
| 1693 | Writer.writeText(Text); |
| 1694 | } |
| 1695 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1696 | void remove(CharSourceRange Range) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1697 | EntryWriter Writer(SourceMgr, OS); |
| 1698 | Writer.writeLoc(Range.getBegin()); |
| 1699 | Writer.writeRemove(Range); |
| 1700 | } |
| 1701 | }; |
| 1702 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1706 | |
| 1707 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1708 | if (ASTMigrateActions & FrontendOptions::ObjCMT_MigrateDecls) { |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1709 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 1710 | D != DEnd; ++D) { |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1711 | FileID FID = PP.getSourceManager().getFileID((*D)->getLocation()); |
| 1712 | if (!FID.isInvalid()) |
| 1713 | if (!FileId.isInvalid() && FileId != FID) { |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1714 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1715 | AnnotateImplicitBridging(Ctx); |
| 1716 | } |
| 1717 | |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1718 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1719 | if (canModify(CDecl)) |
| 1720 | migrateObjCInterfaceDecl(Ctx, CDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 1721 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1722 | if (canModify(CatDecl)) |
| 1723 | migrateObjCInterfaceDecl(Ctx, CatDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 1724 | } |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1725 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
Fariborz Jahanian | 769c04e | 2013-12-17 01:01:33 +0000 | [diff] [blame] | 1726 | ObjCProtocolDecls.insert(PDecl->getCanonicalDecl()); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1727 | else if (const ObjCImplementationDecl *ImpDecl = |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1728 | dyn_cast<ObjCImplementationDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1729 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) && |
| 1730 | canModify(ImpDecl)) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1731 | migrateProtocolConformance(Ctx, ImpDecl); |
| 1732 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1733 | else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1734 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1735 | continue; |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1736 | if (!canModify(ED)) |
| 1737 | continue; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1738 | DeclContext::decl_iterator N = D; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1739 | if (++N != DEnd) { |
| 1740 | const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N); |
| 1741 | if (migrateNSEnumDecl(Ctx, ED, TD) && TD) |
| 1742 | D++; |
| 1743 | } |
| 1744 | else |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1745 | migrateNSEnumDecl(Ctx, ED, /*TypedefDecl */nullptr); |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1746 | } |
| 1747 | else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*D)) { |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1748 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1749 | continue; |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1750 | if (!canModify(TD)) |
| 1751 | continue; |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1752 | DeclContext::decl_iterator N = D; |
| 1753 | if (++N == DEnd) |
| 1754 | continue; |
| 1755 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(*N)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1756 | if (++N != DEnd) |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1757 | if (const TypedefDecl *TDF = dyn_cast<TypedefDecl>(*N)) { |
| 1758 | // prefer typedef-follows-enum to enum-follows-typedef pattern. |
| 1759 | if (migrateNSEnumDecl(Ctx, ED, TDF)) { |
| 1760 | ++D; ++D; |
| 1761 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1762 | continue; |
| 1763 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1764 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1765 | if (migrateNSEnumDecl(Ctx, ED, TD)) { |
| 1766 | ++D; |
| 1767 | continue; |
| 1768 | } |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1769 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1770 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1771 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1772 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1773 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
| 1774 | canModify(FD)) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1775 | migrateCFAnnotation(Ctx, FD); |
| 1776 | } |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1777 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1778 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1779 | bool CanModify = canModify(CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1780 | // migrate methods which can have instancetype as their result type. |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1781 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Instancetype) && |
| 1782 | CanModify) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1783 | migrateAllMethodInstaceType(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1784 | // annotate methods with CF annotations. |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1785 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
| 1786 | CanModify) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1787 | migrateARCSafeAnnotation(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1788 | } |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1789 | |
| 1790 | if (const ObjCImplementationDecl * |
| 1791 | ImplD = dyn_cast<ObjCImplementationDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1792 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_DesignatedInitializer) && |
| 1793 | canModify(ImplD)) |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1794 | inferDesignatedInitializers(Ctx, ImplD); |
| 1795 | } |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1796 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1797 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1798 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1799 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1800 | |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1801 | if (IsOutputFile) { |
| 1802 | std::string Error; |
Rafael Espindola | 4fbd373 | 2014-02-24 18:20:21 +0000 | [diff] [blame] | 1803 | llvm::raw_fd_ostream OS(MigrateDir.c_str(), Error, llvm::sys::fs::F_None); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1804 | if (!Error.empty()) { |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 1805 | DiagnosticsEngine &Diags = Ctx.getDiagnostics(); |
| 1806 | Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) |
| 1807 | << Error; |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1808 | return; |
| 1809 | } |
| 1810 | |
| 1811 | JSONEditWriter Writer(Ctx.getSourceManager(), OS); |
| 1812 | Editor->applyRewrites(Writer); |
| 1813 | return; |
| 1814 | } |
| 1815 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1816 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1817 | RewritesReceiver Rec(rewriter); |
| 1818 | Editor->applyRewrites(Rec); |
| 1819 | |
| 1820 | for (Rewriter::buffer_iterator |
| 1821 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 1822 | FileID FID = I->first; |
| 1823 | RewriteBuffer &buf = I->second; |
| 1824 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 1825 | assert(file); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1826 | SmallString<512> newText; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1827 | llvm::raw_svector_ostream vecOS(newText); |
| 1828 | buf.write(vecOS); |
| 1829 | vecOS.flush(); |
| 1830 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 1831 | StringRef(newText.data(), newText.size()), file->getName()); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1832 | SmallString<64> filePath(file->getName()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1833 | FileMgr.FixupRelativePath(filePath); |
| 1834 | Remapper.remap(filePath.str(), memBuf); |
| 1835 | } |
| 1836 | |
| 1837 | if (IsOutputFile) { |
| 1838 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 1839 | } else { |
| 1840 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
Argyrios Kyrtzidis | b482260 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 1845 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1846 | return true; |
| 1847 | } |
| 1848 | |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 1849 | static std::vector<std::string> getWhiteListFilenames(StringRef DirPath) { |
| 1850 | using namespace llvm::sys::fs; |
| 1851 | using namespace llvm::sys::path; |
| 1852 | |
| 1853 | std::vector<std::string> Filenames; |
| 1854 | if (DirPath.empty() || !is_directory(DirPath)) |
| 1855 | return Filenames; |
| 1856 | |
| 1857 | llvm::error_code EC; |
| 1858 | directory_iterator DI = directory_iterator(DirPath, EC); |
| 1859 | directory_iterator DE; |
| 1860 | for (; !EC && DI != DE; DI = DI.increment(EC)) { |
| 1861 | if (is_regular_file(DI->path())) |
| 1862 | Filenames.push_back(filename(DI->path())); |
| 1863 | } |
| 1864 | |
| 1865 | return Filenames; |
| 1866 | } |
| 1867 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1868 | ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, |
| 1869 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1870 | PPConditionalDirectiveRecord * |
| 1871 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 1872 | unsigned ObjCMTAction = CI.getFrontendOpts().ObjCMTAction; |
Argyrios Kyrtzidis | c47c63b | 2013-11-14 16:33:20 +0000 | [diff] [blame] | 1873 | unsigned ObjCMTOpts = ObjCMTAction; |
| 1874 | // These are companion flags, they do not enable transformations. |
| 1875 | ObjCMTOpts &= ~(FrontendOptions::ObjCMT_AtomicProperty | |
| 1876 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty); |
| 1877 | if (ObjCMTOpts == FrontendOptions::ObjCMT_None) { |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 1878 | // If no specific option was given, enable literals+subscripting transforms |
| 1879 | // by default. |
Argyrios Kyrtzidis | c47c63b | 2013-11-14 16:33:20 +0000 | [diff] [blame] | 1880 | ObjCMTAction |= FrontendOptions::ObjCMT_Literals | |
| 1881 | FrontendOptions::ObjCMT_Subscripting; |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 1882 | } |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1883 | CI.getPreprocessor().addPPCallbacks(PPRec); |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 1884 | std::vector<std::string> WhiteList = |
| 1885 | getWhiteListFilenames(CI.getFrontendOpts().ObjCMTWhiteListPath); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1886 | return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile, |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 1887 | ObjCMTAction, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1888 | Remapper, |
| 1889 | CI.getFileManager(), |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1890 | PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1891 | CI.getPreprocessor(), |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 1892 | /*isOutputFile=*/true, |
| 1893 | WhiteList); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1894 | } |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1895 | |
| 1896 | namespace { |
| 1897 | struct EditEntry { |
| 1898 | const FileEntry *File; |
| 1899 | unsigned Offset; |
| 1900 | unsigned RemoveLen; |
| 1901 | std::string Text; |
| 1902 | |
| 1903 | EditEntry() : File(), Offset(), RemoveLen() {} |
| 1904 | }; |
| 1905 | } |
| 1906 | |
| 1907 | namespace llvm { |
| 1908 | template<> struct DenseMapInfo<EditEntry> { |
| 1909 | static inline EditEntry getEmptyKey() { |
| 1910 | EditEntry Entry; |
| 1911 | Entry.Offset = unsigned(-1); |
| 1912 | return Entry; |
| 1913 | } |
| 1914 | static inline EditEntry getTombstoneKey() { |
| 1915 | EditEntry Entry; |
| 1916 | Entry.Offset = unsigned(-2); |
| 1917 | return Entry; |
| 1918 | } |
| 1919 | static unsigned getHashValue(const EditEntry& Val) { |
| 1920 | llvm::FoldingSetNodeID ID; |
| 1921 | ID.AddPointer(Val.File); |
| 1922 | ID.AddInteger(Val.Offset); |
| 1923 | ID.AddInteger(Val.RemoveLen); |
| 1924 | ID.AddString(Val.Text); |
| 1925 | return ID.ComputeHash(); |
| 1926 | } |
| 1927 | static bool isEqual(const EditEntry &LHS, const EditEntry &RHS) { |
| 1928 | return LHS.File == RHS.File && |
| 1929 | LHS.Offset == RHS.Offset && |
| 1930 | LHS.RemoveLen == RHS.RemoveLen && |
| 1931 | LHS.Text == RHS.Text; |
| 1932 | } |
| 1933 | }; |
| 1934 | } |
| 1935 | |
| 1936 | namespace { |
| 1937 | class RemapFileParser { |
| 1938 | FileManager &FileMgr; |
| 1939 | |
| 1940 | public: |
| 1941 | RemapFileParser(FileManager &FileMgr) : FileMgr(FileMgr) { } |
| 1942 | |
| 1943 | bool parse(StringRef File, SmallVectorImpl<EditEntry> &Entries) { |
| 1944 | using namespace llvm::yaml; |
| 1945 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1946 | std::unique_ptr<llvm::MemoryBuffer> FileBuf; |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1947 | if (llvm::MemoryBuffer::getFile(File, FileBuf)) |
| 1948 | return true; |
| 1949 | |
| 1950 | llvm::SourceMgr SM; |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1951 | Stream YAMLStream(FileBuf.release(), SM); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1952 | document_iterator I = YAMLStream.begin(); |
| 1953 | if (I == YAMLStream.end()) |
| 1954 | return true; |
| 1955 | Node *Root = I->getRoot(); |
| 1956 | if (!Root) |
| 1957 | return true; |
| 1958 | |
| 1959 | SequenceNode *SeqNode = dyn_cast<SequenceNode>(Root); |
| 1960 | if (!SeqNode) |
| 1961 | return true; |
| 1962 | |
| 1963 | for (SequenceNode::iterator |
| 1964 | AI = SeqNode->begin(), AE = SeqNode->end(); AI != AE; ++AI) { |
| 1965 | MappingNode *MapNode = dyn_cast<MappingNode>(&*AI); |
| 1966 | if (!MapNode) |
| 1967 | continue; |
| 1968 | parseEdit(MapNode, Entries); |
| 1969 | } |
| 1970 | |
| 1971 | return false; |
| 1972 | } |
| 1973 | |
| 1974 | private: |
| 1975 | void parseEdit(llvm::yaml::MappingNode *Node, |
| 1976 | SmallVectorImpl<EditEntry> &Entries) { |
| 1977 | using namespace llvm::yaml; |
| 1978 | EditEntry Entry; |
| 1979 | bool Ignore = false; |
| 1980 | |
| 1981 | for (MappingNode::iterator |
| 1982 | KVI = Node->begin(), KVE = Node->end(); KVI != KVE; ++KVI) { |
| 1983 | ScalarNode *KeyString = dyn_cast<ScalarNode>((*KVI).getKey()); |
| 1984 | if (!KeyString) |
| 1985 | continue; |
| 1986 | SmallString<10> KeyStorage; |
| 1987 | StringRef Key = KeyString->getValue(KeyStorage); |
| 1988 | |
| 1989 | ScalarNode *ValueString = dyn_cast<ScalarNode>((*KVI).getValue()); |
| 1990 | if (!ValueString) |
| 1991 | continue; |
| 1992 | SmallString<64> ValueStorage; |
| 1993 | StringRef Val = ValueString->getValue(ValueStorage); |
| 1994 | |
| 1995 | if (Key == "file") { |
| 1996 | const FileEntry *FE = FileMgr.getFile(Val); |
| 1997 | if (!FE) |
| 1998 | Ignore = true; |
| 1999 | Entry.File = FE; |
| 2000 | } else if (Key == "offset") { |
| 2001 | if (Val.getAsInteger(10, Entry.Offset)) |
| 2002 | Ignore = true; |
| 2003 | } else if (Key == "remove") { |
| 2004 | if (Val.getAsInteger(10, Entry.RemoveLen)) |
| 2005 | Ignore = true; |
| 2006 | } else if (Key == "text") { |
| 2007 | Entry.Text = Val; |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | if (!Ignore) |
| 2012 | Entries.push_back(Entry); |
| 2013 | } |
| 2014 | }; |
| 2015 | } |
| 2016 | |
| 2017 | static bool reportDiag(const Twine &Err, DiagnosticsEngine &Diag) { |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 2018 | Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0")) |
| 2019 | << Err.str(); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 2020 | return true; |
| 2021 | } |
| 2022 | |
| 2023 | static std::string applyEditsToTemp(const FileEntry *FE, |
| 2024 | ArrayRef<EditEntry> Edits, |
| 2025 | FileManager &FileMgr, |
| 2026 | DiagnosticsEngine &Diag) { |
| 2027 | using namespace llvm::sys; |
| 2028 | |
| 2029 | SourceManager SM(Diag, FileMgr); |
| 2030 | FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); |
| 2031 | LangOptions LangOpts; |
| 2032 | edit::EditedSource Editor(SM, LangOpts); |
| 2033 | for (ArrayRef<EditEntry>::iterator |
| 2034 | I = Edits.begin(), E = Edits.end(); I != E; ++I) { |
| 2035 | const EditEntry &Entry = *I; |
| 2036 | assert(Entry.File == FE); |
| 2037 | SourceLocation Loc = |
| 2038 | SM.getLocForStartOfFile(FID).getLocWithOffset(Entry.Offset); |
| 2039 | CharSourceRange Range; |
| 2040 | if (Entry.RemoveLen != 0) { |
| 2041 | Range = CharSourceRange::getCharRange(Loc, |
| 2042 | Loc.getLocWithOffset(Entry.RemoveLen)); |
| 2043 | } |
| 2044 | |
| 2045 | edit::Commit commit(Editor); |
| 2046 | if (Range.isInvalid()) { |
| 2047 | commit.insert(Loc, Entry.Text); |
| 2048 | } else if (Entry.Text.empty()) { |
| 2049 | commit.remove(Range); |
| 2050 | } else { |
| 2051 | commit.replace(Range, Entry.Text); |
| 2052 | } |
| 2053 | Editor.commit(commit); |
| 2054 | } |
| 2055 | |
| 2056 | Rewriter rewriter(SM, LangOpts); |
| 2057 | RewritesReceiver Rec(rewriter); |
| 2058 | Editor.applyRewrites(Rec); |
| 2059 | |
| 2060 | const RewriteBuffer *Buf = rewriter.getRewriteBufferFor(FID); |
| 2061 | SmallString<512> NewText; |
| 2062 | llvm::raw_svector_ostream OS(NewText); |
| 2063 | Buf->write(OS); |
| 2064 | OS.flush(); |
| 2065 | |
| 2066 | SmallString<64> TempPath; |
| 2067 | int FD; |
| 2068 | if (fs::createTemporaryFile(path::filename(FE->getName()), |
| 2069 | path::extension(FE->getName()), FD, |
| 2070 | TempPath)) { |
| 2071 | reportDiag("Could not create file: " + TempPath.str(), Diag); |
| 2072 | return std::string(); |
| 2073 | } |
| 2074 | |
| 2075 | llvm::raw_fd_ostream TmpOut(FD, /*shouldClose=*/true); |
| 2076 | TmpOut.write(NewText.data(), NewText.size()); |
| 2077 | TmpOut.close(); |
| 2078 | |
| 2079 | return TempPath.str(); |
| 2080 | } |
| 2081 | |
| 2082 | bool arcmt::getFileRemappingsFromFileList( |
| 2083 | std::vector<std::pair<std::string,std::string> > &remap, |
| 2084 | ArrayRef<StringRef> remapFiles, |
| 2085 | DiagnosticConsumer *DiagClient) { |
| 2086 | bool hasErrorOccurred = false; |
| 2087 | |
| 2088 | FileSystemOptions FSOpts; |
| 2089 | FileManager FileMgr(FSOpts); |
| 2090 | RemapFileParser Parser(FileMgr); |
| 2091 | |
| 2092 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 2093 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 2094 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 2095 | DiagClient, /*ShouldOwnClient=*/false)); |
| 2096 | |
| 2097 | typedef llvm::DenseMap<const FileEntry *, std::vector<EditEntry> > |
| 2098 | FileEditEntriesTy; |
| 2099 | FileEditEntriesTy FileEditEntries; |
| 2100 | |
| 2101 | llvm::DenseSet<EditEntry> EntriesSet; |
| 2102 | |
| 2103 | for (ArrayRef<StringRef>::iterator |
| 2104 | I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { |
| 2105 | SmallVector<EditEntry, 16> Entries; |
| 2106 | if (Parser.parse(*I, Entries)) |
| 2107 | continue; |
| 2108 | |
| 2109 | for (SmallVectorImpl<EditEntry>::iterator |
| 2110 | EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) { |
| 2111 | EditEntry &Entry = *EI; |
| 2112 | if (!Entry.File) |
| 2113 | continue; |
| 2114 | std::pair<llvm::DenseSet<EditEntry>::iterator, bool> |
| 2115 | Insert = EntriesSet.insert(Entry); |
| 2116 | if (!Insert.second) |
| 2117 | continue; |
| 2118 | |
| 2119 | FileEditEntries[Entry.File].push_back(Entry); |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | for (FileEditEntriesTy::iterator |
| 2124 | I = FileEditEntries.begin(), E = FileEditEntries.end(); I != E; ++I) { |
| 2125 | std::string TempFile = applyEditsToTemp(I->first, I->second, |
| 2126 | FileMgr, *Diags); |
| 2127 | if (TempFile.empty()) { |
| 2128 | hasErrorOccurred = true; |
| 2129 | continue; |
| 2130 | } |
| 2131 | |
| 2132 | remap.push_back(std::make_pair(I->first->getName(), TempFile)); |
| 2133 | } |
| 2134 | |
| 2135 | return hasErrorOccurred; |
| 2136 | } |