blob: 0decdd6b43be537787b4d80e73e83286d7e69ec6 [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/Analysis/DomainSpecific/CocoaConventions.h"
16#include "clang/Lex/Lexer.h"
17#include "clang/Basic/SourceManager.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/DenseSet.h"
20#include <map>
21
22using namespace clang;
23using namespace arcmt;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000024using namespace trans;
John McCall8f0e8d22011-06-15 23:25:17 +000025
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +000026ASTTraverser::~ASTTraverser() { }
27
John McCall8f0e8d22011-06-15 23:25:17 +000028//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000029// Helpers.
John McCall8f0e8d22011-06-15 23:25:17 +000030//===----------------------------------------------------------------------===//
31
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000032/// \brief True if the class is one that does not support weak.
33static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
34 if (!cls)
35 return false;
36
37 bool inList = llvm::StringSwitch<bool>(cls->getName())
38 .Case("NSColorSpace", true)
39 .Case("NSFont", true)
40 .Case("NSFontPanel", true)
41 .Case("NSImage", true)
42 .Case("NSLazyBrowserCell", true)
43 .Case("NSWindow", true)
44 .Case("NSWindowController", true)
45 .Case("NSMenuView", true)
46 .Case("NSPersistentUIWindowInfo", true)
47 .Case("NSTableCellView", true)
48 .Case("NSATSTypeSetter", true)
49 .Case("NSATSGlyphStorage", true)
50 .Case("NSLineFragmentRenderingContext", true)
51 .Case("NSAttributeDictionary", true)
52 .Case("NSParagraphStyle", true)
53 .Case("NSTextTab", true)
54 .Case("NSSimpleHorizontalTypesetter", true)
55 .Case("_NSCachedAttributedString", true)
56 .Case("NSStringDrawingTextStorage", true)
57 .Case("NSTextView", true)
58 .Case("NSSubTextStorage", true)
59 .Default(false);
60
61 if (inList)
62 return true;
63
64 return isClassInWeakBlacklist(cls->getSuperClass());
65}
66
67bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
68 if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
69 return false;
70
71 QualType T = type;
72 while (const PointerType *ptr = T->getAs<PointerType>())
73 T = ptr->getPointeeType();
74 if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
75 ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
76 if (!Class || Class->getName() == "NSObject")
77 return false; // id/NSObject is not safe for weak.
Argyrios Kyrtzidis5363e8d2011-07-12 22:16:25 +000078 if (Class->isForwardDecl())
79 return false; // forward classes are not verifiable, therefore not safe.
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000080 if (Class->isArcWeakrefUnavailable())
81 return false;
82 if (isClassInWeakBlacklist(Class))
83 return false;
84 }
85
86 return true;
87}
88
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000089/// \brief 'Loc' is the end of a statement range. This returns the location
90/// immediately after the semicolon following the statement.
91/// If no semicolon is found or the location is inside a macro, the returned
92/// source location will be invalid.
93SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
94 ASTContext &Ctx) {
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000095 SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx);
96 if (SemiLoc.isInvalid())
97 return SourceLocation();
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +000098 return SemiLoc.getLocWithOffset(1);
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +000099}
100
101/// \brief \arg Loc is the end of a statement range. This returns the location
102/// of the semicolon following the statement.
103/// If no semicolon is found or the location is inside a macro, the returned
104/// source location will be invalid.
105SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
106 ASTContext &Ctx) {
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000107 SourceManager &SM = Ctx.getSourceManager();
108 if (loc.isMacroID()) {
Chandler Carruth433db062011-07-14 08:20:40 +0000109 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions()))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000110 return SourceLocation();
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000111 loc = SM.getExpansionRange(loc).second;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000112 }
113 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOptions());
114
115 // Break down the source location.
116 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
117
118 // Try to load the file buffer.
119 bool invalidTemp = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000121 if (invalidTemp)
122 return SourceLocation();
123
124 const char *tokenBegin = file.data() + locInfo.second;
125
126 // Lex from the start of the given location.
127 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
128 Ctx.getLangOptions(),
129 file.begin(), tokenBegin, file.end());
130 Token tok;
131 lexer.LexFromRawLexer(tok);
132 if (tok.isNot(tok::semi))
133 return SourceLocation();
134
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +0000135 return tok.getLocation();
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000136}
137
138bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
139 if (!E || !E->HasSideEffects(Ctx))
140 return false;
141
142 E = E->IgnoreParenCasts();
143 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
144 if (!ME)
145 return true;
146 switch (ME->getMethodFamily()) {
147 case OMF_autorelease:
148 case OMF_dealloc:
149 case OMF_release:
150 case OMF_retain:
151 switch (ME->getReceiverKind()) {
152 case ObjCMessageExpr::SuperInstance:
153 return false;
154 case ObjCMessageExpr::Instance:
155 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
156 default:
157 break;
158 }
159 break;
160 default:
161 break;
162 }
163
164 return true;
165}
166
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000167bool trans::isGlobalVar(Expr *E) {
168 E = E->IgnoreParenCasts();
169 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000170 return DRE->getDecl()->getDeclContext()->isFileContext() &&
171 DRE->getDecl()->getLinkage() == ExternalLinkage;
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000172 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
173 return isGlobalVar(condOp->getTrueExpr()) &&
174 isGlobalVar(condOp->getFalseExpr());
175
176 return false;
177}
178
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000179StringRef trans::getNilString(ASTContext &Ctx) {
180 if (Ctx.Idents.get("nil").hasMacroDefinition())
181 return "nil";
182 else
183 return "0";
184}
185
John McCall8f0e8d22011-06-15 23:25:17 +0000186namespace {
187
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000188class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
189 ExprSet &Refs;
190public:
191 ReferenceClear(ExprSet &refs) : Refs(refs) { }
192 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
193 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; }
194};
195
196class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
197 ValueDecl *Dcl;
198 ExprSet &Refs;
John McCall8f0e8d22011-06-15 23:25:17 +0000199
200public:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000201 ReferenceCollector(ValueDecl *D, ExprSet &refs)
202 : Dcl(D), Refs(refs) { }
203
204 bool VisitDeclRefExpr(DeclRefExpr *E) {
205 if (E->getDecl() == Dcl)
206 Refs.insert(E);
207 return true;
208 }
209
210 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
211 if (E->getDecl() == Dcl)
212 Refs.insert(E);
213 return true;
214 }
215};
216
217class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
218 ExprSet &Removables;
219
220public:
221 RemovablesCollector(ExprSet &removables)
John McCall8f0e8d22011-06-15 23:25:17 +0000222 : Removables(removables) { }
223
224 bool shouldWalkTypesOfTypeLocs() const { return false; }
225
226 bool TraverseStmtExpr(StmtExpr *E) {
227 CompoundStmt *S = E->getSubStmt();
228 for (CompoundStmt::body_iterator
229 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
230 if (I != E - 1)
231 mark(*I);
232 TraverseStmt(*I);
233 }
234 return true;
235 }
236
237 bool VisitCompoundStmt(CompoundStmt *S) {
238 for (CompoundStmt::body_iterator
239 I = S->body_begin(), E = S->body_end(); I != E; ++I)
240 mark(*I);
241 return true;
242 }
243
244 bool VisitIfStmt(IfStmt *S) {
245 mark(S->getThen());
246 mark(S->getElse());
247 return true;
248 }
249
250 bool VisitWhileStmt(WhileStmt *S) {
251 mark(S->getBody());
252 return true;
253 }
254
255 bool VisitDoStmt(DoStmt *S) {
256 mark(S->getBody());
257 return true;
258 }
259
260 bool VisitForStmt(ForStmt *S) {
261 mark(S->getInit());
262 mark(S->getInc());
263 mark(S->getBody());
264 return true;
265 }
266
267private:
268 void mark(Stmt *S) {
269 if (!S) return;
270
John McCall7e5e5f42011-07-07 06:58:02 +0000271 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
272 S = Label->getSubStmt();
273 S = S->IgnoreImplicit();
John McCall8f0e8d22011-06-15 23:25:17 +0000274 if (Expr *E = dyn_cast<Expr>(S))
275 Removables.insert(E);
276 }
277};
278
John McCall8f0e8d22011-06-15 23:25:17 +0000279} // end anonymous namespace
280
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000281void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
282 ReferenceClear(refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000283}
284
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000285void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
286 ReferenceCollector(D, refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000287}
288
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000289void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
290 RemovablesCollector(exprs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000291}
292
293//===----------------------------------------------------------------------===//
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000294// MigrationContext
295//===----------------------------------------------------------------------===//
296
297namespace {
298
299class ASTTransform : public RecursiveASTVisitor<ASTTransform> {
300 MigrationContext &MigrateCtx;
301
302public:
303 ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { }
304
305 bool TraverseStmt(Stmt *rootS) {
306 if (!rootS)
307 return true;
308
309 BodyContext BodyCtx(MigrateCtx, rootS);
310 for (MigrationContext::traverser_iterator
311 I = MigrateCtx.traversers_begin(),
312 E = MigrateCtx.traversers_end(); I != E; ++I)
313 (*I)->traverseBody(BodyCtx);
314
315 return true;
316 }
317};
318
319}
320
321MigrationContext::~MigrationContext() {
322 for (traverser_iterator
323 I = traversers_begin(), E = traversers_end(); I != E; ++I)
324 delete *I;
325}
326
327void MigrationContext::traverse(TranslationUnitDecl *TU) {
328 ASTTransform(*this).TraverseDecl(TU);
329}
330
331//===----------------------------------------------------------------------===//
John McCall8f0e8d22011-06-15 23:25:17 +0000332// getAllTransformations.
333//===----------------------------------------------------------------------===//
334
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000335static void traverseAST(MigrationPass &pass) {
336 MigrationContext MigrateCtx(pass);
337
338 if (pass.isGCMigration()) {
339 MigrateCtx.addTraverser(new GCCollectableCallsTraverser);
340 }
341
342 MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl());
343}
344
John McCall8f0e8d22011-06-15 23:25:17 +0000345static void independentTransforms(MigrationPass &pass) {
346 rewriteAutoreleasePool(pass);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000347 rewriteProperties(pass);
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000348 removeRetainReleaseDeallocFinalize(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000349 rewriteUnusedInitDelegate(pass);
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000350 removeZeroOutPropsInDeallocFinalize(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000351 makeAssignARCSafe(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000352 rewriteUnbridgedCasts(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000353 rewriteBlockObjCVariable(pass);
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000354 checkAPIUses(pass);
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000355 traverseAST(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000356}
357
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000358std::vector<TransformFn> arcmt::getAllTransformations(
359 LangOptions::GCMode OrigGCMode) {
John McCall8f0e8d22011-06-15 23:25:17 +0000360 std::vector<TransformFn> transforms;
361
John McCall8f0e8d22011-06-15 23:25:17 +0000362 transforms.push_back(independentTransforms);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000363 // This depends on previous transformations removing various expressions.
Argyrios Kyrtzidise7ef8552011-11-04 15:58:22 +0000364 transforms.push_back(removeEmptyStatementsAndDeallocFinalize);
John McCall8f0e8d22011-06-15 23:25:17 +0000365
366 return transforms;
367}