blob: eed3805c65729b628e3f4c3810748af97c0a05b6 [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 Jahanian9ef4a262013-08-19 19:13:34 +000027#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Fariborz Jahanian84ac1de2013-08-15 21:44:38 +000028#include "clang/StaticAnalyzer/Checkers/ObjCRetainCount.h"
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +000029#include "clang/AST/Attr.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000030#include "llvm/ADT/SmallString.h"
31
32using namespace clang;
33using namespace arcmt;
Fariborz Jahanian84ac1de2013-08-15 21:44:38 +000034using namespace ento::objc_retain;
Ted Kremenekf7639e12012-03-06 20:06:33 +000035
36namespace {
37
38class ObjCMigrateASTConsumer : public ASTConsumer {
39 void migrateDecl(Decl *D);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000040 void migrateObjCInterfaceDecl(ASTContext &Ctx, ObjCInterfaceDecl *D);
Fariborz Jahanian1be01532013-07-12 22:32:19 +000041 void migrateProtocolConformance(ASTContext &Ctx,
42 const ObjCImplementationDecl *ImpDecl);
Fariborz Jahanian92463272013-07-18 20:11:45 +000043 void migrateNSEnumDecl(ASTContext &Ctx, const EnumDecl *EnumDcl,
44 const TypedefDecl *TypedefDcl);
Fariborz Jahanian71221352013-07-23 22:42:28 +000045 void migrateInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl);
Fariborz Jahanian670ef262013-07-23 23:34:42 +000046 void migrateMethodInstanceType(ASTContext &Ctx, ObjCContainerDecl *CDecl,
47 ObjCMethodDecl *OM);
Fariborz Jahanianc4291852013-08-02 18:00:53 +000048 void migrateFactoryMethod(ASTContext &Ctx, ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +000049 ObjCMethodDecl *OM,
50 ObjCInstanceTypeFamily OIT_Family = OIT_None);
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +000051
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +000052 void migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl);
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +000053
Fariborz Jahanian301b5212013-08-20 22:42:13 +000054 void AnnotateImplicitBridging(ASTContext &Ctx);
55
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +000056 bool migrateAddFunctionAnnotation(ASTContext &Ctx,
57 const FunctionDecl *FuncDecl);
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +000058
Fariborz Jahanian926fafb2013-08-22 18:35:27 +000059 void migrateARCSafeAnnotation(ASTContext &Ctx, ObjCContainerDecl *CDecl);
60
61 bool migrateAddMethodAnnotation(ASTContext &Ctx,
62 const ObjCMethodDecl *MethodDecl);
Ted Kremenekf7639e12012-03-06 20:06:33 +000063public:
64 std::string MigrateDir;
65 bool MigrateLiterals;
66 bool MigrateSubscripting;
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000067 bool MigrateProperty;
Fariborz Jahanianb8343522013-08-20 23:35:26 +000068 unsigned FileId;
Dmitri Gribenkof8579502013-01-12 19:30:44 +000069 OwningPtr<NSAPI> NSAPIObj;
70 OwningPtr<edit::EditedSource> Editor;
Ted Kremenekf7639e12012-03-06 20:06:33 +000071 FileRemapper &Remapper;
72 FileManager &FileMgr;
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000073 const PPConditionalDirectiveRecord *PPRec;
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000074 Preprocessor &PP;
Ted Kremenekf7639e12012-03-06 20:06:33 +000075 bool IsOutputFile;
Fariborz Jahanian1be01532013-07-12 22:32:19 +000076 llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ObjCProtocolDecls;
Fariborz Jahanian926fafb2013-08-22 18:35:27 +000077 llvm::SmallVector<const Decl *, 8> CFFunctionIBCandidates;
Fariborz Jahanian1be01532013-07-12 22:32:19 +000078
Ted Kremenekf7639e12012-03-06 20:06:33 +000079 ObjCMigrateASTConsumer(StringRef migrateDir,
80 bool migrateLiterals,
81 bool migrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +000082 bool migrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +000083 FileRemapper &remapper,
84 FileManager &fileMgr,
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000085 const PPConditionalDirectiveRecord *PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000086 Preprocessor &PP,
Ted Kremenekf7639e12012-03-06 20:06:33 +000087 bool isOutputFile = false)
88 : MigrateDir(migrateDir),
89 MigrateLiterals(migrateLiterals),
90 MigrateSubscripting(migrateSubscripting),
Fariborz Jahanianb8343522013-08-20 23:35:26 +000091 MigrateProperty(migrateProperty), FileId(0),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +000092 Remapper(remapper), FileMgr(fileMgr), PPRec(PPRec), PP(PP),
Ted Kremenekf7639e12012-03-06 20:06:33 +000093 IsOutputFile(isOutputFile) { }
94
95protected:
96 virtual void Initialize(ASTContext &Context) {
97 NSAPIObj.reset(new NSAPI(Context));
98 Editor.reset(new edit::EditedSource(Context.getSourceManager(),
David Blaikiebbafb8a2012-03-11 07:00:24 +000099 Context.getLangOpts(),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000100 PPRec));
101 }
102
103 virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
104 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
105 migrateDecl(*I);
106 return true;
107 }
108 virtual void HandleInterestingDecl(DeclGroupRef DG) {
109 // Ignore decls from the PCH.
110 }
111 virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
112 ObjCMigrateASTConsumer::HandleTopLevelDecl(DG);
113 }
114
115 virtual void HandleTranslationUnit(ASTContext &Ctx);
116};
117
118}
119
120ObjCMigrateAction::ObjCMigrateAction(FrontendAction *WrappedAction,
121 StringRef migrateDir,
122 bool migrateLiterals,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000123 bool migrateSubscripting,
124 bool migrateProperty)
Ted Kremenekf7639e12012-03-06 20:06:33 +0000125 : WrapperFrontendAction(WrappedAction), MigrateDir(migrateDir),
126 MigrateLiterals(migrateLiterals), MigrateSubscripting(migrateSubscripting),
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000127 MigrateProperty(migrateProperty),
Ted Kremenekf7639e12012-03-06 20:06:33 +0000128 CompInst(0) {
129 if (MigrateDir.empty())
130 MigrateDir = "."; // user current directory if none is given.
131}
132
133ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI,
134 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000135 PPConditionalDirectiveRecord *
136 PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
137 CompInst->getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000138 ASTConsumer *
139 WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
140 ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir,
141 MigrateLiterals,
142 MigrateSubscripting,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +0000143 MigrateProperty,
Ted Kremenekf7639e12012-03-06 20:06:33 +0000144 Remapper,
145 CompInst->getFileManager(),
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000146 PPRec,
147 CompInst->getPreprocessor());
Ted Kremenekf7639e12012-03-06 20:06:33 +0000148 ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer };
149 return new MultiplexConsumer(Consumers);
150}
151
152bool ObjCMigrateAction::BeginInvocation(CompilerInstance &CI) {
153 Remapper.initFromDisk(MigrateDir, CI.getDiagnostics(),
154 /*ignoreIfFilesChanges=*/true);
155 CompInst = &CI;
156 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000157 return true;
158}
159
160namespace {
161class ObjCMigrator : public RecursiveASTVisitor<ObjCMigrator> {
162 ObjCMigrateASTConsumer &Consumer;
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000163 ParentMap &PMap;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000164
165public:
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000166 ObjCMigrator(ObjCMigrateASTConsumer &consumer, ParentMap &PMap)
167 : Consumer(consumer), PMap(PMap) { }
Ted Kremenekf7639e12012-03-06 20:06:33 +0000168
169 bool shouldVisitTemplateInstantiations() const { return false; }
170 bool shouldWalkTypesOfTypeLocs() const { return false; }
171
172 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
173 if (Consumer.MigrateLiterals) {
174 edit::Commit commit(*Consumer.Editor);
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000175 edit::rewriteToObjCLiteralSyntax(E, *Consumer.NSAPIObj, commit, &PMap);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000176 Consumer.Editor->commit(commit);
177 }
178
179 if (Consumer.MigrateSubscripting) {
180 edit::Commit commit(*Consumer.Editor);
181 edit::rewriteToObjCSubscriptSyntax(E, *Consumer.NSAPIObj, commit);
182 Consumer.Editor->commit(commit);
183 }
184
185 return true;
186 }
187
188 bool TraverseObjCMessageExpr(ObjCMessageExpr *E) {
189 // Do depth first; we want to rewrite the subexpressions first so that if
190 // we have to move expressions we will move them already rewritten.
191 for (Stmt::child_range range = E->children(); range; ++range)
192 if (!TraverseStmt(*range))
193 return false;
194
195 return WalkUpFromObjCMessageExpr(E);
196 }
197};
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000198
199class BodyMigrator : public RecursiveASTVisitor<BodyMigrator> {
200 ObjCMigrateASTConsumer &Consumer;
201 OwningPtr<ParentMap> PMap;
202
203public:
204 BodyMigrator(ObjCMigrateASTConsumer &consumer) : Consumer(consumer) { }
205
206 bool shouldVisitTemplateInstantiations() const { return false; }
207 bool shouldWalkTypesOfTypeLocs() const { return false; }
208
209 bool TraverseStmt(Stmt *S) {
210 PMap.reset(new ParentMap(S));
211 ObjCMigrator(Consumer, *PMap).TraverseStmt(S);
212 return true;
213 }
214};
Ted Kremenekf7639e12012-03-06 20:06:33 +0000215}
216
217void ObjCMigrateASTConsumer::migrateDecl(Decl *D) {
218 if (!D)
219 return;
220 if (isa<ObjCMethodDecl>(D))
221 return; // Wait for the ObjC container declaration.
222
Argyrios Kyrtzidis6b4f3412013-01-16 23:54:48 +0000223 BodyMigrator(*this).TraverseDecl(D);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000224}
225
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000226static void append_attr(std::string &PropertyString, const char *attr) {
227 PropertyString += ", ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000228 PropertyString += attr;
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000229}
230
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000231static bool rewriteToObjCProperty(const ObjCMethodDecl *Getter,
232 const ObjCMethodDecl *Setter,
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000233 const NSAPI &NS, edit::Commit &commit,
234 bool GetterHasIsPrefix) {
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000235 ASTContext &Context = NS.getASTContext();
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000236 std::string PropertyString = "@property(nonatomic";
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000237 std::string PropertyNameString = Getter->getNameAsString();
238 StringRef PropertyName(PropertyNameString);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000239 if (GetterHasIsPrefix) {
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000240 PropertyString += ", getter=";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000241 PropertyString += PropertyNameString;
242 }
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000243 // Short circuit properties that contain the name "delegate" or "dataSource",
244 // or have exact name "target" to have unsafe_unretained attribute.
245 if (PropertyName.equals("target") ||
246 (PropertyName.find("delegate") != StringRef::npos) ||
247 (PropertyName.find("dataSource") != StringRef::npos))
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000248 append_attr(PropertyString, "unsafe_unretained");
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000249 else {
250 const ParmVarDecl *argDecl = *Setter->param_begin();
251 QualType ArgType = Context.getCanonicalType(argDecl->getType());
252 Qualifiers::ObjCLifetime propertyLifetime = ArgType.getObjCLifetime();
253 bool RetainableObject = ArgType->isObjCRetainableType();
254 if (RetainableObject && propertyLifetime == Qualifiers::OCL_Strong) {
255 if (const ObjCObjectPointerType *ObjPtrTy =
256 ArgType->getAs<ObjCObjectPointerType>()) {
257 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
258 if (IDecl &&
259 IDecl->lookupNestedProtocol(&Context.Idents.get("NSCopying")))
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000260 append_attr(PropertyString, "copy");
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000261 else
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000262 append_attr(PropertyString, "retain");
263 }
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000264 } else if (propertyLifetime == Qualifiers::OCL_Weak)
265 // TODO. More precise determination of 'weak' attribute requires
266 // looking into setter's implementation for backing weak ivar.
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000267 append_attr(PropertyString, "weak");
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000268 else if (RetainableObject)
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000269 append_attr(PropertyString, "retain");
Fariborz Jahaniancf387c62013-08-06 18:06:23 +0000270 }
Fariborz Jahanian447b15e2013-08-21 18:49:03 +0000271 PropertyString += ')';
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000272
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000273 QualType RT = Getter->getResultType();
274 if (!isa<TypedefType>(RT)) {
275 // strip off any ARC lifetime qualifier.
276 QualType CanResultTy = Context.getCanonicalType(RT);
277 if (CanResultTy.getQualifiers().hasObjCLifetime()) {
278 Qualifiers Qs = CanResultTy.getQualifiers();
279 Qs.removeObjCLifetime();
280 RT = Context.getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
281 }
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000282 }
283 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000284 PropertyString += RT.getAsString(Context.getPrintingPolicy());
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000285 PropertyString += " ";
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000286 if (GetterHasIsPrefix) {
287 // property name must strip off "is" and lower case the first character
288 // after that; e.g. isContinuous will become continuous.
289 StringRef PropertyNameStringRef(PropertyNameString);
290 PropertyNameStringRef = PropertyNameStringRef.drop_front(2);
291 PropertyNameString = PropertyNameStringRef;
292 std::string NewPropertyNameString = PropertyNameString;
Fariborz Jahaniandb8bf832013-08-08 21:51:06 +0000293 NewPropertyNameString[0] = toLowercase(NewPropertyNameString[0]);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000294 PropertyString += NewPropertyNameString;
295 }
296 else
297 PropertyString += PropertyNameString;
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000298 commit.replace(CharSourceRange::getCharRange(Getter->getLocStart(),
299 Getter->getDeclaratorEndLoc()),
300 PropertyString);
301 SourceLocation EndLoc = Setter->getDeclaratorEndLoc();
302 // Get location past ';'
303 EndLoc = EndLoc.getLocWithOffset(1);
304 commit.remove(CharSourceRange::getCharRange(Setter->getLocStart(), EndLoc));
305 return true;
306}
307
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000308void ObjCMigrateASTConsumer::migrateObjCInterfaceDecl(ASTContext &Ctx,
309 ObjCInterfaceDecl *D) {
310 for (ObjCContainerDecl::method_iterator M = D->meth_begin(), MEnd = D->meth_end();
311 M != MEnd; ++M) {
312 ObjCMethodDecl *Method = (*M);
Fariborz Jahaniande661002013-07-03 23:44:11 +0000313 if (Method->isPropertyAccessor() || Method->param_size() != 0)
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000314 continue;
315 // Is this method candidate to be a getter?
Fariborz Jahaniande661002013-07-03 23:44:11 +0000316 QualType GRT = Method->getResultType();
317 if (GRT->isVoidType())
318 continue;
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000319 // FIXME. Don't know what todo with attributes, skip for now.
320 if (Method->hasAttrs())
321 continue;
322
Fariborz Jahaniande661002013-07-03 23:44:11 +0000323 Selector GetterSelector = Method->getSelector();
324 IdentifierInfo *getterName = GetterSelector.getIdentifierInfoForSlot(0);
325 Selector SetterSelector =
326 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
327 PP.getSelectorTable(),
328 getterName);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000329 ObjCMethodDecl *SetterMethod = D->lookupMethod(SetterSelector, true);
330 bool GetterHasIsPrefix = false;
331 if (!SetterMethod) {
332 // try a different naming convention for getter: isXxxxx
333 StringRef getterNameString = getterName->getName();
Fariborz Jahanian261fdb72013-08-08 21:20:01 +0000334 if (getterNameString.startswith("is") && !GRT->isObjCRetainableType()) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000335 GetterHasIsPrefix = true;
336 const char *CGetterName = getterNameString.data() + 2;
Fariborz Jahanian261fdb72013-08-08 21:20:01 +0000337 if (CGetterName[0] && isUppercase(CGetterName[0])) {
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000338 getterName = &Ctx.Idents.get(CGetterName);
339 SetterSelector =
340 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
341 PP.getSelectorTable(),
342 getterName);
343 SetterMethod = D->lookupMethod(SetterSelector, true);
344 }
345 }
346 }
347 if (SetterMethod) {
Fariborz Jahaniande661002013-07-03 23:44:11 +0000348 // Is this a valid setter, matching the target getter?
349 QualType SRT = SetterMethod->getResultType();
350 if (!SRT->isVoidType())
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000351 continue;
Fariborz Jahaniande661002013-07-03 23:44:11 +0000352 const ParmVarDecl *argDecl = *SetterMethod->param_begin();
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000353 QualType ArgType = argDecl->getType();
Fariborz Jahanian7ac20e12013-07-08 22:49:25 +0000354 if (!Ctx.hasSameUnqualifiedType(ArgType, GRT) ||
355 SetterMethod->hasAttrs())
Fariborz Jahanian43bbaac2013-07-04 00:24:32 +0000356 continue;
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000357 edit::Commit commit(*Editor);
Fariborz Jahaniancf2ff9b2013-08-08 20:51:58 +0000358 rewriteToObjCProperty(Method, SetterMethod, *NSAPIObj, commit,
359 GetterHasIsPrefix);
Fariborz Jahanian266926d2013-07-05 20:46:03 +0000360 Editor->commit(commit);
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000361 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +0000362 }
363}
364
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000365static bool
Fariborz Jahanian9a3512b2013-07-13 17:16:41 +0000366ClassImplementsAllMethodsAndProperties(ASTContext &Ctx,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000367 const ObjCImplementationDecl *ImpDecl,
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000368 const ObjCInterfaceDecl *IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000369 ObjCProtocolDecl *Protocol) {
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000370 // In auto-synthesis, protocol properties are not synthesized. So,
371 // a conforming protocol must have its required properties declared
372 // in class interface.
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000373 bool HasAtleastOneRequiredProperty = false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000374 if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition())
375 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
376 E = PDecl->prop_end(); P != E; ++P) {
377 ObjCPropertyDecl *Property = *P;
378 if (Property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
379 continue;
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000380 HasAtleastOneRequiredProperty = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000381 DeclContext::lookup_const_result R = IDecl->lookup(Property->getDeclName());
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000382 if (R.size() == 0) {
383 // Relax the rule and look into class's implementation for a synthesize
384 // or dynamic declaration. Class is implementing a property coming from
385 // another protocol. This still makes the target protocol as conforming.
386 if (!ImpDecl->FindPropertyImplDecl(
387 Property->getDeclName().getAsIdentifierInfo()))
388 return false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000389 }
Fariborz Jahanian2bc3dda2013-07-16 21:59:42 +0000390 else if (ObjCPropertyDecl *ClassProperty = dyn_cast<ObjCPropertyDecl>(R[0])) {
391 if ((ClassProperty->getPropertyAttributes()
392 != Property->getPropertyAttributes()) ||
393 !Ctx.hasSameType(ClassProperty->getType(), Property->getType()))
394 return false;
395 }
396 else
397 return false;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000398 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000399
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000400 // At this point, all required properties in this protocol conform to those
401 // declared in the class.
402 // Check that class implements the required methods of the protocol too.
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000403 bool HasAtleastOneRequiredMethod = false;
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000404 if (const ObjCProtocolDecl *PDecl = Protocol->getDefinition()) {
405 if (PDecl->meth_begin() == PDecl->meth_end())
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000406 return HasAtleastOneRequiredProperty;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000407 for (ObjCContainerDecl::method_iterator M = PDecl->meth_begin(),
408 MEnd = PDecl->meth_end(); M != MEnd; ++M) {
409 ObjCMethodDecl *MD = (*M);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000410 if (MD->isImplicit())
411 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000412 if (MD->getImplementationControl() == ObjCMethodDecl::Optional)
413 continue;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000414 DeclContext::lookup_const_result R = ImpDecl->lookup(MD->getDeclName());
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000415 if (R.size() == 0)
416 return false;
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000417 bool match = false;
418 HasAtleastOneRequiredMethod = true;
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000419 for (unsigned I = 0, N = R.size(); I != N; ++I)
420 if (ObjCMethodDecl *ImpMD = dyn_cast<ObjCMethodDecl>(R[0]))
421 if (Ctx.ObjCMethodsAreEqual(MD, ImpMD)) {
422 match = true;
423 break;
424 }
425 if (!match)
426 return false;
427 }
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000428 }
Fariborz Jahanian9e543af2013-07-22 23:50:04 +0000429 if (HasAtleastOneRequiredProperty || HasAtleastOneRequiredMethod)
430 return true;
431 return false;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000432}
433
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000434static bool rewriteToObjCInterfaceDecl(const ObjCInterfaceDecl *IDecl,
435 llvm::SmallVectorImpl<ObjCProtocolDecl*> &ConformingProtocols,
436 const NSAPI &NS, edit::Commit &commit) {
437 const ObjCList<ObjCProtocolDecl> &Protocols = IDecl->getReferencedProtocols();
438 std::string ClassString;
439 SourceLocation EndLoc =
440 IDecl->getSuperClass() ? IDecl->getSuperClassLoc() : IDecl->getLocation();
441
442 if (Protocols.empty()) {
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 ClassString += "> ";
450 }
451 else {
452 ClassString = ", ";
453 for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
454 ClassString += ConformingProtocols[i]->getNameAsString();
455 if (i != (e-1))
456 ClassString += ", ";
457 }
458 ObjCInterfaceDecl::protocol_loc_iterator PL = IDecl->protocol_loc_end() - 1;
459 EndLoc = *PL;
460 }
461
462 commit.insertAfterToken(EndLoc, ClassString);
463 return true;
464}
465
466static bool rewriteToNSEnumDecl(const EnumDecl *EnumDcl,
467 const TypedefDecl *TypedefDcl,
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000468 const NSAPI &NS, edit::Commit &commit,
469 bool IsNSIntegerType) {
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000470 std::string ClassString =
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000471 IsNSIntegerType ? "typedef NS_ENUM(NSInteger, " : "typedef NS_OPTIONS(NSUInteger, ";
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000472 ClassString += TypedefDcl->getIdentifier()->getName();
473 ClassString += ')';
474 SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
475 commit.replace(R, ClassString);
476 SourceLocation EndOfTypedefLoc = TypedefDcl->getLocEnd();
477 EndOfTypedefLoc = trans::findLocationAfterSemi(EndOfTypedefLoc, NS.getASTContext());
478 if (!EndOfTypedefLoc.isInvalid()) {
479 commit.remove(SourceRange(TypedefDcl->getLocStart(), EndOfTypedefLoc));
480 return true;
481 }
482 return false;
483}
484
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000485static bool rewriteToNSMacroDecl(const EnumDecl *EnumDcl,
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000486 const TypedefDecl *TypedefDcl,
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000487 const NSAPI &NS, edit::Commit &commit,
488 bool IsNSIntegerType) {
489 std::string ClassString =
490 IsNSIntegerType ? "NS_ENUM(NSInteger, " : "NS_OPTIONS(NSUInteger, ";
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000491 ClassString += TypedefDcl->getIdentifier()->getName();
492 ClassString += ')';
493 SourceRange R(EnumDcl->getLocStart(), EnumDcl->getLocStart());
494 commit.replace(R, ClassString);
495 SourceLocation TypedefLoc = TypedefDcl->getLocEnd();
496 commit.remove(SourceRange(TypedefLoc, TypedefLoc));
497 return true;
498}
499
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000500static bool UseNSOptionsMacro(ASTContext &Ctx,
501 const EnumDecl *EnumDcl) {
502 bool PowerOfTwo = true;
Fariborz Jahanianbe7bc112013-08-15 18:46:37 +0000503 uint64_t MaxPowerOfTwoVal = 0;
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000504 for (EnumDecl::enumerator_iterator EI = EnumDcl->enumerator_begin(),
505 EE = EnumDcl->enumerator_end(); EI != EE; ++EI) {
506 EnumConstantDecl *Enumerator = (*EI);
507 const Expr *InitExpr = Enumerator->getInitExpr();
508 if (!InitExpr) {
509 PowerOfTwo = false;
510 continue;
511 }
512 InitExpr = InitExpr->IgnoreImpCasts();
513 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(InitExpr))
514 if (BO->isShiftOp() || BO->isBitwiseOp())
515 return true;
516
517 uint64_t EnumVal = Enumerator->getInitVal().getZExtValue();
Fariborz Jahanianbe7bc112013-08-15 18:46:37 +0000518 if (PowerOfTwo && EnumVal) {
519 if (!llvm::isPowerOf2_64(EnumVal))
520 PowerOfTwo = false;
521 else if (EnumVal > MaxPowerOfTwoVal)
522 MaxPowerOfTwoVal = EnumVal;
523 }
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000524 }
Fariborz Jahanian0b48e7c2013-08-15 18:58:00 +0000525 return PowerOfTwo && (MaxPowerOfTwoVal > 2);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000526}
527
Fariborz Jahanian008ef722013-07-19 17:44:32 +0000528void ObjCMigrateASTConsumer::migrateProtocolConformance(ASTContext &Ctx,
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000529 const ObjCImplementationDecl *ImpDecl) {
530 const ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface();
531 if (!IDecl || ObjCProtocolDecls.empty())
532 return;
533 // Find all implicit conforming protocols for this class
534 // and make them explicit.
535 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ExplicitProtocols;
536 Ctx.CollectInheritedProtocols(IDecl, ExplicitProtocols);
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000537 llvm::SmallVector<ObjCProtocolDecl *, 8> PotentialImplicitProtocols;
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000538
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000539 for (llvm::SmallPtrSet<ObjCProtocolDecl*, 32>::iterator I =
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000540 ObjCProtocolDecls.begin(),
541 E = ObjCProtocolDecls.end(); I != E; ++I)
542 if (!ExplicitProtocols.count(*I))
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000543 PotentialImplicitProtocols.push_back(*I);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000544
545 if (PotentialImplicitProtocols.empty())
546 return;
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000547
548 // go through list of non-optional methods and properties in each protocol
549 // in the PotentialImplicitProtocols list. If class implements every one of the
550 // methods and properties, then this class conforms to this protocol.
551 llvm::SmallVector<ObjCProtocolDecl*, 8> ConformingProtocols;
552 for (unsigned i = 0, e = PotentialImplicitProtocols.size(); i != e; i++)
Fariborz Jahaniand36150d2013-07-15 21:22:08 +0000553 if (ClassImplementsAllMethodsAndProperties(Ctx, ImpDecl, IDecl,
Fariborz Jahanian9eabf452013-07-13 00:04:20 +0000554 PotentialImplicitProtocols[i]))
555 ConformingProtocols.push_back(PotentialImplicitProtocols[i]);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000556
557 if (ConformingProtocols.empty())
558 return;
Fariborz Jahaniancb7b8de2013-07-17 00:02:22 +0000559
560 // Further reduce number of conforming protocols. If protocol P1 is in the list
561 // protocol P2 (P2<P1>), No need to include P1.
562 llvm::SmallVector<ObjCProtocolDecl*, 8> MinimalConformingProtocols;
563 for (unsigned i = 0, e = ConformingProtocols.size(); i != e; i++) {
564 bool DropIt = false;
565 ObjCProtocolDecl *TargetPDecl = ConformingProtocols[i];
566 for (unsigned i1 = 0, e1 = ConformingProtocols.size(); i1 != e1; i1++) {
567 ObjCProtocolDecl *PDecl = ConformingProtocols[i1];
568 if (PDecl == TargetPDecl)
569 continue;
570 if (PDecl->lookupProtocolNamed(
571 TargetPDecl->getDeclName().getAsIdentifierInfo())) {
572 DropIt = true;
573 break;
574 }
575 }
576 if (!DropIt)
577 MinimalConformingProtocols.push_back(TargetPDecl);
578 }
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000579 edit::Commit commit(*Editor);
Fariborz Jahanian85e988b2013-07-18 22:17:33 +0000580 rewriteToObjCInterfaceDecl(IDecl, MinimalConformingProtocols,
581 *NSAPIObj, commit);
Fariborz Jahanian5bd5aff2013-07-16 00:20:21 +0000582 Editor->commit(commit);
Fariborz Jahanian1be01532013-07-12 22:32:19 +0000583}
584
Fariborz Jahanian92463272013-07-18 20:11:45 +0000585void ObjCMigrateASTConsumer::migrateNSEnumDecl(ASTContext &Ctx,
586 const EnumDecl *EnumDcl,
587 const TypedefDecl *TypedefDcl) {
588 if (!EnumDcl->isCompleteDefinition() || EnumDcl->getIdentifier() ||
589 !TypedefDcl->getIdentifier())
590 return;
591
592 QualType qt = TypedefDcl->getTypeSourceInfo()->getType();
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000593 bool IsNSIntegerType = NSAPIObj->isObjCNSIntegerType(qt);
594 bool IsNSUIntegerType = !IsNSIntegerType && NSAPIObj->isObjCNSUIntegerType(qt);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000595
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000596 if (!IsNSIntegerType && !IsNSUIntegerType) {
597 // Also check for typedef enum {...} TD;
598 if (const EnumType *EnumTy = qt->getAs<EnumType>()) {
599 if (EnumTy->getDecl() == EnumDcl) {
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000600 bool NSOptions = UseNSOptionsMacro(Ctx, EnumDcl);
601 if (NSOptions) {
602 if (!Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition())
603 return;
604 }
605 else if (!Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000606 return;
607 edit::Commit commit(*Editor);
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000608 rewriteToNSMacroDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, !NSOptions);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000609 Editor->commit(commit);
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000610 }
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000611 }
Fariborz Jahaniand0f6f792013-07-22 18:53:45 +0000612 return;
613 }
614 if (IsNSIntegerType && UseNSOptionsMacro(Ctx, EnumDcl)) {
615 // We may still use NS_OPTIONS based on what we find in the enumertor list.
616 IsNSIntegerType = false;
617 IsNSUIntegerType = true;
Fariborz Jahanianc1c44f62013-07-19 20:18:36 +0000618 }
Fariborz Jahanian92463272013-07-18 20:11:45 +0000619
620 // NS_ENUM must be available.
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000621 if (IsNSIntegerType && !Ctx.Idents.get("NS_ENUM").hasMacroDefinition())
622 return;
623 // NS_OPTIONS must be available.
624 if (IsNSUIntegerType && !Ctx.Idents.get("NS_OPTIONS").hasMacroDefinition())
Fariborz Jahanian92463272013-07-18 20:11:45 +0000625 return;
626 edit::Commit commit(*Editor);
Fariborz Jahanianb0057bb2013-07-19 01:05:49 +0000627 rewriteToNSEnumDecl(EnumDcl, TypedefDcl, *NSAPIObj, commit, IsNSIntegerType);
Fariborz Jahanian92463272013-07-18 20:11:45 +0000628 Editor->commit(commit);
629}
630
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000631static void ReplaceWithInstancetype(const ObjCMigrateASTConsumer &ASTC,
632 ObjCMethodDecl *OM) {
633 SourceRange R;
634 std::string ClassString;
635 if (TypeSourceInfo *TSInfo = OM->getResultTypeSourceInfo()) {
636 TypeLoc TL = TSInfo->getTypeLoc();
637 R = SourceRange(TL.getBeginLoc(), TL.getEndLoc());
638 ClassString = "instancetype";
639 }
640 else {
641 R = SourceRange(OM->getLocStart(), OM->getLocStart());
642 ClassString = OM->isInstanceMethod() ? '-' : '+';
643 ClassString += " (instancetype)";
644 }
645 edit::Commit commit(*ASTC.Editor);
646 commit.replace(R, ClassString);
647 ASTC.Editor->commit(commit);
648}
649
Fariborz Jahanian670ef262013-07-23 23:34:42 +0000650void ObjCMigrateASTConsumer::migrateMethodInstanceType(ASTContext &Ctx,
651 ObjCContainerDecl *CDecl,
652 ObjCMethodDecl *OM) {
Fariborz Jahanian71221352013-07-23 22:42:28 +0000653 ObjCInstanceTypeFamily OIT_Family =
654 Selector::getInstTypeMethodFamily(OM->getSelector());
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000655
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000656 std::string ClassName;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000657 switch (OIT_Family) {
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000658 case OIT_None:
659 migrateFactoryMethod(Ctx, CDecl, OM);
660 return;
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000661 case OIT_Array:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000662 ClassName = "NSArray";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000663 break;
664 case OIT_Dictionary:
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000665 ClassName = "NSDictionary";
666 break;
667 case OIT_MemManage:
668 ClassName = "NSObject";
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000669 break;
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000670 case OIT_Singleton:
671 migrateFactoryMethod(Ctx, CDecl, OM, OIT_Singleton);
Fariborz Jahanian631925f2013-07-23 23:55:55 +0000672 return;
673 }
Fariborz Jahanian71221352013-07-23 22:42:28 +0000674 if (!OM->getResultType()->isObjCIdType())
675 return;
676
677 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
678 if (!IDecl) {
679 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
680 IDecl = CatDecl->getClassInterface();
681 else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
682 IDecl = ImpDecl->getClassInterface();
683 }
Fariborz Jahanian267bae32013-07-24 19:18:37 +0000684 if (!IDecl ||
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000685 !IDecl->lookupInheritedClass(&Ctx.Idents.get(ClassName))) {
686 migrateFactoryMethod(Ctx, CDecl, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000687 return;
Fariborz Jahanian280954a2013-07-24 18:31:42 +0000688 }
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000689 ReplaceWithInstancetype(*this, OM);
Fariborz Jahanian71221352013-07-23 22:42:28 +0000690}
691
692void ObjCMigrateASTConsumer::migrateInstanceType(ASTContext &Ctx,
693 ObjCContainerDecl *CDecl) {
694 // migrate methods which can have instancetype as their result type.
695 for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(),
696 MEnd = CDecl->meth_end();
697 M != MEnd; ++M) {
698 ObjCMethodDecl *Method = (*M);
699 migrateMethodInstanceType(Ctx, CDecl, Method);
700 }
701}
702
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000703void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
704 ObjCContainerDecl *CDecl,
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000705 ObjCMethodDecl *OM,
706 ObjCInstanceTypeFamily OIT_Family) {
Fariborz Jahanian3bfc35e2013-08-02 22:34:18 +0000707 if (OM->isInstanceMethod() ||
708 OM->getResultType() == Ctx.getObjCInstanceType() ||
709 !OM->getResultType()->isObjCIdType())
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000710 return;
711
712 // Candidate factory methods are + (id) NaMeXXX : ... which belong to a class
713 // NSYYYNamE with matching names be at least 3 characters long.
714 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
715 if (!IDecl) {
716 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl))
717 IDecl = CatDecl->getClassInterface();
718 else if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(CDecl))
719 IDecl = ImpDecl->getClassInterface();
720 }
721 if (!IDecl)
722 return;
723
724 std::string StringClassName = IDecl->getName();
725 StringRef LoweredClassName(StringClassName);
726 std::string StringLoweredClassName = LoweredClassName.lower();
727 LoweredClassName = StringLoweredClassName;
728
729 IdentifierInfo *MethodIdName = OM->getSelector().getIdentifierInfoForSlot(0);
Fariborz Jahanianc13c1b02013-08-13 18:01:42 +0000730 // Handle method with no name at its first selector slot; e.g. + (id):(int)x.
731 if (!MethodIdName)
732 return;
733
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000734 std::string MethodName = MethodIdName->getName();
Fariborz Jahanian9275c682013-08-02 20:54:18 +0000735 if (OIT_Family == OIT_Singleton) {
736 StringRef STRefMethodName(MethodName);
737 size_t len = 0;
738 if (STRefMethodName.startswith("standard"))
739 len = strlen("standard");
740 else if (STRefMethodName.startswith("shared"))
741 len = strlen("shared");
742 else if (STRefMethodName.startswith("default"))
743 len = strlen("default");
744 else
745 return;
746 MethodName = STRefMethodName.substr(len);
747 }
Fariborz Jahanianc4291852013-08-02 18:00:53 +0000748 std::string MethodNameSubStr = MethodName.substr(0, 3);
749 StringRef MethodNamePrefix(MethodNameSubStr);
750 std::string StringLoweredMethodNamePrefix = MethodNamePrefix.lower();
751 MethodNamePrefix = StringLoweredMethodNamePrefix;
752 size_t Ix = LoweredClassName.rfind(MethodNamePrefix);
753 if (Ix == StringRef::npos)
754 return;
755 std::string ClassNamePostfix = LoweredClassName.substr(Ix);
756 StringRef LoweredMethodName(MethodName);
757 std::string StringLoweredMethodName = LoweredMethodName.lower();
758 LoweredMethodName = StringLoweredMethodName;
759 if (!LoweredMethodName.startswith(ClassNamePostfix))
760 return;
761 ReplaceWithInstancetype(*this, OM);
762}
763
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000764static bool IsVoidStarType(QualType Ty) {
765 if (!Ty->isPointerType())
766 return false;
767
768 while (const TypedefType *TD = dyn_cast<TypedefType>(Ty.getTypePtr()))
769 Ty = TD->getDecl()->getUnderlyingType();
770
771 // Is the type void*?
772 const PointerType* PT = Ty->getAs<PointerType>();
773 if (PT->getPointeeType().getUnqualifiedType()->isVoidType())
774 return true;
775 return IsVoidStarType(PT->getPointeeType());
776}
777
Fariborz Jahanian94279392013-08-20 18:54:39 +0000778/// AuditedType - This routine audits the type AT and returns false if it is one of known
779/// CF object types or of the "void *" variety. It returns true if we don't care about the type
780/// such as a non-pointer or pointers which have no ownership issues (such as "int *").
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000781static bool
Fariborz Jahanian94279392013-08-20 18:54:39 +0000782AuditedType (QualType AT, bool &IsPoniter) {
783 IsPoniter = (AT->isAnyPointerType() || AT->isBlockPointerType());
784 if (!IsPoniter)
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000785 return true;
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000786 // FIXME. There isn't much we can say about CF pointer type; or is there?
Fariborz Jahanian94279392013-08-20 18:54:39 +0000787 if (ento::coreFoundation::isCFObjectRef(AT) ||
788 IsVoidStarType(AT) ||
789 // If an ObjC object is type, assuming that it is not a CF function and
790 // that it is an un-audited function.
Fariborz Jahanian2e9c19c2013-08-22 22:27:36 +0000791 AT->isObjCObjectPointerType() || AT->isObjCBuiltinType())
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000792 return false;
Fariborz Jahanian94279392013-08-20 18:54:39 +0000793 // All other pointers are assumed audited as harmless.
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000794 return true;
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000795}
796
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000797void ObjCMigrateASTConsumer::AnnotateImplicitBridging(ASTContext &Ctx) {
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000798 if (CFFunctionIBCandidates.empty())
799 return;
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000800 if (!Ctx.Idents.get("CF_IMPLICIT_BRIDGING_ENABLED").hasMacroDefinition()) {
801 CFFunctionIBCandidates.clear();
Fariborz Jahanianb8343522013-08-20 23:35:26 +0000802 FileId = 0;
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000803 return;
804 }
805 // Insert CF_IMPLICIT_BRIDGING_ENABLE/CF_IMPLICIT_BRIDGING_DISABLED
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000806 const Decl *FirstFD = CFFunctionIBCandidates[0];
807 const Decl *LastFD =
808 CFFunctionIBCandidates[CFFunctionIBCandidates.size()-1];
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000809 const char *PragmaString = "\nCF_IMPLICIT_BRIDGING_ENABLED\n\n";
810 edit::Commit commit(*Editor);
811 commit.insertBefore(FirstFD->getLocStart(), PragmaString);
812 PragmaString = "\n\nCF_IMPLICIT_BRIDGING_DISABLED\n";
813 SourceLocation EndLoc = LastFD->getLocEnd();
814 // get location just past end of function location.
815 EndLoc = PP.getLocForEndOfToken(EndLoc);
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000816 if (isa<FunctionDecl>(LastFD)) {
817 // For Methods, EndLoc points to the ending semcolon. So,
818 // not of these extra work is needed.
819 Token Tok;
820 // get locaiton of token that comes after end of function.
821 bool Failed = PP.getRawToken(EndLoc, Tok, /*IgnoreWhiteSpace=*/true);
822 if (!Failed)
823 EndLoc = Tok.getLocation();
824 }
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000825 commit.insertAfterToken(EndLoc, PragmaString);
826 Editor->commit(commit);
Fariborz Jahanianb8343522013-08-20 23:35:26 +0000827 FileId = 0;
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000828 CFFunctionIBCandidates.clear();
829}
830
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000831void ObjCMigrateASTConsumer::migrateCFAnnotation(ASTContext &Ctx, const Decl *Decl) {
832 if (Decl->hasAttr<CFAuditedTransferAttr>()) {
Fariborz Jahanianc6dfd3f2013-08-19 22:00:50 +0000833 assert(CFFunctionIBCandidates.empty() &&
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000834 "Cannot have audited functions/methods inside user "
Fariborz Jahanianc6dfd3f2013-08-19 22:00:50 +0000835 "provided CF_IMPLICIT_BRIDGING_ENABLE");
836 return;
837 }
838
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000839 // Finction must be annotated first.
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000840 bool Audited;
841 if (const FunctionDecl *FuncDecl = dyn_cast<FunctionDecl>(Decl))
842 Audited = migrateAddFunctionAnnotation(Ctx, FuncDecl);
843 else
844 Audited = migrateAddMethodAnnotation(Ctx, cast<ObjCMethodDecl>(Decl));
Fariborz Jahanianb8343522013-08-20 23:35:26 +0000845 if (Audited) {
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000846 CFFunctionIBCandidates.push_back(Decl);
Fariborz Jahanianb8343522013-08-20 23:35:26 +0000847 if (!FileId)
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000848 FileId = PP.getSourceManager().getFileID(Decl->getLocation()).getHashValue();
Fariborz Jahanianb8343522013-08-20 23:35:26 +0000849 }
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000850 else
Fariborz Jahanian301b5212013-08-20 22:42:13 +0000851 AnnotateImplicitBridging(Ctx);
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000852}
853
854bool ObjCMigrateASTConsumer::migrateAddFunctionAnnotation(
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000855 ASTContext &Ctx,
856 const FunctionDecl *FuncDecl) {
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000857 if (FuncDecl->hasBody())
858 return false;
Fariborz Jahanian5874e6d2013-08-20 20:45:28 +0000859
Fariborz Jahanian84ac1de2013-08-15 21:44:38 +0000860 CallEffects CE = CallEffects::getEffect(FuncDecl);
Fariborz Jahanian5874e6d2013-08-20 20:45:28 +0000861 bool FuncIsReturnAnnotated = (FuncDecl->getAttr<CFReturnsRetainedAttr>() ||
862 FuncDecl->getAttr<CFReturnsNotRetainedAttr>());
863
864 // Trivial case of when funciton is annotated and has no argument.
865 if (FuncIsReturnAnnotated && FuncDecl->getNumParams() == 0)
866 return false;
867
868 bool HasAtLeastOnePointer = FuncIsReturnAnnotated;
869 if (!FuncIsReturnAnnotated) {
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000870 RetEffect Ret = CE.getReturnValue();
871 const char *AnnotationString = 0;
872 if (Ret.getObjKind() == RetEffect::CF && Ret.isOwned()) {
873 if (Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
874 AnnotationString = " CF_RETURNS_RETAINED";
875 }
876 else if (Ret.getObjKind() == RetEffect::CF && !Ret.isOwned()) {
877 if (Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition())
878 AnnotationString = " CF_RETURNS_NOT_RETAINED";
879 }
880 if (AnnotationString) {
881 edit::Commit commit(*Editor);
882 commit.insertAfterToken(FuncDecl->getLocEnd(), AnnotationString);
883 Editor->commit(commit);
Fariborz Jahanian94279392013-08-20 18:54:39 +0000884 HasAtLeastOnePointer = true;
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000885 }
Fariborz Jahanian94279392013-08-20 18:54:39 +0000886 else if (!AuditedType(FuncDecl->getResultType(), HasAtLeastOnePointer))
Fariborz Jahanian2ec4d7b2013-08-16 23:35:05 +0000887 return false;
Fariborz Jahanian84ac1de2013-08-15 21:44:38 +0000888 }
Fariborz Jahanian94279392013-08-20 18:54:39 +0000889
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000890 // At this point result type is either annotated or audited.
891 // Now, how about argument types.
892 llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs();
893 unsigned i = 0;
894 for (FunctionDecl::param_const_iterator pi = FuncDecl->param_begin(),
895 pe = FuncDecl->param_end(); pi != pe; ++pi, ++i) {
Fariborz Jahanianb918d7a2013-08-21 19:37:47 +0000896 const ParmVarDecl *pd = *pi;
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000897 ArgEffect AE = AEArgs[i];
Fariborz Jahanian94279392013-08-20 18:54:39 +0000898 if (AE == DecRef /*CFConsumed annotated*/ ||
899 AE == IncRef) {
Fariborz Jahanianb918d7a2013-08-21 19:37:47 +0000900 if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() &&
901 Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) {
902 edit::Commit commit(*Editor);
903 commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
904 Editor->commit(commit);
905 HasAtLeastOnePointer = true;
906 }
907 // When AE == IncRef, there is no attribute to annotate with.
908 // It is assumed that compiler will extract the info. from function
909 // API name.
Fariborz Jahanian94279392013-08-20 18:54:39 +0000910 HasAtLeastOnePointer = true;
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000911 continue;
Fariborz Jahanian94279392013-08-20 18:54:39 +0000912 }
Fariborz Jahanianc6dfd3f2013-08-19 22:00:50 +0000913
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000914 QualType AT = pd->getType();
Fariborz Jahanian94279392013-08-20 18:54:39 +0000915 bool IsPointer;
916 if (!AuditedType(AT, IsPointer))
Fariborz Jahanian9ef4a262013-08-19 19:13:34 +0000917 return false;
Fariborz Jahanian94279392013-08-20 18:54:39 +0000918 else if (IsPointer)
919 HasAtLeastOnePointer = true;
Fariborz Jahanian84ac1de2013-08-15 21:44:38 +0000920 }
Fariborz Jahanian94279392013-08-20 18:54:39 +0000921 return HasAtLeastOnePointer;
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +0000922}
923
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000924void ObjCMigrateASTConsumer::migrateARCSafeAnnotation(ASTContext &Ctx,
925 ObjCContainerDecl *CDecl) {
926 if (!isa<ObjCInterfaceDecl>(CDecl))
927 return;
928
929 // migrate methods which can have instancetype as their result type.
930 for (ObjCContainerDecl::method_iterator M = CDecl->meth_begin(),
931 MEnd = CDecl->meth_end();
932 M != MEnd; ++M) {
933 ObjCMethodDecl *Method = (*M);
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +0000934 migrateCFAnnotation(Ctx, Method);
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000935 }
936}
937
Fariborz Jahanian926fafb2013-08-22 18:35:27 +0000938bool ObjCMigrateASTConsumer::migrateAddMethodAnnotation(ASTContext &Ctx,
939 const ObjCMethodDecl *MethodDecl) {
940 if (MethodDecl->hasBody())
941 return false;
942
943 CallEffects CE = CallEffects::getEffect(MethodDecl);
944 bool MethodIsReturnAnnotated = (MethodDecl->getAttr<CFReturnsRetainedAttr>() ||
945 MethodDecl->getAttr<CFReturnsNotRetainedAttr>());
946
947 // Trivial case of when funciton is annotated and has no argument.
948 if (MethodIsReturnAnnotated &&
949 (MethodDecl->param_begin() == MethodDecl->param_end()))
950 return false;
951
952 bool HasAtLeastOnePointer = MethodIsReturnAnnotated;
953 if (!MethodIsReturnAnnotated) {
954 RetEffect Ret = CE.getReturnValue();
955 const char *AnnotationString = 0;
956 if (Ret.getObjKind() == RetEffect::CF && Ret.isOwned()) {
957 if (Ctx.Idents.get("CF_RETURNS_RETAINED").hasMacroDefinition())
958 AnnotationString = " CF_RETURNS_RETAINED";
959 }
960 else if (Ret.getObjKind() == RetEffect::CF && !Ret.isOwned()) {
961 if (Ctx.Idents.get("CF_RETURNS_NOT_RETAINED").hasMacroDefinition())
962 AnnotationString = " CF_RETURNS_NOT_RETAINED";
963 }
964 if (AnnotationString) {
965 edit::Commit commit(*Editor);
966 commit.insertBefore(MethodDecl->getLocEnd(), AnnotationString);
967 Editor->commit(commit);
968 HasAtLeastOnePointer = true;
969 }
970 else if (!AuditedType(MethodDecl->getResultType(), HasAtLeastOnePointer))
971 return false;
972 }
973
974 // At this point result type is either annotated or audited.
975 // Now, how about argument types.
976 llvm::ArrayRef<ArgEffect> AEArgs = CE.getArgs();
977 unsigned i = 0;
978 for (ObjCMethodDecl::param_const_iterator pi = MethodDecl->param_begin(),
979 pe = MethodDecl->param_end(); pi != pe; ++pi, ++i) {
980 const ParmVarDecl *pd = *pi;
981 ArgEffect AE = AEArgs[i];
982 if (AE == DecRef /*CFConsumed annotated*/ ||
983 AE == IncRef) {
984 if (AE == DecRef && !pd->getAttr<CFConsumedAttr>() &&
985 Ctx.Idents.get("CF_CONSUMED").hasMacroDefinition()) {
986 edit::Commit commit(*Editor);
987 commit.insertBefore(pd->getLocation(), "CF_CONSUMED ");
988 Editor->commit(commit);
989 HasAtLeastOnePointer = true;
990 }
991 // When AE == IncRef, there is no attribute to annotate with.
992 // It is assumed that compiler will extract the info. from function
993 // API name.
994 HasAtLeastOnePointer = true;
995 continue;
996 }
997
998 QualType AT = pd->getType();
999 bool IsPointer;
1000 if (!AuditedType(AT, IsPointer))
1001 return false;
1002 else if (IsPointer)
1003 HasAtLeastOnePointer = true;
1004 }
1005 return HasAtLeastOnePointer;
Fariborz Jahaniandfe6ed92013-08-12 23:17:13 +00001006}
1007
Ted Kremenekf7639e12012-03-06 20:06:33 +00001008namespace {
1009
1010class RewritesReceiver : public edit::EditsReceiver {
1011 Rewriter &Rewrite;
1012
1013public:
1014 RewritesReceiver(Rewriter &Rewrite) : Rewrite(Rewrite) { }
1015
1016 virtual void insert(SourceLocation loc, StringRef text) {
1017 Rewrite.InsertText(loc, text);
1018 }
1019 virtual void replace(CharSourceRange range, StringRef text) {
1020 Rewrite.ReplaceText(range.getBegin(), Rewrite.getRangeSize(range), text);
1021 }
1022};
1023
1024}
1025
1026void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) {
Fariborz Jahaniana7437f02013-07-03 23:05:00 +00001027
1028 TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
Fariborz Jahanian301b5212013-08-20 22:42:13 +00001029 if (MigrateProperty) {
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00001030 for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end();
1031 D != DEnd; ++D) {
Fariborz Jahanianb8343522013-08-20 23:35:26 +00001032 if (unsigned FID =
1033 PP.getSourceManager().getFileID((*D)->getLocation()).getHashValue())
1034 if (FileId && FileId != FID) {
1035 assert(!CFFunctionIBCandidates.empty());
1036 AnnotateImplicitBridging(Ctx);
1037 }
1038
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00001039 if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D))
1040 migrateObjCInterfaceDecl(Ctx, CDecl);
Fariborz Jahanian1be01532013-07-12 22:32:19 +00001041 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D))
1042 ObjCProtocolDecls.insert(PDecl);
1043 else if (const ObjCImplementationDecl *ImpDecl =
1044 dyn_cast<ObjCImplementationDecl>(*D))
1045 migrateProtocolConformance(Ctx, ImpDecl);
Fariborz Jahanian92463272013-07-18 20:11:45 +00001046 else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) {
1047 DeclContext::decl_iterator N = D;
1048 ++N;
Fariborz Jahanian008ef722013-07-19 17:44:32 +00001049 if (N != DEnd)
1050 if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N))
1051 migrateNSEnumDecl(Ctx, ED, TD);
Fariborz Jahanian92463272013-07-18 20:11:45 +00001052 }
Fariborz Jahanianc13c1b02013-08-13 18:01:42 +00001053 else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*D))
Fariborz Jahanian4f64dd22013-08-22 21:40:15 +00001054 migrateCFAnnotation(Ctx, FD);
Fariborz Jahanianc13c1b02013-08-13 18:01:42 +00001055
Fariborz Jahanian926fafb2013-08-22 18:35:27 +00001056 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) {
1057 // migrate methods which can have instancetype as their result type.
Fariborz Jahanian71221352013-07-23 22:42:28 +00001058 migrateInstanceType(Ctx, CDecl);
Fariborz Jahanian926fafb2013-08-22 18:35:27 +00001059 // annotate methods with CF annotations.
1060 migrateARCSafeAnnotation(Ctx, CDecl);
1061 }
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00001062 }
Fariborz Jahanian926fafb2013-08-22 18:35:27 +00001063 AnnotateImplicitBridging(Ctx);
Fariborz Jahanian301b5212013-08-20 22:42:13 +00001064 }
Fariborz Jahaniana7437f02013-07-03 23:05:00 +00001065
David Blaikiebbafb8a2012-03-11 07:00:24 +00001066 Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts());
Ted Kremenekf7639e12012-03-06 20:06:33 +00001067 RewritesReceiver Rec(rewriter);
1068 Editor->applyRewrites(Rec);
1069
1070 for (Rewriter::buffer_iterator
1071 I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) {
1072 FileID FID = I->first;
1073 RewriteBuffer &buf = I->second;
1074 const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID);
1075 assert(file);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001076 SmallString<512> newText;
Ted Kremenekf7639e12012-03-06 20:06:33 +00001077 llvm::raw_svector_ostream vecOS(newText);
1078 buf.write(vecOS);
1079 vecOS.flush();
1080 llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy(
1081 StringRef(newText.data(), newText.size()), file->getName());
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001082 SmallString<64> filePath(file->getName());
Ted Kremenekf7639e12012-03-06 20:06:33 +00001083 FileMgr.FixupRelativePath(filePath);
1084 Remapper.remap(filePath.str(), memBuf);
1085 }
1086
1087 if (IsOutputFile) {
1088 Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics());
1089 } else {
1090 Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics());
1091 }
1092}
1093
1094bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
Argyrios Kyrtzidisb4822602012-05-24 16:48:23 +00001095 CI.getDiagnostics().setIgnoreAllWarnings(true);
Ted Kremenekf7639e12012-03-06 20:06:33 +00001096 return true;
1097}
1098
1099ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI,
1100 StringRef InFile) {
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +00001101 PPConditionalDirectiveRecord *
1102 PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
1103 CI.getPreprocessor().addPPCallbacks(PPRec);
Ted Kremenekf7639e12012-03-06 20:06:33 +00001104 return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile,
1105 /*MigrateLiterals=*/true,
1106 /*MigrateSubscripting=*/true,
Fariborz Jahaniand83ef842013-07-09 16:59:14 +00001107 /*MigrateProperty*/true,
Ted Kremenekf7639e12012-03-06 20:06:33 +00001108 Remapper,
1109 CI.getFileManager(),
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +00001110 PPRec,
Fariborz Jahaniana7437f02013-07-03 23:05:00 +00001111 CI.getPreprocessor(),
Ted Kremenekf7639e12012-03-06 20:06:33 +00001112 /*isOutputFile=*/true);
1113}