John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 1 | //===--- 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 McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 9 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 10 | #include "Transforms.h" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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" |
| 21 | #include <map> |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace arcmt; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 25 | using namespace trans; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 26 | using llvm::StringRef; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 29 | // Helpers. |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Argyrios Kyrtzidis | 86625b5 | 2011-07-12 22:05:17 +0000 | [diff] [blame^] | 32 | /// \brief True if the class is one that does not support weak. |
| 33 | static 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 | |
| 67 | bool 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 Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 87 | /// \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. |
| 91 | SourceLocation trans::findLocationAfterSemi(SourceLocation loc, |
| 92 | ASTContext &Ctx) { |
| 93 | SourceManager &SM = Ctx.getSourceManager(); |
| 94 | if (loc.isMacroID()) { |
Argyrios Kyrtzidis | 7a75960 | 2011-07-07 21:54:45 +0000 | [diff] [blame] | 95 | if (!Lexer::isAtEndOfMacroInstantiation(loc, SM, Ctx.getLangOptions())) |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 96 | 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 | |
| 124 | bool 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 McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 153 | namespace { |
| 154 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 155 | class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> { |
| 156 | ExprSet &Refs; |
| 157 | public: |
| 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 | |
| 163 | class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> { |
| 164 | ValueDecl *Dcl; |
| 165 | ExprSet &Refs; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 166 | |
| 167 | public: |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 168 | 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 | |
| 184 | class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> { |
| 185 | ExprSet &Removables; |
| 186 | |
| 187 | public: |
| 188 | RemovablesCollector(ExprSet &removables) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 189 | : 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 | |
| 234 | private: |
| 235 | void mark(Stmt *S) { |
| 236 | if (!S) return; |
| 237 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 238 | while (LabelStmt *Label = dyn_cast<LabelStmt>(S)) |
| 239 | S = Label->getSubStmt(); |
| 240 | S = S->IgnoreImplicit(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 241 | if (Expr *E = dyn_cast<Expr>(S)) |
| 242 | Removables.insert(E); |
| 243 | } |
| 244 | }; |
| 245 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 246 | } // end anonymous namespace |
| 247 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 248 | void trans::clearRefsIn(Stmt *S, ExprSet &refs) { |
| 249 | ReferenceClear(refs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 252 | void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) { |
| 253 | ReferenceCollector(D, refs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 256 | void trans::collectRemovables(Stmt *S, ExprSet &exprs) { |
| 257 | RemovablesCollector(exprs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | // getAllTransformations. |
| 262 | //===----------------------------------------------------------------------===// |
| 263 | |
| 264 | static void independentTransforms(MigrationPass &pass) { |
| 265 | rewriteAutoreleasePool(pass); |
| 266 | changeIvarsOfAssignProperties(pass); |
| 267 | removeRetainReleaseDealloc(pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 268 | rewriteUnusedInitDelegate(pass); |
| 269 | removeZeroOutPropsInDealloc(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 270 | makeAssignARCSafe(pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 271 | rewriteUnbridgedCasts(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 272 | rewriteBlockObjCVariable(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | std::vector<TransformFn> arcmt::getAllTransformations() { |
| 276 | std::vector<TransformFn> transforms; |
| 277 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 278 | transforms.push_back(independentTransforms); |
Argyrios Kyrtzidis | fd3455a | 2011-06-21 20:20:42 +0000 | [diff] [blame] | 279 | // This depends on previous transformations removing various expressions. |
| 280 | transforms.push_back(removeEmptyStatementsAndDealloc); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 281 | |
| 282 | return transforms; |
| 283 | } |