xref: /freebsd-src/contrib/llvm-project/clang/lib/ARCMigrate/TransZeroOutPropsInDealloc.cpp (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
10b57cec5SDimitry Andric //===--- TransZeroOutPropsInDealloc.cpp - Transformations to ARC mode -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // removeZeroOutPropsInDealloc:
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Removes zero'ing out "strong" @synthesized properties in a -dealloc method.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "Transforms.h"
160b57cec5SDimitry Andric #include "Internals.h"
170b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace clang;
200b57cec5SDimitry Andric using namespace arcmt;
210b57cec5SDimitry Andric using namespace trans;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace {
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric class ZeroOutInDeallocRemover :
260b57cec5SDimitry Andric                            public RecursiveASTVisitor<ZeroOutInDeallocRemover> {
270b57cec5SDimitry Andric   typedef RecursiveASTVisitor<ZeroOutInDeallocRemover> base;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric   MigrationPass &Pass;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric   llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*> SynthesizedProperties;
320b57cec5SDimitry Andric   ImplicitParamDecl *SelfD;
330b57cec5SDimitry Andric   ExprSet Removables;
340b57cec5SDimitry Andric   Selector FinalizeSel;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric public:
ZeroOutInDeallocRemover(MigrationPass & pass)370b57cec5SDimitry Andric   ZeroOutInDeallocRemover(MigrationPass &pass) : Pass(pass), SelfD(nullptr) {
380b57cec5SDimitry Andric     FinalizeSel =
390b57cec5SDimitry Andric         Pass.Ctx.Selectors.getNullarySelector(&Pass.Ctx.Idents.get("finalize"));
400b57cec5SDimitry Andric   }
410b57cec5SDimitry Andric 
VisitObjCMessageExpr(ObjCMessageExpr * ME)420b57cec5SDimitry Andric   bool VisitObjCMessageExpr(ObjCMessageExpr *ME) {
430b57cec5SDimitry Andric     ASTContext &Ctx = Pass.Ctx;
440b57cec5SDimitry Andric     TransformActions &TA = Pass.TA;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric     if (ME->getReceiverKind() != ObjCMessageExpr::Instance)
470b57cec5SDimitry Andric       return true;
480b57cec5SDimitry Andric     Expr *receiver = ME->getInstanceReceiver();
490b57cec5SDimitry Andric     if (!receiver)
500b57cec5SDimitry Andric       return true;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric     DeclRefExpr *refE = dyn_cast<DeclRefExpr>(receiver->IgnoreParenCasts());
530b57cec5SDimitry Andric     if (!refE || refE->getDecl() != SelfD)
540b57cec5SDimitry Andric       return true;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric     bool BackedBySynthesizeSetter = false;
570b57cec5SDimitry Andric     for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
580b57cec5SDimitry Andric          P = SynthesizedProperties.begin(),
590b57cec5SDimitry Andric          E = SynthesizedProperties.end(); P != E; ++P) {
600b57cec5SDimitry Andric       ObjCPropertyDecl *PropDecl = P->first;
610b57cec5SDimitry Andric       if (PropDecl->getSetterName() == ME->getSelector()) {
620b57cec5SDimitry Andric         BackedBySynthesizeSetter = true;
630b57cec5SDimitry Andric         break;
640b57cec5SDimitry Andric       }
650b57cec5SDimitry Andric     }
660b57cec5SDimitry Andric     if (!BackedBySynthesizeSetter)
670b57cec5SDimitry Andric       return true;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric     // Remove the setter message if RHS is null
700b57cec5SDimitry Andric     Transaction Trans(TA);
710b57cec5SDimitry Andric     Expr *RHS = ME->getArg(0);
720b57cec5SDimitry Andric     bool RHSIsNull =
730b57cec5SDimitry Andric       RHS->isNullPointerConstant(Ctx,
740b57cec5SDimitry Andric                                  Expr::NPC_ValueDependentIsNull);
750b57cec5SDimitry Andric     if (RHSIsNull && isRemovable(ME))
760b57cec5SDimitry Andric       TA.removeStmt(ME);
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric     return true;
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric 
VisitPseudoObjectExpr(PseudoObjectExpr * POE)810b57cec5SDimitry Andric   bool VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
820b57cec5SDimitry Andric     if (isZeroingPropIvar(POE) && isRemovable(POE)) {
830b57cec5SDimitry Andric       Transaction Trans(Pass.TA);
840b57cec5SDimitry Andric       Pass.TA.removeStmt(POE);
850b57cec5SDimitry Andric     }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric     return true;
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric 
VisitBinaryOperator(BinaryOperator * BOE)900b57cec5SDimitry Andric   bool VisitBinaryOperator(BinaryOperator *BOE) {
910b57cec5SDimitry Andric     if (isZeroingPropIvar(BOE) && isRemovable(BOE)) {
920b57cec5SDimitry Andric       Transaction Trans(Pass.TA);
930b57cec5SDimitry Andric       Pass.TA.removeStmt(BOE);
940b57cec5SDimitry Andric     }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric     return true;
970b57cec5SDimitry Andric   }
980b57cec5SDimitry Andric 
TraverseObjCMethodDecl(ObjCMethodDecl * D)990b57cec5SDimitry Andric   bool TraverseObjCMethodDecl(ObjCMethodDecl *D) {
1000b57cec5SDimitry Andric     if (D->getMethodFamily() != OMF_dealloc &&
1010b57cec5SDimitry Andric         !(D->isInstanceMethod() && D->getSelector() == FinalizeSel))
1020b57cec5SDimitry Andric       return true;
1030b57cec5SDimitry Andric     if (!D->hasBody())
1040b57cec5SDimitry Andric       return true;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric     ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(D->getDeclContext());
1070b57cec5SDimitry Andric     if (!IMD)
1080b57cec5SDimitry Andric       return true;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     SelfD = D->getSelfDecl();
1110b57cec5SDimitry Andric     collectRemovables(D->getBody(), Removables);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric     // For a 'dealloc' method use, find all property implementations in
1140b57cec5SDimitry Andric     // this class implementation.
1150b57cec5SDimitry Andric     for (auto *PID : IMD->property_impls()) {
1160b57cec5SDimitry Andric       if (PID->getPropertyImplementation() ==
1170b57cec5SDimitry Andric           ObjCPropertyImplDecl::Synthesize) {
1180b57cec5SDimitry Andric         ObjCPropertyDecl *PD = PID->getPropertyDecl();
1190b57cec5SDimitry Andric         ObjCMethodDecl *setterM = PD->getSetterMethodDecl();
1200b57cec5SDimitry Andric         if (!(setterM && setterM->isDefined())) {
121*5ffd83dbSDimitry Andric           ObjCPropertyAttribute::Kind AttrKind = PD->getPropertyAttributes();
122*5ffd83dbSDimitry Andric           if (AttrKind & (ObjCPropertyAttribute::kind_retain |
123*5ffd83dbSDimitry Andric                           ObjCPropertyAttribute::kind_copy |
124*5ffd83dbSDimitry Andric                           ObjCPropertyAttribute::kind_strong))
1250b57cec5SDimitry Andric             SynthesizedProperties[PD] = PID;
1260b57cec5SDimitry Andric         }
1270b57cec5SDimitry Andric       }
1280b57cec5SDimitry Andric     }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     // Now, remove all zeroing of ivars etc.
1310b57cec5SDimitry Andric     base::TraverseObjCMethodDecl(D);
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric     // clear out for next method.
1340b57cec5SDimitry Andric     SynthesizedProperties.clear();
1350b57cec5SDimitry Andric     SelfD = nullptr;
1360b57cec5SDimitry Andric     Removables.clear();
1370b57cec5SDimitry Andric     return true;
1380b57cec5SDimitry Andric   }
1390b57cec5SDimitry Andric 
TraverseFunctionDecl(FunctionDecl * D)1400b57cec5SDimitry Andric   bool TraverseFunctionDecl(FunctionDecl *D) { return true; }
TraverseBlockDecl(BlockDecl * block)1410b57cec5SDimitry Andric   bool TraverseBlockDecl(BlockDecl *block) { return true; }
TraverseBlockExpr(BlockExpr * block)1420b57cec5SDimitry Andric   bool TraverseBlockExpr(BlockExpr *block) { return true; }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric private:
isRemovable(Expr * E) const1450b57cec5SDimitry Andric   bool isRemovable(Expr *E) const {
1460b57cec5SDimitry Andric     return Removables.count(E);
1470b57cec5SDimitry Andric   }
1480b57cec5SDimitry Andric 
isZeroingPropIvar(Expr * E)1490b57cec5SDimitry Andric   bool isZeroingPropIvar(Expr *E) {
1500b57cec5SDimitry Andric     E = E->IgnoreParens();
1510b57cec5SDimitry Andric     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
1520b57cec5SDimitry Andric       return isZeroingPropIvar(BO);
1530b57cec5SDimitry Andric     if (PseudoObjectExpr *PO = dyn_cast<PseudoObjectExpr>(E))
1540b57cec5SDimitry Andric       return isZeroingPropIvar(PO);
1550b57cec5SDimitry Andric     return false;
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric 
isZeroingPropIvar(BinaryOperator * BOE)1580b57cec5SDimitry Andric   bool isZeroingPropIvar(BinaryOperator *BOE) {
1590b57cec5SDimitry Andric     if (BOE->getOpcode() == BO_Comma)
1600b57cec5SDimitry Andric       return isZeroingPropIvar(BOE->getLHS()) &&
1610b57cec5SDimitry Andric              isZeroingPropIvar(BOE->getRHS());
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric     if (BOE->getOpcode() != BO_Assign)
1640b57cec5SDimitry Andric       return false;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     Expr *LHS = BOE->getLHS();
1670b57cec5SDimitry Andric     if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(LHS)) {
1680b57cec5SDimitry Andric       ObjCIvarDecl *IVDecl = IV->getDecl();
1690b57cec5SDimitry Andric       if (!IVDecl->getType()->isObjCObjectPointerType())
1700b57cec5SDimitry Andric         return false;
1710b57cec5SDimitry Andric       bool IvarBacksPropertySynthesis = false;
1720b57cec5SDimitry Andric       for (llvm::DenseMap<ObjCPropertyDecl*, ObjCPropertyImplDecl*>::iterator
1730b57cec5SDimitry Andric            P = SynthesizedProperties.begin(),
1740b57cec5SDimitry Andric            E = SynthesizedProperties.end(); P != E; ++P) {
1750b57cec5SDimitry Andric         ObjCPropertyImplDecl *PropImpDecl = P->second;
1760b57cec5SDimitry Andric         if (PropImpDecl && PropImpDecl->getPropertyIvarDecl() == IVDecl) {
1770b57cec5SDimitry Andric           IvarBacksPropertySynthesis = true;
1780b57cec5SDimitry Andric           break;
1790b57cec5SDimitry Andric         }
1800b57cec5SDimitry Andric       }
1810b57cec5SDimitry Andric       if (!IvarBacksPropertySynthesis)
1820b57cec5SDimitry Andric         return false;
1830b57cec5SDimitry Andric     }
1840b57cec5SDimitry Andric     else
1850b57cec5SDimitry Andric         return false;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric     return isZero(BOE->getRHS());
1880b57cec5SDimitry Andric   }
1890b57cec5SDimitry Andric 
isZeroingPropIvar(PseudoObjectExpr * PO)1900b57cec5SDimitry Andric   bool isZeroingPropIvar(PseudoObjectExpr *PO) {
1910b57cec5SDimitry Andric     BinaryOperator *BO = dyn_cast<BinaryOperator>(PO->getSyntacticForm());
1920b57cec5SDimitry Andric     if (!BO) return false;
1930b57cec5SDimitry Andric     if (BO->getOpcode() != BO_Assign) return false;
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric     ObjCPropertyRefExpr *PropRefExp =
1960b57cec5SDimitry Andric       dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParens());
1970b57cec5SDimitry Andric     if (!PropRefExp) return false;
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric     // TODO: Using implicit property decl.
2000b57cec5SDimitry Andric     if (PropRefExp->isImplicitProperty())
2010b57cec5SDimitry Andric       return false;
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     if (ObjCPropertyDecl *PDecl = PropRefExp->getExplicitProperty()) {
2040b57cec5SDimitry Andric       if (!SynthesizedProperties.count(PDecl))
2050b57cec5SDimitry Andric         return false;
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric     return isZero(cast<OpaqueValueExpr>(BO->getRHS())->getSourceExpr());
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric 
isZero(Expr * E)2110b57cec5SDimitry Andric   bool isZero(Expr *E) {
2120b57cec5SDimitry Andric     if (E->isNullPointerConstant(Pass.Ctx, Expr::NPC_ValueDependentIsNull))
2130b57cec5SDimitry Andric       return true;
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric     return isZeroingPropIvar(E);
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric };
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric } // anonymous namespace
2200b57cec5SDimitry Andric 
removeZeroOutPropsInDeallocFinalize(MigrationPass & pass)2210b57cec5SDimitry Andric void trans::removeZeroOutPropsInDeallocFinalize(MigrationPass &pass) {
2220b57cec5SDimitry Andric   ZeroOutInDeallocRemover trans(pass);
2230b57cec5SDimitry Andric   trans.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
2240b57cec5SDimitry Andric }
225