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