blob: 14d5cf6112b00fab16b6dd918d04b09c53c55eaf [file] [log] [blame]
Ted Kremenekf7639e12012-03-06 20:06:33 +00001//===--- 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 Jahanian85e988b2013-07-18 22:17:33 +000010#include "Transforms.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000011#include "clang/ARCMigrate/ARCMTActions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "clang/AST/ASTConsumer.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/NSAPI.h"
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +000015#include "clang/AST/ParentMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#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 Kremenekf7639e12012-03-06 20:06:33 +000022#include "clang/Frontend/CompilerInstance.h"
23#include "clang/Frontend/MultiplexConsumer.h"
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000024#include "clang/Lex/PPConditionalDirectiveRecord.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000025#include "clang/Lex/Preprocessor.h"
26#include "clang/Rewrite/Core/Rewriter.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000027#include "llvm/ADT/SmallString.h"
28
29using namespace clang;
30using namespace arcmt;
31
32namespace {
33
34class ObjCMigrateASTConsumer : public ASTConsumer {
35 void migrateDecl(Decl *D);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000036 void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D);
Fariborz Jahanian1be01532013-07-12 22:32:19 +000037 void migrateProtocolConformance(ASTContext &Ctx,
38 const ObjCImplementationDecl *ImpDecl);
Fariborz Jahanian92463272013-07-18 20:11:45 +000039 void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
40 const TypedefDecl *TypedefDcl);
Fariborz Jahanian71221352013-07-23 22:42:28 +000041 void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
Fariborz Jahanian670ef262013-07-23 23:34:42 +000042 void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
43 ObjCMethodDecl *OM);
Fariborz Jahanianc4291852013-08-02 18:00:53 +000044 void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +000045 ObjCMethodDecl *OM,
46 ObjCInstanceTypeFamily OIT_Family = OIT_None);
Ted Kremenekf7639e12012-03-06 20:06:33 +000047
48public:
49 std::string MigrateDir;
50 bool MigrateLiterals;
51 bool MigrateSubscripting;
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000052 bool MigrateProperty;
Dmitri Gribenkof8579502013-01-12 19:30:44 +000053 OwningPtr<NSAPI> NSAPIObj;
54 OwningPtr<edit::EditedSource> Editor;
Ted Kremenekf7639e12012-03-06 20:06:33 +000055 FileRemapper &Remapper;
56 FileManager &FileMgr;
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000057 const PPConditionalDirectiveRecord *PPRec;
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000058 Preprocessor &PP;
Ted Kremenekf7639e12012-03-06 20:06:33 +000059 bool IsOutputFile;
Fariborz Jahanian1be01532013-07-12 22:32:19 +000060 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls;
61
Ted Kremenekf7639e12012-03-06 20:06:33 +000062 ObjCMigrateASTConsumer(StringRef migrateDir,
63 bool migrateLiterals,
64 bool migrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000065 bool migrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +000066 FileRemapper &remapper,
67 FileManager &fileMgr,
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000068 const PPConditionalDirectiveRecord *PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000069 Preprocessor &PP,
Ted Kremenekf7639e12012-03-06 20:06:33 +000070 bool isOutputFile = false)
71 : MigrateDir(migrateDir),
72 MigrateLiterals(migrateLiterals),
73 MigrateSubscripting(migrateSubscripting),
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000074 MigrateProperty(migrateProperty),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000075 Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
Ted Kremenekf7639e12012-03-06 20:06:33 +000076 IsOutputFile(isOutputFile) { }
77
78protected:
79 virtual void Initialize(ASTContext &Context) {
80 NSAPIObj.reset(new NSAPI(Context));
81 Editor.reset(new edit::EditedSource(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000082 Context.getLangOpts(),
Ted Kremenekf7639e12012-03-06 20:06:33 +000083 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
103ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction,
104 StringRef migrateDir,
105 bool migrateLiterals,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000106 bool migrateSubscripting,
107 bool migrateProperty)
Ted Kremenekf7639e12012-03-06 20:06:33 +0000108 : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
109 MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting),
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000110 MigrateProperty(migrateProperty),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000111 CompInst(0) {
112 if (MigrateDir.empty())
113 MigrateDir = "."; // user current directory if none is given.
114}
115
116ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI,
117 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000118 PPConditionalDirectiveRecord *
119 PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
120 CompInst->getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000121 ASTConsumer *
122 WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
123 ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir,
124 MigrateLiterals,
125 MigrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000126 MigrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +0000127 Remapper,
128 CompInst->getFileManager(),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000129 PPRec,
130 CompInst->getPreprocessor());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000131 ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer };
132 return new MultiplexConsumer(Consumers);
133}
134
135bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) {
136 Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(),
137 /*ignoreIfFilesChanges=*/true);
138 CompInst = &CI;
139 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000140 return true;
141}
142
143namespace {
144class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
145 ObjCMigrateASTConsumer &Consumer;
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000146 ParentMap &PMap;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000147
148public:
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000149 ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
150 : Consumer(consumer), PMap(PMap) { }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000151
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 Kyrtzidis6b4f3412013-01-16 23:54:48 +0000158 edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000159 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 Kyrtzidis6b4f3412013-01-16 23:54:48 +0000181
182class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
183 ObjCMigrateASTConsumer &Consumer;
184 OwningPtr<ParentMap> PMap;
185
186public:
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 Kremenekf7639e12012-03-06 20:06:33 +0000198}
199
200void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
201 if (!D)
202 return;
203 if (isa<ObjCMethodDecl>(D))
204 return; // Wait for the ObjC container declaration.
205
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000206 BodyMigrator(*this).TraverseDecl(D);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000207}
208
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000209static void append_attr(std::string &PropertyString, const char *attr,
210 bool GetterHasIsPrefix) {
211 PropertyString += (GetterHasIsPrefix ? ", " : "(");
212 PropertyString += attr;
213 PropertyString += ')';
214}
215
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000216static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter,
217 const ObjCMethodDecl *Setter,
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000218 const NSAPI &NS, edit::Commit &commit,
219 bool GetterHasIsPrefix) {
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000220 ASTContext &Context = NS.getASTContext();
221 std::string PropertyString = "@property";
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000222 std::string PropertyNameString = Getter->getNameAsString();
223 StringRef PropertyName(PropertyNameString);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000224 if (GetterHasIsPrefix) {
225 PropertyString += "(getter=";
226 PropertyString += PropertyNameString;
227 }
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000228 // 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 Jahaniancf2ff9b2013-08-08 20:51:58 +0000233 append_attr(PropertyString, "unsafe_unretained", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000234 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 Jahaniancf2ff9b2013-08-08 20:51:58 +0000245 append_attr(PropertyString, "copy", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000246 else
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000247 append_attr(PropertyString, "retain", GetterHasIsPrefix);
248 } else if (GetterHasIsPrefix)
249 PropertyString += ')';
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000250 } 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 Jahaniancf2ff9b2013-08-08 20:51:58 +0000253 append_attr(PropertyString, "weak", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000254 else if (RetainableObject)
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000255 append_attr(PropertyString, "retain", GetterHasIsPrefix);
256 else if (GetterHasIsPrefix)
257 PropertyString += ')';
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000258 }
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000259
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000260 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 Jahanian85e988b2013-07-18 22:17:33 +0000269 }
270 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000271 PropertyString += RT.getAsString(Context.getPrintingPolicy());
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000272 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000273 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 Jahanian85e988b2013-07-18 22:17:33 +0000285 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 Jahaniana7437f02013-07-03 23:05:00 +0000295void 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 Jahaniande661002013-07-03 23:44:11 +0000300 if (Method->isPropertyAccessor() || Method->param_size() != 0)
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000301 continue;
302 // Is this method candidate to be a getter?
Fariborz Jahaniande661002013-07-03 23:44:11 +0000303 QualType GRT = Method->getResultType();
304 if (GRT->isVoidType())
305 continue;
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000306 // FIXME. Don't know what todo with attributes, skip for now.
307 if (Method->hasAttrs())
308 continue;
309
Fariborz Jahaniande661002013-07-03 23:44:11 +0000310 Selector GetterSelector = Method->getSelector();
311 IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
312 Selector SetterSelector =
313 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
314 PP.getSelectorTable(),
315 getterName);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000316 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 Jahanian261fdb72013-08-08 21:20:01 +0000321 if (getterNameString.startswith("is") && !GRT->isObjCRetainableType()) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000322 GetterHasIsPrefix = true;
323 const char *CGetterName = getterNameString.data() + 2;
Fariborz Jahanian261fdb72013-08-08 21:20:01 +0000324 if (CGetterName[0] && isUppercase(CGetterName[0])) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000325 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 Jahaniande661002013-07-03 23:44:11 +0000335 // Is this a valid setter, matching the target getter?
336 QualType SRT = SetterMethod->getResultType();
337 if (!SRT->isVoidType())
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000338 continue;
Fariborz Jahaniande661002013-07-03 23:44:11 +0000339 const ParmVarDecl *argDecl = *SetterMethod->param_begin();
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000340 QualType ArgType = argDecl->getType();
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000341 if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) ||
342 SetterMethod->hasAttrs())
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000343 continue;
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000344 edit::Commit commit(*Editor);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000345 rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit,
346 GetterHasIsPrefix);
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000347 Editor->commit(commit);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000348 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000349 }
350}
351
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000352static bool
Fariborz Jahanian9a3512b2013-07-13 17:16:41 +0000353ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000354 const ObjCImplementationDecl *ImpDecl,
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000355 const ObjCInterfaceDecl *IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000356 ObjCProtocolDecl *Protocol) {
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000357 // In auto-synthesis, protocol properties are not synthesized. So,
358 // a conforming protocol must have its required properties declared
359 // in class interface.
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000360 bool HasAtleastOneRequiredProperty = false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000361 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 Jahanian9e543af2013-07-22 23:50:04 +0000367 HasAtleastOneRequiredProperty = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000368 DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName());
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000369 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 Jahaniand36150d2013-07-15 21:22:08 +0000376 }
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000377 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 Jahaniand36150d2013-07-15 21:22:08 +0000385 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000386
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000387 // 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 Jahanian9e543af2013-07-22 23:50:04 +0000390 bool HasAtleastOneRequiredMethod = false;
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000391 if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) {
392 if (PDecl->meth_begin() == PDecl->meth_end())
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000393 return HasAtleastOneRequiredProperty;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000394 for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(),
395 MEnd = PDecl->meth_end(); M != MEnd; ++M) {
396 ObjCMethodDecl *MD = (*M);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000397 if (MD->isImplicit())
398 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000399 if (MD->getImplementationControl() == ObjCMethodDecl::Optional)
400 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000401 DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName());
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000402 if (R.size() == 0)
403 return false;
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000404 bool match = false;
405 HasAtleastOneRequiredMethod = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000406 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 Jahanian5bd5aff2013-07-16 00:20:21 +0000415 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000416 if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod)
417 return true;
418 return false;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000419}
420
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000421static 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
453static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
454 const TypedefDecl *TypedefDcl,
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000455 const NSAPI &NS, edit::Commit &commit,
456 bool IsNSIntegerType) {
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000457 std::string ClassString =
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000458 IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " : "typedef NS_OPTIONS(NSUInteger, ";
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000459 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 Jahaniand0f6f792013-07-22 18:53:45 +0000472static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl,
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000473 const TypedefDecl *TypedefDcl,
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000474 const NSAPI &NS, edit::Commit &commit,
475 bool IsNSIntegerType) {
476 std::string ClassString =
477 IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, ";
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000478 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 Jahaniand0f6f792013-07-22 18:53:45 +0000487static 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 Jahanian008ef722013-07-19 17:44:32 +0000510void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000511 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 Jahanian9eabf452013-07-13 00:04:20 +0000519 llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols;
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000520
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000521 for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I =
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000522 ObjCProtocolDecls.begin(),
523 E = ObjCProtocolDecls.end(); I != E; ++I)
524 if (!ExplicitProtocols.count(*I))
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000525 PotentialImplicitProtocols.push_back(*I);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000526
527 if (PotentialImplicitProtocols.empty())
528 return;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000529
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 Jahaniand36150d2013-07-15 21:22:08 +0000535 if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000536 PotentialImplicitProtocols[i]))
537 ConformingProtocols.push_back(PotentialImplicitProtocols[i]);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000538
539 if (ConformingProtocols.empty())
540 return;
Fariborz Jahaniancb7b8de2013-07-17 00:02:22 +0000541
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 Jahanian5bd5aff2013-07-16 00:20:21 +0000561 edit::Commit commit(*Editor);
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000562 rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols,
563 *NSAPIObj, commit);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000564 Editor->commit(commit);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000565}
566
Fariborz Jahanian92463272013-07-18 20:11:45 +0000567void 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 Jahanianb0057bb2013-07-19 01:05:49 +0000575 bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt);
576 bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000577
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000578 if (!IsNSIntegerType && !IsNSUIntegerType) {
579 // Also check for typedef enum {...} TD;
580 if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
581 if (EnumTy->getDecl() == EnumDcl) {
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000582 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 Jahanianc1c44f62013-07-19 20:18:36 +0000588 return;
589 edit::Commit commit(*Editor);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000590 rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000591 Editor->commit(commit);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000592 }
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000593 }
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000594 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 Jahanianc1c44f62013-07-19 20:18:36 +0000600 }
Fariborz Jahanian92463272013-07-18 20:11:45 +0000601
602 // NS_ENUM must be available.
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000603 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 Jahanian92463272013-07-18 20:11:45 +0000607 return;
608 edit::Commit commit(*Editor);
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000609 rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, IsNSIntegerType);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000610 Editor->commit(commit);
611}
612
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000613static 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 Jahanian670ef262013-07-23 23:34:42 +0000632void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
633 ObjCContainerDecl *CDecl,
634 ObjCMethodDecl *OM) {
Fariborz Jahanian71221352013-07-23 22:42:28 +0000635 ObjCInstanceTypeFamily OIT_Family =
636 Selector::getInstTypeMethodFamily(OM->getSelector());
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000637
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000638 std::string ClassName;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000639 switch (OIT_Family) {
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000640 case OIT_None:
641 migrateFactoryMethod(Ctx, CDecl, OM);
642 return;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000643 case OIT_Array:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000644 ClassName = "NSArray";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000645 break;
646 case OIT_Dictionary:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000647 ClassName = "NSDictionary";
648 break;
649 case OIT_MemManage:
650 ClassName = "NSObject";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000651 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000652 case OIT_Singleton:
653 migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000654 return;
655 }
Fariborz Jahanian71221352013-07-23 22:42:28 +0000656 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 Jahanian267bae32013-07-24 19:18:37 +0000666 if (!IDecl ||
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000667 !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) {
668 migrateFactoryMethod(Ctx, CDecl, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000669 return;
Fariborz Jahanian280954a2013-07-24 18:31:42 +0000670 }
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000671 ReplaceWithInstancetype(*this, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000672}
673
674void 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 Jahanianc4291852013-08-02 18:00:53 +0000685void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
686 ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000687 ObjCMethodDecl *OM,
688 ObjCInstanceTypeFamily OIT_Family) {
Fariborz Jahanian3bfc35e2013-08-02 22:34:18 +0000689 if (OM->isInstanceMethod() ||
690 OM->getResultType() == Ctx.getObjCInstanceType() ||
691 !OM->getResultType()->isObjCIdType())
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000692 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 Jahanian9275c682013-08-02 20:54:18 +0000713 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 Jahanianc4291852013-08-02 18:00:53 +0000726 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 Kremenekf7639e12012-03-06 20:06:33 +0000742namespace {
743
744class RewritesReceiver : public edit::EditsReceiver {
745 Rewriter &Rewrite;
746
747public:
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
760void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000761
762 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000763 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 Jahanian1be01532013-07-12 22:32:19 +0000768 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 Jahanian92463272013-07-18 20:11:45 +0000773 else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) {
774 DeclContext::decl_iterator N = D;
775 ++N;
Fariborz Jahanian008ef722013-07-19 17:44:32 +0000776 if (N != DEnd)
777 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N))
778 migrateNSEnumDecl(Ctx, ED, TD);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000779 }
Fariborz Jahanian71221352013-07-23 22:42:28 +0000780 // migrate methods which can have instancetype as their result type.
781 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D))
782 migrateInstanceType(Ctx, CDecl);
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000783 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000784
David Blaikiebbafb8a2012-03-11 07:00:24 +0000785 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000786 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 Gribenkof8579502013-01-12 19:30:44 +0000795 SmallString<512> newText;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000796 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 Gribenkof8579502013-01-12 19:30:44 +0000801 SmallString<64> filePath(file->getName());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000802 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
813bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
Argyrios Kyrtzidisb4822602012-05-24 16:48:23 +0000814 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000815 return true;
816}
817
818ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI,
819 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000820 PPConditionalDirectiveRecord *
821 PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
822 CI.getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000823 return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile,
824 /*MigrateLiterals=*/true,
825 /*MigrateSubscripting=*/true,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000826 /*MigrateProperty*/true,
Ted Kremenekf7639e12012-03-06 20:06:33 +0000827 Remapper,
828 CI.getFileManager(),
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000829 PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000830 CI.getPreprocessor(),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000831 /*isOutputFile=*/true);
832}