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