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