blob: d342d1aa04776f5f0b493d7831f97dac43153cb2 [file] [log] [blame]
John McCall8f0e8d22011-06-15 23:25:17 +00001//===--- Tranforms.cpp - Tranformations to ARC mode -----------------------===//
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//===----------------------------------------------------------------------===//
John McCall8f0e8d22011-06-15 23:25:17 +00009
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000010#include "Transforms.h"
John McCall8f0e8d22011-06-15 23:25:17 +000011#include "Internals.h"
12#include "clang/Sema/SemaDiagnostic.h"
13#include "clang/AST/RecursiveASTVisitor.h"
14#include "clang/AST/StmtVisitor.h"
John McCall8f0e8d22011-06-15 23:25:17 +000015#include "clang/Lex/Lexer.h"
16#include "clang/Basic/SourceManager.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/DenseSet.h"
19#include <map>
20
21using namespace clang;
22using namespace arcmt;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000023using namespace trans;
John McCall8f0e8d22011-06-15 23:25:17 +000024
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +000025ASTTraverser::~ASTTraverser() { }
26
John McCall8f0e8d22011-06-15 23:25:17 +000027//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000028// Helpers.
John McCall8f0e8d22011-06-15 23:25:17 +000029//===----------------------------------------------------------------------===//
30
Argyrios Kyrtzidis12192cf2011-11-07 18:40:29 +000031bool trans::canApplyWeak(ASTContext &Ctx, QualType type,
32 bool AllowOnUnknownClass) {
David Blaikie4e4d0842012-03-11 07:00:24 +000033 if (!Ctx.getLangOpts().ObjCRuntimeHasWeak)
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000034 return false;
35
36 QualType T = type;
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +000037 if (T.isNull())
38 return false;
39
Argyrios Kyrtzidis2af504b2012-03-16 00:10:35 +000040 // iOS is always safe to use 'weak'.
41 if (Ctx.getTargetInfo().getTriple().getOS() == llvm::Triple::IOS)
42 AllowOnUnknownClass = true;
43
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000044 while (const PointerType *ptr = T->getAs<PointerType>())
45 T = ptr->getPointeeType();
46 if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
47 ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
Argyrios Kyrtzidis12192cf2011-11-07 18:40:29 +000048 if (!AllowOnUnknownClass && (!Class || Class->getName() == "NSObject"))
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000049 return false; // id/NSObject is not safe for weak.
Douglas Gregor7723fec2011-12-15 20:29:51 +000050 if (!AllowOnUnknownClass && !Class->hasDefinition())
Argyrios Kyrtzidis5363e8d2011-07-12 22:16:25 +000051 return false; // forward classes are not verifiable, therefore not safe.
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000052 if (Class->isArcWeakrefUnavailable())
53 return false;
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000054 }
55
56 return true;
57}
58
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000059/// \brief 'Loc' is the end of a statement range. This returns the location
60/// immediately after the semicolon following the statement.
61/// If no semicolon is found or the location is inside a macro, the returned
62/// source location will be invalid.
63SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
64 ASTContext &Ctx) {
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000065 SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx);
66 if (SemiLoc.isInvalid())
67 return SourceLocation();
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000068 return SemiLoc.getLocWithOffset(1);
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000069}
70
71/// \brief \arg Loc is the end of a statement range. This returns the location
72/// of the semicolon following the statement.
73/// If no semicolon is found or the location is inside a macro, the returned
74/// source location will be invalid.
75SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
76 ASTContext &Ctx) {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000077 SourceManager &SM = Ctx.getSourceManager();
78 if (loc.isMacroID()) {
David Blaikie4e4d0842012-03-11 07:00:24 +000079 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOpts(), &loc))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000080 return SourceLocation();
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000081 }
David Blaikie4e4d0842012-03-11 07:00:24 +000082 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOpts());
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000083
84 // Break down the source location.
85 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
86
87 // Try to load the file buffer.
88 bool invalidTemp = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +000089 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000090 if (invalidTemp)
91 return SourceLocation();
92
93 const char *tokenBegin = file.data() + locInfo.second;
94
95 // Lex from the start of the given location.
96 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
David Blaikie4e4d0842012-03-11 07:00:24 +000097 Ctx.getLangOpts(),
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000098 file.begin(), tokenBegin, file.end());
99 Token tok;
100 lexer.LexFromRawLexer(tok);
101 if (tok.isNot(tok::semi))
102 return SourceLocation();
103
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000104 return tok.getLocation();
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000105}
106
107bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
108 if (!E || !E->HasSideEffects(Ctx))
109 return false;
110
111 E = E->IgnoreParenCasts();
112 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
113 if (!ME)
114 return true;
115 switch (ME->getMethodFamily()) {
116 case OMF_autorelease:
117 case OMF_dealloc:
118 case OMF_release:
119 case OMF_retain:
120 switch (ME->getReceiverKind()) {
121 case ObjCMessageExpr::SuperInstance:
122 return false;
123 case ObjCMessageExpr::Instance:
124 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
125 default:
126 break;
127 }
128 break;
129 default:
130 break;
131 }
132
133 return true;
134}
135
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000136bool trans::isGlobalVar(Expr *E) {
137 E = E->IgnoreParenCasts();
138 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000139 return DRE->getDecl()->getDeclContext()->isFileContext() &&
140 DRE->getDecl()->getLinkage() == ExternalLinkage;
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000141 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
142 return isGlobalVar(condOp->getTrueExpr()) &&
143 isGlobalVar(condOp->getFalseExpr());
144
145 return false;
146}
147
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000148StringRef trans::getNilString(ASTContext &Ctx) {
149 if (Ctx.Idents.get("nil").hasMacroDefinition())
150 return "nil";
151 else
152 return "0";
153}
154
John McCall8f0e8d22011-06-15 23:25:17 +0000155namespace {
156
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000157class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
158 ExprSet &Refs;
159public:
160 ReferenceClear(ExprSet &refs) : Refs(refs) { }
161 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000162};
163
164class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
165 ValueDecl *Dcl;
166 ExprSet &Refs;
John McCall8f0e8d22011-06-15 23:25:17 +0000167
168public:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000169 ReferenceCollector(ValueDecl *D, ExprSet &refs)
170 : Dcl(D), Refs(refs) { }
171
172 bool VisitDeclRefExpr(DeclRefExpr *E) {
173 if (E->getDecl() == Dcl)
174 Refs.insert(E);
175 return true;
176 }
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000177};
178
179class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
180 ExprSet &Removables;
181
182public:
183 RemovablesCollector(ExprSet &removables)
John McCall8f0e8d22011-06-15 23:25:17 +0000184 : Removables(removables) { }
185
186 bool shouldWalkTypesOfTypeLocs() const { return false; }
187
188 bool TraverseStmtExpr(StmtExpr *E) {
189 CompoundStmt *S = E->getSubStmt();
190 for (CompoundStmt::body_iterator
191 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
192 if (I != E - 1)
193 mark(*I);
194 TraverseStmt(*I);
195 }
196 return true;
197 }
198
199 bool VisitCompoundStmt(CompoundStmt *S) {
200 for (CompoundStmt::body_iterator
201 I = S->body_begin(), E = S->body_end(); I != E; ++I)
202 mark(*I);
203 return true;
204 }
205
206 bool VisitIfStmt(IfStmt *S) {
207 mark(S->getThen());
208 mark(S->getElse());
209 return true;
210 }
211
212 bool VisitWhileStmt(WhileStmt *S) {
213 mark(S->getBody());
214 return true;
215 }
216
217 bool VisitDoStmt(DoStmt *S) {
218 mark(S->getBody());
219 return true;
220 }
221
222 bool VisitForStmt(ForStmt *S) {
223 mark(S->getInit());
224 mark(S->getInc());
225 mark(S->getBody());
226 return true;
227 }
228
229private:
230 void mark(Stmt *S) {
231 if (!S) return;
232
John McCall7e5e5f42011-07-07 06:58:02 +0000233 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
234 S = Label->getSubStmt();
235 S = S->IgnoreImplicit();
John McCall8f0e8d22011-06-15 23:25:17 +0000236 if (Expr *E = dyn_cast<Expr>(S))
237 Removables.insert(E);
238 }
239};
240
John McCall8f0e8d22011-06-15 23:25:17 +0000241} // end anonymous namespace
242
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000243void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
244 ReferenceClear(refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000245}
246
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000247void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
248 ReferenceCollector(D, refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000249}
250
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000251void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
252 RemovablesCollector(exprs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000253}
254
255//===----------------------------------------------------------------------===//
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000256// MigrationContext
257//===----------------------------------------------------------------------===//
258
259namespace {
260
261class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
262 MigrationContext &MigrateCtx;
Argyrios Kyrtzidisa33849b2011-11-07 18:46:50 +0000263 typedef RecursiveASTVisitor<ASTTransform> base;
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000264
265public:
266 ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
267
Argyrios Kyrtzidisf38fa732011-11-06 18:58:03 +0000268 bool shouldWalkTypesOfTypeLocs() const { return false; }
269
Argyrios Kyrtzidisb0d5db12011-11-06 18:57:57 +0000270 bool TraverseObjCImplementationDecl(ObjCImplementationDecl *D) {
271 ObjCImplementationContext ImplCtx(MigrateCtx, D);
272 for (MigrationContext::traverser_iterator
273 I = MigrateCtx.traversers_begin(),
274 E = MigrateCtx.traversers_end(); I != E; ++I)
275 (*I)->traverseObjCImplementation(ImplCtx);
276
Argyrios Kyrtzidisa33849b2011-11-07 18:46:50 +0000277 return base::TraverseObjCImplementationDecl(D);
Argyrios Kyrtzidisb0d5db12011-11-06 18:57:57 +0000278 }
279
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000280 bool TraverseStmt(Stmt *rootS) {
281 if (!rootS)
282 return true;
283
284 BodyContext BodyCtx(MigrateCtx, rootS);
285 for (MigrationContext::traverser_iterator
286 I = MigrateCtx.traversers_begin(),
287 E = MigrateCtx.traversers_end(); I != E; ++I)
288 (*I)->traverseBody(BodyCtx);
289
290 return true;
291 }
292};
293
294}
295
296MigrationContext::~MigrationContext() {
297 for (traverser_iterator
298 I = traversers_begin(), E = traversers_end(); I != E; ++I)
299 delete *I;
300}
301
Argyrios Kyrtzidis1fe42032011-11-04 23:43:03 +0000302bool MigrationContext::isGCOwnedNonObjC(QualType T) {
303 while (!T.isNull()) {
304 if (const AttributedType *AttrT = T->getAs<AttributedType>()) {
305 if (AttrT->getAttrKind() == AttributedType::attr_objc_ownership)
306 return !AttrT->getModifiedType()->isObjCRetainableType();
307 }
308
309 if (T->isArrayType())
310 T = Pass.Ctx.getBaseElementType(T);
311 else if (const PointerType *PT = T->getAs<PointerType>())
312 T = PT->getPointeeType();
313 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
314 T = RT->getPointeeType();
315 else
316 break;
317 }
318
319 return false;
320}
321
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000322bool MigrationContext::rewritePropertyAttribute(StringRef fromAttr,
323 StringRef toAttr,
324 SourceLocation atLoc) {
325 if (atLoc.isMacroID())
326 return false;
327
328 SourceManager &SM = Pass.Ctx.getSourceManager();
329
330 // Break down the source location.
331 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
332
333 // Try to load the file buffer.
334 bool invalidTemp = false;
335 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
336 if (invalidTemp)
337 return false;
338
339 const char *tokenBegin = file.data() + locInfo.second;
340
341 // Lex from the start of the given location.
342 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
David Blaikie4e4d0842012-03-11 07:00:24 +0000343 Pass.Ctx.getLangOpts(),
Argyrios Kyrtzidisb0e1e122011-11-07 18:46:46 +0000344 file.begin(), tokenBegin, file.end());
345 Token tok;
346 lexer.LexFromRawLexer(tok);
347 if (tok.isNot(tok::at)) return false;
348 lexer.LexFromRawLexer(tok);
349 if (tok.isNot(tok::raw_identifier)) return false;
350 if (StringRef(tok.getRawIdentifierData(), tok.getLength())
351 != "property")
352 return false;
353 lexer.LexFromRawLexer(tok);
354 if (tok.isNot(tok::l_paren)) return false;
355
356 Token BeforeTok = tok;
357 Token AfterTok;
358 AfterTok.startToken();
359 SourceLocation AttrLoc;
360
361 lexer.LexFromRawLexer(tok);
362 if (tok.is(tok::r_paren))
363 return false;
364
365 while (1) {
366 if (tok.isNot(tok::raw_identifier)) return false;
367 StringRef ident(tok.getRawIdentifierData(), tok.getLength());
368 if (ident == fromAttr) {
369 if (!toAttr.empty()) {
370 Pass.TA.replaceText(tok.getLocation(), fromAttr, toAttr);
371 return true;
372 }
373 // We want to remove the attribute.
374 AttrLoc = tok.getLocation();
375 }
376
377 do {
378 lexer.LexFromRawLexer(tok);
379 if (AttrLoc.isValid() && AfterTok.is(tok::unknown))
380 AfterTok = tok;
381 } while (tok.isNot(tok::comma) && tok.isNot(tok::r_paren));
382 if (tok.is(tok::r_paren))
383 break;
384 if (AttrLoc.isInvalid())
385 BeforeTok = tok;
386 lexer.LexFromRawLexer(tok);
387 }
388
389 if (toAttr.empty() && AttrLoc.isValid() && AfterTok.isNot(tok::unknown)) {
390 // We want to remove the attribute.
391 if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::r_paren)) {
392 Pass.TA.remove(SourceRange(BeforeTok.getLocation(),
393 AfterTok.getLocation()));
394 } else if (BeforeTok.is(tok::l_paren) && AfterTok.is(tok::comma)) {
395 Pass.TA.remove(SourceRange(AttrLoc, AfterTok.getLocation()));
396 } else {
397 Pass.TA.remove(SourceRange(BeforeTok.getLocation(), AttrLoc));
398 }
399
400 return true;
401 }
402
403 return false;
404}
405
Argyrios Kyrtzidis6da42742011-11-28 02:04:36 +0000406bool MigrationContext::addPropertyAttribute(StringRef attr,
407 SourceLocation atLoc) {
408 if (atLoc.isMacroID())
409 return false;
410
411 SourceManager &SM = Pass.Ctx.getSourceManager();
412
413 // Break down the source location.
414 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(atLoc);
415
416 // Try to load the file buffer.
417 bool invalidTemp = false;
418 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
419 if (invalidTemp)
420 return false;
421
422 const char *tokenBegin = file.data() + locInfo.second;
423
424 // Lex from the start of the given location.
425 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
David Blaikie4e4d0842012-03-11 07:00:24 +0000426 Pass.Ctx.getLangOpts(),
Argyrios Kyrtzidis6da42742011-11-28 02:04:36 +0000427 file.begin(), tokenBegin, file.end());
428 Token tok;
429 lexer.LexFromRawLexer(tok);
430 if (tok.isNot(tok::at)) return false;
431 lexer.LexFromRawLexer(tok);
432 if (tok.isNot(tok::raw_identifier)) return false;
433 if (StringRef(tok.getRawIdentifierData(), tok.getLength())
434 != "property")
435 return false;
436 lexer.LexFromRawLexer(tok);
437
438 if (tok.isNot(tok::l_paren)) {
439 Pass.TA.insert(tok.getLocation(), std::string("(") + attr.str() + ") ");
440 return true;
441 }
442
443 lexer.LexFromRawLexer(tok);
444 if (tok.is(tok::r_paren)) {
445 Pass.TA.insert(tok.getLocation(), attr);
446 return true;
447 }
448
449 if (tok.isNot(tok::raw_identifier)) return false;
450
451 Pass.TA.insert(tok.getLocation(), std::string(attr) + ", ");
452 return true;
453}
454
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000455void MigrationContext::traverse(TranslationUnitDecl *TU) {
Argyrios Kyrtzidisf38fa732011-11-06 18:58:03 +0000456 for (traverser_iterator
457 I = traversers_begin(), E = traversers_end(); I != E; ++I)
458 (*I)->traverseTU(*this);
459
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000460 ASTTransform(*this).TraverseDecl(TU);
461}
462
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000463static void GCRewriteFinalize(MigrationPass &pass) {
464 ASTContext &Ctx = pass.Ctx;
465 TransformActions &TA = pass.TA;
466 DeclContext *DC = Ctx.getTranslationUnitDecl();
467 Selector FinalizeSel =
468 Ctx.Selectors.getNullarySelector(&pass.Ctx.Idents.get("finalize"));
469
470 typedef DeclContext::specific_decl_iterator<ObjCImplementationDecl>
471 impl_iterator;
472 for (impl_iterator I = impl_iterator(DC->decls_begin()),
473 E = impl_iterator(DC->decls_end()); I != E; ++I) {
474 for (ObjCImplementationDecl::instmeth_iterator
475 MI = (*I)->instmeth_begin(),
476 ME = (*I)->instmeth_end(); MI != ME; ++MI) {
477 ObjCMethodDecl *MD = *MI;
478 if (!MD->hasBody())
479 continue;
480
481 if (MD->isInstanceMethod() && MD->getSelector() == FinalizeSel) {
482 ObjCMethodDecl *FinalizeM = MD;
483 Transaction Trans(TA);
484 TA.insert(FinalizeM->getSourceRange().getBegin(),
485 "#if !__has_feature(objc_arc)\n");
486 CharSourceRange::getTokenRange(FinalizeM->getSourceRange());
487 const SourceManager &SM = pass.Ctx.getSourceManager();
David Blaikie4e4d0842012-03-11 07:00:24 +0000488 const LangOptions &LangOpts = pass.Ctx.getLangOpts();
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000489 bool Invalid;
490 std::string str = "\n#endif\n";
491 str += Lexer::getSourceText(
492 CharSourceRange::getTokenRange(FinalizeM->getSourceRange()),
493 SM, LangOpts, &Invalid);
494 TA.insertAfterToken(FinalizeM->getSourceRange().getEnd(), str);
495
496 break;
497 }
498 }
499 }
500}
501
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000502//===----------------------------------------------------------------------===//
John McCall8f0e8d22011-06-15 23:25:17 +0000503// getAllTransformations.
504//===----------------------------------------------------------------------===//
505
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000506static void traverseAST(MigrationPass &pass) {
507 MigrationContext MigrateCtx(pass);
508
509 if (pass.isGCMigration()) {
510 MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
Argyrios Kyrtzidisf38fa732011-11-06 18:58:03 +0000511 MigrateCtx.addTraverser(new GCAttrsTraverser());
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000512 }
Argyrios Kyrtzidisb0d5db12011-11-06 18:57:57 +0000513 MigrateCtx.addTraverser(new PropertyRewriteTraverser());
Argyrios Kyrtzidis2a278182012-03-05 08:46:24 +0000514 MigrateCtx.addTraverser(new BlockObjCVariableTraverser());
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000515
516 MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
517}
518
John McCall8f0e8d22011-06-15 23:25:17 +0000519static void independentTransforms(MigrationPass &pass) {
520 rewriteAutoreleasePool(pass);
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000521 removeRetainReleaseDeallocFinalize(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000522 rewriteUnusedInitDelegate(pass);
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000523 removeZeroOutPropsInDeallocFinalize(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000524 makeAssignARCSafe(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000525 rewriteUnbridgedCasts(pass);
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000526 checkAPIUses(pass);
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000527 traverseAST(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000528}
529
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000530std::vector<TransformFn> arcmt::getAllTransformations(
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000531 LangOptions::GCMode OrigGCMode,
532 bool NoFinalizeRemoval) {
John McCall8f0e8d22011-06-15 23:25:17 +0000533 std::vector<TransformFn> transforms;
534
Fariborz Jahanianbbdfad52012-01-26 20:57:58 +0000535 if (OrigGCMode == LangOptions::GCOnly && NoFinalizeRemoval)
536 transforms.push_back(GCRewriteFinalize);
John McCall8f0e8d22011-06-15 23:25:17 +0000537 transforms.push_back(independentTransforms);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000538 // This depends on previous transformations removing various expressions.
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000539 transforms.push_back(removeEmptyStatementsAndDeallocFinalize);
John McCall8f0e8d22011-06-15 23:25:17 +0000540
541 return transforms;
542}