1 //===- IvarInvalidationChecker.cpp ------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker implements annotation driven invalidation checking. If a class
11 // contains a method annotated with 'objc_instance_variable_invalidator',
13 // __attribute__((annotate("objc_instance_variable_invalidator")));
14 // all the "ivalidatable" instance variables of this class should be
15 // invalidated. We call an instance variable ivalidatable if it is an object of
16 // a class which contains an invalidation method. There could be multiple
17 // methods annotated with such annotations per class, either one can be used
18 // to invalidate the ivar. An ivar or property are considered to be
19 // invalidated if they are being assigned 'nil' or an invalidation method has
20 // been called on them. An invalidation method should either invalidate all
21 // the ivars or call another invalidation method (on self).
23 // Partial invalidor annotation allows to address cases when ivars are
24 // invalidated by other methods, which might or might not be called from
25 // the invalidation method. The checker checks that each invalidation
26 // method and all the partial methods cumulatively invalidate all ivars.
27 // __attribute__((annotate("objc_instance_variable_invalidator_partial")));
29 //===----------------------------------------------------------------------===//
31 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
32 #include "clang/AST/Attr.h"
33 #include "clang/AST/DeclObjC.h"
34 #include "clang/AST/StmtVisitor.h"
35 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36 #include "clang/StaticAnalyzer/Core/Checker.h"
37 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/SetVector.h"
40 #include "llvm/ADT/SmallString.h"
42 using namespace clang;
47 /// Check for missing invalidation method declarations.
48 DefaultBool check_MissingInvalidationMethod;
49 /// Check that all ivars are invalidated.
50 DefaultBool check_InstanceVariableInvalidation;
52 CheckName checkName_MissingInvalidationMethod;
53 CheckName checkName_InstanceVariableInvalidation;
56 class IvarInvalidationCheckerImpl {
57 typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
58 typedef llvm::DenseMap<const ObjCMethodDecl*,
59 const ObjCIvarDecl*> MethToIvarMapTy;
60 typedef llvm::DenseMap<const ObjCPropertyDecl*,
61 const ObjCIvarDecl*> PropToIvarMapTy;
62 typedef llvm::DenseMap<const ObjCIvarDecl*,
63 const ObjCPropertyDecl*> IvarToPropMapTy;
65 struct InvalidationInfo {
66 /// Has the ivar been invalidated?
69 /// The methods which can be used to invalidate the ivar.
70 MethodSet InvalidationMethods;
72 InvalidationInfo() : IsInvalidated(false) {}
73 void addInvalidationMethod(const ObjCMethodDecl *MD) {
74 InvalidationMethods.insert(MD);
77 bool needsInvalidation() const {
78 return !InvalidationMethods.empty();
81 bool hasMethod(const ObjCMethodDecl *MD) {
84 for (MethodSet::iterator I = InvalidationMethods.begin(),
85 E = InvalidationMethods.end(); I != E; ++I) {
95 typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
97 /// Statement visitor, which walks the method body and flags the ivars
98 /// referenced in it (either directly or via property).
99 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
100 /// The set of Ivars which need to be invalidated.
103 /// Flag is set as the result of a message send to another
104 /// invalidation method.
105 bool &CalledAnotherInvalidationMethod;
107 /// Property setter to ivar mapping.
108 const MethToIvarMapTy &PropertySetterToIvarMap;
110 /// Property getter to ivar mapping.
111 const MethToIvarMapTy &PropertyGetterToIvarMap;
113 /// Property to ivar mapping.
114 const PropToIvarMapTy &PropertyToIvarMap;
116 /// The invalidation method being currently processed.
117 const ObjCMethodDecl *InvalidationMethod;
121 /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
122 const Expr *peel(const Expr *E) const;
124 /// Does this expression represent zero: '0'?
125 bool isZero(const Expr *E) const;
127 /// Mark the given ivar as invalidated.
128 void markInvalidated(const ObjCIvarDecl *Iv);
130 /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
132 void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
134 /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
135 /// it as invalidated.
136 void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
138 /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
139 /// if yes, marks it as invalidated.
140 void checkObjCMessageExpr(const ObjCMessageExpr *ME);
142 /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
143 void check(const Expr *E);
146 MethodCrawler(IvarSet &InIVars,
147 bool &InCalledAnotherInvalidationMethod,
148 const MethToIvarMapTy &InPropertySetterToIvarMap,
149 const MethToIvarMapTy &InPropertyGetterToIvarMap,
150 const PropToIvarMapTy &InPropertyToIvarMap,
153 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
154 PropertySetterToIvarMap(InPropertySetterToIvarMap),
155 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
156 PropertyToIvarMap(InPropertyToIvarMap),
157 InvalidationMethod(nullptr),
160 void VisitStmt(const Stmt *S) { VisitChildren(S); }
162 void VisitBinaryOperator(const BinaryOperator *BO);
164 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
166 void VisitChildren(const Stmt *S) {
167 for (const auto *Child : S->children()) {
170 if (CalledAnotherInvalidationMethod)
176 /// Check if the any of the methods inside the interface are annotated with
177 /// the invalidation annotation, update the IvarInfo accordingly.
178 /// \param LookForPartial is set when we are searching for partial
180 static void containsInvalidationMethod(const ObjCContainerDecl *D,
181 InvalidationInfo &Out,
182 bool LookForPartial);
184 /// Check if ivar should be tracked and add to TrackedIvars if positive.
185 /// Returns true if ivar should be tracked.
186 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
187 const ObjCIvarDecl **FirstIvarDecl);
189 /// Given the property declaration, and the list of tracked ivars, finds
190 /// the ivar backing the property when possible. Returns '0' when no such
191 /// ivar could be found.
192 static const ObjCIvarDecl *findPropertyBackingIvar(
193 const ObjCPropertyDecl *Prop,
194 const ObjCInterfaceDecl *InterfaceD,
195 IvarSet &TrackedIvars,
196 const ObjCIvarDecl **FirstIvarDecl);
198 /// Print ivar name or the property if the given ivar backs a property.
199 static void printIvar(llvm::raw_svector_ostream &os,
200 const ObjCIvarDecl *IvarDecl,
201 const IvarToPropMapTy &IvarToPopertyMap);
203 void reportNoInvalidationMethod(CheckName CheckName,
204 const ObjCIvarDecl *FirstIvarDecl,
205 const IvarToPropMapTy &IvarToPopertyMap,
206 const ObjCInterfaceDecl *InterfaceD,
207 bool MissingDeclaration) const;
209 void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
210 const IvarToPropMapTy &IvarToPopertyMap,
211 const ObjCMethodDecl *MethodD) const;
213 AnalysisManager& Mgr;
215 /// Filter on the checks performed.
216 const ChecksFilter &Filter;
219 IvarInvalidationCheckerImpl(AnalysisManager& InMgr,
221 const ChecksFilter &InFilter) :
222 Mgr (InMgr), BR(InBR), Filter(InFilter) {}
224 void visit(const ObjCImplementationDecl *D) const;
227 static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) {
228 for (const auto *Ann : M->specific_attrs<AnnotateAttr>()) {
229 if (!LookForPartial &&
230 Ann->getAnnotation() == "objc_instance_variable_invalidator")
232 if (LookForPartial &&
233 Ann->getAnnotation() == "objc_instance_variable_invalidator_partial")
239 void IvarInvalidationCheckerImpl::containsInvalidationMethod(
240 const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) {
245 assert(!isa<ObjCImplementationDecl>(D));
246 // TODO: Cache the results.
248 // Check all methods.
249 for (const auto *MDI : D->methods())
250 if (isInvalidationMethod(MDI, Partial))
251 OutInfo.addInvalidationMethod(
252 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
254 // If interface, check all parent protocols and super.
255 if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
257 // Visit all protocols.
258 for (const auto *I : InterfD->protocols())
259 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
261 // Visit all categories in case the invalidation method is declared in
263 for (const auto *Ext : InterfD->visible_extensions())
264 containsInvalidationMethod(Ext, OutInfo, Partial);
266 containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
270 // If protocol, check all parent protocols.
271 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
272 for (const auto *I : ProtD->protocols()) {
273 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
279 bool IvarInvalidationCheckerImpl::trackIvar(const ObjCIvarDecl *Iv,
280 IvarSet &TrackedIvars,
281 const ObjCIvarDecl **FirstIvarDecl) {
282 QualType IvQTy = Iv->getType();
283 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
286 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
288 InvalidationInfo Info;
289 containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false);
290 if (Info.needsInvalidation()) {
291 const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
292 TrackedIvars[I] = Info;
300 const ObjCIvarDecl *IvarInvalidationCheckerImpl::findPropertyBackingIvar(
301 const ObjCPropertyDecl *Prop,
302 const ObjCInterfaceDecl *InterfaceD,
303 IvarSet &TrackedIvars,
304 const ObjCIvarDecl **FirstIvarDecl) {
305 const ObjCIvarDecl *IvarD = nullptr;
307 // Lookup for the synthesized case.
308 IvarD = Prop->getPropertyIvarDecl();
309 // We only track the ivars/properties that are defined in the current
310 // class (not the parent).
311 if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
312 if (TrackedIvars.count(IvarD)) {
315 // If the ivar is synthesized we still want to track it.
316 if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
320 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
321 StringRef PropName = Prop->getIdentifier()->getName();
322 for (IvarSet::const_iterator I = TrackedIvars.begin(),
323 E = TrackedIvars.end(); I != E; ++I) {
324 const ObjCIvarDecl *Iv = I->first;
325 StringRef IvarName = Iv->getName();
327 if (IvarName == PropName)
330 SmallString<128> PropNameWithUnderscore;
332 llvm::raw_svector_ostream os(PropNameWithUnderscore);
333 os << '_' << PropName;
335 if (IvarName == PropNameWithUnderscore)
339 // Note, this is a possible source of false positives. We could look at the
340 // getter implementation to find the ivar when its name is not derived from
341 // the property name.
345 void IvarInvalidationCheckerImpl::printIvar(llvm::raw_svector_ostream &os,
346 const ObjCIvarDecl *IvarDecl,
347 const IvarToPropMapTy &IvarToPopertyMap) {
348 if (IvarDecl->getSynthesize()) {
349 const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
350 assert(PD &&"Do we synthesize ivars for something other than properties?");
351 os << "Property "<< PD->getName() << " ";
353 os << "Instance variable "<< IvarDecl->getName() << " ";
357 // Check that the invalidatable interfaces with ivars/properties implement the
358 // invalidation methods.
359 void IvarInvalidationCheckerImpl::
360 visit(const ObjCImplementationDecl *ImplD) const {
361 // Collect all ivars that need cleanup.
363 // Record the first Ivar needing invalidation; used in reporting when only
364 // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
365 // deterministic output.
366 const ObjCIvarDecl *FirstIvarDecl = nullptr;
367 const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
369 // Collect ivars declared in this class, its extensions and its implementation
370 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
371 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
372 Iv= Iv->getNextIvar())
373 trackIvar(Iv, Ivars, &FirstIvarDecl);
375 // Construct Property/Property Accessor to Ivar maps to assist checking if an
376 // ivar which is backing a property has been reset.
377 MethToIvarMapTy PropSetterToIvarMap;
378 MethToIvarMapTy PropGetterToIvarMap;
379 PropToIvarMapTy PropertyToIvarMap;
380 IvarToPropMapTy IvarToPopertyMap;
382 ObjCInterfaceDecl::PropertyMap PropMap;
383 ObjCInterfaceDecl::PropertyDeclOrder PropOrder;
384 InterfaceD->collectPropertiesToImplement(PropMap, PropOrder);
386 for (ObjCInterfaceDecl::PropertyMap::iterator
387 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
388 const ObjCPropertyDecl *PD = I->second;
389 if (PD->isClassProperty())
392 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
397 // Store the mappings.
398 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
399 PropertyToIvarMap[PD] = ID;
400 IvarToPopertyMap[ID] = PD;
402 // Find the setter and the getter.
403 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
405 SetterD = SetterD->getCanonicalDecl();
406 PropSetterToIvarMap[SetterD] = ID;
409 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
411 GetterD = GetterD->getCanonicalDecl();
412 PropGetterToIvarMap[GetterD] = ID;
416 // If no ivars need invalidation, there is nothing to check here.
420 // Find all partial invalidation methods.
421 InvalidationInfo PartialInfo;
422 containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true);
424 // Remove ivars invalidated by the partial invalidation methods. They do not
425 // need to be invalidated in the regular invalidation methods.
426 bool AtImplementationContainsAtLeastOnePartialInvalidationMethod = false;
427 for (MethodSet::iterator
428 I = PartialInfo.InvalidationMethods.begin(),
429 E = PartialInfo.InvalidationMethods.end(); I != E; ++I) {
430 const ObjCMethodDecl *InterfD = *I;
432 // Get the corresponding method in the @implementation.
433 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
434 InterfD->isInstanceMethod());
435 if (D && D->hasBody()) {
436 AtImplementationContainsAtLeastOnePartialInvalidationMethod = true;
438 bool CalledAnotherInvalidationMethod = false;
439 // The MethodCrowler is going to remove the invalidated ivars.
441 CalledAnotherInvalidationMethod,
445 BR.getContext()).VisitStmt(D->getBody());
446 // If another invalidation method was called, trust that full invalidation
448 if (CalledAnotherInvalidationMethod)
453 // If all ivars have been invalidated by partial invalidators, there is
454 // nothing to check here.
458 // Find all invalidation methods in this @interface declaration and parents.
459 InvalidationInfo Info;
460 containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false);
462 // Report an error in case none of the invalidation methods are declared.
463 if (!Info.needsInvalidation() && !PartialInfo.needsInvalidation()) {
464 if (Filter.check_MissingInvalidationMethod)
465 reportNoInvalidationMethod(Filter.checkName_MissingInvalidationMethod,
466 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
467 /*MissingDeclaration*/ true);
468 // If there are no invalidation methods, there is no ivar validation work
473 // Only check if Ivars are invalidated when InstanceVariableInvalidation
474 // has been requested.
475 if (!Filter.check_InstanceVariableInvalidation)
478 // Check that all ivars are invalidated by the invalidation methods.
479 bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
480 for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
481 E = Info.InvalidationMethods.end(); I != E; ++I) {
482 const ObjCMethodDecl *InterfD = *I;
484 // Get the corresponding method in the @implementation.
485 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
486 InterfD->isInstanceMethod());
487 if (D && D->hasBody()) {
488 AtImplementationContainsAtLeastOneInvalidationMethod = true;
490 // Get a copy of ivars needing invalidation.
491 IvarSet IvarsI = Ivars;
493 bool CalledAnotherInvalidationMethod = false;
494 MethodCrawler(IvarsI,
495 CalledAnotherInvalidationMethod,
499 BR.getContext()).VisitStmt(D->getBody());
500 // If another invalidation method was called, trust that full invalidation
502 if (CalledAnotherInvalidationMethod)
505 // Warn on the ivars that were not invalidated by the method.
506 for (IvarSet::const_iterator
507 I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
508 reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D);
512 // Report an error in case none of the invalidation methods are implemented.
513 if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
514 if (AtImplementationContainsAtLeastOnePartialInvalidationMethod) {
515 // Warn on the ivars that were not invalidated by the prrtial
516 // invalidation methods.
517 for (IvarSet::const_iterator
518 I = Ivars.begin(), E = Ivars.end(); I != E; ++I)
519 reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, nullptr);
521 // Otherwise, no invalidation methods were implemented.
522 reportNoInvalidationMethod(Filter.checkName_InstanceVariableInvalidation,
523 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
524 /*MissingDeclaration*/ false);
529 void IvarInvalidationCheckerImpl::reportNoInvalidationMethod(
530 CheckName CheckName, const ObjCIvarDecl *FirstIvarDecl,
531 const IvarToPropMapTy &IvarToPopertyMap,
532 const ObjCInterfaceDecl *InterfaceD, bool MissingDeclaration) const {
533 SmallString<128> sbuf;
534 llvm::raw_svector_ostream os(sbuf);
535 assert(FirstIvarDecl);
536 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
537 os << "needs to be invalidated; ";
538 if (MissingDeclaration)
539 os << "no invalidation method is declared for ";
541 os << "no invalidation method is defined in the @implementation for ";
542 os << InterfaceD->getName();
544 PathDiagnosticLocation IvarDecLocation =
545 PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
547 BR.EmitBasicReport(FirstIvarDecl, CheckName, "Incomplete invalidation",
548 categories::CoreFoundationObjectiveC, os.str(),
552 void IvarInvalidationCheckerImpl::
553 reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
554 const IvarToPropMapTy &IvarToPopertyMap,
555 const ObjCMethodDecl *MethodD) const {
556 SmallString<128> sbuf;
557 llvm::raw_svector_ostream os(sbuf);
558 printIvar(os, IvarD, IvarToPopertyMap);
559 os << "needs to be invalidated or set to nil";
561 PathDiagnosticLocation MethodDecLocation =
562 PathDiagnosticLocation::createEnd(MethodD->getBody(),
563 BR.getSourceManager(),
564 Mgr.getAnalysisDeclContext(MethodD));
565 BR.EmitBasicReport(MethodD, Filter.checkName_InstanceVariableInvalidation,
566 "Incomplete invalidation",
567 categories::CoreFoundationObjectiveC, os.str(),
571 IvarD, Filter.checkName_InstanceVariableInvalidation,
572 "Incomplete invalidation", categories::CoreFoundationObjectiveC,
574 PathDiagnosticLocation::createBegin(IvarD, BR.getSourceManager()));
578 void IvarInvalidationCheckerImpl::MethodCrawler::markInvalidated(
579 const ObjCIvarDecl *Iv) {
580 IvarSet::iterator I = IVars.find(Iv);
581 if (I != IVars.end()) {
582 // If InvalidationMethod is present, we are processing the message send and
583 // should ensure we are invalidating with the appropriate method,
584 // otherwise, we are processing setting to 'nil'.
585 if (!InvalidationMethod || I->second.hasMethod(InvalidationMethod))
590 const Expr *IvarInvalidationCheckerImpl::MethodCrawler::peel(const Expr *E) const {
591 E = E->IgnoreParenCasts();
592 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
593 E = POE->getSyntacticForm()->IgnoreParenCasts();
594 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
595 E = OVE->getSourceExpr()->IgnoreParenCasts();
599 void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCIvarRefExpr(
600 const ObjCIvarRefExpr *IvarRef) {
601 if (const Decl *D = IvarRef->getDecl())
602 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
605 void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCMessageExpr(
606 const ObjCMessageExpr *ME) {
607 const ObjCMethodDecl *MD = ME->getMethodDecl();
609 MD = MD->getCanonicalDecl();
610 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
611 if (IvI != PropertyGetterToIvarMap.end())
612 markInvalidated(IvI->second);
616 void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCPropertyRefExpr(
617 const ObjCPropertyRefExpr *PA) {
619 if (PA->isExplicitProperty()) {
620 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
622 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
623 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
624 if (IvI != PropertyToIvarMap.end())
625 markInvalidated(IvI->second);
630 if (PA->isImplicitProperty()) {
631 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
633 MD = MD->getCanonicalDecl();
634 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
635 if (IvI != PropertyGetterToIvarMap.end())
636 markInvalidated(IvI->second);
642 bool IvarInvalidationCheckerImpl::MethodCrawler::isZero(const Expr *E) const {
645 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
646 != Expr::NPCK_NotNull);
649 void IvarInvalidationCheckerImpl::MethodCrawler::check(const Expr *E) {
652 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
653 checkObjCIvarRefExpr(IvarRef);
657 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
658 checkObjCPropertyRefExpr(PropRef);
662 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
663 checkObjCMessageExpr(MsgExpr);
668 void IvarInvalidationCheckerImpl::MethodCrawler::VisitBinaryOperator(
669 const BinaryOperator *BO) {
672 // Do we assign/compare against zero? If yes, check the variable we are
674 BinaryOperatorKind Opcode = BO->getOpcode();
675 if (Opcode != BO_Assign &&
680 if (isZero(BO->getRHS())) {
685 if (Opcode != BO_Assign && isZero(BO->getLHS())) {
691 void IvarInvalidationCheckerImpl::MethodCrawler::VisitObjCMessageExpr(
692 const ObjCMessageExpr *ME) {
693 const ObjCMethodDecl *MD = ME->getMethodDecl();
694 const Expr *Receiver = ME->getInstanceReceiver();
696 // Stop if we are calling '[self invalidate]'.
697 if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false))
698 if (Receiver->isObjCSelfExpr()) {
699 CalledAnotherInvalidationMethod = true;
703 // Check if we call a setter and set the property to 'nil'.
704 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
705 MD = MD->getCanonicalDecl();
706 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
707 if (IvI != PropertySetterToIvarMap.end()) {
708 markInvalidated(IvI->second);
713 // Check if we call the 'invalidation' routine on the ivar.
715 InvalidationMethod = MD;
716 check(Receiver->IgnoreParenCasts());
717 InvalidationMethod = nullptr;
722 } // end anonymous namespace
724 // Register the checkers.
726 class IvarInvalidationChecker :
727 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
731 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
732 BugReporter &BR) const {
733 IvarInvalidationCheckerImpl Walker(Mgr, BR, Filter);
737 } // end anonymous namespace
739 #define REGISTER_CHECKER(name) \
740 void ento::register##name(CheckerManager &mgr) { \
741 IvarInvalidationChecker *checker = \
742 mgr.registerChecker<IvarInvalidationChecker>(); \
743 checker->Filter.check_##name = true; \
744 checker->Filter.checkName_##name = mgr.getCurrentCheckName(); \
747 REGISTER_CHECKER(InstanceVariableInvalidation)
748 REGISTER_CHECKER(MissingInvalidationMethod)