1 //===--- Tranforms.cpp - Tranformations to ARC mode -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Transforms.h"
11 #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"
23 using namespace clang;
24 using namespace arcmt;
25 using namespace trans;
26 using llvm::StringRef;
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 /// \brief True if the class is one that does not support weak.
33 static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
37 bool inList = llvm::StringSwitch<bool>(cls->getName())
38 .Case("NSColorSpace", 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)
64 return isClassInWeakBlacklist(cls->getSuperClass());
67 bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
68 if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
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->isForwardDecl())
79 return false; // forward classes are not verifiable, therefore not safe.
80 if (Class->isArcWeakrefUnavailable())
82 if (isClassInWeakBlacklist(Class))
89 /// \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.
93 SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
95 SourceManager &SM = Ctx.getSourceManager();
96 if (loc.isMacroID()) {
97 if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions()))
98 return SourceLocation();
99 loc = SM.getInstantiationRange(loc).second;
101 loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOptions());
103 // Break down the source location.
104 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
106 // Try to load the file buffer.
107 bool invalidTemp = false;
108 llvm::StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
110 return SourceLocation();
112 const char *tokenBegin = file.data() + locInfo.second;
114 // Lex from the start of the given location.
115 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
116 Ctx.getLangOptions(),
117 file.begin(), tokenBegin, file.end());
119 lexer.LexFromRawLexer(tok);
120 if (tok.isNot(tok::semi))
121 return SourceLocation();
123 return tok.getLocation().getFileLocWithOffset(1);
126 bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
127 if (!E || !E->HasSideEffects(Ctx))
130 E = E->IgnoreParenCasts();
131 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E);
134 switch (ME->getMethodFamily()) {
135 case OMF_autorelease:
139 switch (ME->getReceiverKind()) {
140 case ObjCMessageExpr::SuperInstance:
142 case ObjCMessageExpr::Instance:
143 return hasSideEffects(ME->getInstanceReceiver(), Ctx);
155 bool trans::isGlobalVar(Expr *E) {
156 E = E->IgnoreParenCasts();
157 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
158 return DRE->getDecl()->getDeclContext()->isFileContext();
159 if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
160 return isGlobalVar(condOp->getTrueExpr()) &&
161 isGlobalVar(condOp->getFalseExpr());
168 class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
171 ReferenceClear(ExprSet &refs) : Refs(refs) { }
172 bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; }
173 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; }
176 class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> {
181 ReferenceCollector(ValueDecl *D, ExprSet &refs)
182 : Dcl(D), Refs(refs) { }
184 bool VisitDeclRefExpr(DeclRefExpr *E) {
185 if (E->getDecl() == Dcl)
190 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
191 if (E->getDecl() == Dcl)
197 class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> {
201 RemovablesCollector(ExprSet &removables)
202 : Removables(removables) { }
204 bool shouldWalkTypesOfTypeLocs() const { return false; }
206 bool TraverseStmtExpr(StmtExpr *E) {
207 CompoundStmt *S = E->getSubStmt();
208 for (CompoundStmt::body_iterator
209 I = S->body_begin(), E = S->body_end(); I != E; ++I) {
217 bool VisitCompoundStmt(CompoundStmt *S) {
218 for (CompoundStmt::body_iterator
219 I = S->body_begin(), E = S->body_end(); I != E; ++I)
224 bool VisitIfStmt(IfStmt *S) {
230 bool VisitWhileStmt(WhileStmt *S) {
235 bool VisitDoStmt(DoStmt *S) {
240 bool VisitForStmt(ForStmt *S) {
251 while (LabelStmt *Label = dyn_cast<LabelStmt>(S))
252 S = Label->getSubStmt();
253 S = S->IgnoreImplicit();
254 if (Expr *E = dyn_cast<Expr>(S))
255 Removables.insert(E);
259 } // end anonymous namespace
261 void trans::clearRefsIn(Stmt *S, ExprSet &refs) {
262 ReferenceClear(refs).TraverseStmt(S);
265 void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) {
266 ReferenceCollector(D, refs).TraverseStmt(S);
269 void trans::collectRemovables(Stmt *S, ExprSet &exprs) {
270 RemovablesCollector(exprs).TraverseStmt(S);
273 //===----------------------------------------------------------------------===//
274 // getAllTransformations.
275 //===----------------------------------------------------------------------===//
277 static void independentTransforms(MigrationPass &pass) {
278 rewriteAutoreleasePool(pass);
279 rewriteProperties(pass);
280 removeRetainReleaseDealloc(pass);
281 rewriteUnusedInitDelegate(pass);
282 removeZeroOutPropsInDealloc(pass);
283 makeAssignARCSafe(pass);
284 rewriteUnbridgedCasts(pass);
285 rewriteBlockObjCVariable(pass);
288 std::vector<TransformFn> arcmt::getAllTransformations() {
289 std::vector<TransformFn> transforms;
291 transforms.push_back(independentTransforms);
292 // This depends on previous transformations removing various expressions.
293 transforms.push_back(removeEmptyStatementsAndDealloc);