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