blob: 5f944038242153da4d330233b42e7d6306b5a56b [file] [log] [blame]
Anna Zaks31f69cc2012-09-29 00:20:38 +00001//=- IvarInvalidationChecker.cpp - -*- C++ -------------------------------*-==//
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This checker implements annotation driven invalidation checking. If a class
11// contains a method annotated with 'objc_instance_variable_invalidator',
12// - (void) foo
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
Anna Zaks31f69cc2012-09-29 00:20:38 +000016// 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).
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000022//
23//===----------------------------------------------------------------------===//
24
25#include "ClangSACheckers.h"
26#include "clang/StaticAnalyzer/Core/Checker.h"
27#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
28#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
29#include "clang/AST/DeclObjC.h"
30#include "clang/AST/StmtVisitor.h"
31#include "llvm/ADT/DenseMap.h"
32#include "llvm/ADT/SmallString.h"
33
34using namespace clang;
35using namespace ento;
36
37namespace {
38class IvarInvalidationChecker :
39 public Checker<check::ASTDecl<ObjCMethodDecl> > {
40
Anna Zaks31f69cc2012-09-29 00:20:38 +000041 typedef llvm::DenseSet<const ObjCMethodDecl*> MethodSet;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000042 typedef llvm::DenseMap<const ObjCMethodDecl*,
43 const ObjCIvarDecl*> MethToIvarMapTy;
44 typedef llvm::DenseMap<const ObjCPropertyDecl*,
45 const ObjCIvarDecl*> PropToIvarMapTy;
Anna Zaks377945c2012-09-27 21:57:14 +000046 typedef llvm::DenseMap<const ObjCIvarDecl*,
47 const ObjCPropertyDecl*> IvarToPropMapTy;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000048
Anna Zaks31f69cc2012-09-29 00:20:38 +000049
50 struct IvarInfo {
51 /// Has the ivar been invalidated?
52 bool IsInvalidated;
53
54 /// The methods which can be used to invalidate the ivar.
55 MethodSet InvalidationMethods;
56
57 IvarInfo() : IsInvalidated(false) {}
58 void addInvalidationMethod(const ObjCMethodDecl *MD) {
59 InvalidationMethods.insert(MD);
60 }
61
62 bool needsInvalidation() const {
63 return !InvalidationMethods.empty();
64 }
65
66 void markInvalidated() {
67 IsInvalidated = true;
68 }
69
70 bool markInvalidated(const ObjCMethodDecl *MD) {
71 if (IsInvalidated)
72 return true;
73 for (MethodSet::iterator I = InvalidationMethods.begin(),
74 E = InvalidationMethods.end(); I != E; ++I) {
75 if (*I == MD) {
76 IsInvalidated = true;
77 return true;
78 }
79 }
80 return false;
81 }
82
83 bool isInvalidated() const {
84 return IsInvalidated;
85 }
86 };
87
88 typedef llvm::DenseMap<const ObjCIvarDecl*, IvarInfo> IvarSet;
89
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000090 /// Statement visitor, which walks the method body and flags the ivars
91 /// referenced in it (either directly or via property).
92 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
Anna Zaks31f69cc2012-09-29 00:20:38 +000093 const ObjCMethodDecl *EnclosingMethod;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000094
95 /// The set of Ivars which need to be invalidated.
96 IvarSet &IVars;
97
Anna Zaks31f69cc2012-09-29 00:20:38 +000098 /// Flag is set as the result of a message send to another
99 /// invalidation method.
100 bool &CalledAnotherInvalidationMethod;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000101
Anna Zaks31f69cc2012-09-29 00:20:38 +0000102 /// Property setter to ivar mapping.
103 const MethToIvarMapTy &PropertySetterToIvarMap;
104
105 /// Property getter to ivar mapping.
106 const MethToIvarMapTy &PropertyGetterToIvarMap;
107
108 /// Property to ivar mapping.
109 const PropToIvarMapTy &PropertyToIvarMap;
110
111 /// The invalidation method being currently processed.
112 const ObjCMethodDecl *InvalidationMethod;
113
Anna Zaksb9733ac2012-10-01 20:33:58 +0000114 ASTContext &Ctx;
115
116 /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000117 const Expr *peel(const Expr *E) const;
118
119 /// Does this expression represent zero: '0'?
120 bool isZero(const Expr *E) const;
121
122 /// Mark the given ivar as invalidated.
123 void markInvalidated(const ObjCIvarDecl *Iv);
124
125 /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
126 /// invalidated.
127 void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
128
129 /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
130 /// it as invalidated.
131 void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
132
133 /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
134 /// if yes, marks it as invalidated.
135 void checkObjCMessageExpr(const ObjCMessageExpr *ME);
136
137 /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
138 void check(const Expr *E);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000139
140 public:
Anna Zaks31f69cc2012-09-29 00:20:38 +0000141 MethodCrawler(const ObjCMethodDecl *InMeth,
Anna Zaks377945c2012-09-27 21:57:14 +0000142 IvarSet &InIVars,
Anna Zaks31f69cc2012-09-29 00:20:38 +0000143 bool &InCalledAnotherInvalidationMethod,
144 const MethToIvarMapTy &InPropertySetterToIvarMap,
145 const MethToIvarMapTy &InPropertyGetterToIvarMap,
Anna Zaksb9733ac2012-10-01 20:33:58 +0000146 const PropToIvarMapTy &InPropertyToIvarMap,
147 ASTContext &InCtx)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000148 : EnclosingMethod(InMeth),
149 IVars(InIVars),
150 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
151 PropertySetterToIvarMap(InPropertySetterToIvarMap),
152 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
153 PropertyToIvarMap(InPropertyToIvarMap),
Anna Zaksb9733ac2012-10-01 20:33:58 +0000154 InvalidationMethod(0),
155 Ctx(InCtx) {}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000156
157 void VisitStmt(const Stmt *S) { VisitChildren(S); }
158
Anna Zaks31f69cc2012-09-29 00:20:38 +0000159 void VisitBinaryOperator(const BinaryOperator *BO);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000160
161 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
162
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000163 void VisitChildren(const Stmt *S) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000164 for (Stmt::const_child_range I = S->children(); I; ++I) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000165 if (*I)
Anna Zaksb087bbf2012-09-27 19:45:08 +0000166 this->Visit(*I);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000167 if (CalledAnotherInvalidationMethod)
168 return;
169 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000170 }
171 };
172
173 /// Check if the any of the methods inside the interface are annotated with
Anna Zaks31f69cc2012-09-29 00:20:38 +0000174 /// the invalidation annotation, update the IvarInfo accordingly.
175 static void containsInvalidationMethod(const ObjCContainerDecl *D,
176 IvarInfo &Out);
Anna Zaks377945c2012-09-27 21:57:14 +0000177
178 /// Check if ivar should be tracked and add to TrackedIvars if positive.
179 /// Returns true if ivar should be tracked.
180 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000181
182 /// Given the property declaration, and the list of tracked ivars, finds
183 /// the ivar backing the property when possible. Returns '0' when no such
184 /// ivar could be found.
185 static const ObjCIvarDecl *findPropertyBackingIvar(
186 const ObjCPropertyDecl *Prop,
187 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000188 IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000189
190public:
191 void checkASTDecl(const ObjCMethodDecl *D, AnalysisManager& Mgr,
192 BugReporter &BR) const;
193
Anna Zaksb087bbf2012-09-27 19:45:08 +0000194 // TODO: We are currently ignoring the ivars coming from class extensions.
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000195};
196
Anna Zaks31f69cc2012-09-29 00:20:38 +0000197static bool isInvalidationMethod(const ObjCMethodDecl *M) {
Anna Zaksb087bbf2012-09-27 19:45:08 +0000198 for (specific_attr_iterator<AnnotateAttr>
199 AI = M->specific_attr_begin<AnnotateAttr>(),
200 AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
201 const AnnotateAttr *Ann = *AI;
202 if (Ann->getAnnotation() == "objc_instance_variable_invalidator")
203 return true;
204 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000205 return false;
206}
207
Anna Zaks31f69cc2012-09-29 00:20:38 +0000208void IvarInvalidationChecker::containsInvalidationMethod(
209 const ObjCContainerDecl *D, IvarInfo &OutInfo) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000210
211 // TODO: Cache the results.
212
213 if (!D)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000214 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000215
216 // Check all methods.
217 for (ObjCContainerDecl::method_iterator
218 I = D->meth_begin(),
219 E = D->meth_end(); I != E; ++I) {
220 const ObjCMethodDecl *MDI = *I;
221 if (isInvalidationMethod(MDI))
Anna Zaks31f69cc2012-09-29 00:20:38 +0000222 OutInfo.addInvalidationMethod(
223 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000224 }
225
226 // If interface, check all parent protocols and super.
227 // TODO: Visit all categories in case the invalidation method is declared in
228 // a category.
229 if (const ObjCInterfaceDecl *InterfaceD = dyn_cast<ObjCInterfaceDecl>(D)) {
230 for (ObjCInterfaceDecl::protocol_iterator
231 I = InterfaceD->protocol_begin(),
232 E = InterfaceD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000233 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000234 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000235 containsInvalidationMethod(InterfaceD->getSuperClass(), OutInfo);
236 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000237 }
238
239 // If protocol, check all parent protocols.
240 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
241 for (ObjCInterfaceDecl::protocol_iterator
242 I = ProtD->protocol_begin(),
243 E = ProtD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000244 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000245 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000246 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000247 }
248
249 llvm_unreachable("One of the casts above should have succeeded.");
250}
251
Anna Zaks377945c2012-09-27 21:57:14 +0000252bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
253 IvarSet &TrackedIvars) {
254 QualType IvQTy = Iv->getType();
255 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
256 if (!IvTy)
257 return false;
258 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks31f69cc2012-09-29 00:20:38 +0000259
260 IvarInfo Info;
261 containsInvalidationMethod(IvInterf, Info);
262 if (Info.needsInvalidation()) {
263 TrackedIvars[cast<ObjCIvarDecl>(Iv->getCanonicalDecl())] = Info;
Anna Zaks377945c2012-09-27 21:57:14 +0000264 return true;
265 }
266 return false;
267}
268
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000269const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
270 const ObjCPropertyDecl *Prop,
271 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000272 IvarSet &TrackedIvars) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000273 const ObjCIvarDecl *IvarD = 0;
274
275 // Lookup for the synthesized case.
276 IvarD = Prop->getPropertyIvarDecl();
Anna Zaks377945c2012-09-27 21:57:14 +0000277 if (IvarD) {
278 if (TrackedIvars.count(IvarD)) {
279 return IvarD;
280 }
281 // If the ivar is synthesized we still want to track it.
282 if (trackIvar(IvarD, TrackedIvars))
283 return IvarD;
284 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000285
286 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
287 StringRef PropName = Prop->getIdentifier()->getName();
288 for (IvarSet::const_iterator I = TrackedIvars.begin(),
289 E = TrackedIvars.end(); I != E; ++I) {
290 const ObjCIvarDecl *Iv = I->first;
291 StringRef IvarName = Iv->getName();
292
293 if (IvarName == PropName)
294 return Iv;
295
296 SmallString<128> PropNameWithUnderscore;
297 {
298 llvm::raw_svector_ostream os(PropNameWithUnderscore);
299 os << '_' << PropName;
300 }
301 if (IvarName == PropNameWithUnderscore.str())
302 return Iv;
303 }
304
305 // Note, this is a possible source of false positives. We could look at the
306 // getter implementation to find the ivar when its name is not derived from
307 // the property name.
308 return 0;
309}
310
311void IvarInvalidationChecker::checkASTDecl(const ObjCMethodDecl *D,
312 AnalysisManager& Mgr,
313 BugReporter &BR) const {
314 // We are only interested in checking the cleanup methods.
315 if (!D->hasBody() || !isInvalidationMethod(D))
316 return;
317
318 // Collect all ivars that need cleanup.
319 IvarSet Ivars;
320 const ObjCInterfaceDecl *InterfaceD = D->getClassInterface();
321 for (ObjCInterfaceDecl::ivar_iterator
322 II = InterfaceD->ivar_begin(),
323 IE = InterfaceD->ivar_end(); II != IE; ++II) {
324 const ObjCIvarDecl *Iv = *II;
Anna Zaks377945c2012-09-27 21:57:14 +0000325 trackIvar(Iv, Ivars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000326 }
327
Anna Zaks377945c2012-09-27 21:57:14 +0000328 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000329 // ivar which is backing a property has been reset.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000330 MethToIvarMapTy PropSetterToIvarMap;
331 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000332 PropToIvarMapTy PropertyToIvarMap;
Anna Zaks377945c2012-09-27 21:57:14 +0000333 IvarToPropMapTy IvarToPopertyMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000334 for (ObjCInterfaceDecl::prop_iterator
335 I = InterfaceD->prop_begin(),
336 E = InterfaceD->prop_end(); I != E; ++I) {
337 const ObjCPropertyDecl *PD = *I;
338
339 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars);
340 if (!ID) {
341 continue;
342 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000343
344 // Store the mappings.
345 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000346 PropertyToIvarMap[PD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000347 IvarToPopertyMap[ID] = PD;
348
349 // Find the setter and the getter.
350 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
351 if (SetterD) {
352 SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000353 PropSetterToIvarMap[SetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000354 }
355
356 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
357 if (GetterD) {
358 GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000359 PropGetterToIvarMap[GetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000360 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000361 }
362
363
Anna Zaks31f69cc2012-09-29 00:20:38 +0000364 // Check which ivars have been invalidated in the method body.
365 bool CalledAnotherInvalidationMethod = false;
366 MethodCrawler(D, Ivars,
367 CalledAnotherInvalidationMethod,
368 PropSetterToIvarMap,
369 PropGetterToIvarMap,
Anna Zaksb9733ac2012-10-01 20:33:58 +0000370 PropertyToIvarMap,
371 BR.getContext()).VisitStmt(D->getBody());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000372
373 if (CalledAnotherInvalidationMethod)
374 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000375
376 // Warn on the ivars that were not accessed by the method.
377 for (IvarSet::const_iterator I = Ivars.begin(), E = Ivars.end(); I != E; ++I){
Anna Zaks31f69cc2012-09-29 00:20:38 +0000378 if (!I->second.isInvalidated()) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000379 const ObjCIvarDecl *IvarDecl = I->first;
380
381 PathDiagnosticLocation IvarDecLocation =
Anna Zaksb087bbf2012-09-27 19:45:08 +0000382 PathDiagnosticLocation::createEnd(D->getBody(), BR.getSourceManager(),
383 Mgr.getAnalysisDeclContext(D));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000384
385 SmallString<128> sbuf;
386 llvm::raw_svector_ostream os(sbuf);
Anna Zaks377945c2012-09-27 21:57:14 +0000387
388 // Construct the warning message.
389 if (IvarDecl->getSynthesize()) {
390 const ObjCPropertyDecl *PD = IvarToPopertyMap[IvarDecl];
391 assert(PD &&
392 "Do we synthesize ivars for something other than properties?");
393 os << "Property "<< PD->getName() << " needs to be invalidated";
394 } else {
395 os << "Instance variable "<< IvarDecl->getName()
396 << " needs to be invalidated";
397 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000398
Anna Zaksb087bbf2012-09-27 19:45:08 +0000399 BR.EmitBasicReport(D,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000400 "Incomplete invalidation",
401 categories::CoreFoundationObjectiveC, os.str(),
402 IvarDecLocation);
403 }
404 }
405}
406
Anna Zaks31f69cc2012-09-29 00:20:38 +0000407void IvarInvalidationChecker::MethodCrawler::markInvalidated(
408 const ObjCIvarDecl *Iv) {
409 IvarSet::iterator I = IVars.find(Iv);
410 if (I != IVars.end()) {
411 // If InvalidationMethod is present, we are processing the message send and
412 // should ensure we are invalidating with the appropriate method,
413 // otherwise, we are processing setting to 'nil'.
414 if (InvalidationMethod)
415 I->second.markInvalidated(InvalidationMethod);
416 else
417 I->second.markInvalidated();
418 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000419}
420
Anna Zaks31f69cc2012-09-29 00:20:38 +0000421const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
422 E = E->IgnoreParenCasts();
423 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
424 E = POE->getSyntacticForm()->IgnoreParenCasts();
425 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
426 E = OVE->getSourceExpr()->IgnoreParenCasts();
427 return E;
428}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000429
Anna Zaks31f69cc2012-09-29 00:20:38 +0000430void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
431 const ObjCIvarRefExpr *IvarRef) {
432 if (const Decl *D = IvarRef->getDecl())
433 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
434}
435
436void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000437 const ObjCMessageExpr *ME) {
438 const ObjCMethodDecl *MD = ME->getMethodDecl();
439 if (MD) {
440 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000441 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
442 if (IvI != PropertyGetterToIvarMap.end())
443 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000444 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000445}
446
Anna Zaks31f69cc2012-09-29 00:20:38 +0000447void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000448 const ObjCPropertyRefExpr *PA) {
449
450 if (PA->isExplicitProperty()) {
451 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
452 if (PD) {
453 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000454 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
455 if (IvI != PropertyToIvarMap.end())
456 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000457 return;
458 }
459 }
460
461 if (PA->isImplicitProperty()) {
462 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
463 if (MD) {
464 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000465 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
466 if (IvI != PropertyGetterToIvarMap.end())
467 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000468 return;
469 }
470 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000471}
472
473bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
474 E = peel(E);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000475
Anna Zaksb9733ac2012-10-01 20:33:58 +0000476 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
477 != Expr::NPCK_NotNull);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000478}
479
480void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
481 E = peel(E);
482
Anna Zaks31f69cc2012-09-29 00:20:38 +0000483 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
484 checkObjCIvarRefExpr(IvarRef);
485 return;
486 }
487
488 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
489 checkObjCPropertyRefExpr(PropRef);
490 return;
491 }
492
493 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
494 checkObjCMessageExpr(MsgExpr);
495 return;
496 }
497}
498
499void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
500 const BinaryOperator *BO) {
Anna Zaksb9733ac2012-10-01 20:33:58 +0000501 VisitStmt(BO);
502
503 if (BO->getOpcode() != BO_Assign)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000504 return;
505
506 // Do we assign zero?
507 if (!isZero(BO->getRHS()))
508 return;
509
510 // Check the variable we are assigning to.
511 check(BO->getLHS());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000512}
513
514void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
515 const ObjCMessageExpr *ME) {
516 const ObjCMethodDecl *MD = ME->getMethodDecl();
517 const Expr *Receiver = ME->getInstanceReceiver();
518
519 // Stop if we are calling '[self invalidate]'.
520 if (Receiver && isInvalidationMethod(MD))
521 if (const DeclRefExpr *RD =
522 dyn_cast<DeclRefExpr>(Receiver->IgnoreParenCasts())) {
523 if (RD->getDecl() == EnclosingMethod->getSelfDecl()) {
524 CalledAnotherInvalidationMethod = true;
525 return;
526 }
527 }
528
529 // Check if we call a setter and set the property to 'nil'.
530 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
531 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
532 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
533 if (IvI != PropertySetterToIvarMap.end()) {
534 markInvalidated(IvI->second);
535 return;
536 }
537 }
538
539 // Check if we call the 'invalidation' routine on the ivar.
540 if (Receiver) {
541 InvalidationMethod = MD;
542 check(Receiver->IgnoreParenCasts());
543 InvalidationMethod = 0;
544 }
545
546 VisitStmt(ME);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000547}
548}
549
550// Register the checker.
551void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
552 mgr.registerChecker<IvarInvalidationChecker>();
553}