blob: c2f85f65b76b0ac45c91a6e84184d8a8a518d875 [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 +000026using llvm::StringRef;
27
28//===----------------------------------------------------------------------===//
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.
78 if (Class->isArcWeakrefUnavailable())
79 return false;
80 if (isClassInWeakBlacklist(Class))
81 return false;
82 }
83
84 return true;
85}
86
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000087/// \brief 'Loc' is the end of a statement range. This returns the location
88/// immediately after the semicolon following the statement.
89/// If no semicolon is found or the location is inside a macro, the returned
90/// source location will be invalid.
91SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
92 ASTContext &Ctx) {
93 SourceManager &SM = Ctx.getSourceManager();
94 if (loc.isMacroID()) {
Argyrios Kyrtzidis7a759602011-07-07 21:54:45 +000095 if (!Lexer::isAtEndOfMacroInstantiation(loc, SM, Ctx.getLangOptions()))
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000096 return SourceLocation();
97 loc = SM.getInstantiationRange(loc).second;
98 }
99 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOptions());
100
101 // Break down the source location.
102 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
103
104 // Try to load the file buffer.
105 bool invalidTemp = false;
106 llvm::StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
107 if (invalidTemp)
108 return SourceLocation();
109
110 const char *tokenBegin = file.data() + locInfo.second;
111
112 // Lex from the start of the given location.
113 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
114 Ctx.getLangOptions(),
115 file.begin(), tokenBegin, file.end());
116 Token tok;
117 lexer.LexFromRawLexer(tok);
118 if (tok.isNot(tok::semi))
119 return SourceLocation();
120
121 return tok.getLocation().getFileLocWithOffset(1);
122}
123
124bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
125 if (!E || !E->HasSideEffects(Ctx))
126 return false;
127
128 E = E->IgnoreParenCasts();
129 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
130 if (!ME)
131 return true;
132 switch (ME->getMethodFamily()) {
133 case OMF_autorelease:
134 case OMF_dealloc:
135 case OMF_release:
136 case OMF_retain:
137 switch (ME->getReceiverKind()) {
138 case ObjCMessageExpr::SuperInstance:
139 return false;
140 case ObjCMessageExpr::Instance:
141 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
142 default:
143 break;
144 }
145 break;
146 default:
147 break;
148 }
149
150 return true;
151}
152
John McCall8f0e8d22011-06-15 23:25:17 +0000153namespace {
154
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000155class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
156 ExprSet &Refs;
157public:
158 ReferenceClear(ExprSet &refs) : Refs(refs) { }
159 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
160 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; }
161};
162
163class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
164 ValueDecl *Dcl;
165 ExprSet &Refs;
John McCall8f0e8d22011-06-15 23:25:17 +0000166
167public:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000168 ReferenceCollector(ValueDecl *D, ExprSet &refs)
169 : Dcl(D), Refs(refs) { }
170
171 bool VisitDeclRefExpr(DeclRefExpr *E) {
172 if (E->getDecl() == Dcl)
173 Refs.insert(E);
174 return true;
175 }
176
177 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
178 if (E->getDecl() == Dcl)
179 Refs.insert(E);
180 return true;
181 }
182};
183
184class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
185 ExprSet &Removables;
186
187public:
188 RemovablesCollector(ExprSet &removables)
John McCall8f0e8d22011-06-15 23:25:17 +0000189 : Removables(removables) { }
190
191 bool shouldWalkTypesOfTypeLocs() const { return false; }
192
193 bool TraverseStmtExpr(StmtExpr *E) {
194 CompoundStmt *S = E->getSubStmt();
195 for (CompoundStmt::body_iterator
196 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
197 if (I != E - 1)
198 mark(*I);
199 TraverseStmt(*I);
200 }
201 return true;
202 }
203
204 bool VisitCompoundStmt(CompoundStmt *S) {
205 for (CompoundStmt::body_iterator
206 I = S->body_begin(), E = S->body_end(); I != E; ++I)
207 mark(*I);
208 return true;
209 }
210
211 bool VisitIfStmt(IfStmt *S) {
212 mark(S->getThen());
213 mark(S->getElse());
214 return true;
215 }
216
217 bool VisitWhileStmt(WhileStmt *S) {
218 mark(S->getBody());
219 return true;
220 }
221
222 bool VisitDoStmt(DoStmt *S) {
223 mark(S->getBody());
224 return true;
225 }
226
227 bool VisitForStmt(ForStmt *S) {
228 mark(S->getInit());
229 mark(S->getInc());
230 mark(S->getBody());
231 return true;
232 }
233
234private:
235 void mark(Stmt *S) {
236 if (!S) return;
237
John McCall7e5e5f42011-07-07 06:58:02 +0000238 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
239 S = Label->getSubStmt();
240 S = S->IgnoreImplicit();
John McCall8f0e8d22011-06-15 23:25:17 +0000241 if (Expr *E = dyn_cast<Expr>(S))
242 Removables.insert(E);
243 }
244};
245
John McCall8f0e8d22011-06-15 23:25:17 +0000246} // end anonymous namespace
247
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000248void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
249 ReferenceClear(refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000250}
251
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000252void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
253 ReferenceCollector(D, refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000254}
255
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000256void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
257 RemovablesCollector(exprs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000258}
259
260//===----------------------------------------------------------------------===//
261// getAllTransformations.
262//===----------------------------------------------------------------------===//
263
264static void independentTransforms(MigrationPass &pass) {
265 rewriteAutoreleasePool(pass);
266 changeIvarsOfAssignProperties(pass);
267 removeRetainReleaseDealloc(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000268 rewriteUnusedInitDelegate(pass);
269 removeZeroOutPropsInDealloc(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000270 makeAssignARCSafe(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000271 rewriteUnbridgedCasts(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000272 rewriteBlockObjCVariable(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000273}
274
275std::vector<TransformFn> arcmt::getAllTransformations() {
276 std::vector<TransformFn> transforms;
277
John McCall8f0e8d22011-06-15 23:25:17 +0000278 transforms.push_back(independentTransforms);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000279 // This depends on previous transformations removing various expressions.
280 transforms.push_back(removeEmptyStatementsAndDealloc);
John McCall8f0e8d22011-06-15 23:25:17 +0000281
282 return transforms;
283}