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 | ed6de56 | 2014-10-09 18:30:56 +0000 | [diff] [blame^] | 336 | if (Consumer.ASTMigrateActions & FrontendOptions::ObjCMT_Property) { |
| 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 | 80ebf8d | 2014-10-07 19:01:46 +0000 | [diff] [blame] | 921 | if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition() && |
| 922 | !InsertFoundation(Ctx, TypedefDcl->getLocStart())) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 923 | return false; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 924 | edit::Commit commit(*Editor); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 925 | rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 926 | Editor->commit(commit); |
Fariborz Jahanian | de79e81 | 2013-10-17 22:23:32 +0000 | [diff] [blame] | 927 | return true; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 928 | } |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 929 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 930 | return false; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 931 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 932 | |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 933 | // We may still use NS_OPTIONS based on what we find in the enumertor list. |
| 934 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | 35ee87d | 2014-10-06 23:50:37 +0000 | [diff] [blame] | 935 | // For sanity check, see if macro NS_ENUM can be seen. |
Fariborz Jahanian | 80ebf8d | 2014-10-07 19:01:46 +0000 | [diff] [blame] | 936 | if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition() |
| 937 | && !InsertFoundation(Ctx, TypedefDcl->getLocStart())) |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 938 | return false; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 939 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 940 | bool Res = rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, |
Fariborz Jahanian | 35ee87d | 2014-10-06 23:50:37 +0000 | [diff] [blame] | 941 | commit, NSIntegerName, NSOptions); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 942 | Editor->commit(commit); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 943 | return Res; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame] | 946 | static void ReplaceWithInstancetype(ASTContext &Ctx, |
| 947 | const ObjCMigrateASTConsumer &ASTC, |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 948 | ObjCMethodDecl *OM) { |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame] | 949 | if (OM->getReturnType() == Ctx.getObjCInstanceType()) |
| 950 | return; // already has instancetype. |
| 951 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 952 | SourceRange R; |
| 953 | std::string ClassString; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 954 | if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) { |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 955 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 956 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); |
| 957 | ClassString = "instancetype"; |
| 958 | } |
| 959 | else { |
| 960 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 961 | ClassString = OM->isInstanceMethod() ? '-' : '+'; |
| 962 | ClassString += " (instancetype)"; |
| 963 | } |
| 964 | edit::Commit commit(*ASTC.Editor); |
| 965 | commit.replace(R, ClassString); |
| 966 | ASTC.Editor->commit(commit); |
| 967 | } |
| 968 | |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 969 | static void ReplaceWithClasstype(const ObjCMigrateASTConsumer &ASTC, |
| 970 | ObjCMethodDecl *OM) { |
| 971 | ObjCInterfaceDecl *IDecl = OM->getClassInterface(); |
| 972 | SourceRange R; |
| 973 | std::string ClassString; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 974 | if (TypeSourceInfo *TSInfo = OM->getReturnTypeSourceInfo()) { |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 975 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 976 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); { |
| 977 | ClassString = IDecl->getName(); |
| 978 | ClassString += "*"; |
| 979 | } |
| 980 | } |
| 981 | else { |
| 982 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 983 | ClassString = "+ ("; |
| 984 | ClassString += IDecl->getName(); ClassString += "*)"; |
| 985 | } |
| 986 | edit::Commit commit(*ASTC.Editor); |
| 987 | commit.replace(R, ClassString); |
| 988 | ASTC.Editor->commit(commit); |
| 989 | } |
| 990 | |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 991 | void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, |
| 992 | ObjCContainerDecl *CDecl, |
| 993 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 994 | ObjCInstanceTypeFamily OIT_Family = |
| 995 | Selector::getInstTypeMethodFamily(OM->getSelector()); |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 996 | |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 997 | std::string ClassName; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 998 | switch (OIT_Family) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 999 | case OIT_None: |
| 1000 | migrateFactoryMethod(Ctx, CDecl, OM); |
| 1001 | return; |
Fariborz Jahanian | 7dd7143 | 2013-08-28 21:23:00 +0000 | [diff] [blame] | 1002 | case OIT_Array: |
| 1003 | ClassName = "NSArray"; |
| 1004 | break; |
| 1005 | case OIT_Dictionary: |
| 1006 | ClassName = "NSDictionary"; |
| 1007 | break; |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1008 | case OIT_Singleton: |
| 1009 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton); |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 1010 | return; |
Fariborz Jahanian | 1c900bc | 2013-09-18 20:35:47 +0000 | [diff] [blame] | 1011 | case OIT_Init: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1012 | if (OM->getReturnType()->isObjCIdType()) |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame] | 1013 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | 1c900bc | 2013-09-18 20:35:47 +0000 | [diff] [blame] | 1014 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1015 | case OIT_ReturnsSelf: |
| 1016 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_ReturnsSelf); |
| 1017 | return; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 1018 | } |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1019 | if (!OM->getReturnType()->isObjCIdType()) |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1020 | return; |
| 1021 | |
| 1022 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 1023 | if (!IDecl) { |
| 1024 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 1025 | IDecl = CatDecl->getClassInterface(); |
| 1026 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 1027 | IDecl = ImpDecl->getClassInterface(); |
| 1028 | } |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 1029 | if (!IDecl || |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1030 | !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { |
| 1031 | migrateFactoryMethod(Ctx, CDecl, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1032 | return; |
Fariborz Jahanian | 280954a | 2013-07-24 18:31:42 +0000 | [diff] [blame] | 1033 | } |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame] | 1034 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1037 | static bool TypeIsInnerPointer(QualType T) { |
| 1038 | if (!T->isAnyPointerType()) |
| 1039 | return false; |
| 1040 | if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() || |
Fariborz Jahanian | 73466ca | 2013-09-26 21:43:47 +0000 | [diff] [blame] | 1041 | T->isBlockPointerType() || T->isFunctionPointerType() || |
| 1042 | ento::coreFoundation::isCFObjectRef(T)) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1043 | return false; |
Fariborz Jahanian | 9d5fffb | 2013-09-09 23:56:14 +0000 | [diff] [blame] | 1044 | // Also, typedef-of-pointer-to-incomplete-struct is something that we assume |
| 1045 | // is not an innter pointer type. |
| 1046 | QualType OrigT = T; |
| 1047 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) |
| 1048 | T = TD->getDecl()->getUnderlyingType(); |
| 1049 | if (OrigT == T || !T->isPointerType()) |
| 1050 | return true; |
| 1051 | const PointerType* PT = T->getAs<PointerType>(); |
| 1052 | QualType UPointeeT = PT->getPointeeType().getUnqualifiedType(); |
| 1053 | if (UPointeeT->isRecordType()) { |
| 1054 | const RecordType *RecordTy = UPointeeT->getAs<RecordType>(); |
| 1055 | if (!RecordTy->getDecl()->isCompleteDefinition()) |
| 1056 | return false; |
| 1057 | } |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1058 | return true; |
| 1059 | } |
| 1060 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1061 | /// \brief Check whether the two versions match. |
| 1062 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y) { |
| 1063 | return (X == Y); |
| 1064 | } |
| 1065 | |
| 1066 | /// AvailabilityAttrsMatch - This routine checks that if comparing two |
| 1067 | /// availability attributes, all their components match. It returns |
| 1068 | /// true, if not dealing with availability or when all components of |
| 1069 | /// availability attributes match. This routine is only called when |
| 1070 | /// the attributes are of the same kind. |
| 1071 | static bool AvailabilityAttrsMatch(Attr *At1, Attr *At2) { |
| 1072 | const AvailabilityAttr *AA1 = dyn_cast<AvailabilityAttr>(At1); |
| 1073 | if (!AA1) |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1074 | return true; |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1075 | const AvailabilityAttr *AA2 = dyn_cast<AvailabilityAttr>(At2); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1076 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1077 | VersionTuple Introduced1 = AA1->getIntroduced(); |
| 1078 | VersionTuple Deprecated1 = AA1->getDeprecated(); |
| 1079 | VersionTuple Obsoleted1 = AA1->getObsoleted(); |
| 1080 | bool IsUnavailable1 = AA1->getUnavailable(); |
| 1081 | VersionTuple Introduced2 = AA2->getIntroduced(); |
| 1082 | VersionTuple Deprecated2 = AA2->getDeprecated(); |
| 1083 | VersionTuple Obsoleted2 = AA2->getObsoleted(); |
| 1084 | bool IsUnavailable2 = AA2->getUnavailable(); |
| 1085 | return (versionsMatch(Introduced1, Introduced2) && |
| 1086 | versionsMatch(Deprecated1, Deprecated2) && |
| 1087 | versionsMatch(Obsoleted1, Obsoleted2) && |
| 1088 | IsUnavailable1 == IsUnavailable2); |
| 1089 | |
| 1090 | } |
| 1091 | |
| 1092 | static bool MatchTwoAttributeLists(const AttrVec &Attrs1, const AttrVec &Attrs2, |
| 1093 | bool &AvailabilityArgsMatch) { |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1094 | // This list is very small, so this need not be optimized. |
| 1095 | for (unsigned i = 0, e = Attrs1.size(); i != e; i++) { |
| 1096 | bool match = false; |
| 1097 | for (unsigned j = 0, f = Attrs2.size(); j != f; j++) { |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1098 | // Matching attribute kind only. Except for Availabilty attributes, |
| 1099 | // 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] | 1100 | // this is sufficient. |
| 1101 | if (Attrs1[i]->getKind() == Attrs2[j]->getKind()) { |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1102 | if (AvailabilityArgsMatch) |
| 1103 | AvailabilityArgsMatch = AvailabilityAttrsMatch(Attrs1[i], Attrs2[j]); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1104 | match = true; |
| 1105 | break; |
| 1106 | } |
| 1107 | } |
| 1108 | if (!match) |
| 1109 | return false; |
| 1110 | } |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1114 | /// AttributesMatch - This routine checks list of attributes for two |
| 1115 | /// decls. It returns false, if there is a mismatch in kind of |
| 1116 | /// attributes seen in the decls. It returns true if the two decls |
| 1117 | /// have list of same kind of attributes. Furthermore, when there |
| 1118 | /// are availability attributes in the two decls, it sets the |
| 1119 | /// AvailabilityArgsMatch to false if availability attributes have |
| 1120 | /// different versions, etc. |
| 1121 | static bool AttributesMatch(const Decl *Decl1, const Decl *Decl2, |
| 1122 | bool &AvailabilityArgsMatch) { |
| 1123 | if (!Decl1->hasAttrs() || !Decl2->hasAttrs()) { |
| 1124 | AvailabilityArgsMatch = (Decl1->hasAttrs() == Decl2->hasAttrs()); |
| 1125 | return true; |
| 1126 | } |
| 1127 | AvailabilityArgsMatch = true; |
| 1128 | const AttrVec &Attrs1 = Decl1->getAttrs(); |
| 1129 | const AttrVec &Attrs2 = Decl2->getAttrs(); |
| 1130 | bool match = MatchTwoAttributeLists(Attrs1, Attrs2, AvailabilityArgsMatch); |
| 1131 | if (match && (Attrs2.size() > Attrs1.size())) |
| 1132 | return MatchTwoAttributeLists(Attrs2, Attrs1, AvailabilityArgsMatch); |
| 1133 | return match; |
| 1134 | } |
| 1135 | |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 1136 | static bool IsValidIdentifier(ASTContext &Ctx, |
| 1137 | const char *Name) { |
| 1138 | if (!isIdentifierHead(Name[0])) |
| 1139 | return false; |
| 1140 | std::string NameString = Name; |
| 1141 | NameString[0] = toLowercase(NameString[0]); |
| 1142 | IdentifierInfo *II = &Ctx.Idents.get(NameString); |
| 1143 | return II->getTokenID() == tok::identifier; |
| 1144 | } |
| 1145 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1146 | bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1147 | ObjCContainerDecl *D, |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1148 | ObjCMethodDecl *Method) { |
| 1149 | if (Method->isPropertyAccessor() || !Method->isInstanceMethod() || |
| 1150 | Method->param_size() != 0) |
| 1151 | return false; |
| 1152 | // Is this method candidate to be a getter? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1153 | QualType GRT = Method->getReturnType(); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1154 | if (GRT->isVoidType()) |
| 1155 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1156 | |
| 1157 | Selector GetterSelector = Method->getSelector(); |
Fariborz Jahanian | 7391a7b5a | 2013-09-25 00:17:07 +0000 | [diff] [blame] | 1158 | ObjCInstanceTypeFamily OIT_Family = |
| 1159 | Selector::getInstTypeMethodFamily(GetterSelector); |
| 1160 | |
| 1161 | if (OIT_Family != OIT_None) |
| 1162 | return false; |
| 1163 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1164 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 1165 | Selector SetterSelector = |
| 1166 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1167 | PP.getSelectorTable(), |
| 1168 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1169 | ObjCMethodDecl *SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1170 | unsigned LengthOfPrefix = 0; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1171 | if (!SetterMethod) { |
| 1172 | // try a different naming convention for getter: isXxxxx |
| 1173 | StringRef getterNameString = getterName->getName(); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1174 | bool IsPrefix = getterNameString.startswith("is"); |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 1175 | // Note that we don't want to change an isXXX method of retainable object |
| 1176 | // type to property (readonly or otherwise). |
| 1177 | if (IsPrefix && GRT->isObjCRetainableType()) |
| 1178 | return false; |
| 1179 | if (IsPrefix || getterNameString.startswith("get")) { |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1180 | LengthOfPrefix = (IsPrefix ? 2 : 3); |
| 1181 | const char *CGetterName = getterNameString.data() + LengthOfPrefix; |
| 1182 | // Make sure that first character after "is" or "get" prefix can |
| 1183 | // start an identifier. |
Fariborz Jahanian | 5d783df | 2013-09-27 22:55:54 +0000 | [diff] [blame] | 1184 | if (!IsValidIdentifier(Ctx, CGetterName)) |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame] | 1185 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1186 | if (CGetterName[0] && isUppercase(CGetterName[0])) { |
| 1187 | getterName = &Ctx.Idents.get(CGetterName); |
| 1188 | SetterSelector = |
| 1189 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1190 | PP.getSelectorTable(), |
| 1191 | getterName); |
Fariborz Jahanian | 92f7242 | 2013-09-17 19:00:30 +0000 | [diff] [blame] | 1192 | SetterMethod = D->getInstanceMethod(SetterSelector); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | } |
Fariborz Jahanian | 4c3d9c5 | 2013-09-17 19:38:55 +0000 | [diff] [blame] | 1196 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1197 | if (SetterMethod) { |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1198 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_ReadwriteProperty) == 0) |
| 1199 | return false; |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1200 | bool AvailabilityArgsMatch; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1201 | if (SetterMethod->isDeprecated() || |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1202 | !AttributesMatch(Method, SetterMethod, AvailabilityArgsMatch)) |
Fariborz Jahanian | f6c6505 | 2013-09-17 22:41:25 +0000 | [diff] [blame] | 1203 | return false; |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1204 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1205 | // Is this a valid setter, matching the target getter? |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1206 | QualType SRT = SetterMethod->getReturnType(); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1207 | if (!SRT->isVoidType()) |
| 1208 | return false; |
| 1209 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
| 1210 | QualType ArgType = argDecl->getType(); |
Fariborz Jahanian | 2ba62a7 | 2013-09-18 17:22:25 +0000 | [diff] [blame] | 1211 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT)) |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1212 | return false; |
| 1213 | edit::Commit commit(*Editor); |
| 1214 | rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1215 | LengthOfPrefix, |
| 1216 | (ASTMigrateActions & |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1217 | FrontendOptions::ObjCMT_AtomicProperty) != 0, |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 1218 | (ASTMigrateActions & |
| 1219 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0, |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1220 | AvailabilityArgsMatch); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1221 | Editor->commit(commit); |
| 1222 | return true; |
| 1223 | } |
Fariborz Jahanian | 182486c | 2013-10-02 17:08:12 +0000 | [diff] [blame] | 1224 | else if (ASTMigrateActions & FrontendOptions::ObjCMT_ReadonlyProperty) { |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1225 | // Try a non-void method with no argument (and no setter or property of same name |
| 1226 | // as a 'readonly' property. |
| 1227 | edit::Commit commit(*Editor); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1228 | rewriteToObjCProperty(Method, nullptr /*SetterMethod*/, *NSAPIObj, commit, |
Fariborz Jahanian | 20a1124 | 2013-10-09 19:06:08 +0000 | [diff] [blame] | 1229 | LengthOfPrefix, |
| 1230 | (ASTMigrateActions & |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1231 | FrontendOptions::ObjCMT_AtomicProperty) != 0, |
Fariborz Jahanian | 2e793d6 | 2013-11-13 00:08:36 +0000 | [diff] [blame] | 1232 | (ASTMigrateActions & |
| 1233 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty) != 0, |
Fariborz Jahanian | 071b98e | 2013-11-01 00:26:48 +0000 | [diff] [blame] | 1234 | /*AvailabilityArgsMatch*/false); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 1235 | Editor->commit(commit); |
| 1236 | return true; |
| 1237 | } |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1241 | void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, |
| 1242 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1243 | if (OM->isImplicit() || |
Fariborz Jahanian | 7c1a445 | 2013-09-26 22:43:41 +0000 | [diff] [blame] | 1244 | !OM->isInstanceMethod() || |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1245 | OM->hasAttr<ObjCReturnsInnerPointerAttr>()) |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1246 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1247 | |
| 1248 | QualType RT = OM->getReturnType(); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1249 | if (!TypeIsInnerPointer(RT) || |
| 1250 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1251 | return; |
| 1252 | |
| 1253 | edit::Commit commit(*Editor); |
| 1254 | commit.insertBefore(OM->getLocEnd(), " NS_RETURNS_INNER_POINTER"); |
| 1255 | Editor->commit(commit); |
| 1256 | } |
| 1257 | |
Fariborz Jahanian | 10b7435 | 2013-09-24 20:20:52 +0000 | [diff] [blame] | 1258 | void ObjCMigrateASTConsumer::migratePropertyNsReturnsInnerPointer(ASTContext &Ctx, |
| 1259 | ObjCPropertyDecl *P) { |
| 1260 | QualType T = P->getType(); |
| 1261 | |
| 1262 | if (!TypeIsInnerPointer(T) || |
| 1263 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 1264 | return; |
| 1265 | edit::Commit commit(*Editor); |
| 1266 | commit.insertBefore(P->getLocEnd(), " NS_RETURNS_INNER_POINTER "); |
| 1267 | Editor->commit(commit); |
| 1268 | } |
| 1269 | |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1270 | void ObjCMigrateASTConsumer::migrateAllMethodInstaceType(ASTContext &Ctx, |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1271 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 48a44d4 | 2013-11-19 18:17:31 +0000 | [diff] [blame] | 1272 | if (CDecl->isDeprecated() || IsCategoryNameWithDeprecatedSuffix(CDecl)) |
Fariborz Jahanian | e47a14a | 2013-09-18 15:43:52 +0000 | [diff] [blame] | 1273 | return; |
| 1274 | |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1275 | // migrate methods which can have instancetype as their result type. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1276 | for (auto *Method : CDecl->methods()) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1277 | if (Method->isDeprecated()) |
| 1278 | continue; |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 1279 | migrateMethodInstanceType(Ctx, CDecl, Method); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1283 | void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, |
| 1284 | ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1285 | ObjCMethodDecl *OM, |
| 1286 | ObjCInstanceTypeFamily OIT_Family) { |
Fariborz Jahanian | 3bfc35e | 2013-08-02 22:34:18 +0000 | [diff] [blame] | 1287 | if (OM->isInstanceMethod() || |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1288 | OM->getReturnType() == Ctx.getObjCInstanceType() || |
| 1289 | !OM->getReturnType()->isObjCIdType()) |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | |
| 1292 | // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class |
| 1293 | // NSYYYNamE with matching names be at least 3 characters long. |
| 1294 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 1295 | if (!IDecl) { |
| 1296 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 1297 | IDecl = CatDecl->getClassInterface(); |
| 1298 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 1299 | IDecl = ImpDecl->getClassInterface(); |
| 1300 | } |
| 1301 | if (!IDecl) |
| 1302 | return; |
| 1303 | |
| 1304 | std::string StringClassName = IDecl->getName(); |
| 1305 | StringRef LoweredClassName(StringClassName); |
| 1306 | std::string StringLoweredClassName = LoweredClassName.lower(); |
| 1307 | LoweredClassName = StringLoweredClassName; |
| 1308 | |
| 1309 | IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1310 | // Handle method with no name at its first selector slot; e.g. + (id):(int)x. |
| 1311 | if (!MethodIdName) |
| 1312 | return; |
| 1313 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1314 | std::string MethodName = MethodIdName->getName(); |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1315 | if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 1316 | StringRef STRefMethodName(MethodName); |
| 1317 | size_t len = 0; |
| 1318 | if (STRefMethodName.startswith("standard")) |
| 1319 | len = strlen("standard"); |
| 1320 | else if (STRefMethodName.startswith("shared")) |
| 1321 | len = strlen("shared"); |
| 1322 | else if (STRefMethodName.startswith("default")) |
| 1323 | len = strlen("default"); |
| 1324 | else |
| 1325 | return; |
| 1326 | MethodName = STRefMethodName.substr(len); |
| 1327 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1328 | std::string MethodNameSubStr = MethodName.substr(0, 3); |
| 1329 | StringRef MethodNamePrefix(MethodNameSubStr); |
| 1330 | std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower(); |
| 1331 | MethodNamePrefix = StringLoweredMethodNamePrefix; |
| 1332 | size_t Ix = LoweredClassName.rfind(MethodNamePrefix); |
| 1333 | if (Ix == StringRef::npos) |
| 1334 | return; |
| 1335 | std::string ClassNamePostfix = LoweredClassName.substr(Ix); |
| 1336 | StringRef LoweredMethodName(MethodName); |
| 1337 | std::string StringLoweredMethodName = LoweredMethodName.lower(); |
| 1338 | LoweredMethodName = StringLoweredMethodName; |
| 1339 | if (!LoweredMethodName.startswith(ClassNamePostfix)) |
| 1340 | return; |
Fariborz Jahanian | 7c87b43 | 2013-10-10 18:23:13 +0000 | [diff] [blame] | 1341 | if (OIT_Family == OIT_ReturnsSelf) |
| 1342 | ReplaceWithClasstype(*this, OM); |
| 1343 | else |
Argyrios Kyrtzidis | c02a0db | 2014-05-21 00:24:20 +0000 | [diff] [blame] | 1344 | ReplaceWithInstancetype(Ctx, *this, OM); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1347 | static bool IsVoidStarType(QualType Ty) { |
| 1348 | if (!Ty->isPointerType()) |
| 1349 | return false; |
| 1350 | |
| 1351 | while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr())) |
| 1352 | Ty = TD->getDecl()->getUnderlyingType(); |
| 1353 | |
| 1354 | // Is the type void*? |
| 1355 | const PointerType* PT = Ty->getAs<PointerType>(); |
| 1356 | if (PT->getPointeeType().getUnqualifiedType()->isVoidType()) |
| 1357 | return true; |
| 1358 | return IsVoidStarType(PT->getPointeeType()); |
| 1359 | } |
| 1360 | |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1361 | /// AuditedType - This routine audits the type AT and returns false if it is one of known |
| 1362 | /// CF object types or of the "void *" variety. It returns true if we don't care about the type |
| 1363 | /// 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] | 1364 | static bool AuditedType (QualType AT) { |
| 1365 | if (!AT->isAnyPointerType() && !AT->isBlockPointerType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1366 | return true; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1367 | // 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] | 1368 | if (ento::coreFoundation::isCFObjectRef(AT) || |
| 1369 | IsVoidStarType(AT) || |
| 1370 | // If an ObjC object is type, assuming that it is not a CF function and |
| 1371 | // that it is an un-audited function. |
Fariborz Jahanian | 2e9c19c | 2013-08-22 22:27:36 +0000 | [diff] [blame] | 1372 | AT->isObjCObjectPointerType() || AT->isObjCBuiltinType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1373 | return false; |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1374 | // All other pointers are assumed audited as harmless. |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1375 | return true; |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1378 | void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1379 | if (CFFunctionIBCandidates.empty()) |
| 1380 | return; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1381 | if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { |
| 1382 | CFFunctionIBCandidates.clear(); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1383 | FileId = FileID(); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1384 | return; |
| 1385 | } |
| 1386 | // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1387 | const Decl *FirstFD = CFFunctionIBCandidates[0]; |
| 1388 | const Decl *LastFD = |
| 1389 | CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1]; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1390 | const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n"; |
| 1391 | edit::Commit commit(*Editor); |
| 1392 | commit.insertBefore(FirstFD->getLocStart(), PragmaString); |
| 1393 | PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n"; |
| 1394 | SourceLocation EndLoc = LastFD->getLocEnd(); |
| 1395 | // get location just past end of function location. |
| 1396 | EndLoc = PP.getLocForEndOfToken(EndLoc); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1397 | if (isa<FunctionDecl>(LastFD)) { |
| 1398 | // For Methods, EndLoc points to the ending semcolon. So, |
| 1399 | // not of these extra work is needed. |
| 1400 | Token Tok; |
| 1401 | // get locaiton of token that comes after end of function. |
| 1402 | bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true); |
| 1403 | if (!Failed) |
| 1404 | EndLoc = Tok.getLocation(); |
| 1405 | } |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1406 | commit.insertAfterToken(EndLoc, PragmaString); |
| 1407 | Editor->commit(commit); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1408 | FileId = FileID(); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1409 | CFFunctionIBCandidates.clear(); |
| 1410 | } |
| 1411 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1412 | void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1413 | if (Decl->isDeprecated()) |
| 1414 | return; |
| 1415 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1416 | if (Decl->hasAttr<CFAuditedTransferAttr>()) { |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1417 | assert(CFFunctionIBCandidates.empty() && |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1418 | "Cannot have audited functions/methods inside user " |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 1419 | "provided CF_IMPLICIT_BRIDGING_ENABLE"); |
| 1420 | return; |
| 1421 | } |
| 1422 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1423 | // Finction must be annotated first. |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1424 | if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) { |
| 1425 | CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl); |
| 1426 | if (AuditKind == CF_BRIDGING_ENABLE) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1427 | CFFunctionIBCandidates.push_back(Decl); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1428 | if (FileId.isInvalid()) |
| 1429 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1430 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1431 | else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) { |
| 1432 | if (!CFFunctionIBCandidates.empty()) { |
| 1433 | CFFunctionIBCandidates.push_back(Decl); |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1434 | if (FileId.isInvalid()) |
| 1435 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | else |
| 1439 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1440 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1441 | else { |
| 1442 | migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl)); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1443 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1444 | } |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1447 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1448 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1449 | const FunctionDecl *FuncDecl, |
| 1450 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1451 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1452 | if (!ResultAnnotated) { |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1453 | RetEffect Ret = CE.getReturnValue(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1454 | const char *AnnotationString = nullptr; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1455 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1456 | if (Ret.isOwned() && |
| 1457 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1458 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1459 | else if (Ret.notOwned() && |
| 1460 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1461 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1462 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1463 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
| 1464 | if (Ret.isOwned() && |
| 1465 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1466 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1469 | if (AnnotationString) { |
| 1470 | edit::Commit commit(*Editor); |
| 1471 | commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString); |
| 1472 | Editor->commit(commit); |
| 1473 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1474 | } |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 1475 | ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1476 | unsigned i = 0; |
| 1477 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1478 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
Fariborz Jahanian | b918d7a | 2013-08-21 19:37:47 +0000 | [diff] [blame] | 1479 | const ParmVarDecl *pd = *pi; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1480 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1481 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1482 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1483 | edit::Commit commit(*Editor); |
| 1484 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1485 | Editor->commit(commit); |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1486 | } |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1487 | else if (AE == DecRefMsg && !pd->hasAttr<NSConsumedAttr>() && |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1488 | Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { |
| 1489 | edit::Commit commit(*Editor); |
| 1490 | commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); |
| 1491 | Editor->commit(commit); |
| 1492 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1493 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | ObjCMigrateASTConsumer::CF_BRIDGING_KIND |
| 1498 | ObjCMigrateASTConsumer::migrateAddFunctionAnnotation( |
| 1499 | ASTContext &Ctx, |
| 1500 | const FunctionDecl *FuncDecl) { |
| 1501 | if (FuncDecl->hasBody()) |
| 1502 | return CF_BRIDGING_NONE; |
| 1503 | |
| 1504 | CallEffects CE = CallEffects::getEffect(FuncDecl); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1505 | bool FuncIsReturnAnnotated = (FuncDecl->hasAttr<CFReturnsRetainedAttr>() || |
| 1506 | FuncDecl->hasAttr<CFReturnsNotRetainedAttr>() || |
| 1507 | FuncDecl->hasAttr<NSReturnsRetainedAttr>() || |
| 1508 | FuncDecl->hasAttr<NSReturnsNotRetainedAttr>() || |
| 1509 | FuncDecl->hasAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1510 | |
| 1511 | // Trivial case of when funciton is annotated and has no argument. |
| 1512 | if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0) |
| 1513 | return CF_BRIDGING_NONE; |
| 1514 | |
| 1515 | bool ReturnCFAudited = false; |
| 1516 | if (!FuncIsReturnAnnotated) { |
| 1517 | RetEffect Ret = CE.getReturnValue(); |
| 1518 | if (Ret.getObjKind() == RetEffect::CF && |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1519 | (Ret.isOwned() || Ret.notOwned())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1520 | ReturnCFAudited = true; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1521 | else if (!AuditedType(FuncDecl->getReturnType())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1522 | return CF_BRIDGING_NONE; |
| 1523 | } |
| 1524 | |
| 1525 | // At this point result type is audited for potential inclusion. |
| 1526 | // Now, how about argument types. |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 1527 | ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1528 | unsigned i = 0; |
| 1529 | bool ArgCFAudited = false; |
| 1530 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1531 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1532 | const ParmVarDecl *pd = *pi; |
| 1533 | ArgEffect AE = AEArgs[i]; |
| 1534 | if (AE == DecRef /*CFConsumed annotated*/ || AE == IncRef) { |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1535 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>()) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1536 | ArgCFAudited = true; |
| 1537 | else if (AE == IncRef) |
| 1538 | ArgCFAudited = true; |
| 1539 | } |
| 1540 | else { |
| 1541 | QualType AT = pd->getType(); |
| 1542 | if (!AuditedType(AT)) { |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1543 | AddCFAnnotations(Ctx, CE, FuncDecl, FuncIsReturnAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1544 | return CF_BRIDGING_NONE; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | if (ReturnCFAudited || ArgCFAudited) |
| 1549 | return CF_BRIDGING_ENABLE; |
| 1550 | |
| 1551 | return CF_BRIDGING_MAY_INCLUDE; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1554 | void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx, |
| 1555 | ObjCContainerDecl *CDecl) { |
Fariborz Jahanian | 75226d5 | 2013-09-17 21:56:04 +0000 | [diff] [blame] | 1556 | if (!isa<ObjCInterfaceDecl>(CDecl) || CDecl->isDeprecated()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | |
| 1559 | // migrate methods which can have instancetype as their result type. |
Aaron Ballman | aff18c0 | 2014-03-13 19:03:34 +0000 | [diff] [blame] | 1560 | for (const auto *Method : CDecl->methods()) |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1561 | migrateCFAnnotation(Ctx, Method); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1564 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1565 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1566 | const ObjCMethodDecl *MethodDecl, |
| 1567 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1568 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1569 | if (!ResultAnnotated) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1570 | RetEffect Ret = CE.getReturnValue(); |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1571 | const char *AnnotationString = nullptr; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1572 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1573 | if (Ret.isOwned() && |
| 1574 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1575 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1576 | else if (Ret.notOwned() && |
| 1577 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1578 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1579 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1580 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1581 | ObjCMethodFamily OMF = MethodDecl->getMethodFamily(); |
| 1582 | switch (OMF) { |
| 1583 | case clang::OMF_alloc: |
| 1584 | case clang::OMF_new: |
| 1585 | case clang::OMF_copy: |
| 1586 | case clang::OMF_init: |
| 1587 | case clang::OMF_mutableCopy: |
| 1588 | break; |
| 1589 | |
| 1590 | default: |
| 1591 | if (Ret.isOwned() && |
| 1592 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1593 | AnnotationString = " NS_RETURNS_RETAINED"; |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1598 | if (AnnotationString) { |
| 1599 | edit::Commit commit(*Editor); |
| 1600 | commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString); |
| 1601 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1602 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1603 | } |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 1604 | ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1605 | unsigned i = 0; |
| 1606 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1607 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1608 | const ParmVarDecl *pd = *pi; |
| 1609 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1610 | if (AE == DecRef && !pd->hasAttr<CFConsumedAttr>() && |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1611 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1612 | edit::Commit commit(*Editor); |
| 1613 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1614 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1615 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1616 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1619 | void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1620 | ASTContext &Ctx, |
| 1621 | const ObjCMethodDecl *MethodDecl) { |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1622 | if (MethodDecl->hasBody() || MethodDecl->isImplicit()) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1623 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1624 | |
| 1625 | CallEffects CE = CallEffects::getEffect(MethodDecl); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1626 | bool MethodIsReturnAnnotated = (MethodDecl->hasAttr<CFReturnsRetainedAttr>() || |
| 1627 | MethodDecl->hasAttr<CFReturnsNotRetainedAttr>() || |
| 1628 | MethodDecl->hasAttr<NSReturnsRetainedAttr>() || |
| 1629 | MethodDecl->hasAttr<NSReturnsNotRetainedAttr>() || |
| 1630 | MethodDecl->hasAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1631 | |
| 1632 | if (CE.getReceiver() == DecRefMsg && |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1633 | !MethodDecl->hasAttr<NSConsumesSelfAttr>() && |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1634 | MethodDecl->getMethodFamily() != OMF_init && |
| 1635 | MethodDecl->getMethodFamily() != OMF_release && |
| 1636 | Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { |
| 1637 | edit::Commit commit(*Editor); |
| 1638 | commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); |
| 1639 | Editor->commit(commit); |
| 1640 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1641 | |
| 1642 | // Trivial case of when funciton is annotated and has no argument. |
| 1643 | if (MethodIsReturnAnnotated && |
| 1644 | (MethodDecl->param_begin() == MethodDecl->param_end())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1645 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1646 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1647 | if (!MethodIsReturnAnnotated) { |
| 1648 | RetEffect Ret = CE.getReturnValue(); |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1649 | if ((Ret.getObjKind() == RetEffect::CF || |
| 1650 | Ret.getObjKind() == RetEffect::ObjC) && |
| 1651 | (Ret.isOwned() || Ret.notOwned())) { |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1652 | AddCFAnnotations(Ctx, CE, MethodDecl, false); |
| 1653 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1654 | } else if (!AuditedType(MethodDecl->getReturnType())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1655 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // At this point result type is either annotated or audited. |
| 1659 | // Now, how about argument types. |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 1660 | ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1661 | unsigned i = 0; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1662 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1663 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1664 | const ParmVarDecl *pd = *pi; |
| 1665 | ArgEffect AE = AEArgs[i]; |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1666 | if ((AE == DecRef && !pd->hasAttr<CFConsumedAttr>()) || AE == IncRef || |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1667 | !AuditedType(pd->getType())) { |
| 1668 | AddCFAnnotations(Ctx, CE, MethodDecl, MethodIsReturnAnnotated); |
| 1669 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1672 | return; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1675 | namespace { |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1676 | class SuperInitChecker : public RecursiveASTVisitor<SuperInitChecker> { |
| 1677 | public: |
| 1678 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 1679 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 1680 | |
| 1681 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 1682 | if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance) { |
| 1683 | if (E->getMethodFamily() == OMF_init) |
| 1684 | return false; |
| 1685 | } |
| 1686 | return true; |
| 1687 | } |
| 1688 | }; |
| 1689 | } // anonymous namespace |
| 1690 | |
| 1691 | static bool hasSuperInitCall(const ObjCMethodDecl *MD) { |
| 1692 | return !SuperInitChecker().TraverseStmt(MD->getBody()); |
| 1693 | } |
| 1694 | |
| 1695 | void ObjCMigrateASTConsumer::inferDesignatedInitializers( |
| 1696 | ASTContext &Ctx, |
| 1697 | const ObjCImplementationDecl *ImplD) { |
| 1698 | |
| 1699 | const ObjCInterfaceDecl *IFace = ImplD->getClassInterface(); |
| 1700 | if (!IFace || IFace->hasDesignatedInitializers()) |
| 1701 | return; |
| 1702 | if (!Ctx.Idents.get("NS_DESIGNATED_INITIALIZER").hasMacroDefinition()) |
| 1703 | return; |
| 1704 | |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1705 | for (const auto *MD : ImplD->instance_methods()) { |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1706 | if (MD->isDeprecated() || |
| 1707 | MD->getMethodFamily() != OMF_init || |
| 1708 | MD->isDesignatedInitializerForTheInterface()) |
| 1709 | continue; |
| 1710 | const ObjCMethodDecl *IFaceM = IFace->getMethod(MD->getSelector(), |
| 1711 | /*isInstance=*/true); |
| 1712 | if (!IFaceM) |
| 1713 | continue; |
| 1714 | if (hasSuperInitCall(MD)) { |
| 1715 | edit::Commit commit(*Editor); |
| 1716 | commit.insert(IFaceM->getLocEnd(), " NS_DESIGNATED_INITIALIZER"); |
| 1717 | Editor->commit(commit); |
| 1718 | } |
| 1719 | } |
| 1720 | } |
| 1721 | |
Fariborz Jahanian | 80ebf8d | 2014-10-07 19:01:46 +0000 | [diff] [blame] | 1722 | bool ObjCMigrateASTConsumer::InsertFoundation(ASTContext &Ctx, |
| 1723 | SourceLocation Loc) { |
| 1724 | if (FoundationIncluded) |
| 1725 | return true; |
| 1726 | if (Loc.isInvalid()) |
| 1727 | return false; |
| 1728 | edit::Commit commit(*Editor); |
| 1729 | if (Ctx.getLangOpts().Modules) |
| 1730 | commit.insert(Loc, "@import Foundation;\n"); |
| 1731 | else |
| 1732 | commit.insert(Loc, "#import <Foundation/Foundation.h>\n"); |
| 1733 | Editor->commit(commit); |
| 1734 | FoundationIncluded = true; |
| 1735 | return true; |
| 1736 | } |
| 1737 | |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1738 | namespace { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1739 | |
| 1740 | class RewritesReceiver : public edit::EditsReceiver { |
| 1741 | Rewriter &Rewrite; |
| 1742 | |
| 1743 | public: |
| 1744 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 1745 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1746 | void insert(SourceLocation loc, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1747 | Rewrite.InsertText(loc, text); |
| 1748 | } |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1749 | void replace(CharSourceRange range, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1750 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 1751 | } |
| 1752 | }; |
| 1753 | |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1754 | class JSONEditWriter : public edit::EditsReceiver { |
| 1755 | SourceManager &SourceMgr; |
| 1756 | llvm::raw_ostream &OS; |
| 1757 | |
| 1758 | public: |
| 1759 | JSONEditWriter(SourceManager &SM, llvm::raw_ostream &OS) |
| 1760 | : SourceMgr(SM), OS(OS) { |
| 1761 | OS << "[\n"; |
| 1762 | } |
| 1763 | ~JSONEditWriter() { |
| 1764 | OS << "]\n"; |
| 1765 | } |
| 1766 | |
| 1767 | private: |
| 1768 | struct EntryWriter { |
| 1769 | SourceManager &SourceMgr; |
| 1770 | llvm::raw_ostream &OS; |
| 1771 | |
| 1772 | EntryWriter(SourceManager &SM, llvm::raw_ostream &OS) |
| 1773 | : SourceMgr(SM), OS(OS) { |
| 1774 | OS << " {\n"; |
| 1775 | } |
| 1776 | ~EntryWriter() { |
| 1777 | OS << " },\n"; |
| 1778 | } |
| 1779 | |
| 1780 | void writeLoc(SourceLocation Loc) { |
| 1781 | FileID FID; |
| 1782 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1783 | std::tie(FID, Offset) = SourceMgr.getDecomposedLoc(Loc); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1784 | assert(!FID.isInvalid()); |
| 1785 | SmallString<200> Path = |
| 1786 | StringRef(SourceMgr.getFileEntryForID(FID)->getName()); |
| 1787 | llvm::sys::fs::make_absolute(Path); |
| 1788 | OS << " \"file\": \""; |
| 1789 | OS.write_escaped(Path.str()) << "\",\n"; |
| 1790 | OS << " \"offset\": " << Offset << ",\n"; |
| 1791 | } |
| 1792 | |
| 1793 | void writeRemove(CharSourceRange Range) { |
| 1794 | assert(Range.isCharRange()); |
| 1795 | std::pair<FileID, unsigned> Begin = |
| 1796 | SourceMgr.getDecomposedLoc(Range.getBegin()); |
| 1797 | std::pair<FileID, unsigned> End = |
| 1798 | SourceMgr.getDecomposedLoc(Range.getEnd()); |
| 1799 | assert(Begin.first == End.first); |
| 1800 | assert(Begin.second <= End.second); |
| 1801 | unsigned Length = End.second - Begin.second; |
| 1802 | |
| 1803 | OS << " \"remove\": " << Length << ",\n"; |
| 1804 | } |
| 1805 | |
| 1806 | void writeText(StringRef Text) { |
| 1807 | OS << " \"text\": \""; |
| 1808 | OS.write_escaped(Text) << "\",\n"; |
| 1809 | } |
| 1810 | }; |
| 1811 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1812 | void insert(SourceLocation Loc, StringRef Text) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1813 | EntryWriter Writer(SourceMgr, OS); |
| 1814 | Writer.writeLoc(Loc); |
| 1815 | Writer.writeText(Text); |
| 1816 | } |
| 1817 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1818 | void replace(CharSourceRange Range, StringRef Text) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1819 | EntryWriter Writer(SourceMgr, OS); |
| 1820 | Writer.writeLoc(Range.getBegin()); |
| 1821 | Writer.writeRemove(Range); |
| 1822 | Writer.writeText(Text); |
| 1823 | } |
| 1824 | |
Craig Topper | b45acb8 | 2014-03-14 06:02:07 +0000 | [diff] [blame] | 1825 | void remove(CharSourceRange Range) override { |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1826 | EntryWriter Writer(SourceMgr, OS); |
| 1827 | Writer.writeLoc(Range.getBegin()); |
| 1828 | Writer.writeRemove(Range); |
| 1829 | } |
| 1830 | }; |
| 1831 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1835 | |
| 1836 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
Fariborz Jahanian | c121386 | 2013-10-02 21:32:39 +0000 | [diff] [blame] | 1837 | if (ASTMigrateActions & FrontendOptions::ObjCMT_MigrateDecls) { |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1838 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 1839 | D != DEnd; ++D) { |
Argyrios Kyrtzidis | 3e8547a | 2013-11-13 18:20:31 +0000 | [diff] [blame] | 1840 | FileID FID = PP.getSourceManager().getFileID((*D)->getLocation()); |
| 1841 | if (!FID.isInvalid()) |
| 1842 | if (!FileId.isInvalid() && FileId != FID) { |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1843 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1844 | AnnotateImplicitBridging(Ctx); |
| 1845 | } |
| 1846 | |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1847 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1848 | if (canModify(CDecl)) |
| 1849 | migrateObjCInterfaceDecl(Ctx, CDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 1850 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1851 | if (canModify(CatDecl)) |
| 1852 | migrateObjCInterfaceDecl(Ctx, CatDecl); |
Fariborz Jahanian | 9d2ffea | 2013-10-31 00:06:58 +0000 | [diff] [blame] | 1853 | } |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1854 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
Fariborz Jahanian | 769c04e | 2013-12-17 01:01:33 +0000 | [diff] [blame] | 1855 | ObjCProtocolDecls.insert(PDecl->getCanonicalDecl()); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1856 | else if (const ObjCImplementationDecl *ImpDecl = |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1857 | dyn_cast<ObjCImplementationDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1858 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_ProtocolConformance) && |
| 1859 | canModify(ImpDecl)) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1860 | migrateProtocolConformance(Ctx, ImpDecl); |
| 1861 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1862 | else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1863 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1864 | continue; |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1865 | if (!canModify(ED)) |
| 1866 | continue; |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1867 | DeclContext::decl_iterator N = D; |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1868 | if (++N != DEnd) { |
| 1869 | const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N); |
| 1870 | if (migrateNSEnumDecl(Ctx, ED, TD) && TD) |
| 1871 | D++; |
| 1872 | } |
| 1873 | else |
Craig Topper | 8ae1203 | 2014-05-07 06:21:57 +0000 | [diff] [blame] | 1874 | migrateNSEnumDecl(Ctx, ED, /*TypedefDecl */nullptr); |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1875 | } |
| 1876 | else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*D)) { |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1877 | if (!(ASTMigrateActions & FrontendOptions::ObjCMT_NsMacros)) |
| 1878 | continue; |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1879 | if (!canModify(TD)) |
| 1880 | continue; |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1881 | DeclContext::decl_iterator N = D; |
| 1882 | if (++N == DEnd) |
| 1883 | continue; |
| 1884 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(*N)) { |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1885 | if (++N != DEnd) |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1886 | if (const TypedefDecl *TDF = dyn_cast<TypedefDecl>(*N)) { |
| 1887 | // prefer typedef-follows-enum to enum-follows-typedef pattern. |
| 1888 | if (migrateNSEnumDecl(Ctx, ED, TDF)) { |
| 1889 | ++D; ++D; |
| 1890 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1891 | continue; |
| 1892 | } |
Fariborz Jahanian | 11dd4b1 | 2013-10-11 21:34:56 +0000 | [diff] [blame] | 1893 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1894 | if (migrateNSEnumDecl(Ctx, ED, TD)) { |
| 1895 | ++D; |
| 1896 | continue; |
| 1897 | } |
Fariborz Jahanian | 059e05e | 2013-10-15 00:00:28 +0000 | [diff] [blame] | 1898 | } |
Fariborz Jahanian | 403425b | 2013-10-17 23:13:13 +0000 | [diff] [blame] | 1899 | CacheObjCNSIntegerTypedefed(TD); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1900 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1901 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1902 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
| 1903 | canModify(FD)) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1904 | migrateCFAnnotation(Ctx, FD); |
| 1905 | } |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1906 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1907 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1908 | bool CanModify = canModify(CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1909 | // migrate methods which can have instancetype as their result type. |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1910 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Instancetype) && |
| 1911 | CanModify) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1912 | migrateAllMethodInstaceType(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1913 | // annotate methods with CF annotations. |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1914 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) && |
| 1915 | CanModify) |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1916 | migrateARCSafeAnnotation(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1917 | } |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1918 | |
| 1919 | if (const ObjCImplementationDecl * |
| 1920 | ImplD = dyn_cast<ObjCImplementationDecl>(*D)) { |
Argyrios Kyrtzidis | 3f72934 | 2013-12-11 21:39:00 +0000 | [diff] [blame] | 1921 | if ((ASTMigrateActions & FrontendOptions::ObjCMT_DesignatedInitializer) && |
| 1922 | canModify(ImplD)) |
Argyrios Kyrtzidis | 4f2ecc6 | 2013-12-10 18:36:49 +0000 | [diff] [blame] | 1923 | inferDesignatedInitializers(Ctx, ImplD); |
| 1924 | } |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1925 | } |
Fariborz Jahanian | 8c45e28 | 2013-10-02 22:49:59 +0000 | [diff] [blame] | 1926 | if (ASTMigrateActions & FrontendOptions::ObjCMT_Annotation) |
| 1927 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1928 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1929 | |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1930 | if (IsOutputFile) { |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 1931 | std::error_code EC; |
| 1932 | llvm::raw_fd_ostream OS(MigrateDir, EC, llvm::sys::fs::F_None); |
| 1933 | if (EC) { |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 1934 | DiagnosticsEngine &Diags = Ctx.getDiagnostics(); |
| 1935 | Diags.Report(Diags.getCustomDiagID(DiagnosticsEngine::Error, "%0")) |
Rafael Espindola | dae941a | 2014-08-25 18:17:04 +0000 | [diff] [blame] | 1936 | << EC.message(); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 1937 | return; |
| 1938 | } |
| 1939 | |
| 1940 | JSONEditWriter Writer(Ctx.getSourceManager(), OS); |
| 1941 | Editor->applyRewrites(Writer); |
| 1942 | return; |
| 1943 | } |
| 1944 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1945 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1946 | RewritesReceiver Rec(rewriter); |
| 1947 | Editor->applyRewrites(Rec); |
| 1948 | |
| 1949 | for (Rewriter::buffer_iterator |
| 1950 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 1951 | FileID FID = I->first; |
| 1952 | RewriteBuffer &buf = I->second; |
| 1953 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 1954 | assert(file); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1955 | SmallString<512> newText; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1956 | llvm::raw_svector_ostream vecOS(newText); |
| 1957 | buf.write(vecOS); |
| 1958 | vecOS.flush(); |
Rafael Espindola | 1a1b156 | 2014-08-17 23:12:27 +0000 | [diff] [blame] | 1959 | std::unique_ptr<llvm::MemoryBuffer> memBuf( |
| 1960 | llvm::MemoryBuffer::getMemBufferCopy( |
| 1961 | StringRef(newText.data(), newText.size()), file->getName())); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1962 | SmallString<64> filePath(file->getName()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1963 | FileMgr.FixupRelativePath(filePath); |
Rafael Espindola | 1a1b156 | 2014-08-17 23:12:27 +0000 | [diff] [blame] | 1964 | Remapper.remap(filePath.str(), std::move(memBuf)); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | if (IsOutputFile) { |
| 1968 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 1969 | } else { |
| 1970 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
Argyrios Kyrtzidis | b482260 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 1975 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1976 | return true; |
| 1977 | } |
| 1978 | |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 1979 | static std::vector<std::string> getWhiteListFilenames(StringRef DirPath) { |
| 1980 | using namespace llvm::sys::fs; |
| 1981 | using namespace llvm::sys::path; |
| 1982 | |
| 1983 | std::vector<std::string> Filenames; |
| 1984 | if (DirPath.empty() || !is_directory(DirPath)) |
| 1985 | return Filenames; |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 1986 | |
| 1987 | std::error_code EC; |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 1988 | directory_iterator DI = directory_iterator(DirPath, EC); |
| 1989 | directory_iterator DE; |
| 1990 | for (; !EC && DI != DE; DI = DI.increment(EC)) { |
| 1991 | if (is_regular_file(DI->path())) |
| 1992 | Filenames.push_back(filename(DI->path())); |
| 1993 | } |
| 1994 | |
| 1995 | return Filenames; |
| 1996 | } |
| 1997 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1998 | std::unique_ptr<ASTConsumer> |
| 1999 | MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 2000 | PPConditionalDirectiveRecord * |
| 2001 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 2002 | unsigned ObjCMTAction = CI.getFrontendOpts().ObjCMTAction; |
Argyrios Kyrtzidis | c47c63b | 2013-11-14 16:33:20 +0000 | [diff] [blame] | 2003 | unsigned ObjCMTOpts = ObjCMTAction; |
| 2004 | // These are companion flags, they do not enable transformations. |
| 2005 | ObjCMTOpts &= ~(FrontendOptions::ObjCMT_AtomicProperty | |
| 2006 | FrontendOptions::ObjCMT_NsAtomicIOSOnlyProperty); |
| 2007 | if (ObjCMTOpts == FrontendOptions::ObjCMT_None) { |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 2008 | // If no specific option was given, enable literals+subscripting transforms |
| 2009 | // by default. |
Argyrios Kyrtzidis | c47c63b | 2013-11-14 16:33:20 +0000 | [diff] [blame] | 2010 | ObjCMTAction |= FrontendOptions::ObjCMT_Literals | |
| 2011 | FrontendOptions::ObjCMT_Subscripting; |
Argyrios Kyrtzidis | ee02a8a | 2013-11-13 23:38:22 +0000 | [diff] [blame] | 2012 | } |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 2013 | CI.getPreprocessor().addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec)); |
Argyrios Kyrtzidis | 61f2032 | 2013-11-14 16:33:29 +0000 | [diff] [blame] | 2014 | std::vector<std::string> WhiteList = |
| 2015 | getWhiteListFilenames(CI.getFrontendOpts().ObjCMTWhiteListPath); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 2016 | return llvm::make_unique<ObjCMigrateASTConsumer>( |
| 2017 | CI.getFrontendOpts().OutputFile, ObjCMTAction, Remapper, |
| 2018 | CI.getFileManager(), PPRec, CI.getPreprocessor(), |
| 2019 | /*isOutputFile=*/true, WhiteList); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 2020 | } |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 2021 | |
| 2022 | namespace { |
| 2023 | struct EditEntry { |
| 2024 | const FileEntry *File; |
| 2025 | unsigned Offset; |
| 2026 | unsigned RemoveLen; |
| 2027 | std::string Text; |
| 2028 | |
| 2029 | EditEntry() : File(), Offset(), RemoveLen() {} |
| 2030 | }; |
| 2031 | } |
| 2032 | |
| 2033 | namespace llvm { |
| 2034 | template<> struct DenseMapInfo<EditEntry> { |
| 2035 | static inline EditEntry getEmptyKey() { |
| 2036 | EditEntry Entry; |
| 2037 | Entry.Offset = unsigned(-1); |
| 2038 | return Entry; |
| 2039 | } |
| 2040 | static inline EditEntry getTombstoneKey() { |
| 2041 | EditEntry Entry; |
| 2042 | Entry.Offset = unsigned(-2); |
| 2043 | return Entry; |
| 2044 | } |
| 2045 | static unsigned getHashValue(const EditEntry& Val) { |
| 2046 | llvm::FoldingSetNodeID ID; |
| 2047 | ID.AddPointer(Val.File); |
| 2048 | ID.AddInteger(Val.Offset); |
| 2049 | ID.AddInteger(Val.RemoveLen); |
| 2050 | ID.AddString(Val.Text); |
| 2051 | return ID.ComputeHash(); |
| 2052 | } |
| 2053 | static bool isEqual(const EditEntry &LHS, const EditEntry &RHS) { |
| 2054 | return LHS.File == RHS.File && |
| 2055 | LHS.Offset == RHS.Offset && |
| 2056 | LHS.RemoveLen == RHS.RemoveLen && |
| 2057 | LHS.Text == RHS.Text; |
| 2058 | } |
| 2059 | }; |
| 2060 | } |
| 2061 | |
| 2062 | namespace { |
| 2063 | class RemapFileParser { |
| 2064 | FileManager &FileMgr; |
| 2065 | |
| 2066 | public: |
| 2067 | RemapFileParser(FileManager &FileMgr) : FileMgr(FileMgr) { } |
| 2068 | |
| 2069 | bool parse(StringRef File, SmallVectorImpl<EditEntry> &Entries) { |
| 2070 | using namespace llvm::yaml; |
| 2071 | |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 2072 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr = |
| 2073 | llvm::MemoryBuffer::getFile(File); |
| 2074 | if (!FileBufOrErr) |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 2075 | return true; |
| 2076 | |
| 2077 | llvm::SourceMgr SM; |
Rafael Espindola | 85d7892 | 2014-08-27 19:03:27 +0000 | [diff] [blame] | 2078 | Stream YAMLStream(FileBufOrErr.get()->getMemBufferRef(), SM); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 2079 | document_iterator I = YAMLStream.begin(); |
| 2080 | if (I == YAMLStream.end()) |
| 2081 | return true; |
| 2082 | Node *Root = I->getRoot(); |
| 2083 | if (!Root) |
| 2084 | return true; |
| 2085 | |
| 2086 | SequenceNode *SeqNode = dyn_cast<SequenceNode>(Root); |
| 2087 | if (!SeqNode) |
| 2088 | return true; |
| 2089 | |
| 2090 | for (SequenceNode::iterator |
| 2091 | AI = SeqNode->begin(), AE = SeqNode->end(); AI != AE; ++AI) { |
| 2092 | MappingNode *MapNode = dyn_cast<MappingNode>(&*AI); |
| 2093 | if (!MapNode) |
| 2094 | continue; |
| 2095 | parseEdit(MapNode, Entries); |
| 2096 | } |
| 2097 | |
| 2098 | return false; |
| 2099 | } |
| 2100 | |
| 2101 | private: |
| 2102 | void parseEdit(llvm::yaml::MappingNode *Node, |
| 2103 | SmallVectorImpl<EditEntry> &Entries) { |
| 2104 | using namespace llvm::yaml; |
| 2105 | EditEntry Entry; |
| 2106 | bool Ignore = false; |
| 2107 | |
| 2108 | for (MappingNode::iterator |
| 2109 | KVI = Node->begin(), KVE = Node->end(); KVI != KVE; ++KVI) { |
| 2110 | ScalarNode *KeyString = dyn_cast<ScalarNode>((*KVI).getKey()); |
| 2111 | if (!KeyString) |
| 2112 | continue; |
| 2113 | SmallString<10> KeyStorage; |
| 2114 | StringRef Key = KeyString->getValue(KeyStorage); |
| 2115 | |
| 2116 | ScalarNode *ValueString = dyn_cast<ScalarNode>((*KVI).getValue()); |
| 2117 | if (!ValueString) |
| 2118 | continue; |
| 2119 | SmallString<64> ValueStorage; |
| 2120 | StringRef Val = ValueString->getValue(ValueStorage); |
| 2121 | |
| 2122 | if (Key == "file") { |
| 2123 | const FileEntry *FE = FileMgr.getFile(Val); |
| 2124 | if (!FE) |
| 2125 | Ignore = true; |
| 2126 | Entry.File = FE; |
| 2127 | } else if (Key == "offset") { |
| 2128 | if (Val.getAsInteger(10, Entry.Offset)) |
| 2129 | Ignore = true; |
| 2130 | } else if (Key == "remove") { |
| 2131 | if (Val.getAsInteger(10, Entry.RemoveLen)) |
| 2132 | Ignore = true; |
| 2133 | } else if (Key == "text") { |
| 2134 | Entry.Text = Val; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | if (!Ignore) |
| 2139 | Entries.push_back(Entry); |
| 2140 | } |
| 2141 | }; |
| 2142 | } |
| 2143 | |
| 2144 | static bool reportDiag(const Twine &Err, DiagnosticsEngine &Diag) { |
Alp Toker | 29cb66b | 2014-01-26 06:17:37 +0000 | [diff] [blame] | 2145 | Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0")) |
| 2146 | << Err.str(); |
Hans Wennborg | bf6dda5 | 2013-12-12 02:24:20 +0000 | [diff] [blame] | 2147 | return true; |
| 2148 | } |
| 2149 | |
| 2150 | static std::string applyEditsToTemp(const FileEntry *FE, |
| 2151 | ArrayRef<EditEntry> Edits, |
| 2152 | FileManager &FileMgr, |
| 2153 | DiagnosticsEngine &Diag) { |
| 2154 | using namespace llvm::sys; |
| 2155 | |
| 2156 | SourceManager SM(Diag, FileMgr); |
| 2157 | FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); |
| 2158 | LangOptions LangOpts; |
| 2159 | edit::EditedSource Editor(SM, LangOpts); |
| 2160 | for (ArrayRef<EditEntry>::iterator |
| 2161 | I = Edits.begin(), E = Edits.end(); I != E; ++I) { |
| 2162 | const EditEntry &Entry = *I; |
| 2163 | assert(Entry.File == FE); |
| 2164 | SourceLocation Loc = |
| 2165 | SM.getLocForStartOfFile(FID).getLocWithOffset(Entry.Offset); |
| 2166 | CharSourceRange Range; |
| 2167 | if (Entry.RemoveLen != 0) { |
| 2168 | Range = CharSourceRange::getCharRange(Loc, |
| 2169 | Loc.getLocWithOffset(Entry.RemoveLen)); |
| 2170 | } |
| 2171 | |
| 2172 | edit::Commit commit(Editor); |
| 2173 | if (Range.isInvalid()) { |
| 2174 | commit.insert(Loc, Entry.Text); |
| 2175 | } else if (Entry.Text.empty()) { |
| 2176 | commit.remove(Range); |
| 2177 | } else { |
| 2178 | commit.replace(Range, Entry.Text); |
| 2179 | } |
| 2180 | Editor.commit(commit); |
| 2181 | } |
| 2182 | |
| 2183 | Rewriter rewriter(SM, LangOpts); |
| 2184 | RewritesReceiver Rec(rewriter); |
| 2185 | Editor.applyRewrites(Rec); |
| 2186 | |
| 2187 | const RewriteBuffer *Buf = rewriter.getRewriteBufferFor(FID); |
| 2188 | SmallString<512> NewText; |
| 2189 | llvm::raw_svector_ostream OS(NewText); |
| 2190 | Buf->write(OS); |
| 2191 | OS.flush(); |
| 2192 | |
| 2193 | SmallString<64> TempPath; |
| 2194 | int FD; |
| 2195 | if (fs::createTemporaryFile(path::filename(FE->getName()), |
| 2196 | path::extension(FE->getName()), FD, |
| 2197 | TempPath)) { |
| 2198 | reportDiag("Could not create file: " + TempPath.str(), Diag); |
| 2199 | return std::string(); |
| 2200 | } |
| 2201 | |
| 2202 | llvm::raw_fd_ostream TmpOut(FD, /*shouldClose=*/true); |
| 2203 | TmpOut.write(NewText.data(), NewText.size()); |
| 2204 | TmpOut.close(); |
| 2205 | |
| 2206 | return TempPath.str(); |
| 2207 | } |
| 2208 | |
| 2209 | bool arcmt::getFileRemappingsFromFileList( |
| 2210 | std::vector<std::pair<std::string,std::string> > &remap, |
| 2211 | ArrayRef<StringRef> remapFiles, |
| 2212 | DiagnosticConsumer *DiagClient) { |
| 2213 | bool hasErrorOccurred = false; |
| 2214 | |
| 2215 | FileSystemOptions FSOpts; |
| 2216 | FileManager FileMgr(FSOpts); |
| 2217 | RemapFileParser Parser(FileMgr); |
| 2218 | |
| 2219 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 2220 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags( |
| 2221 | new DiagnosticsEngine(DiagID, new DiagnosticOptions, |
| 2222 | DiagClient, /*ShouldOwnClient=*/false)); |
| 2223 | |
| 2224 | typedef llvm::DenseMap<const FileEntry *, std::vector<EditEntry> > |
| 2225 | FileEditEntriesTy; |
| 2226 | FileEditEntriesTy FileEditEntries; |
| 2227 | |
| 2228 | llvm::DenseSet<EditEntry> EntriesSet; |
| 2229 | |
| 2230 | for (ArrayRef<StringRef>::iterator |
| 2231 | I = remapFiles.begin(), E = remapFiles.end(); I != E; ++I) { |
| 2232 | SmallVector<EditEntry, 16> Entries; |
| 2233 | if (Parser.parse(*I, Entries)) |
| 2234 | continue; |
| 2235 | |
| 2236 | for (SmallVectorImpl<EditEntry>::iterator |
| 2237 | EI = Entries.begin(), EE = Entries.end(); EI != EE; ++EI) { |
| 2238 | EditEntry &Entry = *EI; |
| 2239 | if (!Entry.File) |
| 2240 | continue; |
| 2241 | std::pair<llvm::DenseSet<EditEntry>::iterator, bool> |
| 2242 | Insert = EntriesSet.insert(Entry); |
| 2243 | if (!Insert.second) |
| 2244 | continue; |
| 2245 | |
| 2246 | FileEditEntries[Entry.File].push_back(Entry); |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | for (FileEditEntriesTy::iterator |
| 2251 | I = FileEditEntries.begin(), E = FileEditEntries.end(); I != E; ++I) { |
| 2252 | std::string TempFile = applyEditsToTemp(I->first, I->second, |
| 2253 | FileMgr, *Diags); |
| 2254 | if (TempFile.empty()) { |
| 2255 | hasErrorOccurred = true; |
| 2256 | continue; |
| 2257 | } |
| 2258 | |
| 2259 | remap.push_back(std::make_pair(I->first->getName(), TempFile)); |
| 2260 | } |
| 2261 | |
| 2262 | return hasErrorOccurred; |
| 2263 | } |