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