Lines Matching +full:bool +full:- +full:property

1 //==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This checker analyzes Objective-C -dealloc methods and their callees
12 // - When a class has a synthesized instance variable for a 'retain' or 'copy'
13 // property and lacks a -dealloc method in its implementation.
14 // - When a class has a synthesized instance variable for a 'retain'/'copy'
15 // property but the ivar is not released in -dealloc by either -release
16 // or by nilling out the property.
18 // It warns about extra releases in -dealloc (but not in callees) when a
20 // - When the property is 'assign' and is not 'readonly'.
21 // - When the property is 'weak'.
26 // this is specified in the property declaration itself.
28 //===----------------------------------------------------------------------===//
54 /// -dealloc.
57 /// -release on it directly or by nilling it out with a property setter.
60 /// The instance variable must not be directly released with -release.
67 /// Returns true if the property implementation is synthesized and the
68 /// type of the property is retainable.
69 static bool isSynthesizedRetainableProperty(const ObjCPropertyImplDecl *I,
73 if (I->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
76 (*ID) = I->getPropertyIvarDecl();
80 QualType T = (*ID)->getType();
81 if (!T->isObjCRetainableType())
84 (*PD) = I->getPropertyDecl();
85 // Shouldn't be able to synthesize a property that doesn't exist.
127 bool Assumption) const;
139 bool diagnoseExtraRelease(SymbolRef ReleasedValue, const ObjCMethodCall &M,
142 bool diagnoseMistakenDealloc(SymbolRef DeallocedValue,
159 bool isInInstanceDealloc(const CheckerContext &C, SVal &SelfValOut) const;
160 bool isInInstanceDealloc(const CheckerContext &C, const LocationContext *LCtx,
162 bool instanceDeallocIsOnStack(const CheckerContext &C,
165 bool isSuperDeallocMessage(const ObjCMethodCall &M) const;
179 bool classHasSeparateTeardown(const ObjCInterfaceDecl *ID) const;
181 bool isReleasedByCIFilterDealloc(const ObjCPropertyImplDecl *PropImpl) const;
182 bool isNibLoadedIvarWithoutRetain(const ObjCPropertyImplDecl *PropImpl) const;
188 /// symbols remaining that must be released in -dealloc.
193 /// An AST check that diagnose when the class requires a -dealloc method and
202 const ObjCInterfaceDecl *ID = D->getClassInterface();
204 // then it may not require a -dealloc method.
211 bool HasOthers = false;
212 for (const auto *I : D->property_impls()) {
229 for (const auto *I : D->instance_methods()) {
230 if (I->getSelector() == DeallocSel) {
237 const char* Name = "Missing -dealloc";
242 << "must release '" << *PropImplRequiringRelease->getPropertyIvarDecl()
256 /// If this is the beginning of -dealloc, mark the values initially stored in
257 /// instance variables that must be released by the end of -dealloc
263 // Only do this if the current method is -dealloc.
275 SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
277 // Symbols that must be released by the end of the -dealloc;
280 // If we're an inlined -dealloc, we should add our symbols to the existing
282 if (const SymbolSet *CurrSet = State->get<UnreleasedIvarMap>(SelfSymbol))
285 for (auto *PropImpl : getContainingObjCImpl(LCtx)->property_impls()) {
290 SVal LVal = State->getLValue(PropImpl->getPropertyIvarDecl(), SelfVal);
295 SVal InitialVal = State->getSVal(*LValLoc);
305 State = State->set<UnreleasedIvarMap>(SelfSymbol, RequiredReleases);
317 return dyn_cast_or_null<ObjCIvarRegion>(IvarSym->getOriginRegion());
329 const SymbolicRegion *SR = IvarRegion->getSymbolicBase();
331 return SR->getSymbol();
334 /// If we are in -dealloc or -dealloc is on the stack, handle the call if it is
335 /// a release or a nilling-out property setter.
338 // Only run if -dealloc is on the stack.
353 // An instance variable symbol was released with -release:
358 // An instance variable symbol was released nilling out its property:
359 // self.property = nil;
369 /// If we are in -dealloc or -dealloc is on the stack, handle the call if it is
390 // We perform this check post-message so that if the super -dealloc
397 /// Check for missing releases even when -dealloc does not call
413 bool Assumption) const {
414 if (State->get<UnreleasedIvarMap>().isEmpty())
421 BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
432 const llvm::APInt &RHS = SIE->getRHS();
435 NullSymbol = SIE->getLHS();
437 const llvm::APInt &LHS = SIE->getLHS();
440 NullSymbol = SIE->getRHS();
459 if (State->get<UnreleasedIvarMap>().isEmpty())
464 // post-message handler for '[super dealloc], escaping here would cause
471 if (!Call || (Call && !Call->isInSystemHeader())) {
476 // and are frequently called on 'self' in -dealloc (e.g., to remove
477 // observers) -- we want to avoid false negatives from escaping on
479 State = State->remove<UnreleasedIvarMap>(Sym);
511 const SymbolSet *OldUnreleased = State->get<UnreleasedIvarMap>(SelfSym);
516 SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
522 cast<SymbolRegionValue>(IvarSymbol)->getRegion();
526 if (SelfRegion != IvarRegion->getSuperRegion())
529 const ObjCIvarDecl *IvarDecl = IvarRegion->getDecl();
530 // Prevent an inlined call to -dealloc in a super class from warning
531 // about the values the subclass's -dealloc should release.
532 if (IvarDecl->getContainingInterface() !=
533 cast<ObjCMethodDecl>(LCtx->getDecl())->getClassInterface())
540 if (State->getStateManager()
547 // A missing release manifests as a leak, so treat as a non-fatal error.
558 const ObjCInterfaceDecl *Interface = IvarDecl->getContainingInterface();
560 // separate from -dealloc, do not warn about missing releases. We
566 ObjCImplDecl *ImplDecl = Interface->getImplementation();
569 ImplDecl->FindPropertyImplIvarDecl(IvarDecl->getIdentifier());
571 const ObjCPropertyDecl *PropDecl = PropImpl->getPropertyDecl();
573 assert(PropDecl->getSetterKind() == ObjCPropertyDecl::Copy ||
574 PropDecl->getSetterKind() == ObjCPropertyDecl::Retain);
579 if (PropDecl->getSetterKind() == ObjCPropertyDecl::Retain)
584 OS << " by a synthesized property but not released"
593 State = State->remove<UnreleasedIvarMap>(SelfSym);
595 State = State->set<UnreleasedIvarMap>(SelfSym, NewUnreleased);
604 // Make sure that after checking in the top-most frame the list of
607 assert(!LCtx->inTopFrame() || State->get<UnreleasedIvarMap>().isEmpty());
611 /// the top-most deallocating instance. If so, find the property for that
625 // Don't try to find the property if the ivar was not loaded from the
628 IvarRegion->getSuperRegion())
632 const ObjCIvarDecl *IvarDecl = IvarRegion->getDecl();
636 Container->FindPropertyImplIvarDecl(IvarDecl->getIdentifier());
640 /// Emits a warning if the current context is -dealloc and ReleasedValue
641 /// must not be directly released in a -dealloc. Returns true if a diagnostic
643 bool ObjCDeallocChecker::diagnoseExtraRelease(SymbolRef ReleasedValue,
650 // release them in -dealloc.
657 // If the ivar belongs to a property that must not be released directly
664 // If the property is readwrite but it shadows a read-only property in its
665 // external interface, treat the property a read-only. If the outside
666 // world cannot write to a property then the internal implementation is free
672 if (PropDecl->isReadOnly())
675 PropDecl = PropImpl->getPropertyDecl();
685 assert(PropDecl->getSetterKind() == ObjCPropertyDecl::Weak ||
686 (PropDecl->getSetterKind() == ObjCPropertyDecl::Assign &&
687 !PropDecl->isReadOnly()) ||
692 OS << "The '" << *PropImpl->getPropertyIvarDecl()
697 OS << "' will be released by '-[CIFilter dealloc]' but also released here";
701 if (PropDecl->getSetterKind() == ObjCPropertyDecl::Weak)
706 OS << " property but was released in 'dealloc'";
711 BR->addRange(M.getOriginExpr()->getSourceRange());
718 /// Emits a warning if the current context is -dealloc and DeallocedValue
719 /// must not be directly dealloced in a -dealloc. Returns true if a diagnostic
721 bool ObjCDeallocChecker::diagnoseMistakenDealloc(SymbolRef DeallocedValue,
729 // Find the property backing the instance variable that M
748 OS << "'" << *PropImpl->getPropertyIvarDecl()
753 BR->addRange(M.getOriginExpr()->getSourceRange());
778 bool ObjCDeallocChecker::isSuperDeallocMessage(
780 if (M.getOriginExpr()->getReceiverKind() != ObjCMessageExpr::SuperInstance)
789 auto *MD = cast<ObjCMethodDecl>(LCtx->getDecl());
790 return cast<ObjCImplDecl>(MD->getDeclContext());
793 /// Returns the property that shadowed by PropImpl if one exists and
797 const ObjCPropertyDecl *PropDecl = PropImpl->getPropertyDecl();
800 if (PropDecl->isReadOnly())
803 auto *CatDecl = dyn_cast<ObjCCategoryDecl>(PropDecl->getDeclContext());
806 if (!CatDecl || !CatDecl->IsClassExtension())
809 IdentifierInfo *ID = PropDecl->getIdentifier();
810 DeclContext::lookup_result R = CatDecl->getClassInterface()->lookup(ID);
816 if (ShadowedPropDecl->isInstanceProperty()) {
817 assert(ShadowedPropDecl->isReadOnly());
852 const SymbolSet *Unreleased = State->get<UnreleasedIvarMap>(Instance);
857 SymbolSet::Factory &F = State->getStateManager().get_context<SymbolSet>();
862 if (RemovedRegion->getDecl() == UnreleasedRegion->getDecl()) {
868 return State->remove<UnreleasedIvarMap>(Instance);
871 return State->set<UnreleasedIvarMap>(Instance, NewUnreleased);
875 /// released in -dealloc or whether it cannot be determined.
883 ObjCPropertyDecl::SetterKind SK = PropDecl->getSetterKind();
887 // the value in their instance variables must be released in -dealloc.
902 // It is common for the ivars for read-only assign properties to
905 if (PropDecl->isReadOnly())
925 if (!M.getArgExpr(0)->getType()->isObjCRetainableType())
932 M.getState()->assume(Arg.castAs<DefinedOrUnknownSVal>());
940 ObjCIvarDecl *PropIvarDecl = Prop->getPropertyIvarDecl();
946 SVal LVal = State->getLValue(PropIvarDecl, ReceiverVal);
951 SVal CurrentValInIvar = State->getSVal(*LValLoc);
955 /// Returns true if the current context is a call to -dealloc and false
958 bool ObjCDeallocChecker::isInInstanceDealloc(const CheckerContext &C,
963 /// Returns true if LCtx is a call to -dealloc and false
966 bool ObjCDeallocChecker::isInInstanceDealloc(const CheckerContext &C,
969 auto *MD = dyn_cast<ObjCMethodDecl>(LCtx->getDecl());
970 if (!MD || !MD->isInstanceMethod() || MD->getSelector() != DeallocSel)
973 const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
974 assert(SelfDecl && "No self in -dealloc?");
977 SelfValOut = State->getSVal(State->getRegion(SelfDecl, LCtx));
981 /// Returns true if there is a call to -dealloc anywhere on the stack and false
983 /// 'self' in the frame for -dealloc.
984 bool ObjCDeallocChecker::instanceDeallocIsOnStack(const CheckerContext &C,
992 LCtx = LCtx->getParent();
999 /// a separate teardown lifecycle. In this case, -dealloc warnings
1001 bool ObjCDeallocChecker::classHasSeparateTeardown(
1004 for ( ; ID ; ID = ID->getSuperClass()) {
1005 IdentifierInfo *II = ID->getIdentifier();
1011 // as these don't need to implement -dealloc. They implement tear down in
1021 /// The -dealloc method in CIFilter highly unusual in that is will release
1023 /// starts with "input" or backs a property whose name starts with "input".
1024 /// Subclasses should not release these ivars in their own -dealloc method --
1027 /// This method returns true if the property will be released by
1028 /// -[CIFilter dealloc].
1029 bool ObjCDeallocChecker::isReleasedByCIFilterDealloc(
1031 assert(PropImpl->getPropertyIvarDecl());
1032 StringRef PropName = PropImpl->getPropertyDecl()->getName();
1033 StringRef IvarName = PropImpl->getPropertyIvarDecl()->getName();
1042 PropImpl->getPropertyIvarDecl()->getContainingInterface();
1043 for ( ; ID ; ID = ID->getSuperClass()) {
1044 IdentifierInfo *II = ID->getIdentifier();
1052 /// Returns whether the ivar backing the property is an IBOutlet that
1055 /// On macOS, if there is no setter, the nib-loading code sets the ivar
1058 /// On iOS and its derivatives, the nib-loading code will call
1059 /// -setValue:forKey:, which retains the value before directly setting the ivar.
1060 bool ObjCDeallocChecker::isNibLoadedIvarWithoutRetain(
1062 const ObjCIvarDecl *IvarDecl = PropImpl->getPropertyIvarDecl();
1063 if (!IvarDecl->hasAttr<IBOutletAttr>())
1067 IvarDecl->getASTContext().getTargetInfo().getTriple();
1072 if (PropImpl->getPropertyDecl()->getSetterMethodDecl())
1082 bool ento::shouldRegisterObjCDeallocChecker(const CheckerManager &mgr) {