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" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 11 | #include "clang/ARCMigrate/ARCMTActions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/NSAPI.h" |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ParentMap.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecursiveASTVisitor.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Edit/Commit.h" |
| 19 | #include "clang/Edit/EditedSource.h" |
| 20 | #include "clang/Edit/EditsReceiver.h" |
| 21 | #include "clang/Edit/Rewriters.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
| 23 | #include "clang/Frontend/MultiplexConsumer.h" |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 24 | #include "clang/Lex/PPConditionalDirectiveRecord.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
| 26 | #include "clang/Rewrite/Core/Rewriter.h" |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 28 | #include "clang/StaticAnalyzer/Checkers/ObjCRetainCount.h" |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 29 | #include "clang/AST/Attr.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
| 31 | |
| 32 | using namespace clang; |
| 33 | using namespace arcmt; |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 34 | using namespace ento::objc_retain; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
| 38 | class ObjCMigrateASTConsumer : public ASTConsumer { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 39 | enum CF_BRIDGING_KIND { |
| 40 | CF_BRIDGING_NONE, |
| 41 | CF_BRIDGING_ENABLE, |
| 42 | CF_BRIDGING_MAY_INCLUDE |
| 43 | }; |
| 44 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 45 | void migrateDecl(Decl *D); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 46 | void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 47 | void migrateProtocolConformance(ASTContext &Ctx, |
| 48 | const ObjCImplementationDecl *ImpDecl); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 49 | void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl, |
| 50 | const TypedefDecl *TypedefDcl); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 51 | void migrateMethods(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 52 | void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
| 53 | ObjCMethodDecl *OM); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 54 | bool migrateProperty(ASTContext &Ctx, ObjCInterfaceDecl *D, ObjCMethodDecl *OM); |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 55 | void migrateNsReturnsInnerPointer(ASTContext &Ctx, ObjCMethodDecl *OM); |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 56 | void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 57 | ObjCMethodDecl *OM, |
| 58 | ObjCInstanceTypeFamily OIT_Family = OIT_None); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 59 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 60 | void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 61 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 62 | const FunctionDecl *FuncDecl, bool ResultAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 63 | void AddCFAnnotations(ASTContext &Ctx, const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 64 | const ObjCMethodDecl *MethodDecl, bool ResultAnnotated); |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 65 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 66 | void AnnotateImplicitBridging(ASTContext &Ctx); |
| 67 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 68 | CF_BRIDGING_KIND migrateAddFunctionAnnotation(ASTContext &Ctx, |
| 69 | const FunctionDecl *FuncDecl); |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 70 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 71 | void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl); |
| 72 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 73 | void migrateAddMethodAnnotation(ASTContext &Ctx, |
| 74 | const ObjCMethodDecl *MethodDecl); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 75 | public: |
| 76 | std::string MigrateDir; |
| 77 | bool MigrateLiterals; |
| 78 | bool MigrateSubscripting; |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 79 | bool MigrateProperty; |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 80 | bool MigrateReadonlyProperty; |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 81 | unsigned FileId; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 82 | OwningPtr<NSAPI> NSAPIObj; |
| 83 | OwningPtr<edit::EditedSource> Editor; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 84 | FileRemapper &Remapper; |
| 85 | FileManager &FileMgr; |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 86 | const PPConditionalDirectiveRecord *PPRec; |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 87 | Preprocessor &PP; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 88 | bool IsOutputFile; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 89 | llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls; |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 90 | llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 92 | ObjCMigrateASTConsumer(StringRef migrateDir, |
| 93 | bool migrateLiterals, |
| 94 | bool migrateSubscripting, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 95 | bool migrateProperty, |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 96 | bool migrateReadonlyProperty, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 97 | FileRemapper &remapper, |
| 98 | FileManager &fileMgr, |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 99 | const PPConditionalDirectiveRecord *PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 100 | Preprocessor &PP, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 101 | bool isOutputFile = false) |
| 102 | : MigrateDir(migrateDir), |
| 103 | MigrateLiterals(migrateLiterals), |
| 104 | MigrateSubscripting(migrateSubscripting), |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 105 | MigrateProperty(migrateProperty), |
| 106 | MigrateReadonlyProperty(migrateReadonlyProperty), |
| 107 | FileId(0), Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 108 | IsOutputFile(isOutputFile) { } |
| 109 | |
| 110 | protected: |
| 111 | virtual void Initialize(ASTContext &Context) { |
| 112 | NSAPIObj.reset(new NSAPI(Context)); |
| 113 | Editor.reset(new edit::EditedSource(Context.getSourceManager(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 114 | Context.getLangOpts(), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 115 | PPRec)); |
| 116 | } |
| 117 | |
| 118 | virtual bool HandleTopLevelDecl(DeclGroupRef DG) { |
| 119 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 120 | migrateDecl(*I); |
| 121 | return true; |
| 122 | } |
| 123 | virtual void HandleInterestingDecl(DeclGroupRef DG) { |
| 124 | // Ignore decls from the PCH. |
| 125 | } |
| 126 | virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 127 | ObjCMigrateASTConsumer::HandleTopLevelDecl(DG); |
| 128 | } |
| 129 | |
| 130 | virtual void HandleTranslationUnit(ASTContext &Ctx); |
| 131 | }; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction, |
| 136 | StringRef migrateDir, |
| 137 | bool migrateLiterals, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 138 | bool migrateSubscripting, |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 139 | bool migrateProperty, |
| 140 | bool migrateReadonlyProperty) |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 141 | : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir), |
| 142 | MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting), |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 143 | MigrateProperty(migrateProperty), |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 144 | MigrateReadonlyProperty(migrateReadonlyProperty), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 145 | CompInst(0) { |
| 146 | if (MigrateDir.empty()) |
| 147 | MigrateDir = "."; // user current directory if none is given. |
| 148 | } |
| 149 | |
| 150 | ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI, |
| 151 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 152 | PPConditionalDirectiveRecord * |
| 153 | PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager()); |
| 154 | CompInst->getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 155 | ASTConsumer * |
| 156 | WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile); |
| 157 | ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir, |
| 158 | MigrateLiterals, |
| 159 | MigrateSubscripting, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 160 | MigrateProperty, |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 161 | MigrateReadonlyProperty, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 162 | Remapper, |
| 163 | CompInst->getFileManager(), |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 164 | PPRec, |
| 165 | CompInst->getPreprocessor()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 166 | ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer }; |
| 167 | return new MultiplexConsumer(Consumers); |
| 168 | } |
| 169 | |
| 170 | bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) { |
| 171 | Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(), |
| 172 | /*ignoreIfFilesChanges=*/true); |
| 173 | CompInst = &CI; |
| 174 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
| 177 | |
| 178 | namespace { |
| 179 | class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> { |
| 180 | ObjCMigrateASTConsumer &Consumer; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 181 | ParentMap &PMap; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 182 | |
| 183 | public: |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 184 | ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap) |
| 185 | : Consumer(consumer), PMap(PMap) { } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 186 | |
| 187 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 188 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 189 | |
| 190 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 191 | if (Consumer.MigrateLiterals) { |
| 192 | edit::Commit commit(*Consumer.Editor); |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 193 | edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 194 | Consumer.Editor->commit(commit); |
| 195 | } |
| 196 | |
| 197 | if (Consumer.MigrateSubscripting) { |
| 198 | edit::Commit commit(*Consumer.Editor); |
| 199 | edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit); |
| 200 | Consumer.Editor->commit(commit); |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool TraverseObjCMessageExpr(ObjCMessageExpr *E) { |
| 207 | // Do depth first; we want to rewrite the subexpressions first so that if |
| 208 | // we have to move expressions we will move them already rewritten. |
| 209 | for (Stmt::child_range range = E->children(); range; ++range) |
| 210 | if (!TraverseStmt(*range)) |
| 211 | return false; |
| 212 | |
| 213 | return WalkUpFromObjCMessageExpr(E); |
| 214 | } |
| 215 | }; |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 216 | |
| 217 | class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> { |
| 218 | ObjCMigrateASTConsumer &Consumer; |
| 219 | OwningPtr<ParentMap> PMap; |
| 220 | |
| 221 | public: |
| 222 | BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { } |
| 223 | |
| 224 | bool shouldVisitTemplateInstantiations() const { return false; } |
| 225 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 226 | |
| 227 | bool TraverseStmt(Stmt *S) { |
| 228 | PMap.reset(new ParentMap(S)); |
| 229 | ObjCMigrator(Consumer, *PMap).TraverseStmt(S); |
| 230 | return true; |
| 231 | } |
| 232 | }; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void ObjCMigrateASTConsumer::migrateDecl(Decl *D) { |
| 236 | if (!D) |
| 237 | return; |
| 238 | if (isa<ObjCMethodDecl>(D)) |
| 239 | return; // Wait for the ObjC container declaration. |
| 240 | |
Argyrios Kyrtzidis | 6b4f341 | 2013-01-16 23:54:48 +0000 | [diff] [blame] | 241 | BodyMigrator(*this).TraverseDecl(D); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 244 | static void append_attr(std::string &PropertyString, const char *attr) { |
| 245 | PropertyString += ", "; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 246 | PropertyString += attr; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 249 | static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter, |
| 250 | const ObjCMethodDecl *Setter, |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 251 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 252 | unsigned LengthOfPrefix) { |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 253 | ASTContext &Context = NS.getASTContext(); |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 254 | std::string PropertyString = "@property(nonatomic"; |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 255 | std::string PropertyNameString = Getter->getNameAsString(); |
| 256 | StringRef PropertyName(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 257 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 258 | PropertyString += ", getter="; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 259 | PropertyString += PropertyNameString; |
| 260 | } |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 261 | // Property with no setter may be suggested as a 'readonly' property. |
| 262 | if (!Setter) |
| 263 | append_attr(PropertyString, "readonly"); |
| 264 | |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 265 | // Short circuit properties that contain the name "delegate" or "dataSource", |
| 266 | // or have exact name "target" to have unsafe_unretained attribute. |
| 267 | if (PropertyName.equals("target") || |
| 268 | (PropertyName.find("delegate") != StringRef::npos) || |
| 269 | (PropertyName.find("dataSource") != StringRef::npos)) |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 270 | append_attr(PropertyString, "unsafe_unretained"); |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 271 | else if (Setter) { |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 272 | const ParmVarDecl *argDecl = *Setter->param_begin(); |
| 273 | QualType ArgType = Context.getCanonicalType(argDecl->getType()); |
| 274 | Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime(); |
| 275 | bool RetainableObject = ArgType->isObjCRetainableType(); |
| 276 | if (RetainableObject && propertyLifetime == Qualifiers::OCL_Strong) { |
| 277 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 278 | ArgType->getAs<ObjCObjectPointerType>()) { |
| 279 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 280 | if (IDecl && |
| 281 | IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying"))) |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 282 | append_attr(PropertyString, "copy"); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 283 | else |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 284 | append_attr(PropertyString, "retain"); |
| 285 | } |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 286 | } else if (propertyLifetime == Qualifiers::OCL_Weak) |
| 287 | // TODO. More precise determination of 'weak' attribute requires |
| 288 | // looking into setter's implementation for backing weak ivar. |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 289 | append_attr(PropertyString, "weak"); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 290 | else if (RetainableObject) |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 291 | append_attr(PropertyString, "retain"); |
Fariborz Jahanian | cf387c6 | 2013-08-06 18:06:23 +0000 | [diff] [blame] | 292 | } |
Fariborz Jahanian | 447b15e | 2013-08-21 18:49:03 +0000 | [diff] [blame] | 293 | PropertyString += ')'; |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 294 | |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 295 | QualType RT = Getter->getResultType(); |
| 296 | if (!isa<TypedefType>(RT)) { |
| 297 | // strip off any ARC lifetime qualifier. |
| 298 | QualType CanResultTy = Context.getCanonicalType(RT); |
| 299 | if (CanResultTy.getQualifiers().hasObjCLifetime()) { |
| 300 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 301 | Qs.removeObjCLifetime(); |
| 302 | RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 303 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 304 | } |
| 305 | PropertyString += " "; |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 306 | PropertyString += RT.getAsString(Context.getPrintingPolicy()); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 307 | PropertyString += " "; |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 308 | if (LengthOfPrefix > 0) { |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 309 | // property name must strip off "is" and lower case the first character |
| 310 | // after that; e.g. isContinuous will become continuous. |
| 311 | StringRef PropertyNameStringRef(PropertyNameString); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 312 | PropertyNameStringRef = PropertyNameStringRef.drop_front(LengthOfPrefix); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 313 | PropertyNameString = PropertyNameStringRef; |
| 314 | std::string NewPropertyNameString = PropertyNameString; |
Fariborz Jahanian | db8bf83 | 2013-08-08 21:51:06 +0000 | [diff] [blame] | 315 | NewPropertyNameString[0] = toLowercase(NewPropertyNameString[0]); |
Fariborz Jahanian | cf2ff9b | 2013-08-08 20:51:58 +0000 | [diff] [blame] | 316 | PropertyString += NewPropertyNameString; |
| 317 | } |
| 318 | else |
| 319 | PropertyString += PropertyNameString; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 320 | SourceLocation StartGetterSelectorLoc = Getter->getSelectorStartLoc(); |
| 321 | Selector GetterSelector = Getter->getSelector(); |
| 322 | |
| 323 | SourceLocation EndGetterSelectorLoc = |
| 324 | StartGetterSelectorLoc.getLocWithOffset(GetterSelector.getNameForSlot(0).size()); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 325 | commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(), |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 326 | EndGetterSelectorLoc), |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 327 | PropertyString); |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 328 | if (Setter) { |
| 329 | SourceLocation EndLoc = Setter->getDeclaratorEndLoc(); |
| 330 | // Get location past ';' |
| 331 | EndLoc = EndLoc.getLocWithOffset(1); |
| 332 | commit.remove(CharSourceRange::getCharRange(Setter->getLocStart(), EndLoc)); |
| 333 | } |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 337 | void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx, |
| 338 | ObjCInterfaceDecl *D) { |
| 339 | for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end(); |
| 340 | M != MEnd; ++M) { |
| 341 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 342 | if (!migrateProperty(Ctx, D, Method)) |
| 343 | migrateNsReturnsInnerPointer(Ctx, Method); |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 347 | static bool |
Fariborz Jahanian | 9a3512b | 2013-07-13 17:16:41 +0000 | [diff] [blame] | 348 | ClassImplementsAllMethodsAndProperties(ASTContext &Ctx, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 349 | const ObjCImplementationDecl *ImpDecl, |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 350 | const ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 351 | ObjCProtocolDecl *Protocol) { |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 352 | // In auto-synthesis, protocol properties are not synthesized. So, |
| 353 | // a conforming protocol must have its required properties declared |
| 354 | // in class interface. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 355 | bool HasAtleastOneRequiredProperty = false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 356 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) |
| 357 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 358 | E = PDecl->prop_end(); P != E; ++P) { |
| 359 | ObjCPropertyDecl *Property = *P; |
| 360 | if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 361 | continue; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 362 | HasAtleastOneRequiredProperty = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 363 | DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName()); |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 364 | if (R.size() == 0) { |
| 365 | // Relax the rule and look into class's implementation for a synthesize |
| 366 | // or dynamic declaration. Class is implementing a property coming from |
| 367 | // another protocol. This still makes the target protocol as conforming. |
| 368 | if (!ImpDecl->FindPropertyImplDecl( |
| 369 | Property->getDeclName().getAsIdentifierInfo())) |
| 370 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 371 | } |
Fariborz Jahanian | 2bc3dda | 2013-07-16 21:59:42 +0000 | [diff] [blame] | 372 | else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) { |
| 373 | if ((ClassProperty->getPropertyAttributes() |
| 374 | != Property->getPropertyAttributes()) || |
| 375 | !Ctx.hasSameType(ClassProperty->getType(), Property->getType())) |
| 376 | return false; |
| 377 | } |
| 378 | else |
| 379 | return false; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 380 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 381 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 382 | // At this point, all required properties in this protocol conform to those |
| 383 | // declared in the class. |
| 384 | // Check that class implements the required methods of the protocol too. |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 385 | bool HasAtleastOneRequiredMethod = false; |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 386 | if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) { |
| 387 | if (PDecl->meth_begin() == PDecl->meth_end()) |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 388 | return HasAtleastOneRequiredProperty; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 389 | for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(), |
| 390 | MEnd = PDecl->meth_end(); M != MEnd; ++M) { |
| 391 | ObjCMethodDecl *MD = (*M); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 392 | if (MD->isImplicit()) |
| 393 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 394 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) |
| 395 | continue; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 396 | DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName()); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 397 | if (R.size() == 0) |
| 398 | return false; |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 399 | bool match = false; |
| 400 | HasAtleastOneRequiredMethod = true; |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 401 | for (unsigned I = 0, N = R.size(); I != N; ++I) |
| 402 | if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0])) |
| 403 | if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) { |
| 404 | match = true; |
| 405 | break; |
| 406 | } |
| 407 | if (!match) |
| 408 | return false; |
| 409 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 410 | } |
Fariborz Jahanian | 9e543af | 2013-07-22 23:50:04 +0000 | [diff] [blame] | 411 | if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod) |
| 412 | return true; |
| 413 | return false; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 416 | static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl, |
| 417 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols, |
| 418 | const NSAPI &NS, edit::Commit &commit) { |
| 419 | const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols(); |
| 420 | std::string ClassString; |
| 421 | SourceLocation EndLoc = |
| 422 | IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation(); |
| 423 | |
| 424 | if (Protocols.empty()) { |
| 425 | ClassString = '<'; |
| 426 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 427 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 428 | if (i != (e-1)) |
| 429 | ClassString += ", "; |
| 430 | } |
| 431 | ClassString += "> "; |
| 432 | } |
| 433 | else { |
| 434 | ClassString = ", "; |
| 435 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 436 | ClassString += ConformingProtocols[i]->getNameAsString(); |
| 437 | if (i != (e-1)) |
| 438 | ClassString += ", "; |
| 439 | } |
| 440 | ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1; |
| 441 | EndLoc = *PL; |
| 442 | } |
| 443 | |
| 444 | commit.insertAfterToken(EndLoc, ClassString); |
| 445 | return true; |
| 446 | } |
| 447 | |
| 448 | static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl, |
| 449 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 450 | const NSAPI &NS, edit::Commit &commit, |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 451 | bool IsNSIntegerType, |
| 452 | bool NSOptions) { |
| 453 | std::string ClassString; |
| 454 | if (NSOptions) |
| 455 | ClassString = "typedef NS_OPTIONS(NSUInteger, "; |
| 456 | else |
| 457 | ClassString = |
| 458 | IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " |
| 459 | : "typedef NS_ENUM(NSUInteger, "; |
| 460 | |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 461 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 462 | ClassString += ')'; |
| 463 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 464 | commit.replace(R, ClassString); |
| 465 | SourceLocation EndOfTypedefLoc = TypedefDcl->getLocEnd(); |
| 466 | EndOfTypedefLoc = trans::findLocationAfterSemi(EndOfTypedefLoc, NS.getASTContext()); |
| 467 | if (!EndOfTypedefLoc.isInvalid()) { |
| 468 | commit.remove(SourceRange(TypedefDcl->getLocStart(), EndOfTypedefLoc)); |
| 469 | return true; |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 474 | static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl, |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 475 | const TypedefDecl *TypedefDcl, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 476 | const NSAPI &NS, edit::Commit &commit, |
| 477 | bool IsNSIntegerType) { |
| 478 | std::string ClassString = |
| 479 | IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, "; |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 480 | ClassString += TypedefDcl->getIdentifier()->getName(); |
| 481 | ClassString += ')'; |
| 482 | SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart()); |
| 483 | commit.replace(R, ClassString); |
| 484 | SourceLocation TypedefLoc = TypedefDcl->getLocEnd(); |
| 485 | commit.remove(SourceRange(TypedefLoc, TypedefLoc)); |
| 486 | return true; |
| 487 | } |
| 488 | |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 489 | static bool UseNSOptionsMacro(Preprocessor &PP, ASTContext &Ctx, |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 490 | const EnumDecl *EnumDcl) { |
| 491 | bool PowerOfTwo = true; |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 492 | bool FoundHexdecimalEnumerator = false; |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 493 | uint64_t MaxPowerOfTwoVal = 0; |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 494 | for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(), |
| 495 | EE = EnumDcl->enumerator_end(); EI != EE; ++EI) { |
| 496 | EnumConstantDecl *Enumerator = (*EI); |
| 497 | const Expr *InitExpr = Enumerator->getInitExpr(); |
| 498 | if (!InitExpr) { |
| 499 | PowerOfTwo = false; |
| 500 | continue; |
| 501 | } |
| 502 | InitExpr = InitExpr->IgnoreImpCasts(); |
| 503 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr)) |
| 504 | if (BO->isShiftOp() || BO->isBitwiseOp()) |
| 505 | return true; |
| 506 | |
| 507 | uint64_t EnumVal = Enumerator->getInitVal().getZExtValue(); |
Fariborz Jahanian | be7bc11 | 2013-08-15 18:46:37 +0000 | [diff] [blame] | 508 | if (PowerOfTwo && EnumVal) { |
| 509 | if (!llvm::isPowerOf2_64(EnumVal)) |
| 510 | PowerOfTwo = false; |
| 511 | else if (EnumVal > MaxPowerOfTwoVal) |
| 512 | MaxPowerOfTwoVal = EnumVal; |
| 513 | } |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 514 | if (!FoundHexdecimalEnumerator) { |
| 515 | SourceLocation EndLoc = Enumerator->getLocEnd(); |
| 516 | Token Tok; |
| 517 | if (!PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true)) |
| 518 | if (Tok.isLiteral() && Tok.getLength() > 2) { |
| 519 | if (const char *StringLit = Tok.getLiteralData()) |
| 520 | FoundHexdecimalEnumerator = |
| 521 | (StringLit[0] == '0' && (toLowercase(StringLit[1]) == 'x')); |
| 522 | } |
| 523 | } |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 524 | } |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 525 | return FoundHexdecimalEnumerator || (PowerOfTwo && (MaxPowerOfTwoVal > 2)); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Fariborz Jahanian | 008ef72 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 528 | void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx, |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 529 | const ObjCImplementationDecl *ImpDecl) { |
| 530 | const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface(); |
| 531 | if (!IDecl || ObjCProtocolDecls.empty()) |
| 532 | return; |
| 533 | // Find all implicit conforming protocols for this class |
| 534 | // and make them explicit. |
| 535 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols; |
| 536 | Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols); |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 537 | llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols; |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 538 | |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 539 | for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I = |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 540 | ObjCProtocolDecls.begin(), |
| 541 | E = ObjCProtocolDecls.end(); I != E; ++I) |
| 542 | if (!ExplicitProtocols.count(*I)) |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 543 | PotentialImplicitProtocols.push_back(*I); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 544 | |
| 545 | if (PotentialImplicitProtocols.empty()) |
| 546 | return; |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 547 | |
| 548 | // go through list of non-optional methods and properties in each protocol |
| 549 | // in the PotentialImplicitProtocols list. If class implements every one of the |
| 550 | // methods and properties, then this class conforms to this protocol. |
| 551 | llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols; |
| 552 | for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 553 | if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl, |
Fariborz Jahanian | 9eabf45 | 2013-07-13 00:04:20 +0000 | [diff] [blame] | 554 | PotentialImplicitProtocols[i])) |
| 555 | ConformingProtocols.push_back(PotentialImplicitProtocols[i]); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 556 | |
| 557 | if (ConformingProtocols.empty()) |
| 558 | return; |
Fariborz Jahanian | cb7b8de | 2013-07-17 00:02:22 +0000 | [diff] [blame] | 559 | |
| 560 | // Further reduce number of conforming protocols. If protocol P1 is in the list |
| 561 | // protocol P2 (P2<P1>), No need to include P1. |
| 562 | llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols; |
| 563 | for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) { |
| 564 | bool DropIt = false; |
| 565 | ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i]; |
| 566 | for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) { |
| 567 | ObjCProtocolDecl *PDecl = ConformingProtocols[i1]; |
| 568 | if (PDecl == TargetPDecl) |
| 569 | continue; |
| 570 | if (PDecl->lookupProtocolNamed( |
| 571 | TargetPDecl->getDeclName().getAsIdentifierInfo())) { |
| 572 | DropIt = true; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | if (!DropIt) |
| 577 | MinimalConformingProtocols.push_back(TargetPDecl); |
| 578 | } |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 579 | edit::Commit commit(*Editor); |
Fariborz Jahanian | 85e988b | 2013-07-18 22:17:33 +0000 | [diff] [blame] | 580 | rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols, |
| 581 | *NSAPIObj, commit); |
Fariborz Jahanian | 5bd5aff | 2013-07-16 00:20:21 +0000 | [diff] [blame] | 582 | Editor->commit(commit); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 585 | void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx, |
| 586 | const EnumDecl *EnumDcl, |
| 587 | const TypedefDecl *TypedefDcl) { |
| 588 | if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() || |
| 589 | !TypedefDcl->getIdentifier()) |
| 590 | return; |
| 591 | |
| 592 | QualType qt = TypedefDcl->getTypeSourceInfo()->getType(); |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 593 | bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt); |
| 594 | bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 595 | |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 596 | if (!IsNSIntegerType && !IsNSUIntegerType) { |
| 597 | // Also check for typedef enum {...} TD; |
| 598 | if (const EnumType *EnumTy = qt->getAs<EnumType>()) { |
| 599 | if (EnumTy->getDecl() == EnumDcl) { |
Fariborz Jahanian | a23f4fb | 2013-08-30 00:10:37 +0000 | [diff] [blame] | 600 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 601 | if (NSOptions) { |
| 602 | if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
| 603 | return; |
| 604 | } |
| 605 | else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 606 | return; |
| 607 | edit::Commit commit(*Editor); |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 608 | rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 609 | Editor->commit(commit); |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 610 | } |
Fariborz Jahanian | c1c44f6 | 2013-07-19 20:18:36 +0000 | [diff] [blame] | 611 | } |
Fariborz Jahanian | d0f6f79 | 2013-07-22 18:53:45 +0000 | [diff] [blame] | 612 | return; |
| 613 | } |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 614 | |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 615 | // We may still use NS_OPTIONS based on what we find in the enumertor list. |
| 616 | bool NSOptions = UseNSOptionsMacro(PP, Ctx, EnumDcl); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 617 | // NS_ENUM must be available. |
Fariborz Jahanian | b0057bb | 2013-07-19 01:05:49 +0000 | [diff] [blame] | 618 | if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition()) |
| 619 | return; |
| 620 | // NS_OPTIONS must be available. |
| 621 | if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition()) |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 622 | return; |
| 623 | edit::Commit commit(*Editor); |
Fariborz Jahanian | ff3476e | 2013-08-30 17:46:01 +0000 | [diff] [blame] | 624 | rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, IsNSIntegerType, NSOptions); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 625 | Editor->commit(commit); |
| 626 | } |
| 627 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 628 | static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC, |
| 629 | ObjCMethodDecl *OM) { |
| 630 | SourceRange R; |
| 631 | std::string ClassString; |
| 632 | if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) { |
| 633 | TypeLoc TL = TSInfo->getTypeLoc(); |
| 634 | R = SourceRange(TL.getBeginLoc(), TL.getEndLoc()); |
| 635 | ClassString = "instancetype"; |
| 636 | } |
| 637 | else { |
| 638 | R = SourceRange(OM->getLocStart(), OM->getLocStart()); |
| 639 | ClassString = OM->isInstanceMethod() ? '-' : '+'; |
| 640 | ClassString += " (instancetype)"; |
| 641 | } |
| 642 | edit::Commit commit(*ASTC.Editor); |
| 643 | commit.replace(R, ClassString); |
| 644 | ASTC.Editor->commit(commit); |
| 645 | } |
| 646 | |
Fariborz Jahanian | 670ef26 | 2013-07-23 23:34:42 +0000 | [diff] [blame] | 647 | void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx, |
| 648 | ObjCContainerDecl *CDecl, |
| 649 | ObjCMethodDecl *OM) { |
Fariborz Jahanian | 7dd7143 | 2013-08-28 21:23:00 +0000 | [diff] [blame] | 650 | // bail out early and do not suggest 'instancetype' when the method already |
| 651 | // has a related result type, |
| 652 | if (OM->hasRelatedResultType()) |
| 653 | return; |
| 654 | |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 655 | ObjCInstanceTypeFamily OIT_Family = |
| 656 | Selector::getInstTypeMethodFamily(OM->getSelector()); |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 657 | |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 658 | std::string ClassName; |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 659 | switch (OIT_Family) { |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 660 | case OIT_None: |
| 661 | migrateFactoryMethod(Ctx, CDecl, OM); |
| 662 | return; |
Fariborz Jahanian | 7dd7143 | 2013-08-28 21:23:00 +0000 | [diff] [blame] | 663 | case OIT_Array: |
| 664 | ClassName = "NSArray"; |
| 665 | break; |
| 666 | case OIT_Dictionary: |
| 667 | ClassName = "NSDictionary"; |
| 668 | break; |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 669 | case OIT_Singleton: |
| 670 | migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton); |
Fariborz Jahanian | 631925f | 2013-07-23 23:55:55 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 673 | if (!OM->getResultType()->isObjCIdType()) |
| 674 | return; |
| 675 | |
| 676 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 677 | if (!IDecl) { |
| 678 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 679 | IDecl = CatDecl->getClassInterface(); |
| 680 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 681 | IDecl = ImpDecl->getClassInterface(); |
| 682 | } |
Fariborz Jahanian | 267bae3 | 2013-07-24 19:18:37 +0000 | [diff] [blame] | 683 | if (!IDecl || |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 684 | !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) { |
| 685 | migrateFactoryMethod(Ctx, CDecl, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 686 | return; |
Fariborz Jahanian | 280954a | 2013-07-24 18:31:42 +0000 | [diff] [blame] | 687 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 688 | ReplaceWithInstancetype(*this, OM); |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 691 | static bool TypeIsInnerPointer(QualType T) { |
| 692 | if (!T->isAnyPointerType()) |
| 693 | return false; |
| 694 | if (T->isObjCObjectPointerType() || T->isObjCBuiltinType() || |
| 695 | T->isBlockPointerType() || ento::coreFoundation::isCFObjectRef(T)) |
| 696 | return false; |
Fariborz Jahanian | 9d5fffb | 2013-09-09 23:56:14 +0000 | [diff] [blame] | 697 | // Also, typedef-of-pointer-to-incomplete-struct is something that we assume |
| 698 | // is not an innter pointer type. |
| 699 | QualType OrigT = T; |
| 700 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) |
| 701 | T = TD->getDecl()->getUnderlyingType(); |
| 702 | if (OrigT == T || !T->isPointerType()) |
| 703 | return true; |
| 704 | const PointerType* PT = T->getAs<PointerType>(); |
| 705 | QualType UPointeeT = PT->getPointeeType().getUnqualifiedType(); |
| 706 | if (UPointeeT->isRecordType()) { |
| 707 | const RecordType *RecordTy = UPointeeT->getAs<RecordType>(); |
| 708 | if (!RecordTy->getDecl()->isCompleteDefinition()) |
| 709 | return false; |
| 710 | } |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 714 | bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx, |
| 715 | ObjCInterfaceDecl *D, |
| 716 | ObjCMethodDecl *Method) { |
| 717 | if (Method->isPropertyAccessor() || !Method->isInstanceMethod() || |
| 718 | Method->param_size() != 0) |
| 719 | return false; |
| 720 | // Is this method candidate to be a getter? |
| 721 | QualType GRT = Method->getResultType(); |
| 722 | if (GRT->isVoidType()) |
| 723 | return false; |
| 724 | // FIXME. Don't know what todo with attributes, skip for now. |
| 725 | if (Method->hasAttrs()) |
| 726 | return false; |
| 727 | |
| 728 | Selector GetterSelector = Method->getSelector(); |
| 729 | IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0); |
| 730 | Selector SetterSelector = |
| 731 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 732 | PP.getSelectorTable(), |
| 733 | getterName); |
| 734 | ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 735 | unsigned LengthOfPrefix = 0; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 736 | if (!SetterMethod) { |
| 737 | // try a different naming convention for getter: isXxxxx |
| 738 | StringRef getterNameString = getterName->getName(); |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 739 | bool IsPrefix = getterNameString.startswith("is"); |
| 740 | if ((IsPrefix && !GRT->isObjCRetainableType()) || |
| 741 | getterNameString.startswith("get")) { |
| 742 | LengthOfPrefix = (IsPrefix ? 2 : 3); |
| 743 | const char *CGetterName = getterNameString.data() + LengthOfPrefix; |
| 744 | // Make sure that first character after "is" or "get" prefix can |
| 745 | // start an identifier. |
| 746 | if (!isIdentifierHead(CGetterName[0])) |
| 747 | return false; |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 748 | if (CGetterName[0] && isUppercase(CGetterName[0])) { |
| 749 | getterName = &Ctx.Idents.get(CGetterName); |
| 750 | SetterSelector = |
| 751 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 752 | PP.getSelectorTable(), |
| 753 | getterName); |
| 754 | SetterMethod = D->lookupMethod(SetterSelector, true); |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | if (SetterMethod) { |
| 759 | // Is this a valid setter, matching the target getter? |
| 760 | QualType SRT = SetterMethod->getResultType(); |
| 761 | if (!SRT->isVoidType()) |
| 762 | return false; |
| 763 | const ParmVarDecl *argDecl = *SetterMethod->param_begin(); |
| 764 | QualType ArgType = argDecl->getType(); |
| 765 | if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) || |
| 766 | SetterMethod->hasAttrs()) |
| 767 | return false; |
| 768 | edit::Commit commit(*Editor); |
| 769 | rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit, |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 770 | LengthOfPrefix); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 771 | Editor->commit(commit); |
| 772 | return true; |
| 773 | } |
| 774 | else if (MigrateReadonlyProperty) { |
| 775 | // Try a non-void method with no argument (and no setter or property of same name |
| 776 | // as a 'readonly' property. |
| 777 | edit::Commit commit(*Editor); |
| 778 | rewriteToObjCProperty(Method, 0 /*SetterMethod*/, *NSAPIObj, commit, |
Fariborz Jahanian | ca39960 | 2013-09-11 17:05:15 +0000 | [diff] [blame^] | 779 | LengthOfPrefix); |
Fariborz Jahanian | 215f96c | 2013-09-06 23:45:20 +0000 | [diff] [blame] | 780 | Editor->commit(commit); |
| 781 | return true; |
| 782 | } |
| 783 | return false; |
| 784 | } |
| 785 | |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 786 | void ObjCMigrateASTConsumer::migrateNsReturnsInnerPointer(ASTContext &Ctx, |
| 787 | ObjCMethodDecl *OM) { |
| 788 | if (OM->hasAttr<ObjCReturnsInnerPointerAttr>()) |
| 789 | return; |
| 790 | |
| 791 | QualType RT = OM->getResultType(); |
| 792 | if (!TypeIsInnerPointer(RT) || |
| 793 | !Ctx.Idents.get("NS_RETURNS_INNER_POINTER").hasMacroDefinition()) |
| 794 | return; |
| 795 | |
| 796 | edit::Commit commit(*Editor); |
| 797 | commit.insertBefore(OM->getLocEnd(), " NS_RETURNS_INNER_POINTER"); |
| 798 | Editor->commit(commit); |
| 799 | } |
| 800 | |
| 801 | void ObjCMigrateASTConsumer::migrateMethods(ASTContext &Ctx, |
Fariborz Jahanian | 7122135 | 2013-07-23 22:42:28 +0000 | [diff] [blame] | 802 | ObjCContainerDecl *CDecl) { |
| 803 | // migrate methods which can have instancetype as their result type. |
| 804 | for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), |
| 805 | MEnd = CDecl->meth_end(); |
| 806 | M != MEnd; ++M) { |
| 807 | ObjCMethodDecl *Method = (*M); |
| 808 | migrateMethodInstanceType(Ctx, CDecl, Method); |
| 809 | } |
| 810 | } |
| 811 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 812 | void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx, |
| 813 | ObjCContainerDecl *CDecl, |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 814 | ObjCMethodDecl *OM, |
| 815 | ObjCInstanceTypeFamily OIT_Family) { |
Fariborz Jahanian | 3bfc35e | 2013-08-02 22:34:18 +0000 | [diff] [blame] | 816 | if (OM->isInstanceMethod() || |
| 817 | OM->getResultType() == Ctx.getObjCInstanceType() || |
| 818 | !OM->getResultType()->isObjCIdType()) |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 819 | return; |
| 820 | |
| 821 | // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class |
| 822 | // NSYYYNamE with matching names be at least 3 characters long. |
| 823 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 824 | if (!IDecl) { |
| 825 | if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) |
| 826 | IDecl = CatDecl->getClassInterface(); |
| 827 | else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl)) |
| 828 | IDecl = ImpDecl->getClassInterface(); |
| 829 | } |
| 830 | if (!IDecl) |
| 831 | return; |
| 832 | |
| 833 | std::string StringClassName = IDecl->getName(); |
| 834 | StringRef LoweredClassName(StringClassName); |
| 835 | std::string StringLoweredClassName = LoweredClassName.lower(); |
| 836 | LoweredClassName = StringLoweredClassName; |
| 837 | |
| 838 | IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0); |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 839 | // Handle method with no name at its first selector slot; e.g. + (id):(int)x. |
| 840 | if (!MethodIdName) |
| 841 | return; |
| 842 | |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 843 | std::string MethodName = MethodIdName->getName(); |
Fariborz Jahanian | 9275c68 | 2013-08-02 20:54:18 +0000 | [diff] [blame] | 844 | if (OIT_Family == OIT_Singleton) { |
| 845 | StringRef STRefMethodName(MethodName); |
| 846 | size_t len = 0; |
| 847 | if (STRefMethodName.startswith("standard")) |
| 848 | len = strlen("standard"); |
| 849 | else if (STRefMethodName.startswith("shared")) |
| 850 | len = strlen("shared"); |
| 851 | else if (STRefMethodName.startswith("default")) |
| 852 | len = strlen("default"); |
| 853 | else |
| 854 | return; |
| 855 | MethodName = STRefMethodName.substr(len); |
| 856 | } |
Fariborz Jahanian | c429185 | 2013-08-02 18:00:53 +0000 | [diff] [blame] | 857 | std::string MethodNameSubStr = MethodName.substr(0, 3); |
| 858 | StringRef MethodNamePrefix(MethodNameSubStr); |
| 859 | std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower(); |
| 860 | MethodNamePrefix = StringLoweredMethodNamePrefix; |
| 861 | size_t Ix = LoweredClassName.rfind(MethodNamePrefix); |
| 862 | if (Ix == StringRef::npos) |
| 863 | return; |
| 864 | std::string ClassNamePostfix = LoweredClassName.substr(Ix); |
| 865 | StringRef LoweredMethodName(MethodName); |
| 866 | std::string StringLoweredMethodName = LoweredMethodName.lower(); |
| 867 | LoweredMethodName = StringLoweredMethodName; |
| 868 | if (!LoweredMethodName.startswith(ClassNamePostfix)) |
| 869 | return; |
| 870 | ReplaceWithInstancetype(*this, OM); |
| 871 | } |
| 872 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 873 | static bool IsVoidStarType(QualType Ty) { |
| 874 | if (!Ty->isPointerType()) |
| 875 | return false; |
| 876 | |
| 877 | while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr())) |
| 878 | Ty = TD->getDecl()->getUnderlyingType(); |
| 879 | |
| 880 | // Is the type void*? |
| 881 | const PointerType* PT = Ty->getAs<PointerType>(); |
| 882 | if (PT->getPointeeType().getUnqualifiedType()->isVoidType()) |
| 883 | return true; |
| 884 | return IsVoidStarType(PT->getPointeeType()); |
| 885 | } |
| 886 | |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 887 | /// AuditedType - This routine audits the type AT and returns false if it is one of known |
| 888 | /// CF object types or of the "void *" variety. It returns true if we don't care about the type |
| 889 | /// 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] | 890 | static bool AuditedType (QualType AT) { |
| 891 | if (!AT->isAnyPointerType() && !AT->isBlockPointerType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 892 | return true; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 893 | // 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] | 894 | if (ento::coreFoundation::isCFObjectRef(AT) || |
| 895 | IsVoidStarType(AT) || |
| 896 | // If an ObjC object is type, assuming that it is not a CF function and |
| 897 | // that it is an un-audited function. |
Fariborz Jahanian | 2e9c19c | 2013-08-22 22:27:36 +0000 | [diff] [blame] | 898 | AT->isObjCObjectPointerType() || AT->isObjCBuiltinType()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 899 | return false; |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 900 | // All other pointers are assumed audited as harmless. |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 901 | return true; |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 904 | void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 905 | if (CFFunctionIBCandidates.empty()) |
| 906 | return; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 907 | if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) { |
| 908 | CFFunctionIBCandidates.clear(); |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 909 | FileId = 0; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 910 | return; |
| 911 | } |
| 912 | // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 913 | const Decl *FirstFD = CFFunctionIBCandidates[0]; |
| 914 | const Decl *LastFD = |
| 915 | CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1]; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 916 | const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n"; |
| 917 | edit::Commit commit(*Editor); |
| 918 | commit.insertBefore(FirstFD->getLocStart(), PragmaString); |
| 919 | PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n"; |
| 920 | SourceLocation EndLoc = LastFD->getLocEnd(); |
| 921 | // get location just past end of function location. |
| 922 | EndLoc = PP.getLocForEndOfToken(EndLoc); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 923 | if (isa<FunctionDecl>(LastFD)) { |
| 924 | // For Methods, EndLoc points to the ending semcolon. So, |
| 925 | // not of these extra work is needed. |
| 926 | Token Tok; |
| 927 | // get locaiton of token that comes after end of function. |
| 928 | bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true); |
| 929 | if (!Failed) |
| 930 | EndLoc = Tok.getLocation(); |
| 931 | } |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 932 | commit.insertAfterToken(EndLoc, PragmaString); |
| 933 | Editor->commit(commit); |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 934 | FileId = 0; |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 935 | CFFunctionIBCandidates.clear(); |
| 936 | } |
| 937 | |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 938 | void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) { |
| 939 | if (Decl->hasAttr<CFAuditedTransferAttr>()) { |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 940 | assert(CFFunctionIBCandidates.empty() && |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 941 | "Cannot have audited functions/methods inside user " |
Fariborz Jahanian | c6dfd3f | 2013-08-19 22:00:50 +0000 | [diff] [blame] | 942 | "provided CF_IMPLICIT_BRIDGING_ENABLE"); |
| 943 | return; |
| 944 | } |
| 945 | |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 946 | // Finction must be annotated first. |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 947 | if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl)) { |
| 948 | CF_BRIDGING_KIND AuditKind = migrateAddFunctionAnnotation(Ctx, FuncDecl); |
| 949 | if (AuditKind == CF_BRIDGING_ENABLE) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 950 | CFFunctionIBCandidates.push_back(Decl); |
| 951 | if (!FileId) |
| 952 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()).getHashValue(); |
| 953 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 954 | else if (AuditKind == CF_BRIDGING_MAY_INCLUDE) { |
| 955 | if (!CFFunctionIBCandidates.empty()) { |
| 956 | CFFunctionIBCandidates.push_back(Decl); |
| 957 | if (!FileId) |
| 958 | FileId = PP.getSourceManager().getFileID(Decl->getLocation()).getHashValue(); |
| 959 | } |
| 960 | } |
| 961 | else |
| 962 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 963 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 964 | else { |
| 965 | migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl)); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 966 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 967 | } |
Fariborz Jahanian | 2ec4d7b | 2013-08-16 23:35:05 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 970 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 971 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 972 | const FunctionDecl *FuncDecl, |
| 973 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 974 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 975 | if (!ResultAnnotated) { |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 976 | RetEffect Ret = CE.getReturnValue(); |
| 977 | const char *AnnotationString = 0; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 978 | if (Ret.getObjKind() == RetEffect::CF) { |
| 979 | if (Ret.isOwned() && |
| 980 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 981 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 982 | else if (Ret.notOwned() && |
| 983 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 984 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 985 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 986 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
| 987 | if (Ret.isOwned() && |
| 988 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 989 | AnnotationString = " NS_RETURNS_RETAINED"; |
| 990 | else if (Ret.notOwned() && |
| 991 | Ctx.Idents.get("NS_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
| 992 | AnnotationString = " NS_RETURNS_NOT_RETAINED"; |
| 993 | } |
| 994 | |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 995 | if (AnnotationString) { |
| 996 | edit::Commit commit(*Editor); |
| 997 | commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString); |
| 998 | Editor->commit(commit); |
| 999 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1000 | } |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1001 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1002 | unsigned i = 0; |
| 1003 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1004 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
Fariborz Jahanian | b918d7a | 2013-08-21 19:37:47 +0000 | [diff] [blame] | 1005 | const ParmVarDecl *pd = *pi; |
Fariborz Jahanian | 9ef4a26 | 2013-08-19 19:13:34 +0000 | [diff] [blame] | 1006 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1007 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() && |
| 1008 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1009 | edit::Commit commit(*Editor); |
| 1010 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1011 | Editor->commit(commit); |
Fariborz Jahanian | 9427939 | 2013-08-20 18:54:39 +0000 | [diff] [blame] | 1012 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1013 | else if (AE == DecRefMsg && !pd->getAttr<NSConsumedAttr>() && |
| 1014 | Ctx.Idents.get("NS_CONSUMED").hasMacroDefinition()) { |
| 1015 | edit::Commit commit(*Editor); |
| 1016 | commit.insertBefore(pd->getLocation(), "NS_CONSUMED "); |
| 1017 | Editor->commit(commit); |
| 1018 | } |
Fariborz Jahanian | 84ac1de | 2013-08-15 21:44:38 +0000 | [diff] [blame] | 1019 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | ObjCMigrateASTConsumer::CF_BRIDGING_KIND |
| 1024 | ObjCMigrateASTConsumer::migrateAddFunctionAnnotation( |
| 1025 | ASTContext &Ctx, |
| 1026 | const FunctionDecl *FuncDecl) { |
| 1027 | if (FuncDecl->hasBody()) |
| 1028 | return CF_BRIDGING_NONE; |
| 1029 | |
| 1030 | CallEffects CE = CallEffects::getEffect(FuncDecl); |
| 1031 | bool FuncIsReturnAnnotated = (FuncDecl->getAttr<CFReturnsRetainedAttr>() || |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1032 | FuncDecl->getAttr<CFReturnsNotRetainedAttr>() || |
| 1033 | FuncDecl->getAttr<NSReturnsRetainedAttr>() || |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1034 | FuncDecl->getAttr<NSReturnsNotRetainedAttr>() || |
| 1035 | FuncDecl->getAttr<NSReturnsAutoreleasedAttr>()); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1036 | |
| 1037 | // Trivial case of when funciton is annotated and has no argument. |
| 1038 | if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0) |
| 1039 | return CF_BRIDGING_NONE; |
| 1040 | |
| 1041 | bool ReturnCFAudited = false; |
| 1042 | if (!FuncIsReturnAnnotated) { |
| 1043 | RetEffect Ret = CE.getReturnValue(); |
| 1044 | if (Ret.getObjKind() == RetEffect::CF && |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1045 | (Ret.isOwned() || Ret.notOwned())) |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1046 | ReturnCFAudited = true; |
| 1047 | else if (!AuditedType(FuncDecl->getResultType())) |
| 1048 | return CF_BRIDGING_NONE; |
| 1049 | } |
| 1050 | |
| 1051 | // At this point result type is audited for potential inclusion. |
| 1052 | // Now, how about argument types. |
| 1053 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1054 | unsigned i = 0; |
| 1055 | bool ArgCFAudited = false; |
| 1056 | for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(), |
| 1057 | pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1058 | const ParmVarDecl *pd = *pi; |
| 1059 | ArgEffect AE = AEArgs[i]; |
| 1060 | if (AE == DecRef /*CFConsumed annotated*/ || AE == IncRef) { |
| 1061 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>()) |
| 1062 | ArgCFAudited = true; |
| 1063 | else if (AE == IncRef) |
| 1064 | ArgCFAudited = true; |
| 1065 | } |
| 1066 | else { |
| 1067 | QualType AT = pd->getType(); |
| 1068 | if (!AuditedType(AT)) { |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1069 | AddCFAnnotations(Ctx, CE, FuncDecl, FuncIsReturnAnnotated); |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1070 | return CF_BRIDGING_NONE; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | if (ReturnCFAudited || ArgCFAudited) |
| 1075 | return CF_BRIDGING_ENABLE; |
| 1076 | |
| 1077 | return CF_BRIDGING_MAY_INCLUDE; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1080 | void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx, |
| 1081 | ObjCContainerDecl *CDecl) { |
| 1082 | if (!isa<ObjCInterfaceDecl>(CDecl)) |
| 1083 | return; |
| 1084 | |
| 1085 | // migrate methods which can have instancetype as their result type. |
| 1086 | for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(), |
| 1087 | MEnd = CDecl->meth_end(); |
| 1088 | M != MEnd; ++M) { |
| 1089 | ObjCMethodDecl *Method = (*M); |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1090 | migrateCFAnnotation(Ctx, Method); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1094 | void ObjCMigrateASTConsumer::AddCFAnnotations(ASTContext &Ctx, |
| 1095 | const CallEffects &CE, |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1096 | const ObjCMethodDecl *MethodDecl, |
| 1097 | bool ResultAnnotated) { |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1098 | // Annotate function. |
Fariborz Jahanian | 7ca1d30 | 2013-08-27 23:56:54 +0000 | [diff] [blame] | 1099 | if (!ResultAnnotated) { |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1100 | RetEffect Ret = CE.getReturnValue(); |
| 1101 | const char *AnnotationString = 0; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1102 | if (Ret.getObjKind() == RetEffect::CF) { |
| 1103 | if (Ret.isOwned() && |
| 1104 | Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1105 | AnnotationString = " CF_RETURNS_RETAINED"; |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1106 | else if (Ret.notOwned() && |
| 1107 | Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1108 | AnnotationString = " CF_RETURNS_NOT_RETAINED"; |
| 1109 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1110 | else if (Ret.getObjKind() == RetEffect::ObjC) { |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1111 | ObjCMethodFamily OMF = MethodDecl->getMethodFamily(); |
| 1112 | switch (OMF) { |
| 1113 | case clang::OMF_alloc: |
| 1114 | case clang::OMF_new: |
| 1115 | case clang::OMF_copy: |
| 1116 | case clang::OMF_init: |
| 1117 | case clang::OMF_mutableCopy: |
| 1118 | break; |
| 1119 | |
| 1120 | default: |
| 1121 | if (Ret.isOwned() && |
| 1122 | Ctx.Idents.get("NS_RETURNS_RETAINED").hasMacroDefinition()) |
| 1123 | AnnotationString = " NS_RETURNS_RETAINED"; |
| 1124 | else if (Ret.notOwned() && |
| 1125 | Ctx.Idents.get("NS_RETURNS_NOT_RETAINED").hasMacroDefinition()) |
| 1126 | AnnotationString = " NS_RETURNS_NOT_RETAINED"; |
| 1127 | break; |
| 1128 | } |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1131 | if (AnnotationString) { |
| 1132 | edit::Commit commit(*Editor); |
| 1133 | commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString); |
| 1134 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1135 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1136 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1137 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1138 | unsigned i = 0; |
| 1139 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1140 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1141 | const ParmVarDecl *pd = *pi; |
| 1142 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1143 | if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() && |
| 1144 | Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) { |
| 1145 | edit::Commit commit(*Editor); |
| 1146 | commit.insertBefore(pd->getLocation(), "CF_CONSUMED "); |
| 1147 | Editor->commit(commit); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1148 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1149 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1152 | void ObjCMigrateASTConsumer::migrateAddMethodAnnotation( |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1153 | ASTContext &Ctx, |
| 1154 | const ObjCMethodDecl *MethodDecl) { |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1155 | if (MethodDecl->hasBody() || MethodDecl->isImplicit()) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1156 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1157 | |
| 1158 | CallEffects CE = CallEffects::getEffect(MethodDecl); |
| 1159 | bool MethodIsReturnAnnotated = (MethodDecl->getAttr<CFReturnsRetainedAttr>() || |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1160 | MethodDecl->getAttr<CFReturnsNotRetainedAttr>() || |
| 1161 | MethodDecl->getAttr<NSReturnsRetainedAttr>() || |
Fariborz Jahanian | c24879e | 2013-09-05 23:04:33 +0000 | [diff] [blame] | 1162 | MethodDecl->getAttr<NSReturnsNotRetainedAttr>() || |
| 1163 | MethodDecl->getAttr<NSReturnsAutoreleasedAttr>()); |
| 1164 | |
| 1165 | if (CE.getReceiver() == DecRefMsg && |
| 1166 | !MethodDecl->getAttr<NSConsumesSelfAttr>() && |
| 1167 | MethodDecl->getMethodFamily() != OMF_init && |
| 1168 | MethodDecl->getMethodFamily() != OMF_release && |
| 1169 | Ctx.Idents.get("NS_CONSUMES_SELF").hasMacroDefinition()) { |
| 1170 | edit::Commit commit(*Editor); |
| 1171 | commit.insertBefore(MethodDecl->getLocEnd(), " NS_CONSUMES_SELF"); |
| 1172 | Editor->commit(commit); |
| 1173 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Trivial case of when funciton is annotated and has no argument. |
| 1176 | if (MethodIsReturnAnnotated && |
| 1177 | (MethodDecl->param_begin() == MethodDecl->param_end())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1178 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1180 | if (!MethodIsReturnAnnotated) { |
| 1181 | RetEffect Ret = CE.getReturnValue(); |
Fariborz Jahanian | 1a26927 | 2013-09-04 22:49:19 +0000 | [diff] [blame] | 1182 | if ((Ret.getObjKind() == RetEffect::CF || |
| 1183 | Ret.getObjKind() == RetEffect::ObjC) && |
| 1184 | (Ret.isOwned() || Ret.notOwned())) { |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1185 | AddCFAnnotations(Ctx, CE, MethodDecl, false); |
| 1186 | return; |
| 1187 | } |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1188 | else if (!AuditedType(MethodDecl->getResultType())) |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1189 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // At this point result type is either annotated or audited. |
| 1193 | // Now, how about argument types. |
| 1194 | llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs(); |
| 1195 | unsigned i = 0; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1196 | for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(), |
| 1197 | pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) { |
| 1198 | const ParmVarDecl *pd = *pi; |
| 1199 | ArgEffect AE = AEArgs[i]; |
Fariborz Jahanian | 8b10230 | 2013-09-04 16:43:57 +0000 | [diff] [blame] | 1200 | if ((AE == DecRef && !pd->getAttr<CFConsumedAttr>()) || AE == IncRef || |
| 1201 | !AuditedType(pd->getType())) { |
| 1202 | AddCFAnnotations(Ctx, CE, MethodDecl, MethodIsReturnAnnotated); |
| 1203 | return; |
Fariborz Jahanian | 63ffce2 | 2013-08-27 22:42:30 +0000 | [diff] [blame] | 1204 | } |
| 1205 | } |
Fariborz Jahanian | 89f6d10 | 2013-09-04 00:10:06 +0000 | [diff] [blame] | 1206 | return; |
Fariborz Jahanian | dfe6ed9 | 2013-08-12 23:17:13 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1209 | namespace { |
| 1210 | |
| 1211 | class RewritesReceiver : public edit::EditsReceiver { |
| 1212 | Rewriter &Rewrite; |
| 1213 | |
| 1214 | public: |
| 1215 | RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { } |
| 1216 | |
| 1217 | virtual void insert(SourceLocation loc, StringRef text) { |
| 1218 | Rewrite.InsertText(loc, text); |
| 1219 | } |
| 1220 | virtual void replace(CharSourceRange range, StringRef text) { |
| 1221 | Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text); |
| 1222 | } |
| 1223 | }; |
| 1224 | |
| 1225 | } |
| 1226 | |
| 1227 | void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1228 | |
| 1229 | TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1230 | if (MigrateProperty) { |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1231 | for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); |
| 1232 | D != DEnd; ++D) { |
Fariborz Jahanian | b834352 | 2013-08-20 23:35:26 +0000 | [diff] [blame] | 1233 | if (unsigned FID = |
| 1234 | PP.getSourceManager().getFileID((*D)->getLocation()).getHashValue()) |
| 1235 | if (FileId && FileId != FID) { |
| 1236 | assert(!CFFunctionIBCandidates.empty()); |
| 1237 | AnnotateImplicitBridging(Ctx); |
| 1238 | } |
| 1239 | |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1240 | if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) |
| 1241 | migrateObjCInterfaceDecl(Ctx, CDecl); |
Fariborz Jahanian | 1be0153 | 2013-07-12 22:32:19 +0000 | [diff] [blame] | 1242 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) |
| 1243 | ObjCProtocolDecls.insert(PDecl); |
| 1244 | else if (const ObjCImplementationDecl *ImpDecl = |
| 1245 | dyn_cast<ObjCImplementationDecl>(*D)) |
| 1246 | migrateProtocolConformance(Ctx, ImpDecl); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1247 | else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { |
| 1248 | DeclContext::decl_iterator N = D; |
| 1249 | ++N; |
Fariborz Jahanian | 008ef72 | 2013-07-19 17:44:32 +0000 | [diff] [blame] | 1250 | if (N != DEnd) |
| 1251 | if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N)) |
| 1252 | migrateNSEnumDecl(Ctx, ED, TD); |
Fariborz Jahanian | 9246327 | 2013-07-18 20:11:45 +0000 | [diff] [blame] | 1253 | } |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1254 | else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) |
Fariborz Jahanian | 4f64dd2 | 2013-08-22 21:40:15 +0000 | [diff] [blame] | 1255 | migrateCFAnnotation(Ctx, FD); |
Fariborz Jahanian | c13c1b0 | 2013-08-13 18:01:42 +0000 | [diff] [blame] | 1256 | |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1257 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) { |
| 1258 | // migrate methods which can have instancetype as their result type. |
Fariborz Jahanian | d0fbf6c | 2013-08-30 23:52:08 +0000 | [diff] [blame] | 1259 | migrateMethods(Ctx, CDecl); |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1260 | // annotate methods with CF annotations. |
| 1261 | migrateARCSafeAnnotation(Ctx, CDecl); |
| 1262 | } |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1263 | } |
Fariborz Jahanian | 926fafb | 2013-08-22 18:35:27 +0000 | [diff] [blame] | 1264 | AnnotateImplicitBridging(Ctx); |
Fariborz Jahanian | 301b521 | 2013-08-20 22:42:13 +0000 | [diff] [blame] | 1265 | } |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1266 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1267 | Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1268 | RewritesReceiver Rec(rewriter); |
| 1269 | Editor->applyRewrites(Rec); |
| 1270 | |
| 1271 | for (Rewriter::buffer_iterator |
| 1272 | I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { |
| 1273 | FileID FID = I->first; |
| 1274 | RewriteBuffer &buf = I->second; |
| 1275 | const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); |
| 1276 | assert(file); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1277 | SmallString<512> newText; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1278 | llvm::raw_svector_ostream vecOS(newText); |
| 1279 | buf.write(vecOS); |
| 1280 | vecOS.flush(); |
| 1281 | llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( |
| 1282 | StringRef(newText.data(), newText.size()), file->getName()); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1283 | SmallString<64> filePath(file->getName()); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1284 | FileMgr.FixupRelativePath(filePath); |
| 1285 | Remapper.remap(filePath.str(), memBuf); |
| 1286 | } |
| 1287 | |
| 1288 | if (IsOutputFile) { |
| 1289 | Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); |
| 1290 | } else { |
| 1291 | Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) { |
Argyrios Kyrtzidis | b482260 | 2012-05-24 16:48:23 +0000 | [diff] [blame] | 1296 | CI.getDiagnostics().setIgnoreAllWarnings(true); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1297 | return true; |
| 1298 | } |
| 1299 | |
| 1300 | ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI, |
| 1301 | StringRef InFile) { |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1302 | PPConditionalDirectiveRecord * |
| 1303 | PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager()); |
| 1304 | CI.getPreprocessor().addPPCallbacks(PPRec); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1305 | return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile, |
| 1306 | /*MigrateLiterals=*/true, |
| 1307 | /*MigrateSubscripting=*/true, |
Fariborz Jahanian | d83ef84 | 2013-07-09 16:59:14 +0000 | [diff] [blame] | 1308 | /*MigrateProperty*/true, |
Fariborz Jahanian | 55d6e6c | 2013-08-28 23:22:46 +0000 | [diff] [blame] | 1309 | /*MigrateReadonlyProperty*/true, |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1310 | Remapper, |
| 1311 | CI.getFileManager(), |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1312 | PPRec, |
Fariborz Jahanian | a7437f0 | 2013-07-03 23:05:00 +0000 | [diff] [blame] | 1313 | CI.getPreprocessor(), |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 1314 | /*isOutputFile=*/true); |
| 1315 | } |