blob: 42bda9c44301322de8c31570f8cfca482810604d [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"
15#include "clang/AST/ParentMap.h"
16#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
17#include "clang/Lex/Lexer.h"
18#include "clang/Basic/SourceManager.h"
19#include "llvm/ADT/StringSwitch.h"
20#include "llvm/ADT/DenseSet.h"
21#include <map>
22
23using namespace clang;
24using namespace arcmt;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000025using namespace trans;
John McCall8f0e8d22011-06-15 23:25:17 +000026
27//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000028// Helpers.
John McCall8f0e8d22011-06-15 23:25:17 +000029//===----------------------------------------------------------------------===//
30
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000031/// \brief True if the class is one that does not support weak.
32static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
33 if (!cls)
34 return false;
35
36 bool inList = llvm::StringSwitch<bool>(cls->getName())
37 .Case("NSColorSpace", true)
38 .Case("NSFont", true)
39 .Case("NSFontPanel", true)
40 .Case("NSImage", true)
41 .Case("NSLazyBrowserCell", true)
42 .Case("NSWindow", true)
43 .Case("NSWindowController", true)
44 .Case("NSMenuView", true)
45 .Case("NSPersistentUIWindowInfo", true)
46 .Case("NSTableCellView", true)
47 .Case("NSATSTypeSetter", true)
48 .Case("NSATSGlyphStorage", true)
49 .Case("NSLineFragmentRenderingContext", true)
50 .Case("NSAttributeDictionary", true)
51 .Case("NSParagraphStyle", true)
52 .Case("NSTextTab", true)
53 .Case("NSSimpleHorizontalTypesetter", true)
54 .Case("_NSCachedAttributedString", true)
55 .Case("NSStringDrawingTextStorage", true)
56 .Case("NSTextView", true)
57 .Case("NSSubTextStorage", true)
58 .Default(false);
59
60 if (inList)
61 return true;
62
63 return isClassInWeakBlacklist(cls->getSuperClass());
64}
65
66bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
67 if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
68 return false;
69
70 QualType T = type;
71 while (const PointerType *ptr = T->getAs<PointerType>())
72 T = ptr->getPointeeType();
73 if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
74 ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
75 if (!Class || Class->getName() == "NSObject")
76 return false; // id/NSObject is not safe for weak.
Argyrios Kyrtzidis5363e8d2011-07-12 22:16:25 +000077 if (Class->isForwardDecl())
78 return false; // forward classes are not verifiable, therefore not safe.
Argyrios Kyrtzidis86625b52011-07-12 22:05:17 +000079 if (Class->isArcWeakrefUnavailable())
80 return false;
81 if (isClassInWeakBlacklist(Class))
82 return false;
83 }
84
85 return true;
86}
87
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000088/// \brief 'Loc' is the end of a statement range. This returns the location
89/// immediately after the semicolon following the statement.
90/// If no semicolon is found or the location is inside a macro, the returned
91/// source location will be invalid.
92SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
93 ASTContext &Ctx) {
94 SourceManager &SM = Ctx.getSourceManager();
95 if (loc.isMacroID()) {
Chandler Carruth433db062011-07-14 08:20:40 +000096 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions()))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000097 return SourceLocation();
98 loc = SM.getInstantiationRange(loc).second;
99 }
100 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOptions());
101
102 // Break down the source location.
103 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
104
105 // Try to load the file buffer.
106 bool invalidTemp = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000107 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000108 if (invalidTemp)
109 return SourceLocation();
110
111 const char *tokenBegin = file.data() + locInfo.second;
112
113 // Lex from the start of the given location.
114 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
115 Ctx.getLangOptions(),
116 file.begin(), tokenBegin, file.end());
117 Token tok;
118 lexer.LexFromRawLexer(tok);
119 if (tok.isNot(tok::semi))
120 return SourceLocation();
121
122 return tok.getLocation().getFileLocWithOffset(1);
123}
124
125bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
126 if (!E || !E->HasSideEffects(Ctx))
127 return false;
128
129 E = E->IgnoreParenCasts();
130 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
131 if (!ME)
132 return true;
133 switch (ME->getMethodFamily()) {
134 case OMF_autorelease:
135 case OMF_dealloc:
136 case OMF_release:
137 case OMF_retain:
138 switch (ME->getReceiverKind()) {
139 case ObjCMessageExpr::SuperInstance:
140 return false;
141 case ObjCMessageExpr::Instance:
142 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
143 default:
144 break;
145 }
146 break;
147 default:
148 break;
149 }
150
151 return true;
152}
153
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000154bool trans::isGlobalVar(Expr *E) {
155 E = E->IgnoreParenCasts();
156 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
157 return DRE->getDecl()->getDeclContext()->isFileContext();
158 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
159 return isGlobalVar(condOp->getTrueExpr()) &&
160 isGlobalVar(condOp->getFalseExpr());
161
162 return false;
163}
164
John McCall8f0e8d22011-06-15 23:25:17 +0000165namespace {
166
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000167class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
168 ExprSet &Refs;
169public:
170 ReferenceClear(ExprSet &refs) : Refs(refs) { }
171 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
172 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; }
173};
174
175class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
176 ValueDecl *Dcl;
177 ExprSet &Refs;
John McCall8f0e8d22011-06-15 23:25:17 +0000178
179public:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000180 ReferenceCollector(ValueDecl *D, ExprSet &refs)
181 : Dcl(D), Refs(refs) { }
182
183 bool VisitDeclRefExpr(DeclRefExpr *E) {
184 if (E->getDecl() == Dcl)
185 Refs.insert(E);
186 return true;
187 }
188
189 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
190 if (E->getDecl() == Dcl)
191 Refs.insert(E);
192 return true;
193 }
194};
195
196class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
197 ExprSet &Removables;
198
199public:
200 RemovablesCollector(ExprSet &removables)
John McCall8f0e8d22011-06-15 23:25:17 +0000201 : Removables(removables) { }
202
203 bool shouldWalkTypesOfTypeLocs() const { return false; }
204
205 bool TraverseStmtExpr(StmtExpr *E) {
206 CompoundStmt *S = E->getSubStmt();
207 for (CompoundStmt::body_iterator
208 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
209 if (I != E - 1)
210 mark(*I);
211 TraverseStmt(*I);
212 }
213 return true;
214 }
215
216 bool VisitCompoundStmt(CompoundStmt *S) {
217 for (CompoundStmt::body_iterator
218 I = S->body_begin(), E = S->body_end(); I != E; ++I)
219 mark(*I);
220 return true;
221 }
222
223 bool VisitIfStmt(IfStmt *S) {
224 mark(S->getThen());
225 mark(S->getElse());
226 return true;
227 }
228
229 bool VisitWhileStmt(WhileStmt *S) {
230 mark(S->getBody());
231 return true;
232 }
233
234 bool VisitDoStmt(DoStmt *S) {
235 mark(S->getBody());
236 return true;
237 }
238
239 bool VisitForStmt(ForStmt *S) {
240 mark(S->getInit());
241 mark(S->getInc());
242 mark(S->getBody());
243 return true;
244 }
245
246private:
247 void mark(Stmt *S) {
248 if (!S) return;
249
John McCall7e5e5f42011-07-07 06:58:02 +0000250 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
251 S = Label->getSubStmt();
252 S = S->IgnoreImplicit();
John McCall8f0e8d22011-06-15 23:25:17 +0000253 if (Expr *E = dyn_cast<Expr>(S))
254 Removables.insert(E);
255 }
256};
257
John McCall8f0e8d22011-06-15 23:25:17 +0000258} // end anonymous namespace
259
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000260void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
261 ReferenceClear(refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000262}
263
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000264void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
265 ReferenceCollector(D, refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000266}
267
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000268void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
269 RemovablesCollector(exprs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000270}
271
272//===----------------------------------------------------------------------===//
273// getAllTransformations.
274//===----------------------------------------------------------------------===//
275
276static void independentTransforms(MigrationPass &pass) {
277 rewriteAutoreleasePool(pass);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000278 rewriteProperties(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000279 removeRetainReleaseDealloc(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000280 rewriteUnusedInitDelegate(pass);
281 removeZeroOutPropsInDealloc(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000282 makeAssignARCSafe(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000283 rewriteUnbridgedCasts(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000284 rewriteBlockObjCVariable(pass);
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000285 checkAPIUses(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000286}
287
288std::vector<TransformFn> arcmt::getAllTransformations() {
289 std::vector<TransformFn> transforms;
290
John McCall8f0e8d22011-06-15 23:25:17 +0000291 transforms.push_back(independentTransforms);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000292 // This depends on previous transformations removing various expressions.
293 transforms.push_back(removeEmptyStatementsAndDealloc);
John McCall8f0e8d22011-06-15 23:25:17 +0000294
295 return transforms;
296}