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