blob: b521b560263397e2fb3c2d181115f61be674bafd [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"
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +000027#include "clang/AST/Attr.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000028#include "llvm/ADT/SmallString.h"
29
30using namespace clang;
31using namespace arcmt;
32
33namespace {
34
35class ObjCMigrateASTConsumer : public ASTConsumer {
36 void migrateDecl(Decl *D);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000037 void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D);
Fariborz Jahanian1be01532013-07-12 22:32:19 +000038 void migrateProtocolConformance(ASTContext &Ctx,
39 const ObjCImplementationDecl *ImpDecl);
Fariborz Jahanian92463272013-07-18 20:11:45 +000040 void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
41 const TypedefDecl *TypedefDcl);
Fariborz Jahanian71221352013-07-23 22:42:28 +000042 void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
Fariborz Jahanian670ef262013-07-23 23:34:42 +000043 void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
44 ObjCMethodDecl *OM);
Fariborz Jahanianc4291852013-08-02 18:00:53 +000045 void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +000046 ObjCMethodDecl *OM,
47 ObjCInstanceTypeFamily OIT_Family = OIT_None);
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +000048
49 void migrateFunctionDeclAnnotation(ASTContext &Ctx, FunctionDecl *FuncDecl);
50
51 void migrateObjCMethodDeclAnnotation(ASTContext &Ctx, ObjCMethodDecl *MethodDecl);
Ted Kremenekf7639e12012-03-06 20:06:33 +000052public:
53 std::string MigrateDir;
54 bool MigrateLiterals;
55 bool MigrateSubscripting;
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000056 bool MigrateProperty;
Dmitri Gribenkof8579502013-01-12 19:30:44 +000057 OwningPtr<NSAPI> NSAPIObj;
58 OwningPtr<edit::EditedSource> Editor;
Ted Kremenekf7639e12012-03-06 20:06:33 +000059 FileRemapper &Remapper;
60 FileManager &FileMgr;
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000061 const PPConditionalDirectiveRecord *PPRec;
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000062 Preprocessor &PP;
Ted Kremenekf7639e12012-03-06 20:06:33 +000063 bool IsOutputFile;
Fariborz Jahanian1be01532013-07-12 22:32:19 +000064 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls;
65
Ted Kremenekf7639e12012-03-06 20:06:33 +000066 ObjCMigrateASTConsumer(StringRef migrateDir,
67 bool migrateLiterals,
68 bool migrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000069 bool migrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +000070 FileRemapper &remapper,
71 FileManager &fileMgr,
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000072 const PPConditionalDirectiveRecord *PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000073 Preprocessor &PP,
Ted Kremenekf7639e12012-03-06 20:06:33 +000074 bool isOutputFile = false)
75 : MigrateDir(migrateDir),
76 MigrateLiterals(migrateLiterals),
77 MigrateSubscripting(migrateSubscripting),
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000078 MigrateProperty(migrateProperty),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000079 Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
Ted Kremenekf7639e12012-03-06 20:06:33 +000080 IsOutputFile(isOutputFile) { }
81
82protected:
83 virtual void Initialize(ASTContext &Context) {
84 NSAPIObj.reset(new NSAPI(Context));
85 Editor.reset(new edit::EditedSource(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000086 Context.getLangOpts(),
Ted Kremenekf7639e12012-03-06 20:06:33 +000087 PPRec));
88 }
89
90 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
91 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
92 migrateDecl(*I);
93 return true;
94 }
95 virtual void HandleInterestingDecl(DeclGroupRef DG) {
96 // Ignore decls from the PCH.
97 }
98 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
99 ObjCMigrateASTConsumer::HandleTopLevelDecl(DG);
100 }
101
102 virtual void HandleTranslationUnit(ASTContext &Ctx);
103};
104
105}
106
107ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction,
108 StringRef migrateDir,
109 bool migrateLiterals,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000110 bool migrateSubscripting,
111 bool migrateProperty)
Ted Kremenekf7639e12012-03-06 20:06:33 +0000112 : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
113 MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting),
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000114 MigrateProperty(migrateProperty),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000115 CompInst(0) {
116 if (MigrateDir.empty())
117 MigrateDir = "."; // user current directory if none is given.
118}
119
120ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI,
121 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000122 PPConditionalDirectiveRecord *
123 PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
124 CompInst->getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000125 ASTConsumer *
126 WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
127 ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir,
128 MigrateLiterals,
129 MigrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000130 MigrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +0000131 Remapper,
132 CompInst->getFileManager(),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000133 PPRec,
134 CompInst->getPreprocessor());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000135 ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer };
136 return new MultiplexConsumer(Consumers);
137}
138
139bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) {
140 Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(),
141 /*ignoreIfFilesChanges=*/true);
142 CompInst = &CI;
143 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000144 return true;
145}
146
147namespace {
148class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
149 ObjCMigrateASTConsumer &Consumer;
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000150 ParentMap &PMap;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000151
152public:
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000153 ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
154 : Consumer(consumer), PMap(PMap) { }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000155
156 bool shouldVisitTemplateInstantiations() const { return false; }
157 bool shouldWalkTypesOfTypeLocs() const { return false; }
158
159 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
160 if (Consumer.MigrateLiterals) {
161 edit::Commit commit(*Consumer.Editor);
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000162 edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000163 Consumer.Editor->commit(commit);
164 }
165
166 if (Consumer.MigrateSubscripting) {
167 edit::Commit commit(*Consumer.Editor);
168 edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
169 Consumer.Editor->commit(commit);
170 }
171
172 return true;
173 }
174
175 bool TraverseObjCMessageExpr(ObjCMessageExpr *E) {
176 // Do depth first; we want to rewrite the subexpressions first so that if
177 // we have to move expressions we will move them already rewritten.
178 for (Stmt::child_range range = E->children(); range; ++range)
179 if (!TraverseStmt(*range))
180 return false;
181
182 return WalkUpFromObjCMessageExpr(E);
183 }
184};
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000185
186class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
187 ObjCMigrateASTConsumer &Consumer;
188 OwningPtr<ParentMap> PMap;
189
190public:
191 BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { }
192
193 bool shouldVisitTemplateInstantiations() const { return false; }
194 bool shouldWalkTypesOfTypeLocs() const { return false; }
195
196 bool TraverseStmt(Stmt *S) {
197 PMap.reset(new ParentMap(S));
198 ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
199 return true;
200 }
201};
Ted Kremenekf7639e12012-03-06 20:06:33 +0000202}
203
204void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
205 if (!D)
206 return;
207 if (isa<ObjCMethodDecl>(D))
208 return; // Wait for the ObjC container declaration.
209
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000210 BodyMigrator(*this).TraverseDecl(D);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000211}
212
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000213static void append_attr(std::string &PropertyString, const char *attr,
214 bool GetterHasIsPrefix) {
215 PropertyString += (GetterHasIsPrefix ? ", " : "(");
216 PropertyString += attr;
217 PropertyString += ')';
218}
219
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000220static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter,
221 const ObjCMethodDecl *Setter,
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000222 const NSAPI &NS, edit::Commit &commit,
223 bool GetterHasIsPrefix) {
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000224 ASTContext &Context = NS.getASTContext();
225 std::string PropertyString = "@property";
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000226 std::string PropertyNameString = Getter->getNameAsString();
227 StringRef PropertyName(PropertyNameString);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000228 if (GetterHasIsPrefix) {
229 PropertyString += "(getter=";
230 PropertyString += PropertyNameString;
231 }
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000232 // Short circuit properties that contain the name "delegate" or "dataSource",
233 // or have exact name "target" to have unsafe_unretained attribute.
234 if (PropertyName.equals("target") ||
235 (PropertyName.find("delegate") != StringRef::npos) ||
236 (PropertyName.find("dataSource") != StringRef::npos))
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000237 append_attr(PropertyString, "unsafe_unretained", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000238 else {
239 const ParmVarDecl *argDecl = *Setter->param_begin();
240 QualType ArgType = Context.getCanonicalType(argDecl->getType());
241 Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
242 bool RetainableObject = ArgType->isObjCRetainableType();
243 if (RetainableObject && propertyLifetime == Qualifiers::OCL_Strong) {
244 if (const ObjCObjectPointerType *ObjPtrTy =
245 ArgType->getAs<ObjCObjectPointerType>()) {
246 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
247 if (IDecl &&
248 IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000249 append_attr(PropertyString, "copy", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000250 else
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000251 append_attr(PropertyString, "retain", GetterHasIsPrefix);
252 } else if (GetterHasIsPrefix)
253 PropertyString += ')';
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000254 } else if (propertyLifetime == Qualifiers::OCL_Weak)
255 // TODO. More precise determination of 'weak' attribute requires
256 // looking into setter's implementation for backing weak ivar.
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000257 append_attr(PropertyString, "weak", GetterHasIsPrefix);
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000258 else if (RetainableObject)
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000259 append_attr(PropertyString, "retain", GetterHasIsPrefix);
260 else if (GetterHasIsPrefix)
261 PropertyString += ')';
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000262 }
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000263
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000264 QualType RT = Getter->getResultType();
265 if (!isa<TypedefType>(RT)) {
266 // strip off any ARC lifetime qualifier.
267 QualType CanResultTy = Context.getCanonicalType(RT);
268 if (CanResultTy.getQualifiers().hasObjCLifetime()) {
269 Qualifiers Qs = CanResultTy.getQualifiers();
270 Qs.removeObjCLifetime();
271 RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
272 }
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000273 }
274 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000275 PropertyString += RT.getAsString(Context.getPrintingPolicy());
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000276 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000277 if (GetterHasIsPrefix) {
278 // property name must strip off "is" and lower case the first character
279 // after that; e.g. isContinuous will become continuous.
280 StringRef PropertyNameStringRef(PropertyNameString);
281 PropertyNameStringRef = PropertyNameStringRef.drop_front(2);
282 PropertyNameString = PropertyNameStringRef;
283 std::string NewPropertyNameString = PropertyNameString;
Fariborz Jahaniandb8bf832013-08-08 21:51:06 +0000284 NewPropertyNameString[0] = toLowercase(NewPropertyNameString[0]);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000285 PropertyString += NewPropertyNameString;
286 }
287 else
288 PropertyString += PropertyNameString;
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000289 commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),
290 Getter->getDeclaratorEndLoc()),
291 PropertyString);
292 SourceLocation EndLoc = Setter->getDeclaratorEndLoc();
293 // Get location past ';'
294 EndLoc = EndLoc.getLocWithOffset(1);
295 commit.remove(CharSourceRange::getCharRange(Setter->getLocStart(), EndLoc));
296 return true;
297}
298
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000299void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx,
300 ObjCInterfaceDecl *D) {
301 for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end();
302 M != MEnd; ++M) {
303 ObjCMethodDecl *Method = (*M);
Fariborz Jahaniande661002013-07-03 23:44:11 +0000304 if (Method->isPropertyAccessor() || Method->param_size() != 0)
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000305 continue;
306 // Is this method candidate to be a getter?
Fariborz Jahaniande661002013-07-03 23:44:11 +0000307 QualType GRT = Method->getResultType();
308 if (GRT->isVoidType())
309 continue;
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000310 // FIXME. Don't know what todo with attributes, skip for now.
311 if (Method->hasAttrs())
312 continue;
313
Fariborz Jahaniande661002013-07-03 23:44:11 +0000314 Selector GetterSelector = Method->getSelector();
315 IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
316 Selector SetterSelector =
317 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
318 PP.getSelectorTable(),
319 getterName);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000320 ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true);
321 bool GetterHasIsPrefix = false;
322 if (!SetterMethod) {
323 // try a different naming convention for getter: isXxxxx
324 StringRef getterNameString = getterName->getName();
Fariborz Jahanian261fdb72013-08-08 21:20:01 +0000325 if (getterNameString.startswith("is") && !GRT->isObjCRetainableType()) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000326 GetterHasIsPrefix = true;
327 const char *CGetterName = getterNameString.data() + 2;
Fariborz Jahanian261fdb72013-08-08 21:20:01 +0000328 if (CGetterName[0] && isUppercase(CGetterName[0])) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000329 getterName = &Ctx.Idents.get(CGetterName);
330 SetterSelector =
331 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
332 PP.getSelectorTable(),
333 getterName);
334 SetterMethod = D->lookupMethod(SetterSelector, true);
335 }
336 }
337 }
338 if (SetterMethod) {
Fariborz Jahaniande661002013-07-03 23:44:11 +0000339 // Is this a valid setter, matching the target getter?
340 QualType SRT = SetterMethod->getResultType();
341 if (!SRT->isVoidType())
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000342 continue;
Fariborz Jahaniande661002013-07-03 23:44:11 +0000343 const ParmVarDecl *argDecl = *SetterMethod->param_begin();
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000344 QualType ArgType = argDecl->getType();
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000345 if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) ||
346 SetterMethod->hasAttrs())
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000347 continue;
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000348 edit::Commit commit(*Editor);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000349 rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit,
350 GetterHasIsPrefix);
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000351 Editor->commit(commit);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000352 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000353 }
354}
355
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000356static bool
Fariborz Jahanian9a3512b2013-07-13 17:16:41 +0000357ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000358 const ObjCImplementationDecl *ImpDecl,
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000359 const ObjCInterfaceDecl *IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000360 ObjCProtocolDecl *Protocol) {
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000361 // In auto-synthesis, protocol properties are not synthesized. So,
362 // a conforming protocol must have its required properties declared
363 // in class interface.
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000364 bool HasAtleastOneRequiredProperty = false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000365 if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition())
366 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
367 E = PDecl->prop_end(); P != E; ++P) {
368 ObjCPropertyDecl *Property = *P;
369 if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
370 continue;
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000371 HasAtleastOneRequiredProperty = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000372 DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName());
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000373 if (R.size() == 0) {
374 // Relax the rule and look into class's implementation for a synthesize
375 // or dynamic declaration. Class is implementing a property coming from
376 // another protocol. This still makes the target protocol as conforming.
377 if (!ImpDecl->FindPropertyImplDecl(
378 Property->getDeclName().getAsIdentifierInfo()))
379 return false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000380 }
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000381 else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) {
382 if ((ClassProperty->getPropertyAttributes()
383 != Property->getPropertyAttributes()) ||
384 !Ctx.hasSameType(ClassProperty->getType(), Property->getType()))
385 return false;
386 }
387 else
388 return false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000389 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000390
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000391 // At this point, all required properties in this protocol conform to those
392 // declared in the class.
393 // Check that class implements the required methods of the protocol too.
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000394 bool HasAtleastOneRequiredMethod = false;
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000395 if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) {
396 if (PDecl->meth_begin() == PDecl->meth_end())
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000397 return HasAtleastOneRequiredProperty;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000398 for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(),
399 MEnd = PDecl->meth_end(); M != MEnd; ++M) {
400 ObjCMethodDecl *MD = (*M);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000401 if (MD->isImplicit())
402 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000403 if (MD->getImplementationControl() == ObjCMethodDecl::Optional)
404 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000405 DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName());
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000406 if (R.size() == 0)
407 return false;
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000408 bool match = false;
409 HasAtleastOneRequiredMethod = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000410 for (unsigned I = 0, N = R.size(); I != N; ++I)
411 if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0]))
412 if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) {
413 match = true;
414 break;
415 }
416 if (!match)
417 return false;
418 }
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000419 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000420 if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod)
421 return true;
422 return false;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000423}
424
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000425static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl,
426 llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols,
427 const NSAPI &NS, edit::Commit &commit) {
428 const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols();
429 std::string ClassString;
430 SourceLocation EndLoc =
431 IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation();
432
433 if (Protocols.empty()) {
434 ClassString = '<';
435 for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
436 ClassString += ConformingProtocols[i]->getNameAsString();
437 if (i != (e-1))
438 ClassString += ", ";
439 }
440 ClassString += "> ";
441 }
442 else {
443 ClassString = ", ";
444 for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
445 ClassString += ConformingProtocols[i]->getNameAsString();
446 if (i != (e-1))
447 ClassString += ", ";
448 }
449 ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1;
450 EndLoc = *PL;
451 }
452
453 commit.insertAfterToken(EndLoc, ClassString);
454 return true;
455}
456
457static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
458 const TypedefDecl *TypedefDcl,
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000459 const NSAPI &NS, edit::Commit &commit,
460 bool IsNSIntegerType) {
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000461 std::string ClassString =
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000462 IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " : "typedef NS_OPTIONS(NSUInteger, ";
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000463 ClassString += TypedefDcl->getIdentifier()->getName();
464 ClassString += ')';
465 SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
466 commit.replace(R, ClassString);
467 SourceLocation EndOfTypedefLoc = TypedefDcl->getLocEnd();
468 EndOfTypedefLoc = trans::findLocationAfterSemi(EndOfTypedefLoc, NS.getASTContext());
469 if (!EndOfTypedefLoc.isInvalid()) {
470 commit.remove(SourceRange(TypedefDcl->getLocStart(), EndOfTypedefLoc));
471 return true;
472 }
473 return false;
474}
475
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000476static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl,
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000477 const TypedefDecl *TypedefDcl,
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000478 const NSAPI &NS, edit::Commit &commit,
479 bool IsNSIntegerType) {
480 std::string ClassString =
481 IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, ";
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000482 ClassString += TypedefDcl->getIdentifier()->getName();
483 ClassString += ')';
484 SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
485 commit.replace(R, ClassString);
486 SourceLocation TypedefLoc = TypedefDcl->getLocEnd();
487 commit.remove(SourceRange(TypedefLoc, TypedefLoc));
488 return true;
489}
490
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000491static bool UseNSOptionsMacro(ASTContext &Ctx,
492 const EnumDecl *EnumDcl) {
493 bool PowerOfTwo = true;
494 for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(),
495 EE = EnumDcl->enumerator_end(); EI != EE; ++EI) {
496 EnumConstantDecl *Enumerator = (*EI);
497 const Expr *InitExpr = Enumerator->getInitExpr();
498 if (!InitExpr) {
499 PowerOfTwo = false;
500 continue;
501 }
502 InitExpr = InitExpr->IgnoreImpCasts();
503 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr))
504 if (BO->isShiftOp() || BO->isBitwiseOp())
505 return true;
506
507 uint64_t EnumVal = Enumerator->getInitVal().getZExtValue();
508 if (PowerOfTwo && EnumVal && !llvm::isPowerOf2_64(EnumVal))
509 PowerOfTwo = false;
510 }
511 return PowerOfTwo;
512}
513
Fariborz Jahanian008ef722013-07-19 17:44:32 +0000514void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000515 const ObjCImplementationDecl *ImpDecl) {
516 const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface();
517 if (!IDecl || ObjCProtocolDecls.empty())
518 return;
519 // Find all implicit conforming protocols for this class
520 // and make them explicit.
521 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols;
522 Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols);
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000523 llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols;
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000524
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000525 for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I =
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000526 ObjCProtocolDecls.begin(),
527 E = ObjCProtocolDecls.end(); I != E; ++I)
528 if (!ExplicitProtocols.count(*I))
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000529 PotentialImplicitProtocols.push_back(*I);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000530
531 if (PotentialImplicitProtocols.empty())
532 return;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000533
534 // go through list of non-optional methods and properties in each protocol
535 // in the PotentialImplicitProtocols list. If class implements every one of the
536 // methods and properties, then this class conforms to this protocol.
537 llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols;
538 for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++)
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000539 if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000540 PotentialImplicitProtocols[i]))
541 ConformingProtocols.push_back(PotentialImplicitProtocols[i]);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000542
543 if (ConformingProtocols.empty())
544 return;
Fariborz Jahaniancb7b8de2013-07-17 00:02:22 +0000545
546 // Further reduce number of conforming protocols. If protocol P1 is in the list
547 // protocol P2 (P2<P1>), No need to include P1.
548 llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols;
549 for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
550 bool DropIt = false;
551 ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i];
552 for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) {
553 ObjCProtocolDecl *PDecl = ConformingProtocols[i1];
554 if (PDecl == TargetPDecl)
555 continue;
556 if (PDecl->lookupProtocolNamed(
557 TargetPDecl->getDeclName().getAsIdentifierInfo())) {
558 DropIt = true;
559 break;
560 }
561 }
562 if (!DropIt)
563 MinimalConformingProtocols.push_back(TargetPDecl);
564 }
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000565 edit::Commit commit(*Editor);
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000566 rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols,
567 *NSAPIObj, commit);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000568 Editor->commit(commit);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000569}
570
Fariborz Jahanian92463272013-07-18 20:11:45 +0000571void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx,
572 const EnumDecl *EnumDcl,
573 const TypedefDecl *TypedefDcl) {
574 if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() ||
575 !TypedefDcl->getIdentifier())
576 return;
577
578 QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000579 bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt);
580 bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000581
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000582 if (!IsNSIntegerType && !IsNSUIntegerType) {
583 // Also check for typedef enum {...} TD;
584 if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
585 if (EnumTy->getDecl() == EnumDcl) {
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000586 bool NSOptions = UseNSOptionsMacro(Ctx, EnumDcl);
587 if (NSOptions) {
588 if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition())
589 return;
590 }
591 else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000592 return;
593 edit::Commit commit(*Editor);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000594 rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000595 Editor->commit(commit);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000596 }
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000597 }
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000598 return;
599 }
600 if (IsNSIntegerType && UseNSOptionsMacro(Ctx, EnumDcl)) {
601 // We may still use NS_OPTIONS based on what we find in the enumertor list.
602 IsNSIntegerType = false;
603 IsNSUIntegerType = true;
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000604 }
Fariborz Jahanian92463272013-07-18 20:11:45 +0000605
606 // NS_ENUM must be available.
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000607 if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
608 return;
609 // NS_OPTIONS must be available.
610 if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition())
Fariborz Jahanian92463272013-07-18 20:11:45 +0000611 return;
612 edit::Commit commit(*Editor);
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000613 rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, IsNSIntegerType);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000614 Editor->commit(commit);
615}
616
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000617static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC,
618 ObjCMethodDecl *OM) {
619 SourceRange R;
620 std::string ClassString;
621 if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) {
622 TypeLoc TL = TSInfo->getTypeLoc();
623 R = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
624 ClassString = "instancetype";
625 }
626 else {
627 R = SourceRange(OM->getLocStart(), OM->getLocStart());
628 ClassString = OM->isInstanceMethod() ? '-' : '+';
629 ClassString += " (instancetype)";
630 }
631 edit::Commit commit(*ASTC.Editor);
632 commit.replace(R, ClassString);
633 ASTC.Editor->commit(commit);
634}
635
Fariborz Jahanian670ef262013-07-23 23:34:42 +0000636void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
637 ObjCContainerDecl *CDecl,
638 ObjCMethodDecl *OM) {
Fariborz Jahanian71221352013-07-23 22:42:28 +0000639 ObjCInstanceTypeFamily OIT_Family =
640 Selector::getInstTypeMethodFamily(OM->getSelector());
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000641
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000642 std::string ClassName;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000643 switch (OIT_Family) {
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000644 case OIT_None:
645 migrateFactoryMethod(Ctx, CDecl, OM);
646 return;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000647 case OIT_Array:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000648 ClassName = "NSArray";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000649 break;
650 case OIT_Dictionary:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000651 ClassName = "NSDictionary";
652 break;
653 case OIT_MemManage:
654 ClassName = "NSObject";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000655 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000656 case OIT_Singleton:
657 migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000658 return;
659 }
Fariborz Jahanian71221352013-07-23 22:42:28 +0000660 if (!OM->getResultType()->isObjCIdType())
661 return;
662
663 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
664 if (!IDecl) {
665 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
666 IDecl = CatDecl->getClassInterface();
667 else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
668 IDecl = ImpDecl->getClassInterface();
669 }
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000670 if (!IDecl ||
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000671 !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) {
672 migrateFactoryMethod(Ctx, CDecl, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000673 return;
Fariborz Jahanian280954a2013-07-24 18:31:42 +0000674 }
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000675 ReplaceWithInstancetype(*this, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000676}
677
678void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx,
679 ObjCContainerDecl *CDecl) {
680 // migrate methods which can have instancetype as their result type.
681 for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(),
682 MEnd = CDecl->meth_end();
683 M != MEnd; ++M) {
684 ObjCMethodDecl *Method = (*M);
685 migrateMethodInstanceType(Ctx, CDecl, Method);
686 }
687}
688
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000689void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
690 ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000691 ObjCMethodDecl *OM,
692 ObjCInstanceTypeFamily OIT_Family) {
Fariborz Jahanian3bfc35e2013-08-02 22:34:18 +0000693 if (OM->isInstanceMethod() ||
694 OM->getResultType() == Ctx.getObjCInstanceType() ||
695 !OM->getResultType()->isObjCIdType())
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000696 return;
697
698 // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class
699 // NSYYYNamE with matching names be at least 3 characters long.
700 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
701 if (!IDecl) {
702 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
703 IDecl = CatDecl->getClassInterface();
704 else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
705 IDecl = ImpDecl->getClassInterface();
706 }
707 if (!IDecl)
708 return;
709
710 std::string StringClassName = IDecl->getName();
711 StringRef LoweredClassName(StringClassName);
712 std::string StringLoweredClassName = LoweredClassName.lower();
713 LoweredClassName = StringLoweredClassName;
714
715 IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0);
716 std::string MethodName = MethodIdName->getName();
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000717 if (OIT_Family == OIT_Singleton) {
718 StringRef STRefMethodName(MethodName);
719 size_t len = 0;
720 if (STRefMethodName.startswith("standard"))
721 len = strlen("standard");
722 else if (STRefMethodName.startswith("shared"))
723 len = strlen("shared");
724 else if (STRefMethodName.startswith("default"))
725 len = strlen("default");
726 else
727 return;
728 MethodName = STRefMethodName.substr(len);
729 }
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000730 std::string MethodNameSubStr = MethodName.substr(0, 3);
731 StringRef MethodNamePrefix(MethodNameSubStr);
732 std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower();
733 MethodNamePrefix = StringLoweredMethodNamePrefix;
734 size_t Ix = LoweredClassName.rfind(MethodNamePrefix);
735 if (Ix == StringRef::npos)
736 return;
737 std::string ClassNamePostfix = LoweredClassName.substr(Ix);
738 StringRef LoweredMethodName(MethodName);
739 std::string StringLoweredMethodName = LoweredMethodName.lower();
740 LoweredMethodName = StringLoweredMethodName;
741 if (!LoweredMethodName.startswith(ClassNamePostfix))
742 return;
743 ReplaceWithInstancetype(*this, OM);
744}
745
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +0000746void ObjCMigrateASTConsumer::migrateFunctionDeclAnnotation(
747 ASTContext &Ctx,
748 FunctionDecl *FuncDecl) {
749 if (FuncDecl->hasAttr<CFAuditedTransferAttr>() ||
750 FuncDecl->getAttr<CFReturnsRetainedAttr>() ||
751 FuncDecl->getAttr<CFReturnsNotRetainedAttr>())
752 return;
753}
754
755void ObjCMigrateASTConsumer::migrateObjCMethodDeclAnnotation(
756 ASTContext &Ctx,
757 ObjCMethodDecl *MethodDecl) {
758 if (MethodDecl->hasAttr<CFAuditedTransferAttr>() ||
759 MethodDecl->getAttr<CFReturnsRetainedAttr>() ||
760 MethodDecl->getAttr<CFReturnsNotRetainedAttr>())
761 return;
762}
763
Ted Kremenekf7639e12012-03-06 20:06:33 +0000764namespace {
765
766class RewritesReceiver : public edit::EditsReceiver {
767 Rewriter &Rewrite;
768
769public:
770 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
771
772 virtual void insert(SourceLocation loc, StringRef text) {
773 Rewrite.InsertText(loc, text);
774 }
775 virtual void replace(CharSourceRange range, StringRef text) {
776 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
777 }
778};
779
780}
781
782void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000783
784 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000785 if (MigrateProperty)
786 for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end();
787 D != DEnd; ++D) {
788 if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D))
789 migrateObjCInterfaceDecl(Ctx, CDecl);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000790 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D))
791 ObjCProtocolDecls.insert(PDecl);
792 else if (const ObjCImplementationDecl *ImpDecl =
793 dyn_cast<ObjCImplementationDecl>(*D))
794 migrateProtocolConformance(Ctx, ImpDecl);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000795 else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) {
796 DeclContext::decl_iterator N = D;
797 ++N;
Fariborz Jahanian008ef722013-07-19 17:44:32 +0000798 if (N != DEnd)
799 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N))
800 migrateNSEnumDecl(Ctx, ED, TD);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000801 }
Fariborz Jahanian71221352013-07-23 22:42:28 +0000802 // migrate methods which can have instancetype as their result type.
803 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D))
804 migrateInstanceType(Ctx, CDecl);
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000805 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000806
David Blaikiebbafb8a2012-03-11 07:00:24 +0000807 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000808 RewritesReceiver Rec(rewriter);
809 Editor->applyRewrites(Rec);
810
811 for (Rewriter::buffer_iterator
812 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
813 FileID FID = I->first;
814 RewriteBuffer &buf = I->second;
815 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
816 assert(file);
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000817 SmallString<512> newText;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000818 llvm::raw_svector_ostream vecOS(newText);
819 buf.write(vecOS);
820 vecOS.flush();
821 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
822 StringRef(newText.data(), newText.size()), file->getName());
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000823 SmallString<64> filePath(file->getName());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000824 FileMgr.FixupRelativePath(filePath);
825 Remapper.remap(filePath.str(), memBuf);
826 }
827
828 if (IsOutputFile) {
829 Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics());
830 } else {
831 Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics());
832 }
833}
834
835bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
Argyrios Kyrtzidisb4822602012-05-24 16:48:23 +0000836 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000837 return true;
838}
839
840ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI,
841 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000842 PPConditionalDirectiveRecord *
843 PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
844 CI.getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000845 return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile,
846 /*MigrateLiterals=*/true,
847 /*MigrateSubscripting=*/true,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000848 /*MigrateProperty*/true,
Ted Kremenekf7639e12012-03-06 20:06:33 +0000849 Remapper,
850 CI.getFileManager(),
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000851 PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000852 CI.getPreprocessor(),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000853 /*isOutputFile=*/true);
854}