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