1 //===--- TransAutoreleasePool.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 //===----------------------------------------------------------------------===// 9 // 10 // rewriteAutoreleasePool: 11 // 12 // Calls to NSAutoreleasePools will be rewritten as an @autorelease scope. 13 // 14 // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 15 // ... 16 // [pool release]; 17 // ----> 18 // @autorelease { 19 // ... 20 // } 21 // 22 // An NSAutoreleasePool will not be touched if: 23 // - There is not a corresponding -release/-drain in the same scope 24 // - Not all references of the NSAutoreleasePool variable can be removed 25 // - There is a variable that is declared inside the intended @autorelease scope 26 // which is also used outside it. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #include "Transforms.h" 31 #include "Internals.h" 32 #include "clang/Sema/SemaDiagnostic.h" 33 #include "clang/Basic/SourceManager.h" 34 #include <map> 35 36 using namespace clang; 37 using namespace arcmt; 38 using namespace trans; 39 40 namespace { 41 42 class ReleaseCollector : public RecursiveASTVisitor<ReleaseCollector> { 43 Decl *Dcl; 44 SmallVectorImpl<ObjCMessageExpr *> &Releases; 45 46 public: 47 ReleaseCollector(Decl *D, SmallVectorImpl<ObjCMessageExpr *> &releases) 48 : Dcl(D), Releases(releases) { } 49 50 bool VisitObjCMessageExpr(ObjCMessageExpr *E) { 51 if (!E->isInstanceMessage()) 52 return true; 53 if (E->getMethodFamily() != OMF_release) 54 return true; 55 Expr *instance = E->getInstanceReceiver()->IgnoreParenCasts(); 56 if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(instance)) { 57 if (DE->getDecl() == Dcl) 58 Releases.push_back(E); 59 } 60 return true; 61 } 62 }; 63 64 } 65 66 namespace { 67 68 class AutoreleasePoolRewriter 69 : public RecursiveASTVisitor<AutoreleasePoolRewriter> { 70 public: 71 AutoreleasePoolRewriter(MigrationPass &pass) 72 : Body(0), Pass(pass) { 73 PoolII = &pass.Ctx.Idents.get("NSAutoreleasePool"); 74 DrainSel = pass.Ctx.Selectors.getNullarySelector( 75 &pass.Ctx.Idents.get("drain")); 76 } 77 78 void transformBody(Stmt *body) { 79 Body = body; 80 TraverseStmt(body); 81 } 82 83 ~AutoreleasePoolRewriter() { 84 SmallVector<VarDecl *, 8> VarsToHandle; 85 86 for (std::map<VarDecl *, PoolVarInfo>::iterator 87 I = PoolVars.begin(), E = PoolVars.end(); I != E; ++I) { 88 VarDecl *var = I->first; 89 PoolVarInfo &info = I->second; 90 91 // Check that we can handle/rewrite all references of the pool. 92 93 clearRefsIn(info.Dcl, info.Refs); 94 for (SmallVectorImpl<PoolScope>::iterator 95 scpI = info.Scopes.begin(), 96 scpE = info.Scopes.end(); scpI != scpE; ++scpI) { 97 PoolScope &scope = *scpI; 98 clearRefsIn(*scope.Begin, info.Refs); 99 clearRefsIn(*scope.End, info.Refs); 100 clearRefsIn(scope.Releases.begin(), scope.Releases.end(), info.Refs); 101 } 102 103 // Even if one reference is not handled we will not do anything about that 104 // pool variable. 105 if (info.Refs.empty()) 106 VarsToHandle.push_back(var); 107 } 108 109 for (unsigned i = 0, e = VarsToHandle.size(); i != e; ++i) { 110 PoolVarInfo &info = PoolVars[VarsToHandle[i]]; 111 112 Transaction Trans(Pass.TA); 113 114 clearUnavailableDiags(info.Dcl); 115 Pass.TA.removeStmt(info.Dcl); 116 117 // Add "@autoreleasepool { }" 118 for (SmallVectorImpl<PoolScope>::iterator 119 scpI = info.Scopes.begin(), 120 scpE = info.Scopes.end(); scpI != scpE; ++scpI) { 121 PoolScope &scope = *scpI; 122 clearUnavailableDiags(*scope.Begin); 123 clearUnavailableDiags(*scope.End); 124 if (scope.IsFollowedBySimpleReturnStmt) { 125 // Include the return in the scope. 126 Pass.TA.replaceStmt(*scope.Begin, "@autoreleasepool {"); 127 Pass.TA.removeStmt(*scope.End); 128 Stmt::child_iterator retI = scope.End; 129 ++retI; 130 SourceLocation afterSemi = findLocationAfterSemi((*retI)->getLocEnd(), 131 Pass.Ctx); 132 assert(afterSemi.isValid() && 133 "Didn't we check before setting IsFollowedBySimpleReturnStmt " 134 "to true?"); 135 Pass.TA.insertAfterToken(afterSemi, "\n}"); 136 Pass.TA.increaseIndentation( 137 SourceRange(scope.getIndentedRange().getBegin(), 138 (*retI)->getLocEnd()), 139 scope.CompoundParent->getLocStart()); 140 } else { 141 Pass.TA.replaceStmt(*scope.Begin, "@autoreleasepool {"); 142 Pass.TA.replaceStmt(*scope.End, "}"); 143 Pass.TA.increaseIndentation(scope.getIndentedRange(), 144 scope.CompoundParent->getLocStart()); 145 } 146 } 147 148 // Remove rest of pool var references. 149 for (SmallVectorImpl<PoolScope>::iterator 150 scpI = info.Scopes.begin(), 151 scpE = info.Scopes.end(); scpI != scpE; ++scpI) { 152 PoolScope &scope = *scpI; 153 for (SmallVectorImpl<ObjCMessageExpr *>::iterator 154 relI = scope.Releases.begin(), 155 relE = scope.Releases.end(); relI != relE; ++relI) { 156 clearUnavailableDiags(*relI); 157 Pass.TA.removeStmt(*relI); 158 } 159 } 160 } 161 } 162 163 bool VisitCompoundStmt(CompoundStmt *S) { 164 SmallVector<PoolScope, 4> Scopes; 165 166 for (Stmt::child_iterator 167 I = S->body_begin(), E = S->body_end(); I != E; ++I) { 168 Stmt *child = getEssential(*I); 169 if (DeclStmt *DclS = dyn_cast<DeclStmt>(child)) { 170 if (DclS->isSingleDecl()) { 171 if (VarDecl *VD = dyn_cast<VarDecl>(DclS->getSingleDecl())) { 172 if (isNSAutoreleasePool(VD->getType())) { 173 PoolVarInfo &info = PoolVars[VD]; 174 info.Dcl = DclS; 175 collectRefs(VD, S, info.Refs); 176 // Does this statement follow the pattern: 177 // NSAutoreleasePool * pool = [NSAutoreleasePool new]; 178 if (isPoolCreation(VD->getInit())) { 179 Scopes.push_back(PoolScope()); 180 Scopes.back().PoolVar = VD; 181 Scopes.back().CompoundParent = S; 182 Scopes.back().Begin = I; 183 } 184 } 185 } 186 } 187 } else if (BinaryOperator *bop = dyn_cast<BinaryOperator>(child)) { 188 if (DeclRefExpr *dref = dyn_cast<DeclRefExpr>(bop->getLHS())) { 189 if (VarDecl *VD = dyn_cast<VarDecl>(dref->getDecl())) { 190 // Does this statement follow the pattern: 191 // pool = [NSAutoreleasePool new]; 192 if (isNSAutoreleasePool(VD->getType()) && 193 isPoolCreation(bop->getRHS())) { 194 Scopes.push_back(PoolScope()); 195 Scopes.back().PoolVar = VD; 196 Scopes.back().CompoundParent = S; 197 Scopes.back().Begin = I; 198 } 199 } 200 } 201 } 202 203 if (Scopes.empty()) 204 continue; 205 206 if (isPoolDrain(Scopes.back().PoolVar, child)) { 207 PoolScope &scope = Scopes.back(); 208 scope.End = I; 209 handlePoolScope(scope, S); 210 Scopes.pop_back(); 211 } 212 } 213 return true; 214 } 215 216 private: 217 void clearUnavailableDiags(Stmt *S) { 218 if (S) 219 Pass.TA.clearDiagnostic(diag::err_unavailable, 220 diag::err_unavailable_message, 221 S->getSourceRange()); 222 } 223 224 struct PoolScope { 225 VarDecl *PoolVar; 226 CompoundStmt *CompoundParent; 227 Stmt::child_iterator Begin; 228 Stmt::child_iterator End; 229 bool IsFollowedBySimpleReturnStmt; 230 SmallVector<ObjCMessageExpr *, 4> Releases; 231 232 PoolScope() : PoolVar(0), CompoundParent(0), Begin(), End(), 233 IsFollowedBySimpleReturnStmt(false) { } 234 235 SourceRange getIndentedRange() const { 236 Stmt::child_iterator rangeS = Begin; 237 ++rangeS; 238 if (rangeS == End) 239 return SourceRange(); 240 Stmt::child_iterator rangeE = Begin; 241 for (Stmt::child_iterator I = rangeS; I != End; ++I) 242 ++rangeE; 243 return SourceRange((*rangeS)->getLocStart(), (*rangeE)->getLocEnd()); 244 } 245 }; 246 247 class NameReferenceChecker : public RecursiveASTVisitor<NameReferenceChecker>{ 248 ASTContext &Ctx; 249 SourceRange ScopeRange; 250 SourceLocation &referenceLoc, &declarationLoc; 251 252 public: 253 NameReferenceChecker(ASTContext &ctx, PoolScope &scope, 254 SourceLocation &referenceLoc, 255 SourceLocation &declarationLoc) 256 : Ctx(ctx), referenceLoc(referenceLoc), 257 declarationLoc(declarationLoc) { 258 ScopeRange = SourceRange((*scope.Begin)->getLocStart(), 259 (*scope.End)->getLocStart()); 260 } 261 262 bool VisitDeclRefExpr(DeclRefExpr *E) { 263 return checkRef(E->getLocation(), E->getDecl()->getLocation()); 264 } 265 266 bool VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { 267 return checkRef(E->getLocation(), E->getDecl()->getLocation()); 268 } 269 270 bool VisitTypedefTypeLoc(TypedefTypeLoc TL) { 271 return checkRef(TL.getBeginLoc(), TL.getTypedefNameDecl()->getLocation()); 272 } 273 274 bool VisitTagTypeLoc(TagTypeLoc TL) { 275 return checkRef(TL.getBeginLoc(), TL.getDecl()->getLocation()); 276 } 277 278 private: 279 bool checkRef(SourceLocation refLoc, SourceLocation declLoc) { 280 if (isInScope(declLoc)) { 281 referenceLoc = refLoc; 282 declarationLoc = declLoc; 283 return false; 284 } 285 return true; 286 } 287 288 bool isInScope(SourceLocation loc) { 289 if (loc.isInvalid()) 290 return false; 291 292 SourceManager &SM = Ctx.getSourceManager(); 293 if (SM.isBeforeInTranslationUnit(loc, ScopeRange.getBegin())) 294 return false; 295 return SM.isBeforeInTranslationUnit(loc, ScopeRange.getEnd()); 296 } 297 }; 298 299 void handlePoolScope(PoolScope &scope, CompoundStmt *compoundS) { 300 // Check that all names declared inside the scope are not used 301 // outside the scope. 302 { 303 bool nameUsedOutsideScope = false; 304 SourceLocation referenceLoc, declarationLoc; 305 Stmt::child_iterator SI = scope.End, SE = compoundS->body_end(); 306 ++SI; 307 // Check if the autoreleasepool scope is followed by a simple return 308 // statement, in which case we will include the return in the scope. 309 if (SI != SE) 310 if (ReturnStmt *retS = dyn_cast<ReturnStmt>(*SI)) 311 if ((retS->getRetValue() == 0 || 312 isa<DeclRefExpr>(retS->getRetValue()->IgnoreParenCasts())) && 313 findLocationAfterSemi(retS->getLocEnd(), Pass.Ctx).isValid()) { 314 scope.IsFollowedBySimpleReturnStmt = true; 315 ++SI; // the return will be included in scope, don't check it. 316 } 317 318 for (; SI != SE; ++SI) { 319 nameUsedOutsideScope = !NameReferenceChecker(Pass.Ctx, scope, 320 referenceLoc, 321 declarationLoc).TraverseStmt(*SI); 322 if (nameUsedOutsideScope) 323 break; 324 } 325 326 // If not all references were cleared it means some variables/typenames/etc 327 // declared inside the pool scope are used outside of it. 328 // We won't try to rewrite the pool. 329 if (nameUsedOutsideScope) { 330 Pass.TA.reportError("a name is referenced outside the " 331 "NSAutoreleasePool scope that it was declared in", referenceLoc); 332 Pass.TA.reportNote("name declared here", declarationLoc); 333 Pass.TA.reportNote("intended @autoreleasepool scope begins here", 334 (*scope.Begin)->getLocStart()); 335 Pass.TA.reportNote("intended @autoreleasepool scope ends here", 336 (*scope.End)->getLocStart()); 337 return; 338 } 339 } 340 341 // Collect all releases of the pool; they will be removed. 342 { 343 ReleaseCollector releaseColl(scope.PoolVar, scope.Releases); 344 Stmt::child_iterator I = scope.Begin; 345 ++I; 346 for (; I != scope.End; ++I) 347 releaseColl.TraverseStmt(*I); 348 } 349 350 PoolVars[scope.PoolVar].Scopes.push_back(scope); 351 } 352 353 bool isPoolCreation(Expr *E) { 354 if (!E) return false; 355 E = getEssential(E); 356 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E); 357 if (!ME) return false; 358 if (ME->getMethodFamily() == OMF_new && 359 ME->getReceiverKind() == ObjCMessageExpr::Class && 360 isNSAutoreleasePool(ME->getReceiverInterface())) 361 return true; 362 if (ME->getReceiverKind() == ObjCMessageExpr::Instance && 363 ME->getMethodFamily() == OMF_init) { 364 Expr *rec = getEssential(ME->getInstanceReceiver()); 365 if (ObjCMessageExpr *recME = dyn_cast_or_null<ObjCMessageExpr>(rec)) { 366 if (recME->getMethodFamily() == OMF_alloc && 367 recME->getReceiverKind() == ObjCMessageExpr::Class && 368 isNSAutoreleasePool(recME->getReceiverInterface())) 369 return true; 370 } 371 } 372 373 return false; 374 } 375 376 bool isPoolDrain(VarDecl *poolVar, Stmt *S) { 377 if (!S) return false; 378 S = getEssential(S); 379 ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S); 380 if (!ME) return false; 381 if (ME->getReceiverKind() == ObjCMessageExpr::Instance) { 382 Expr *rec = getEssential(ME->getInstanceReceiver()); 383 if (DeclRefExpr *dref = dyn_cast<DeclRefExpr>(rec)) 384 if (dref->getDecl() == poolVar) 385 return ME->getMethodFamily() == OMF_release || 386 ME->getSelector() == DrainSel; 387 } 388 389 return false; 390 } 391 392 bool isNSAutoreleasePool(ObjCInterfaceDecl *IDecl) { 393 return IDecl && IDecl->getIdentifier() == PoolII; 394 } 395 396 bool isNSAutoreleasePool(QualType Ty) { 397 QualType pointee = Ty->getPointeeType(); 398 if (pointee.isNull()) 399 return false; 400 if (const ObjCInterfaceType *interT = pointee->getAs<ObjCInterfaceType>()) 401 return isNSAutoreleasePool(interT->getDecl()); 402 return false; 403 } 404 405 static Expr *getEssential(Expr *E) { 406 return cast<Expr>(getEssential((Stmt*)E)); 407 } 408 static Stmt *getEssential(Stmt *S) { 409 if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S)) 410 S = EWC->getSubExpr(); 411 if (Expr *E = dyn_cast<Expr>(S)) 412 S = E->IgnoreParenCasts(); 413 return S; 414 } 415 416 Stmt *Body; 417 MigrationPass &Pass; 418 419 IdentifierInfo *PoolII; 420 Selector DrainSel; 421 422 struct PoolVarInfo { 423 DeclStmt *Dcl; 424 ExprSet Refs; 425 SmallVector<PoolScope, 2> Scopes; 426 427 PoolVarInfo() : Dcl(0) { } 428 }; 429 430 std::map<VarDecl *, PoolVarInfo> PoolVars; 431 }; 432 433 } // anonymous namespace 434 435 void trans::rewriteAutoreleasePool(MigrationPass &pass) { 436 BodyTransform<AutoreleasePoolRewriter> trans(pass); 437 trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl()); 438 } 439