1*67e74705SXin Li //===- IvarInvalidationChecker.cpp ------------------------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This checker implements annotation driven invalidation checking. If a class
11*67e74705SXin Li // contains a method annotated with 'objc_instance_variable_invalidator',
12*67e74705SXin Li // - (void) foo
13*67e74705SXin Li // __attribute__((annotate("objc_instance_variable_invalidator")));
14*67e74705SXin Li // all the "ivalidatable" instance variables of this class should be
15*67e74705SXin Li // invalidated. We call an instance variable ivalidatable if it is an object of
16*67e74705SXin Li // a class which contains an invalidation method. There could be multiple
17*67e74705SXin Li // methods annotated with such annotations per class, either one can be used
18*67e74705SXin Li // to invalidate the ivar. An ivar or property are considered to be
19*67e74705SXin Li // invalidated if they are being assigned 'nil' or an invalidation method has
20*67e74705SXin Li // been called on them. An invalidation method should either invalidate all
21*67e74705SXin Li // the ivars or call another invalidation method (on self).
22*67e74705SXin Li //
23*67e74705SXin Li // Partial invalidor annotation allows to addess cases when ivars are
24*67e74705SXin Li // invalidated by other methods, which might or might not be called from
25*67e74705SXin Li // the invalidation method. The checker checks that each invalidation
26*67e74705SXin Li // method and all the partial methods cumulatively invalidate all ivars.
27*67e74705SXin Li // __attribute__((annotate("objc_instance_variable_invalidator_partial")));
28*67e74705SXin Li //
29*67e74705SXin Li //===----------------------------------------------------------------------===//
30*67e74705SXin Li
31*67e74705SXin Li #include "ClangSACheckers.h"
32*67e74705SXin Li #include "clang/AST/Attr.h"
33*67e74705SXin Li #include "clang/AST/DeclObjC.h"
34*67e74705SXin Li #include "clang/AST/StmtVisitor.h"
35*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
37*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
38*67e74705SXin Li #include "llvm/ADT/DenseMap.h"
39*67e74705SXin Li #include "llvm/ADT/SetVector.h"
40*67e74705SXin Li #include "llvm/ADT/SmallString.h"
41*67e74705SXin Li
42*67e74705SXin Li using namespace clang;
43*67e74705SXin Li using namespace ento;
44*67e74705SXin Li
45*67e74705SXin Li namespace {
46*67e74705SXin Li struct ChecksFilter {
47*67e74705SXin Li /// Check for missing invalidation method declarations.
48*67e74705SXin Li DefaultBool check_MissingInvalidationMethod;
49*67e74705SXin Li /// Check that all ivars are invalidated.
50*67e74705SXin Li DefaultBool check_InstanceVariableInvalidation;
51*67e74705SXin Li
52*67e74705SXin Li CheckName checkName_MissingInvalidationMethod;
53*67e74705SXin Li CheckName checkName_InstanceVariableInvalidation;
54*67e74705SXin Li };
55*67e74705SXin Li
56*67e74705SXin Li class IvarInvalidationCheckerImpl {
57*67e74705SXin Li typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
58*67e74705SXin Li typedef llvm::DenseMap<const ObjCMethodDecl*,
59*67e74705SXin Li const ObjCIvarDecl*> MethToIvarMapTy;
60*67e74705SXin Li typedef llvm::DenseMap<const ObjCPropertyDecl*,
61*67e74705SXin Li const ObjCIvarDecl*> PropToIvarMapTy;
62*67e74705SXin Li typedef llvm::DenseMap<const ObjCIvarDecl*,
63*67e74705SXin Li const ObjCPropertyDecl*> IvarToPropMapTy;
64*67e74705SXin Li
65*67e74705SXin Li struct InvalidationInfo {
66*67e74705SXin Li /// Has the ivar been invalidated?
67*67e74705SXin Li bool IsInvalidated;
68*67e74705SXin Li
69*67e74705SXin Li /// The methods which can be used to invalidate the ivar.
70*67e74705SXin Li MethodSet InvalidationMethods;
71*67e74705SXin Li
InvalidationInfo__anonb9c8af4f0111::IvarInvalidationCheckerImpl::InvalidationInfo72*67e74705SXin Li InvalidationInfo() : IsInvalidated(false) {}
addInvalidationMethod__anonb9c8af4f0111::IvarInvalidationCheckerImpl::InvalidationInfo73*67e74705SXin Li void addInvalidationMethod(const ObjCMethodDecl *MD) {
74*67e74705SXin Li InvalidationMethods.insert(MD);
75*67e74705SXin Li }
76*67e74705SXin Li
needsInvalidation__anonb9c8af4f0111::IvarInvalidationCheckerImpl::InvalidationInfo77*67e74705SXin Li bool needsInvalidation() const {
78*67e74705SXin Li return !InvalidationMethods.empty();
79*67e74705SXin Li }
80*67e74705SXin Li
hasMethod__anonb9c8af4f0111::IvarInvalidationCheckerImpl::InvalidationInfo81*67e74705SXin Li bool hasMethod(const ObjCMethodDecl *MD) {
82*67e74705SXin Li if (IsInvalidated)
83*67e74705SXin Li return true;
84*67e74705SXin Li for (MethodSet::iterator I = InvalidationMethods.begin(),
85*67e74705SXin Li E = InvalidationMethods.end(); I != E; ++I) {
86*67e74705SXin Li if (*I == MD) {
87*67e74705SXin Li IsInvalidated = true;
88*67e74705SXin Li return true;
89*67e74705SXin Li }
90*67e74705SXin Li }
91*67e74705SXin Li return false;
92*67e74705SXin Li }
93*67e74705SXin Li };
94*67e74705SXin Li
95*67e74705SXin Li typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
96*67e74705SXin Li
97*67e74705SXin Li /// Statement visitor, which walks the method body and flags the ivars
98*67e74705SXin Li /// referenced in it (either directly or via property).
99*67e74705SXin Li class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
100*67e74705SXin Li /// The set of Ivars which need to be invalidated.
101*67e74705SXin Li IvarSet &IVars;
102*67e74705SXin Li
103*67e74705SXin Li /// Flag is set as the result of a message send to another
104*67e74705SXin Li /// invalidation method.
105*67e74705SXin Li bool &CalledAnotherInvalidationMethod;
106*67e74705SXin Li
107*67e74705SXin Li /// Property setter to ivar mapping.
108*67e74705SXin Li const MethToIvarMapTy &PropertySetterToIvarMap;
109*67e74705SXin Li
110*67e74705SXin Li /// Property getter to ivar mapping.
111*67e74705SXin Li const MethToIvarMapTy &PropertyGetterToIvarMap;
112*67e74705SXin Li
113*67e74705SXin Li /// Property to ivar mapping.
114*67e74705SXin Li const PropToIvarMapTy &PropertyToIvarMap;
115*67e74705SXin Li
116*67e74705SXin Li /// The invalidation method being currently processed.
117*67e74705SXin Li const ObjCMethodDecl *InvalidationMethod;
118*67e74705SXin Li
119*67e74705SXin Li ASTContext &Ctx;
120*67e74705SXin Li
121*67e74705SXin Li /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
122*67e74705SXin Li const Expr *peel(const Expr *E) const;
123*67e74705SXin Li
124*67e74705SXin Li /// Does this expression represent zero: '0'?
125*67e74705SXin Li bool isZero(const Expr *E) const;
126*67e74705SXin Li
127*67e74705SXin Li /// Mark the given ivar as invalidated.
128*67e74705SXin Li void markInvalidated(const ObjCIvarDecl *Iv);
129*67e74705SXin Li
130*67e74705SXin Li /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
131*67e74705SXin Li /// invalidated.
132*67e74705SXin Li void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
133*67e74705SXin Li
134*67e74705SXin Li /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
135*67e74705SXin Li /// it as invalidated.
136*67e74705SXin Li void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
137*67e74705SXin Li
138*67e74705SXin Li /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
139*67e74705SXin Li /// if yes, marks it as invalidated.
140*67e74705SXin Li void checkObjCMessageExpr(const ObjCMessageExpr *ME);
141*67e74705SXin Li
142*67e74705SXin Li /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
143*67e74705SXin Li void check(const Expr *E);
144*67e74705SXin Li
145*67e74705SXin Li public:
MethodCrawler(IvarSet & InIVars,bool & InCalledAnotherInvalidationMethod,const MethToIvarMapTy & InPropertySetterToIvarMap,const MethToIvarMapTy & InPropertyGetterToIvarMap,const PropToIvarMapTy & InPropertyToIvarMap,ASTContext & InCtx)146*67e74705SXin Li MethodCrawler(IvarSet &InIVars,
147*67e74705SXin Li bool &InCalledAnotherInvalidationMethod,
148*67e74705SXin Li const MethToIvarMapTy &InPropertySetterToIvarMap,
149*67e74705SXin Li const MethToIvarMapTy &InPropertyGetterToIvarMap,
150*67e74705SXin Li const PropToIvarMapTy &InPropertyToIvarMap,
151*67e74705SXin Li ASTContext &InCtx)
152*67e74705SXin Li : IVars(InIVars),
153*67e74705SXin Li CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
154*67e74705SXin Li PropertySetterToIvarMap(InPropertySetterToIvarMap),
155*67e74705SXin Li PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
156*67e74705SXin Li PropertyToIvarMap(InPropertyToIvarMap),
157*67e74705SXin Li InvalidationMethod(nullptr),
158*67e74705SXin Li Ctx(InCtx) {}
159*67e74705SXin Li
VisitStmt(const Stmt * S)160*67e74705SXin Li void VisitStmt(const Stmt *S) { VisitChildren(S); }
161*67e74705SXin Li
162*67e74705SXin Li void VisitBinaryOperator(const BinaryOperator *BO);
163*67e74705SXin Li
164*67e74705SXin Li void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
165*67e74705SXin Li
VisitChildren(const Stmt * S)166*67e74705SXin Li void VisitChildren(const Stmt *S) {
167*67e74705SXin Li for (const auto *Child : S->children()) {
168*67e74705SXin Li if (Child)
169*67e74705SXin Li this->Visit(Child);
170*67e74705SXin Li if (CalledAnotherInvalidationMethod)
171*67e74705SXin Li return;
172*67e74705SXin Li }
173*67e74705SXin Li }
174*67e74705SXin Li };
175*67e74705SXin Li
176*67e74705SXin Li /// Check if the any of the methods inside the interface are annotated with
177*67e74705SXin Li /// the invalidation annotation, update the IvarInfo accordingly.
178*67e74705SXin Li /// \param LookForPartial is set when we are searching for partial
179*67e74705SXin Li /// invalidators.
180*67e74705SXin Li static void containsInvalidationMethod(const ObjCContainerDecl *D,
181*67e74705SXin Li InvalidationInfo &Out,
182*67e74705SXin Li bool LookForPartial);
183*67e74705SXin Li
184*67e74705SXin Li /// Check if ivar should be tracked and add to TrackedIvars if positive.
185*67e74705SXin Li /// Returns true if ivar should be tracked.
186*67e74705SXin Li static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
187*67e74705SXin Li const ObjCIvarDecl **FirstIvarDecl);
188*67e74705SXin Li
189*67e74705SXin Li /// Given the property declaration, and the list of tracked ivars, finds
190*67e74705SXin Li /// the ivar backing the property when possible. Returns '0' when no such
191*67e74705SXin Li /// ivar could be found.
192*67e74705SXin Li static const ObjCIvarDecl *findPropertyBackingIvar(
193*67e74705SXin Li const ObjCPropertyDecl *Prop,
194*67e74705SXin Li const ObjCInterfaceDecl *InterfaceD,
195*67e74705SXin Li IvarSet &TrackedIvars,
196*67e74705SXin Li const ObjCIvarDecl **FirstIvarDecl);
197*67e74705SXin Li
198*67e74705SXin Li /// Print ivar name or the property if the given ivar backs a property.
199*67e74705SXin Li static void printIvar(llvm::raw_svector_ostream &os,
200*67e74705SXin Li const ObjCIvarDecl *IvarDecl,
201*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap);
202*67e74705SXin Li
203*67e74705SXin Li void reportNoInvalidationMethod(CheckName CheckName,
204*67e74705SXin Li const ObjCIvarDecl *FirstIvarDecl,
205*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap,
206*67e74705SXin Li const ObjCInterfaceDecl *InterfaceD,
207*67e74705SXin Li bool MissingDeclaration) const;
208*67e74705SXin Li
209*67e74705SXin Li void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
210*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap,
211*67e74705SXin Li const ObjCMethodDecl *MethodD) const;
212*67e74705SXin Li
213*67e74705SXin Li AnalysisManager& Mgr;
214*67e74705SXin Li BugReporter &BR;
215*67e74705SXin Li /// Filter on the checks performed.
216*67e74705SXin Li const ChecksFilter &Filter;
217*67e74705SXin Li
218*67e74705SXin Li public:
IvarInvalidationCheckerImpl(AnalysisManager & InMgr,BugReporter & InBR,const ChecksFilter & InFilter)219*67e74705SXin Li IvarInvalidationCheckerImpl(AnalysisManager& InMgr,
220*67e74705SXin Li BugReporter &InBR,
221*67e74705SXin Li const ChecksFilter &InFilter) :
222*67e74705SXin Li Mgr (InMgr), BR(InBR), Filter(InFilter) {}
223*67e74705SXin Li
224*67e74705SXin Li void visit(const ObjCImplementationDecl *D) const;
225*67e74705SXin Li };
226*67e74705SXin Li
isInvalidationMethod(const ObjCMethodDecl * M,bool LookForPartial)227*67e74705SXin Li static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) {
228*67e74705SXin Li for (const auto *Ann : M->specific_attrs<AnnotateAttr>()) {
229*67e74705SXin Li if (!LookForPartial &&
230*67e74705SXin Li Ann->getAnnotation() == "objc_instance_variable_invalidator")
231*67e74705SXin Li return true;
232*67e74705SXin Li if (LookForPartial &&
233*67e74705SXin Li Ann->getAnnotation() == "objc_instance_variable_invalidator_partial")
234*67e74705SXin Li return true;
235*67e74705SXin Li }
236*67e74705SXin Li return false;
237*67e74705SXin Li }
238*67e74705SXin Li
containsInvalidationMethod(const ObjCContainerDecl * D,InvalidationInfo & OutInfo,bool Partial)239*67e74705SXin Li void IvarInvalidationCheckerImpl::containsInvalidationMethod(
240*67e74705SXin Li const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) {
241*67e74705SXin Li
242*67e74705SXin Li if (!D)
243*67e74705SXin Li return;
244*67e74705SXin Li
245*67e74705SXin Li assert(!isa<ObjCImplementationDecl>(D));
246*67e74705SXin Li // TODO: Cache the results.
247*67e74705SXin Li
248*67e74705SXin Li // Check all methods.
249*67e74705SXin Li for (const auto *MDI : D->methods())
250*67e74705SXin Li if (isInvalidationMethod(MDI, Partial))
251*67e74705SXin Li OutInfo.addInvalidationMethod(
252*67e74705SXin Li cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
253*67e74705SXin Li
254*67e74705SXin Li // If interface, check all parent protocols and super.
255*67e74705SXin Li if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
256*67e74705SXin Li
257*67e74705SXin Li // Visit all protocols.
258*67e74705SXin Li for (const auto *I : InterfD->protocols())
259*67e74705SXin Li containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
260*67e74705SXin Li
261*67e74705SXin Li // Visit all categories in case the invalidation method is declared in
262*67e74705SXin Li // a category.
263*67e74705SXin Li for (const auto *Ext : InterfD->visible_extensions())
264*67e74705SXin Li containsInvalidationMethod(Ext, OutInfo, Partial);
265*67e74705SXin Li
266*67e74705SXin Li containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
267*67e74705SXin Li return;
268*67e74705SXin Li }
269*67e74705SXin Li
270*67e74705SXin Li // If protocol, check all parent protocols.
271*67e74705SXin Li if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
272*67e74705SXin Li for (const auto *I : ProtD->protocols()) {
273*67e74705SXin Li containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
274*67e74705SXin Li }
275*67e74705SXin Li return;
276*67e74705SXin Li }
277*67e74705SXin Li }
278*67e74705SXin Li
trackIvar(const ObjCIvarDecl * Iv,IvarSet & TrackedIvars,const ObjCIvarDecl ** FirstIvarDecl)279*67e74705SXin Li bool IvarInvalidationCheckerImpl::trackIvar(const ObjCIvarDecl *Iv,
280*67e74705SXin Li IvarSet &TrackedIvars,
281*67e74705SXin Li const ObjCIvarDecl **FirstIvarDecl) {
282*67e74705SXin Li QualType IvQTy = Iv->getType();
283*67e74705SXin Li const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
284*67e74705SXin Li if (!IvTy)
285*67e74705SXin Li return false;
286*67e74705SXin Li const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
287*67e74705SXin Li
288*67e74705SXin Li InvalidationInfo Info;
289*67e74705SXin Li containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false);
290*67e74705SXin Li if (Info.needsInvalidation()) {
291*67e74705SXin Li const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
292*67e74705SXin Li TrackedIvars[I] = Info;
293*67e74705SXin Li if (!*FirstIvarDecl)
294*67e74705SXin Li *FirstIvarDecl = I;
295*67e74705SXin Li return true;
296*67e74705SXin Li }
297*67e74705SXin Li return false;
298*67e74705SXin Li }
299*67e74705SXin Li
findPropertyBackingIvar(const ObjCPropertyDecl * Prop,const ObjCInterfaceDecl * InterfaceD,IvarSet & TrackedIvars,const ObjCIvarDecl ** FirstIvarDecl)300*67e74705SXin Li const ObjCIvarDecl *IvarInvalidationCheckerImpl::findPropertyBackingIvar(
301*67e74705SXin Li const ObjCPropertyDecl *Prop,
302*67e74705SXin Li const ObjCInterfaceDecl *InterfaceD,
303*67e74705SXin Li IvarSet &TrackedIvars,
304*67e74705SXin Li const ObjCIvarDecl **FirstIvarDecl) {
305*67e74705SXin Li const ObjCIvarDecl *IvarD = nullptr;
306*67e74705SXin Li
307*67e74705SXin Li // Lookup for the synthesized case.
308*67e74705SXin Li IvarD = Prop->getPropertyIvarDecl();
309*67e74705SXin Li // We only track the ivars/properties that are defined in the current
310*67e74705SXin Li // class (not the parent).
311*67e74705SXin Li if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
312*67e74705SXin Li if (TrackedIvars.count(IvarD)) {
313*67e74705SXin Li return IvarD;
314*67e74705SXin Li }
315*67e74705SXin Li // If the ivar is synthesized we still want to track it.
316*67e74705SXin Li if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
317*67e74705SXin Li return IvarD;
318*67e74705SXin Li }
319*67e74705SXin Li
320*67e74705SXin Li // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
321*67e74705SXin Li StringRef PropName = Prop->getIdentifier()->getName();
322*67e74705SXin Li for (IvarSet::const_iterator I = TrackedIvars.begin(),
323*67e74705SXin Li E = TrackedIvars.end(); I != E; ++I) {
324*67e74705SXin Li const ObjCIvarDecl *Iv = I->first;
325*67e74705SXin Li StringRef IvarName = Iv->getName();
326*67e74705SXin Li
327*67e74705SXin Li if (IvarName == PropName)
328*67e74705SXin Li return Iv;
329*67e74705SXin Li
330*67e74705SXin Li SmallString<128> PropNameWithUnderscore;
331*67e74705SXin Li {
332*67e74705SXin Li llvm::raw_svector_ostream os(PropNameWithUnderscore);
333*67e74705SXin Li os << '_' << PropName;
334*67e74705SXin Li }
335*67e74705SXin Li if (IvarName == PropNameWithUnderscore)
336*67e74705SXin Li return Iv;
337*67e74705SXin Li }
338*67e74705SXin Li
339*67e74705SXin Li // Note, this is a possible source of false positives. We could look at the
340*67e74705SXin Li // getter implementation to find the ivar when its name is not derived from
341*67e74705SXin Li // the property name.
342*67e74705SXin Li return nullptr;
343*67e74705SXin Li }
344*67e74705SXin Li
printIvar(llvm::raw_svector_ostream & os,const ObjCIvarDecl * IvarDecl,const IvarToPropMapTy & IvarToPopertyMap)345*67e74705SXin Li void IvarInvalidationCheckerImpl::printIvar(llvm::raw_svector_ostream &os,
346*67e74705SXin Li const ObjCIvarDecl *IvarDecl,
347*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap) {
348*67e74705SXin Li if (IvarDecl->getSynthesize()) {
349*67e74705SXin Li const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
350*67e74705SXin Li assert(PD &&"Do we synthesize ivars for something other than properties?");
351*67e74705SXin Li os << "Property "<< PD->getName() << " ";
352*67e74705SXin Li } else {
353*67e74705SXin Li os << "Instance variable "<< IvarDecl->getName() << " ";
354*67e74705SXin Li }
355*67e74705SXin Li }
356*67e74705SXin Li
357*67e74705SXin Li // Check that the invalidatable interfaces with ivars/properties implement the
358*67e74705SXin Li // invalidation methods.
359*67e74705SXin Li void IvarInvalidationCheckerImpl::
visit(const ObjCImplementationDecl * ImplD) const360*67e74705SXin Li visit(const ObjCImplementationDecl *ImplD) const {
361*67e74705SXin Li // Collect all ivars that need cleanup.
362*67e74705SXin Li IvarSet Ivars;
363*67e74705SXin Li // Record the first Ivar needing invalidation; used in reporting when only
364*67e74705SXin Li // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
365*67e74705SXin Li // deterministic output.
366*67e74705SXin Li const ObjCIvarDecl *FirstIvarDecl = nullptr;
367*67e74705SXin Li const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
368*67e74705SXin Li
369*67e74705SXin Li // Collect ivars declared in this class, its extensions and its implementation
370*67e74705SXin Li ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
371*67e74705SXin Li for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
372*67e74705SXin Li Iv= Iv->getNextIvar())
373*67e74705SXin Li trackIvar(Iv, Ivars, &FirstIvarDecl);
374*67e74705SXin Li
375*67e74705SXin Li // Construct Property/Property Accessor to Ivar maps to assist checking if an
376*67e74705SXin Li // ivar which is backing a property has been reset.
377*67e74705SXin Li MethToIvarMapTy PropSetterToIvarMap;
378*67e74705SXin Li MethToIvarMapTy PropGetterToIvarMap;
379*67e74705SXin Li PropToIvarMapTy PropertyToIvarMap;
380*67e74705SXin Li IvarToPropMapTy IvarToPopertyMap;
381*67e74705SXin Li
382*67e74705SXin Li ObjCInterfaceDecl::PropertyMap PropMap;
383*67e74705SXin Li ObjCInterfaceDecl::PropertyDeclOrder PropOrder;
384*67e74705SXin Li InterfaceD->collectPropertiesToImplement(PropMap, PropOrder);
385*67e74705SXin Li
386*67e74705SXin Li for (ObjCInterfaceDecl::PropertyMap::iterator
387*67e74705SXin Li I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
388*67e74705SXin Li const ObjCPropertyDecl *PD = I->second;
389*67e74705SXin Li if (PD->isClassProperty())
390*67e74705SXin Li continue;
391*67e74705SXin Li
392*67e74705SXin Li const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
393*67e74705SXin Li &FirstIvarDecl);
394*67e74705SXin Li if (!ID)
395*67e74705SXin Li continue;
396*67e74705SXin Li
397*67e74705SXin Li // Store the mappings.
398*67e74705SXin Li PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
399*67e74705SXin Li PropertyToIvarMap[PD] = ID;
400*67e74705SXin Li IvarToPopertyMap[ID] = PD;
401*67e74705SXin Li
402*67e74705SXin Li // Find the setter and the getter.
403*67e74705SXin Li const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
404*67e74705SXin Li if (SetterD) {
405*67e74705SXin Li SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
406*67e74705SXin Li PropSetterToIvarMap[SetterD] = ID;
407*67e74705SXin Li }
408*67e74705SXin Li
409*67e74705SXin Li const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
410*67e74705SXin Li if (GetterD) {
411*67e74705SXin Li GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
412*67e74705SXin Li PropGetterToIvarMap[GetterD] = ID;
413*67e74705SXin Li }
414*67e74705SXin Li }
415*67e74705SXin Li
416*67e74705SXin Li // If no ivars need invalidation, there is nothing to check here.
417*67e74705SXin Li if (Ivars.empty())
418*67e74705SXin Li return;
419*67e74705SXin Li
420*67e74705SXin Li // Find all partial invalidation methods.
421*67e74705SXin Li InvalidationInfo PartialInfo;
422*67e74705SXin Li containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true);
423*67e74705SXin Li
424*67e74705SXin Li // Remove ivars invalidated by the partial invalidation methods. They do not
425*67e74705SXin Li // need to be invalidated in the regular invalidation methods.
426*67e74705SXin Li bool AtImplementationContainsAtLeastOnePartialInvalidationMethod = false;
427*67e74705SXin Li for (MethodSet::iterator
428*67e74705SXin Li I = PartialInfo.InvalidationMethods.begin(),
429*67e74705SXin Li E = PartialInfo.InvalidationMethods.end(); I != E; ++I) {
430*67e74705SXin Li const ObjCMethodDecl *InterfD = *I;
431*67e74705SXin Li
432*67e74705SXin Li // Get the corresponding method in the @implementation.
433*67e74705SXin Li const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
434*67e74705SXin Li InterfD->isInstanceMethod());
435*67e74705SXin Li if (D && D->hasBody()) {
436*67e74705SXin Li AtImplementationContainsAtLeastOnePartialInvalidationMethod = true;
437*67e74705SXin Li
438*67e74705SXin Li bool CalledAnotherInvalidationMethod = false;
439*67e74705SXin Li // The MethodCrowler is going to remove the invalidated ivars.
440*67e74705SXin Li MethodCrawler(Ivars,
441*67e74705SXin Li CalledAnotherInvalidationMethod,
442*67e74705SXin Li PropSetterToIvarMap,
443*67e74705SXin Li PropGetterToIvarMap,
444*67e74705SXin Li PropertyToIvarMap,
445*67e74705SXin Li BR.getContext()).VisitStmt(D->getBody());
446*67e74705SXin Li // If another invalidation method was called, trust that full invalidation
447*67e74705SXin Li // has occurred.
448*67e74705SXin Li if (CalledAnotherInvalidationMethod)
449*67e74705SXin Li Ivars.clear();
450*67e74705SXin Li }
451*67e74705SXin Li }
452*67e74705SXin Li
453*67e74705SXin Li // If all ivars have been invalidated by partial invalidators, there is
454*67e74705SXin Li // nothing to check here.
455*67e74705SXin Li if (Ivars.empty())
456*67e74705SXin Li return;
457*67e74705SXin Li
458*67e74705SXin Li // Find all invalidation methods in this @interface declaration and parents.
459*67e74705SXin Li InvalidationInfo Info;
460*67e74705SXin Li containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false);
461*67e74705SXin Li
462*67e74705SXin Li // Report an error in case none of the invalidation methods are declared.
463*67e74705SXin Li if (!Info.needsInvalidation() && !PartialInfo.needsInvalidation()) {
464*67e74705SXin Li if (Filter.check_MissingInvalidationMethod)
465*67e74705SXin Li reportNoInvalidationMethod(Filter.checkName_MissingInvalidationMethod,
466*67e74705SXin Li FirstIvarDecl, IvarToPopertyMap, InterfaceD,
467*67e74705SXin Li /*MissingDeclaration*/ true);
468*67e74705SXin Li // If there are no invalidation methods, there is no ivar validation work
469*67e74705SXin Li // to be done.
470*67e74705SXin Li return;
471*67e74705SXin Li }
472*67e74705SXin Li
473*67e74705SXin Li // Only check if Ivars are invalidated when InstanceVariableInvalidation
474*67e74705SXin Li // has been requested.
475*67e74705SXin Li if (!Filter.check_InstanceVariableInvalidation)
476*67e74705SXin Li return;
477*67e74705SXin Li
478*67e74705SXin Li // Check that all ivars are invalidated by the invalidation methods.
479*67e74705SXin Li bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
480*67e74705SXin Li for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
481*67e74705SXin Li E = Info.InvalidationMethods.end(); I != E; ++I) {
482*67e74705SXin Li const ObjCMethodDecl *InterfD = *I;
483*67e74705SXin Li
484*67e74705SXin Li // Get the corresponding method in the @implementation.
485*67e74705SXin Li const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
486*67e74705SXin Li InterfD->isInstanceMethod());
487*67e74705SXin Li if (D && D->hasBody()) {
488*67e74705SXin Li AtImplementationContainsAtLeastOneInvalidationMethod = true;
489*67e74705SXin Li
490*67e74705SXin Li // Get a copy of ivars needing invalidation.
491*67e74705SXin Li IvarSet IvarsI = Ivars;
492*67e74705SXin Li
493*67e74705SXin Li bool CalledAnotherInvalidationMethod = false;
494*67e74705SXin Li MethodCrawler(IvarsI,
495*67e74705SXin Li CalledAnotherInvalidationMethod,
496*67e74705SXin Li PropSetterToIvarMap,
497*67e74705SXin Li PropGetterToIvarMap,
498*67e74705SXin Li PropertyToIvarMap,
499*67e74705SXin Li BR.getContext()).VisitStmt(D->getBody());
500*67e74705SXin Li // If another invalidation method was called, trust that full invalidation
501*67e74705SXin Li // has occurred.
502*67e74705SXin Li if (CalledAnotherInvalidationMethod)
503*67e74705SXin Li continue;
504*67e74705SXin Li
505*67e74705SXin Li // Warn on the ivars that were not invalidated by the method.
506*67e74705SXin Li for (IvarSet::const_iterator
507*67e74705SXin Li I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
508*67e74705SXin Li reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D);
509*67e74705SXin Li }
510*67e74705SXin Li }
511*67e74705SXin Li
512*67e74705SXin Li // Report an error in case none of the invalidation methods are implemented.
513*67e74705SXin Li if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
514*67e74705SXin Li if (AtImplementationContainsAtLeastOnePartialInvalidationMethod) {
515*67e74705SXin Li // Warn on the ivars that were not invalidated by the prrtial
516*67e74705SXin Li // invalidation methods.
517*67e74705SXin Li for (IvarSet::const_iterator
518*67e74705SXin Li I = Ivars.begin(), E = Ivars.end(); I != E; ++I)
519*67e74705SXin Li reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, nullptr);
520*67e74705SXin Li } else {
521*67e74705SXin Li // Otherwise, no invalidation methods were implemented.
522*67e74705SXin Li reportNoInvalidationMethod(Filter.checkName_InstanceVariableInvalidation,
523*67e74705SXin Li FirstIvarDecl, IvarToPopertyMap, InterfaceD,
524*67e74705SXin Li /*MissingDeclaration*/ false);
525*67e74705SXin Li }
526*67e74705SXin Li }
527*67e74705SXin Li }
528*67e74705SXin Li
reportNoInvalidationMethod(CheckName CheckName,const ObjCIvarDecl * FirstIvarDecl,const IvarToPropMapTy & IvarToPopertyMap,const ObjCInterfaceDecl * InterfaceD,bool MissingDeclaration) const529*67e74705SXin Li void IvarInvalidationCheckerImpl::reportNoInvalidationMethod(
530*67e74705SXin Li CheckName CheckName, const ObjCIvarDecl *FirstIvarDecl,
531*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap,
532*67e74705SXin Li const ObjCInterfaceDecl *InterfaceD, bool MissingDeclaration) const {
533*67e74705SXin Li SmallString<128> sbuf;
534*67e74705SXin Li llvm::raw_svector_ostream os(sbuf);
535*67e74705SXin Li assert(FirstIvarDecl);
536*67e74705SXin Li printIvar(os, FirstIvarDecl, IvarToPopertyMap);
537*67e74705SXin Li os << "needs to be invalidated; ";
538*67e74705SXin Li if (MissingDeclaration)
539*67e74705SXin Li os << "no invalidation method is declared for ";
540*67e74705SXin Li else
541*67e74705SXin Li os << "no invalidation method is defined in the @implementation for ";
542*67e74705SXin Li os << InterfaceD->getName();
543*67e74705SXin Li
544*67e74705SXin Li PathDiagnosticLocation IvarDecLocation =
545*67e74705SXin Li PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
546*67e74705SXin Li
547*67e74705SXin Li BR.EmitBasicReport(FirstIvarDecl, CheckName, "Incomplete invalidation",
548*67e74705SXin Li categories::CoreFoundationObjectiveC, os.str(),
549*67e74705SXin Li IvarDecLocation);
550*67e74705SXin Li }
551*67e74705SXin Li
552*67e74705SXin Li void IvarInvalidationCheckerImpl::
reportIvarNeedsInvalidation(const ObjCIvarDecl * IvarD,const IvarToPropMapTy & IvarToPopertyMap,const ObjCMethodDecl * MethodD) const553*67e74705SXin Li reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
554*67e74705SXin Li const IvarToPropMapTy &IvarToPopertyMap,
555*67e74705SXin Li const ObjCMethodDecl *MethodD) const {
556*67e74705SXin Li SmallString<128> sbuf;
557*67e74705SXin Li llvm::raw_svector_ostream os(sbuf);
558*67e74705SXin Li printIvar(os, IvarD, IvarToPopertyMap);
559*67e74705SXin Li os << "needs to be invalidated or set to nil";
560*67e74705SXin Li if (MethodD) {
561*67e74705SXin Li PathDiagnosticLocation MethodDecLocation =
562*67e74705SXin Li PathDiagnosticLocation::createEnd(MethodD->getBody(),
563*67e74705SXin Li BR.getSourceManager(),
564*67e74705SXin Li Mgr.getAnalysisDeclContext(MethodD));
565*67e74705SXin Li BR.EmitBasicReport(MethodD, Filter.checkName_InstanceVariableInvalidation,
566*67e74705SXin Li "Incomplete invalidation",
567*67e74705SXin Li categories::CoreFoundationObjectiveC, os.str(),
568*67e74705SXin Li MethodDecLocation);
569*67e74705SXin Li } else {
570*67e74705SXin Li BR.EmitBasicReport(
571*67e74705SXin Li IvarD, Filter.checkName_InstanceVariableInvalidation,
572*67e74705SXin Li "Incomplete invalidation", categories::CoreFoundationObjectiveC,
573*67e74705SXin Li os.str(),
574*67e74705SXin Li PathDiagnosticLocation::createBegin(IvarD, BR.getSourceManager()));
575*67e74705SXin Li }
576*67e74705SXin Li }
577*67e74705SXin Li
markInvalidated(const ObjCIvarDecl * Iv)578*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::markInvalidated(
579*67e74705SXin Li const ObjCIvarDecl *Iv) {
580*67e74705SXin Li IvarSet::iterator I = IVars.find(Iv);
581*67e74705SXin Li if (I != IVars.end()) {
582*67e74705SXin Li // If InvalidationMethod is present, we are processing the message send and
583*67e74705SXin Li // should ensure we are invalidating with the appropriate method,
584*67e74705SXin Li // otherwise, we are processing setting to 'nil'.
585*67e74705SXin Li if (!InvalidationMethod || I->second.hasMethod(InvalidationMethod))
586*67e74705SXin Li IVars.erase(I);
587*67e74705SXin Li }
588*67e74705SXin Li }
589*67e74705SXin Li
peel(const Expr * E) const590*67e74705SXin Li const Expr *IvarInvalidationCheckerImpl::MethodCrawler::peel(const Expr *E) const {
591*67e74705SXin Li E = E->IgnoreParenCasts();
592*67e74705SXin Li if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
593*67e74705SXin Li E = POE->getSyntacticForm()->IgnoreParenCasts();
594*67e74705SXin Li if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
595*67e74705SXin Li E = OVE->getSourceExpr()->IgnoreParenCasts();
596*67e74705SXin Li return E;
597*67e74705SXin Li }
598*67e74705SXin Li
checkObjCIvarRefExpr(const ObjCIvarRefExpr * IvarRef)599*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCIvarRefExpr(
600*67e74705SXin Li const ObjCIvarRefExpr *IvarRef) {
601*67e74705SXin Li if (const Decl *D = IvarRef->getDecl())
602*67e74705SXin Li markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
603*67e74705SXin Li }
604*67e74705SXin Li
checkObjCMessageExpr(const ObjCMessageExpr * ME)605*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCMessageExpr(
606*67e74705SXin Li const ObjCMessageExpr *ME) {
607*67e74705SXin Li const ObjCMethodDecl *MD = ME->getMethodDecl();
608*67e74705SXin Li if (MD) {
609*67e74705SXin Li MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
610*67e74705SXin Li MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
611*67e74705SXin Li if (IvI != PropertyGetterToIvarMap.end())
612*67e74705SXin Li markInvalidated(IvI->second);
613*67e74705SXin Li }
614*67e74705SXin Li }
615*67e74705SXin Li
checkObjCPropertyRefExpr(const ObjCPropertyRefExpr * PA)616*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCPropertyRefExpr(
617*67e74705SXin Li const ObjCPropertyRefExpr *PA) {
618*67e74705SXin Li
619*67e74705SXin Li if (PA->isExplicitProperty()) {
620*67e74705SXin Li const ObjCPropertyDecl *PD = PA->getExplicitProperty();
621*67e74705SXin Li if (PD) {
622*67e74705SXin Li PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
623*67e74705SXin Li PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
624*67e74705SXin Li if (IvI != PropertyToIvarMap.end())
625*67e74705SXin Li markInvalidated(IvI->second);
626*67e74705SXin Li return;
627*67e74705SXin Li }
628*67e74705SXin Li }
629*67e74705SXin Li
630*67e74705SXin Li if (PA->isImplicitProperty()) {
631*67e74705SXin Li const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
632*67e74705SXin Li if (MD) {
633*67e74705SXin Li MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
634*67e74705SXin Li MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
635*67e74705SXin Li if (IvI != PropertyGetterToIvarMap.end())
636*67e74705SXin Li markInvalidated(IvI->second);
637*67e74705SXin Li return;
638*67e74705SXin Li }
639*67e74705SXin Li }
640*67e74705SXin Li }
641*67e74705SXin Li
isZero(const Expr * E) const642*67e74705SXin Li bool IvarInvalidationCheckerImpl::MethodCrawler::isZero(const Expr *E) const {
643*67e74705SXin Li E = peel(E);
644*67e74705SXin Li
645*67e74705SXin Li return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
646*67e74705SXin Li != Expr::NPCK_NotNull);
647*67e74705SXin Li }
648*67e74705SXin Li
check(const Expr * E)649*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::check(const Expr *E) {
650*67e74705SXin Li E = peel(E);
651*67e74705SXin Li
652*67e74705SXin Li if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
653*67e74705SXin Li checkObjCIvarRefExpr(IvarRef);
654*67e74705SXin Li return;
655*67e74705SXin Li }
656*67e74705SXin Li
657*67e74705SXin Li if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
658*67e74705SXin Li checkObjCPropertyRefExpr(PropRef);
659*67e74705SXin Li return;
660*67e74705SXin Li }
661*67e74705SXin Li
662*67e74705SXin Li if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
663*67e74705SXin Li checkObjCMessageExpr(MsgExpr);
664*67e74705SXin Li return;
665*67e74705SXin Li }
666*67e74705SXin Li }
667*67e74705SXin Li
VisitBinaryOperator(const BinaryOperator * BO)668*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::VisitBinaryOperator(
669*67e74705SXin Li const BinaryOperator *BO) {
670*67e74705SXin Li VisitStmt(BO);
671*67e74705SXin Li
672*67e74705SXin Li // Do we assign/compare against zero? If yes, check the variable we are
673*67e74705SXin Li // assigning to.
674*67e74705SXin Li BinaryOperatorKind Opcode = BO->getOpcode();
675*67e74705SXin Li if (Opcode != BO_Assign &&
676*67e74705SXin Li Opcode != BO_EQ &&
677*67e74705SXin Li Opcode != BO_NE)
678*67e74705SXin Li return;
679*67e74705SXin Li
680*67e74705SXin Li if (isZero(BO->getRHS())) {
681*67e74705SXin Li check(BO->getLHS());
682*67e74705SXin Li return;
683*67e74705SXin Li }
684*67e74705SXin Li
685*67e74705SXin Li if (Opcode != BO_Assign && isZero(BO->getLHS())) {
686*67e74705SXin Li check(BO->getRHS());
687*67e74705SXin Li return;
688*67e74705SXin Li }
689*67e74705SXin Li }
690*67e74705SXin Li
VisitObjCMessageExpr(const ObjCMessageExpr * ME)691*67e74705SXin Li void IvarInvalidationCheckerImpl::MethodCrawler::VisitObjCMessageExpr(
692*67e74705SXin Li const ObjCMessageExpr *ME) {
693*67e74705SXin Li const ObjCMethodDecl *MD = ME->getMethodDecl();
694*67e74705SXin Li const Expr *Receiver = ME->getInstanceReceiver();
695*67e74705SXin Li
696*67e74705SXin Li // Stop if we are calling '[self invalidate]'.
697*67e74705SXin Li if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false))
698*67e74705SXin Li if (Receiver->isObjCSelfExpr()) {
699*67e74705SXin Li CalledAnotherInvalidationMethod = true;
700*67e74705SXin Li return;
701*67e74705SXin Li }
702*67e74705SXin Li
703*67e74705SXin Li // Check if we call a setter and set the property to 'nil'.
704*67e74705SXin Li if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
705*67e74705SXin Li MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
706*67e74705SXin Li MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
707*67e74705SXin Li if (IvI != PropertySetterToIvarMap.end()) {
708*67e74705SXin Li markInvalidated(IvI->second);
709*67e74705SXin Li return;
710*67e74705SXin Li }
711*67e74705SXin Li }
712*67e74705SXin Li
713*67e74705SXin Li // Check if we call the 'invalidation' routine on the ivar.
714*67e74705SXin Li if (Receiver) {
715*67e74705SXin Li InvalidationMethod = MD;
716*67e74705SXin Li check(Receiver->IgnoreParenCasts());
717*67e74705SXin Li InvalidationMethod = nullptr;
718*67e74705SXin Li }
719*67e74705SXin Li
720*67e74705SXin Li VisitStmt(ME);
721*67e74705SXin Li }
722*67e74705SXin Li } // end anonymous namespace
723*67e74705SXin Li
724*67e74705SXin Li // Register the checkers.
725*67e74705SXin Li namespace {
726*67e74705SXin Li class IvarInvalidationChecker :
727*67e74705SXin Li public Checker<check::ASTDecl<ObjCImplementationDecl> > {
728*67e74705SXin Li public:
729*67e74705SXin Li ChecksFilter Filter;
730*67e74705SXin Li public:
checkASTDecl(const ObjCImplementationDecl * D,AnalysisManager & Mgr,BugReporter & BR) const731*67e74705SXin Li void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
732*67e74705SXin Li BugReporter &BR) const {
733*67e74705SXin Li IvarInvalidationCheckerImpl Walker(Mgr, BR, Filter);
734*67e74705SXin Li Walker.visit(D);
735*67e74705SXin Li }
736*67e74705SXin Li };
737*67e74705SXin Li } // end anonymous namespace
738*67e74705SXin Li
739*67e74705SXin Li #define REGISTER_CHECKER(name) \
740*67e74705SXin Li void ento::register##name(CheckerManager &mgr) { \
741*67e74705SXin Li IvarInvalidationChecker *checker = \
742*67e74705SXin Li mgr.registerChecker<IvarInvalidationChecker>(); \
743*67e74705SXin Li checker->Filter.check_##name = true; \
744*67e74705SXin Li checker->Filter.checkName_##name = mgr.getCurrentCheckName(); \
745*67e74705SXin Li }
746*67e74705SXin Li
747*67e74705SXin Li REGISTER_CHECKER(InstanceVariableInvalidation)
748*67e74705SXin Li REGISTER_CHECKER(MissingInvalidationMethod)
749