48 : Dcl(
D), Releases(releases) { }
51 if (!
E->isInstanceMessage())
56 if (
DeclRefExpr *DE = dyn_cast<DeclRefExpr>(instance)) {
57 if (DE->getDecl() == Dcl)
58 Releases.push_back(
E);
68class AutoreleasePoolRewriter
72 : Body(nullptr), Pass(pass) {
78 void transformBody(
Stmt *body,
Decl *ParentD) {
83 ~AutoreleasePoolRewriter() {
86 for (std::map<VarDecl *, PoolVarInfo>::iterator
87 I = PoolVars.begin(),
E = PoolVars.end();
I !=
E; ++
I) {
89 PoolVarInfo &info =
I->second;
95 scpI = info.Scopes.begin(),
96 scpE = info.Scopes.end(); scpI != scpE; ++scpI) {
97 PoolScope &scope = *scpI;
100 clearRefsIn(scope.Releases.begin(), scope.Releases.end(), info.Refs);
105 if (info.Refs.empty())
106 VarsToHandle.push_back(var);
109 for (
unsigned i = 0, e = VarsToHandle.size(); i != e; ++i) {
110 PoolVarInfo &info = PoolVars[VarsToHandle[i]];
114 clearUnavailableDiags(info.Dcl);
115 Pass.TA.removeStmt(info.Dcl);
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) {
126 Pass.TA.replaceStmt(*scope.Begin,
"@autoreleasepool {");
127 Pass.TA.removeStmt(*scope.End);
133 "Didn't we check before setting IsFollowedBySimpleReturnStmt "
135 Pass.TA.insertAfterToken(afterSemi,
"\n}");
136 Pass.TA.increaseIndentation(
138 (*retI)->getEndLoc()),
139 scope.CompoundParent->getBeginLoc());
141 Pass.TA.replaceStmt(*scope.Begin,
"@autoreleasepool {");
142 Pass.TA.replaceStmt(*scope.End,
"}");
143 Pass.TA.increaseIndentation(scope.getIndentedRange(),
144 scope.CompoundParent->getBeginLoc());
150 scpI = info.Scopes.begin(),
151 scpE = info.Scopes.end(); scpI != scpE; ++scpI) {
152 PoolScope &scope = *scpI;
154 relI = scope.Releases.begin(),
155 relE = scope.Releases.end(); relI != relE; ++relI) {
156 clearUnavailableDiags(*relI);
157 Pass.TA.removeStmt(*relI);
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];
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;
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())) {
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;
206 if (isPoolDrain(Scopes.back().PoolVar, child)) {
207 PoolScope &scope = Scopes.back();
209 handlePoolScope(scope, S);
217 void clearUnavailableDiags(
Stmt *S) {
219 Pass.TA.clearDiagnostic(diag::err_unavailable,
220 diag::err_unavailable_message,
221 S->getSourceRange());
229 bool IsFollowedBySimpleReturnStmt;
233 : PoolVar(nullptr), CompoundParent(nullptr),
234 IsFollowedBySimpleReturnStmt(
false) {}
244 return SourceRange((*rangeS)->getBeginLoc(), (*rangeE)->getEndLoc());
254 NameReferenceChecker(
ASTContext &ctx, PoolScope &scope,
257 : Ctx(ctx), referenceLoc(referenceLoc),
258 declarationLoc(declarationLoc) {
259 ScopeRange =
SourceRange((*scope.Begin)->getBeginLoc(),
260 (*scope.End)->getBeginLoc());
264 return checkRef(
E->getLocation(),
E->getDecl()->getLocation());
277 if (isInScope(declLoc)) {
278 referenceLoc = refLoc;
279 declarationLoc = declLoc;
290 if (
SM.isBeforeInTranslationUnit(loc, ScopeRange.
getBegin()))
292 return SM.isBeforeInTranslationUnit(loc, ScopeRange.
getEnd());
296 void handlePoolScope(PoolScope &scope,
CompoundStmt *compoundS) {
300 bool nameUsedOutsideScope =
false;
307 if (
ReturnStmt *retS = dyn_cast<ReturnStmt>(*SI))
308 if ((retS->getRetValue() ==
nullptr ||
309 isa<DeclRefExpr>(retS->getRetValue()->IgnoreParenCasts())) &&
311 scope.IsFollowedBySimpleReturnStmt =
true;
315 for (; SI != SE; ++SI) {
316 nameUsedOutsideScope = !NameReferenceChecker(Pass.Ctx, scope,
318 declarationLoc).TraverseStmt(*SI);
319 if (nameUsedOutsideScope)
326 if (nameUsedOutsideScope) {
327 Pass.TA.reportError(
"a name is referenced outside the "
328 "NSAutoreleasePool scope that it was declared in", referenceLoc);
329 Pass.TA.reportNote(
"name declared here", declarationLoc);
330 Pass.TA.reportNote(
"intended @autoreleasepool scope begins here",
331 (*scope.Begin)->getBeginLoc());
332 Pass.TA.reportNote(
"intended @autoreleasepool scope ends here",
333 (*scope.End)->getBeginLoc());
340 ReleaseCollector releaseColl(scope.PoolVar, scope.Releases);
343 for (;
I != scope.End; ++
I)
344 releaseColl.TraverseStmt(*
I);
347 PoolVars[scope.PoolVar].Scopes.push_back(scope);
350 bool isPoolCreation(
Expr *
E) {
351 if (!
E)
return false;
354 if (!ME)
return false;
363 if (recME->getMethodFamily() ==
OMF_alloc &&
365 isNSAutoreleasePool(recME->getReceiverInterface()))
374 if (!S)
return false;
377 if (!ME)
return false;
380 if (
DeclRefExpr *dref = dyn_cast<DeclRefExpr>(rec))
381 if (dref->getDecl() == poolVar)
393 bool isNSAutoreleasePool(
QualType Ty) {
398 return isNSAutoreleasePool(interT->getDecl());
403 return cast<Expr>(getEssential((
Stmt*)
E));
405 static Stmt *getEssential(
Stmt *S) {
406 if (
FullExpr *FE = dyn_cast<FullExpr>(S))
407 S = FE->getSubExpr();
408 if (
Expr *
E = dyn_cast<Expr>(S))
424 PoolVarInfo() =
default;
427 std::map<VarDecl *, PoolVarInfo> PoolVars;
Defines the clang::ASTContext interface.
Defines the SourceManager interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
TranslationUnitDecl * getTranslationUnitDecl() const
SelectorTable & Selectors
A builtin binary operation expression such as "x + y" or "x <= y".
CompoundStmt - This represents a group of statements like { stmt stmt }.
A reference to a declared variable, function, enum, etc.
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
SourceLocation getLocation() const
This represents one expression.
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
FullExpr - Represents a "full-expression" node.
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Represents an ObjC class declaration.
Interfaces are the core concept in Objective-C for object oriented design.
An expression that sends a message to the given Objective-C object or class.
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
ObjCMethodFamily getMethodFamily() const
Selector getSelector() const
@ Instance
The receiver is an object instance.
@ Class
The receiver is a class.
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
bool TraverseStmt(Stmt *S, DataRecursionQueue *Queue=nullptr)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Selector getNullarySelector(const IdentifierInfo *ID)
Smart pointer class that efficiently represents Objective-C method names.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Wrapper for source info for tag types.
TagDecl * getDecl() const
SourceLocation getBeginLoc() const
Get the begin source location.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
const T * getAs() const
Member-template getAs<specific type>'.
Wrapper for source info for typedefs.
TypedefNameDecl * getTypedefNameDecl() const
Represents a variable declaration or definition.
void collectRefs(ValueDecl *D, Stmt *S, ExprSet &refs)
void clearRefsIn(Stmt *S, ExprSet &refs)
llvm::DenseSet< Expr * > ExprSet
void rewriteAutoreleasePool(MigrationPass &pass)
SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx, bool IsDecl=false)
'Loc' is the end of a statement range.
constexpr Variable var(Literal L)
Returns the variable of L.
The JSON file list parser is used to communicate input to InstallAPI.