blob: 1d0e26153c4462a5eacc37b018223893c2b39ca5 [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();
Chandler Carruthedc3dcc2011-07-25 16:56:02 +000098 loc = SM.getExpansionRange(loc).second;
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +000099 }
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))
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000157 return DRE->getDecl()->getDeclContext()->isFileContext() &&
158 DRE->getDecl()->getLinkage() == ExternalLinkage;
Argyrios Kyrtzidis2c18ca02011-07-14 23:32:04 +0000159 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
160 return isGlobalVar(condOp->getTrueExpr()) &&
161 isGlobalVar(condOp->getFalseExpr());
162
163 return false;
164}
165
Argyrios Kyrtzidis18fd0c62011-07-27 05:28:18 +0000166StringRef trans::getNilString(ASTContext &Ctx) {
167 if (Ctx.Idents.get("nil").hasMacroDefinition())
168 return "nil";
169 else
170 return "0";
171}
172
John McCall8f0e8d22011-06-15 23:25:17 +0000173namespace {
174
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000175class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
176 ExprSet &Refs;
177public:
178 ReferenceClear(ExprSet &refs) : Refs(refs) { }
179 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
180 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; }
181};
182
183class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
184 ValueDecl *Dcl;
185 ExprSet &Refs;
John McCall8f0e8d22011-06-15 23:25:17 +0000186
187public:
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000188 ReferenceCollector(ValueDecl *D, ExprSet &refs)
189 : Dcl(D), Refs(refs) { }
190
191 bool VisitDeclRefExpr(DeclRefExpr *E) {
192 if (E->getDecl() == Dcl)
193 Refs.insert(E);
194 return true;
195 }
196
197 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
198 if (E->getDecl() == Dcl)
199 Refs.insert(E);
200 return true;
201 }
202};
203
204class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
205 ExprSet &Removables;
206
207public:
208 RemovablesCollector(ExprSet &removables)
John McCall8f0e8d22011-06-15 23:25:17 +0000209 : Removables(removables) { }
210
211 bool shouldWalkTypesOfTypeLocs() const { return false; }
212
213 bool TraverseStmtExpr(StmtExpr *E) {
214 CompoundStmt *S = E->getSubStmt();
215 for (CompoundStmt::body_iterator
216 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
217 if (I != E - 1)
218 mark(*I);
219 TraverseStmt(*I);
220 }
221 return true;
222 }
223
224 bool VisitCompoundStmt(CompoundStmt *S) {
225 for (CompoundStmt::body_iterator
226 I = S->body_begin(), E = S->body_end(); I != E; ++I)
227 mark(*I);
228 return true;
229 }
230
231 bool VisitIfStmt(IfStmt *S) {
232 mark(S->getThen());
233 mark(S->getElse());
234 return true;
235 }
236
237 bool VisitWhileStmt(WhileStmt *S) {
238 mark(S->getBody());
239 return true;
240 }
241
242 bool VisitDoStmt(DoStmt *S) {
243 mark(S->getBody());
244 return true;
245 }
246
247 bool VisitForStmt(ForStmt *S) {
248 mark(S->getInit());
249 mark(S->getInc());
250 mark(S->getBody());
251 return true;
252 }
253
254private:
255 void mark(Stmt *S) {
256 if (!S) return;
257
John McCall7e5e5f42011-07-07 06:58:02 +0000258 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
259 S = Label->getSubStmt();
260 S = S->IgnoreImplicit();
John McCall8f0e8d22011-06-15 23:25:17 +0000261 if (Expr *E = dyn_cast<Expr>(S))
262 Removables.insert(E);
263 }
264};
265
John McCall8f0e8d22011-06-15 23:25:17 +0000266} // end anonymous namespace
267
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000268void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
269 ReferenceClear(refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000270}
271
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000272void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
273 ReferenceCollector(D, refs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000274}
275
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000276void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
277 RemovablesCollector(exprs).TraverseStmt(S);
John McCall8f0e8d22011-06-15 23:25:17 +0000278}
279
280//===----------------------------------------------------------------------===//
281// getAllTransformations.
282//===----------------------------------------------------------------------===//
283
284static void independentTransforms(MigrationPass &pass) {
285 rewriteAutoreleasePool(pass);
Argyrios Kyrtzidis14c4b442011-07-13 19:22:00 +0000286 rewriteProperties(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000287 removeRetainReleaseDealloc(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000288 rewriteUnusedInitDelegate(pass);
289 removeZeroOutPropsInDealloc(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000290 makeAssignARCSafe(pass);
Argyrios Kyrtzidis7196d062011-06-21 20:20:39 +0000291 rewriteUnbridgedCasts(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000292 rewriteBlockObjCVariable(pass);
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000293 checkAPIUses(pass);
John McCall8f0e8d22011-06-15 23:25:17 +0000294}
295
296std::vector<TransformFn> arcmt::getAllTransformations() {
297 std::vector<TransformFn> transforms;
298
John McCall8f0e8d22011-06-15 23:25:17 +0000299 transforms.push_back(independentTransforms);
Argyrios Kyrtzidisfd3455a2011-06-21 20:20:42 +0000300 // This depends on previous transformations removing various expressions.
301 transforms.push_back(removeEmptyStatementsAndDealloc);
John McCall8f0e8d22011-06-15 23:25:17 +0000302
303 return transforms;
304}