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