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