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" |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
| 16 | #include "clang/Lex/Lexer.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | #include "llvm/ADT/DenseSet.h" |
| 20 | #include <map> |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace arcmt; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 24 | using namespace trans; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 25 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 26 | ASTTraverser::~ASTTraverser() { } |
| 27 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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. |
Argyrios Kyrtzidis | 5363e8d | 2011-07-12 22:16:25 +0000 | [diff] [blame] | 78 | if (Class->isForwardDecl()) |
| 79 | return false; // forward classes are not verifiable, therefore not safe. |
Argyrios Kyrtzidis | 86625b5 | 2011-07-12 22:05:17 +0000 | [diff] [blame] | 80 | if (Class->isArcWeakrefUnavailable()) |
| 81 | return false; |
| 82 | if (isClassInWeakBlacklist(Class)) |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 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, |
| 94 | ASTContext &Ctx) { |
Argyrios Kyrtzidis | aec230d | 2011-09-01 20:53:18 +0000 | [diff] [blame] | 95 | SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx); |
| 96 | if (SemiLoc.isInvalid()) |
| 97 | return SourceLocation(); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 98 | return SemiLoc.getLocWithOffset(1); |
Argyrios Kyrtzidis | aec230d | 2011-09-01 20:53:18 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /// \brief \arg Loc is the end of a statement range. This returns the location |
| 102 | /// of the semicolon following the statement. |
| 103 | /// If no semicolon is found or the location is inside a macro, the returned |
| 104 | /// source location will be invalid. |
| 105 | SourceLocation trans::findSemiAfterLocation(SourceLocation loc, |
| 106 | ASTContext &Ctx) { |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 107 | SourceManager &SM = Ctx.getSourceManager(); |
| 108 | if (loc.isMacroID()) { |
Chandler Carruth | 433db06 | 2011-07-14 08:20:40 +0000 | [diff] [blame] | 109 | if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions())) |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 110 | return SourceLocation(); |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 111 | loc = SM.getExpansionRange(loc).second; |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 112 | } |
| 113 | loc = Lexer::getLocForEndOfToken(loc, /*Offset=*/0, SM, Ctx.getLangOptions()); |
| 114 | |
| 115 | // Break down the source location. |
| 116 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); |
| 117 | |
| 118 | // Try to load the file buffer. |
| 119 | bool invalidTemp = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 120 | StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 121 | if (invalidTemp) |
| 122 | return SourceLocation(); |
| 123 | |
| 124 | const char *tokenBegin = file.data() + locInfo.second; |
| 125 | |
| 126 | // Lex from the start of the given location. |
| 127 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), |
| 128 | Ctx.getLangOptions(), |
| 129 | file.begin(), tokenBegin, file.end()); |
| 130 | Token tok; |
| 131 | lexer.LexFromRawLexer(tok); |
| 132 | if (tok.isNot(tok::semi)) |
| 133 | return SourceLocation(); |
| 134 | |
Argyrios Kyrtzidis | aec230d | 2011-09-01 20:53:18 +0000 | [diff] [blame] | 135 | return tok.getLocation(); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) { |
| 139 | if (!E || !E->HasSideEffects(Ctx)) |
| 140 | return false; |
| 141 | |
| 142 | E = E->IgnoreParenCasts(); |
| 143 | ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E); |
| 144 | if (!ME) |
| 145 | return true; |
| 146 | switch (ME->getMethodFamily()) { |
| 147 | case OMF_autorelease: |
| 148 | case OMF_dealloc: |
| 149 | case OMF_release: |
| 150 | case OMF_retain: |
| 151 | switch (ME->getReceiverKind()) { |
| 152 | case ObjCMessageExpr::SuperInstance: |
| 153 | return false; |
| 154 | case ObjCMessageExpr::Instance: |
| 155 | return hasSideEffects(ME->getInstanceReceiver(), Ctx); |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | break; |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return true; |
| 165 | } |
| 166 | |
Argyrios Kyrtzidis | 2c18ca0 | 2011-07-14 23:32:04 +0000 | [diff] [blame] | 167 | bool trans::isGlobalVar(Expr *E) { |
| 168 | E = E->IgnoreParenCasts(); |
| 169 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 170 | return DRE->getDecl()->getDeclContext()->isFileContext() && |
| 171 | DRE->getDecl()->getLinkage() == ExternalLinkage; |
Argyrios Kyrtzidis | 2c18ca0 | 2011-07-14 23:32:04 +0000 | [diff] [blame] | 172 | if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E)) |
| 173 | return isGlobalVar(condOp->getTrueExpr()) && |
| 174 | isGlobalVar(condOp->getFalseExpr()); |
| 175 | |
| 176 | return false; |
| 177 | } |
| 178 | |
Argyrios Kyrtzidis | 18fd0c6 | 2011-07-27 05:28:18 +0000 | [diff] [blame] | 179 | StringRef trans::getNilString(ASTContext &Ctx) { |
| 180 | if (Ctx.Idents.get("nil").hasMacroDefinition()) |
| 181 | return "nil"; |
| 182 | else |
| 183 | return "0"; |
| 184 | } |
| 185 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 186 | namespace { |
| 187 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 188 | class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> { |
| 189 | ExprSet &Refs; |
| 190 | public: |
| 191 | ReferenceClear(ExprSet &refs) : Refs(refs) { } |
| 192 | bool VisitDeclRefExpr(DeclRefExpr *E) { Refs.erase(E); return true; } |
| 193 | bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Refs.erase(E); return true; } |
| 194 | }; |
| 195 | |
| 196 | class ReferenceCollector : public RecursiveASTVisitor<ReferenceCollector> { |
| 197 | ValueDecl *Dcl; |
| 198 | ExprSet &Refs; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 199 | |
| 200 | public: |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 201 | ReferenceCollector(ValueDecl *D, ExprSet &refs) |
| 202 | : Dcl(D), Refs(refs) { } |
| 203 | |
| 204 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 205 | if (E->getDecl() == Dcl) |
| 206 | Refs.insert(E); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 211 | if (E->getDecl() == Dcl) |
| 212 | Refs.insert(E); |
| 213 | return true; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | class RemovablesCollector : public RecursiveASTVisitor<RemovablesCollector> { |
| 218 | ExprSet &Removables; |
| 219 | |
| 220 | public: |
| 221 | RemovablesCollector(ExprSet &removables) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 222 | : Removables(removables) { } |
| 223 | |
| 224 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 225 | |
| 226 | bool TraverseStmtExpr(StmtExpr *E) { |
| 227 | CompoundStmt *S = E->getSubStmt(); |
| 228 | for (CompoundStmt::body_iterator |
| 229 | I = S->body_begin(), E = S->body_end(); I != E; ++I) { |
| 230 | if (I != E - 1) |
| 231 | mark(*I); |
| 232 | TraverseStmt(*I); |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool VisitCompoundStmt(CompoundStmt *S) { |
| 238 | for (CompoundStmt::body_iterator |
| 239 | I = S->body_begin(), E = S->body_end(); I != E; ++I) |
| 240 | mark(*I); |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | bool VisitIfStmt(IfStmt *S) { |
| 245 | mark(S->getThen()); |
| 246 | mark(S->getElse()); |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | bool VisitWhileStmt(WhileStmt *S) { |
| 251 | mark(S->getBody()); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | bool VisitDoStmt(DoStmt *S) { |
| 256 | mark(S->getBody()); |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | bool VisitForStmt(ForStmt *S) { |
| 261 | mark(S->getInit()); |
| 262 | mark(S->getInc()); |
| 263 | mark(S->getBody()); |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | void mark(Stmt *S) { |
| 269 | if (!S) return; |
| 270 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 271 | while (LabelStmt *Label = dyn_cast<LabelStmt>(S)) |
| 272 | S = Label->getSubStmt(); |
| 273 | S = S->IgnoreImplicit(); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 274 | if (Expr *E = dyn_cast<Expr>(S)) |
| 275 | Removables.insert(E); |
| 276 | } |
| 277 | }; |
| 278 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 279 | } // end anonymous namespace |
| 280 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 281 | void trans::clearRefsIn(Stmt *S, ExprSet &refs) { |
| 282 | ReferenceClear(refs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 285 | void trans::collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs) { |
| 286 | ReferenceCollector(D, refs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 289 | void trans::collectRemovables(Stmt *S, ExprSet &exprs) { |
| 290 | RemovablesCollector(exprs).TraverseStmt(S); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 294 | // MigrationContext |
| 295 | //===----------------------------------------------------------------------===// |
| 296 | |
| 297 | namespace { |
| 298 | |
| 299 | class ASTTransform : public RecursiveASTVisitor<ASTTransform> { |
| 300 | MigrationContext &MigrateCtx; |
| 301 | |
| 302 | public: |
| 303 | ASTTransform(MigrationContext &MigrateCtx) : MigrateCtx(MigrateCtx) { } |
| 304 | |
| 305 | bool TraverseStmt(Stmt *rootS) { |
| 306 | if (!rootS) |
| 307 | return true; |
| 308 | |
| 309 | BodyContext BodyCtx(MigrateCtx, rootS); |
| 310 | for (MigrationContext::traverser_iterator |
| 311 | I = MigrateCtx.traversers_begin(), |
| 312 | E = MigrateCtx.traversers_end(); I != E; ++I) |
| 313 | (*I)->traverseBody(BodyCtx); |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | } |
| 320 | |
| 321 | MigrationContext::~MigrationContext() { |
| 322 | for (traverser_iterator |
| 323 | I = traversers_begin(), E = traversers_end(); I != E; ++I) |
| 324 | delete *I; |
| 325 | } |
| 326 | |
| 327 | void MigrationContext::traverse(TranslationUnitDecl *TU) { |
| 328 | ASTTransform(*this).TraverseDecl(TU); |
| 329 | } |
| 330 | |
| 331 | //===----------------------------------------------------------------------===// |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 332 | // getAllTransformations. |
| 333 | //===----------------------------------------------------------------------===// |
| 334 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 335 | static void traverseAST(MigrationPass &pass) { |
| 336 | MigrationContext MigrateCtx(pass); |
| 337 | |
| 338 | if (pass.isGCMigration()) { |
| 339 | MigrateCtx.addTraverser(new GCCollectableCallsTraverser); |
| 340 | } |
| 341 | |
| 342 | MigrateCtx.traverse(pass.Ctx.getTranslationUnitDecl()); |
| 343 | } |
| 344 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 345 | static void independentTransforms(MigrationPass &pass) { |
| 346 | rewriteAutoreleasePool(pass); |
Argyrios Kyrtzidis | 14c4b44 | 2011-07-13 19:22:00 +0000 | [diff] [blame] | 347 | rewriteProperties(pass); |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame^] | 348 | removeRetainReleaseDeallocFinalize(pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 349 | rewriteUnusedInitDelegate(pass); |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame^] | 350 | removeZeroOutPropsInDeallocFinalize(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 351 | makeAssignARCSafe(pass); |
Argyrios Kyrtzidis | 7196d06 | 2011-06-21 20:20:39 +0000 | [diff] [blame] | 352 | rewriteUnbridgedCasts(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 353 | rewriteBlockObjCVariable(pass); |
Argyrios Kyrtzidis | fd10398 | 2011-07-18 07:44:45 +0000 | [diff] [blame] | 354 | checkAPIUses(pass); |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 355 | traverseAST(pass); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Argyrios Kyrtzidis | e0ac745 | 2011-11-04 15:58:08 +0000 | [diff] [blame] | 358 | std::vector<TransformFn> arcmt::getAllTransformations( |
| 359 | LangOptions::GCMode OrigGCMode) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 360 | std::vector<TransformFn> transforms; |
| 361 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 362 | transforms.push_back(independentTransforms); |
Argyrios Kyrtzidis | fd3455a | 2011-06-21 20:20:42 +0000 | [diff] [blame] | 363 | // This depends on previous transformations removing various expressions. |
Argyrios Kyrtzidis | e7ef855 | 2011-11-04 15:58:22 +0000 | [diff] [blame^] | 364 | transforms.push_back(removeEmptyStatementsAndDeallocFinalize); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 365 | |
| 366 | return transforms; |
| 367 | } |