27 #include "llvm/ADT/StringSwitch.h"
29 using namespace clang;
30 using namespace arcmt;
31 using namespace trans;
35 class RetainReleaseDeallocRemover :
41 std::unique_ptr<ParentMap> StmtMap;
47 : Body(nullptr), Pass(pass) {
49 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get(
"delegate"));
51 Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get(
"finalize"));
54 void transformBody(
Stmt *body,
Decl *ParentD) {
69 if (!isCommonUnusedAutorelease(E)) {
74 Pass.TA.reportError(
"it is not safe to remove an unused 'autorelease' "
75 "message; its receiver may be destroyed immediately",
85 rec = rec->IgnoreParenImpCasts();
88 std::string err =
"it is not safe to remove '";
90 "an __unsafe_unretained type";
91 Pass.TA.reportError(err, rec->getLocStart());
97 std::string err =
"it is not safe to remove '";
100 Pass.TA.reportError(err, rec->getLocStart());
105 Pass.TA.reportError(
"it is not safe to remove 'retain' "
106 "message on the result of a 'delegate' message; "
107 "the object that was passed to 'setDelegate:' may not be "
108 "properly retained", rec->getLocStart());
124 Pass.TA.replace(E->getSourceRange(),
"self");
132 if (!rec)
return true;
138 Expr *RecContainer = Msg;
140 checkForGCDOrXPC(Msg, RecContainer, rec, RecRange);
143 isRemovable(RecContainer) && isInAtFinally(RecContainer)) {
146 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
147 std::string str =
" = ";
149 Pass.TA.insertAfterToken(RecRange.
getEnd(), str);
154 Pass.TA.replace(RecContainer->getSourceRange(), RecRange);
174 return isPlusOneAssignBeforeOrAfterAutorelease(E) ||
175 isReturnedAfterAutorelease(E);
183 Decl *RefD = getReferencedDecl(Rec);
187 Stmt *nextStmt = getNextStmt(E);
193 if (
ReturnStmt *RetS = dyn_cast<ReturnStmt>(nextStmt))
194 return RefD == getReferencedDecl(RetS->getRetValue());
204 Decl *RefD = getReferencedDecl(Rec);
208 Stmt *prevStmt, *nextStmt;
209 std::tie(prevStmt, nextStmt) = getPreviousAndNextStmt(E);
211 return isPlusOneAssignToVar(prevStmt, RefD) ||
212 isPlusOneAssignToVar(nextStmt, RefD);
215 bool isPlusOneAssignToVar(
Stmt *
S,
Decl *RefD) {
222 return (RefD == getReferencedDecl(Bop->getLHS())) &&
isPlusOneAssign(Bop);
225 if (
DeclStmt *DS = dyn_cast<DeclStmt>(S)) {
226 if (DS->isSingleDecl() && DS->getSingleDecl() == RefD) {
227 if (
VarDecl *VD = dyn_cast<VarDecl>(RefD))
237 return getPreviousAndNextStmt(E).second;
240 std::pair<Stmt *, Stmt *> getPreviousAndNextStmt(
Expr *E) {
241 Stmt *prevStmt =
nullptr, *nextStmt =
nullptr;
243 return std::make_pair(prevStmt, nextStmt);
245 Stmt *OuterS =
E, *InnerS;
248 OuterS = StmtMap->getParent(InnerS);
250 while (OuterS && (isa<ParenExpr>(OuterS) ||
251 isa<CastExpr>(OuterS) ||
252 isa<ExprWithCleanups>(OuterS)));
255 return std::make_pair(prevStmt, nextStmt);
257 Stmt::child_iterator currChildS = OuterS->child_begin();
258 Stmt::child_iterator childE = OuterS->child_end();
259 Stmt::child_iterator prevChildS = childE;
260 for (; currChildS != childE; ++currChildS) {
261 if (*currChildS == InnerS)
263 prevChildS = currChildS;
266 if (prevChildS != childE) {
267 prevStmt = *prevChildS;
269 prevStmt = prevStmt->IgnoreImplicit();
272 if (currChildS == childE)
273 return std::make_pair(prevStmt, nextStmt);
275 if (currChildS == childE)
276 return std::make_pair(prevStmt, nextStmt);
278 nextStmt = *currChildS;
280 nextStmt = nextStmt->IgnoreImplicit();
282 return std::make_pair(prevStmt, nextStmt);
291 switch (ME->getMethodFamily()) {
296 return getReferencedDecl(ME->getInstanceReceiver());
302 return DRE->getDecl();
304 return ME->getMemberDecl();
306 return IRE->getDecl();
328 Pass.Ctx.getLangOpts());
329 bool isGCDOrXPC = llvm::StringSwitch<bool>(MacroName)
330 .Case(
"dispatch_retain",
true)
331 .Case(
"dispatch_release",
true)
332 .Case(
"xpc_retain",
true)
333 .Case(
"xpc_release",
true)
341 if (
StmtExpr *SE = dyn_cast<StmtExpr>(S)) {
345 S = StmtMap->getParent(S);
351 Stmt::child_range StmtExprChild = StmtE->
children();
352 if (StmtExprChild.begin() == StmtExprChild.end())
354 auto *CompS = dyn_cast_or_null<CompoundStmt>(*StmtExprChild.begin());
358 Stmt::child_range CompStmtChild = CompS->children();
359 if (CompStmtChild.begin() == CompStmtChild.end())
361 auto *DeclS = dyn_cast_or_null<DeclStmt>(*CompStmtChild.begin());
364 if (!DeclS->isSingleDecl())
366 VarDecl *VD = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl());
373 RecContainer = StmtE;
377 RecRange = Rec->getSourceRange();
385 Pass.TA.clearDiagnostic(diag::err_arc_illegal_explicit_message,
386 diag::err_unavailable,
387 diag::err_unavailable_message,
391 bool isDelegateMessage(
Expr *E)
const {
392 if (!E)
return false;
401 return (ME->isInstanceMessage() && ME->getSelector() == DelegateSel);
406 bool isInAtFinally(
Expr *E)
const {
410 if (isa<ObjCAtFinallyStmt>(S))
412 S = StmtMap->getParent(S);
418 bool isRemovable(
Expr *E)
const {
419 return Removables.count(E);
422 bool tryRemoving(
Expr *E)
const {
423 if (isRemovable(E)) {
424 Pass.TA.removeStmt(E);
428 Stmt *parent = StmtMap->getParent(E);
431 return tryRemoving(castE);
433 if (
ParenExpr *parenE = dyn_cast_or_null<ParenExpr>(parent))
434 return tryRemoving(parenE);
437 bopE = dyn_cast_or_null<BinaryOperator>(parent)) {
438 if (bopE->getOpcode() == BO_Comma && bopE->getLHS() == E &&
440 Pass.TA.replace(bopE->getSourceRange(), bopE->getRHS()->getSourceRange());
The receiver is the instance of the superclass object.
bool isMacroArgExpansion(SourceLocation Loc, SourceLocation *StartLoc=nullptr) const
Tests whether the given source location represents a macro argument's expansion into the function-lik...
Defines the clang::ASTContext interface.
SourceLocation getEnd() const
The receiver is an object instance.
Smart pointer class that efficiently represents Objective-C method names.
ObjCMethodFamily getMethodFamily() const
SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID. ...
Defines the SourceManager interface.
ParenExpr - This represents a parethesized expression, e.g.
const Expr * getInit() const
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Expr * IgnoreImplicit() LLVM_READONLY
IgnoreImplicit - Skip past any implicit AST nodes which might surround this expression.
void setBegin(SourceLocation b)
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
bool isGlobalVar(Expr *E)
void removeRetainReleaseDeallocFinalize(MigrationPass &pass)
A builtin binary operation expression such as "x + y" or "x <= y".
Selector getSelector() const
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
A class that does preordor or postorder depth-first traversal on the entire Clang AST and visits each...
bool isPlusOneAssign(const BinaryOperator *E)
This object can be modified without requiring retains or releases.
StringRef getNilString(MigrationPass &Pass)
Returns "nil" or "0" if 'nil' macro is not actually defined.
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
void collectRemovables(Stmt *S, ExprSet &exprs)
Expr - This represents one expression.
TranslationUnitDecl * getTranslationUnitDecl() const
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
An expression that sends a message to the given Objective-C object or class.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Encodes a location in the source.
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
std::string getAsString() const
Derive the full selector name (e.g.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
SourceLocation getBegin() const
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
static StringRef getImmediateMacroName(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts)
Retrieve the name of the immediate macro expansion.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
bool hasSideEffects(Expr *E, ASTContext &Ctx)
SourceLocation getSelectorLoc(unsigned Index) const
SourceLocation getLocStart() const LLVM_READONLY
bool isPlusOne(const Expr *E)
detail::InMemoryDirectory::const_iterator E
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
ObjCIvarRefExpr - A reference to an ObjC instance variable.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
void setEnd(SourceLocation e)
A reference to a declared variable, function, enum, etc.
A trivial tuple used to represent a source range.
ReceiverKind getReceiverKind() const
Determine the kind of receiver that this message is being sent to.
This class handles loading and caching of source files into memory.