blob: d326067e535cd1948221f509e2f8e97af2c2046c [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000026#include "clang/AST/Attr.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000027#include "clang/AST/DeclObjC.h"
28#include "clang/AST/StmtVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000029#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
30#include "clang/StaticAnalyzer/Core/Checker.h"
31#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000032#include "llvm/ADT/DenseMap.h"
33#include "llvm/ADT/SmallString.h"
34
35using namespace clang;
36using namespace ento;
37
38namespace {
39class IvarInvalidationChecker :
40 public Checker<check::ASTDecl<ObjCMethodDecl> > {
41
Anna Zaks31f69cc2012-09-29 00:20:38 +000042 typedef llvm::DenseSet<const ObjCMethodDecl*> MethodSet;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000043 typedef llvm::DenseMap<const ObjCMethodDecl*,
44 const ObjCIvarDecl*> MethToIvarMapTy;
45 typedef llvm::DenseMap<const ObjCPropertyDecl*,
46 const ObjCIvarDecl*> PropToIvarMapTy;
Anna Zaks377945c2012-09-27 21:57:14 +000047 typedef llvm::DenseMap<const ObjCIvarDecl*,
48 const ObjCPropertyDecl*> IvarToPropMapTy;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000049
Anna Zaks31f69cc2012-09-29 00:20:38 +000050
51 struct IvarInfo {
52 /// Has the ivar been invalidated?
53 bool IsInvalidated;
54
55 /// The methods which can be used to invalidate the ivar.
56 MethodSet InvalidationMethods;
57
58 IvarInfo() : IsInvalidated(false) {}
59 void addInvalidationMethod(const ObjCMethodDecl *MD) {
60 InvalidationMethods.insert(MD);
61 }
62
63 bool needsInvalidation() const {
64 return !InvalidationMethods.empty();
65 }
66
67 void markInvalidated() {
68 IsInvalidated = true;
69 }
70
71 bool markInvalidated(const ObjCMethodDecl *MD) {
72 if (IsInvalidated)
73 return true;
74 for (MethodSet::iterator I = InvalidationMethods.begin(),
75 E = InvalidationMethods.end(); I != E; ++I) {
76 if (*I == MD) {
77 IsInvalidated = true;
78 return true;
79 }
80 }
81 return false;
82 }
83
84 bool isInvalidated() const {
85 return IsInvalidated;
86 }
87 };
88
89 typedef llvm::DenseMap<const ObjCIvarDecl*, IvarInfo> IvarSet;
90
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000091 /// Statement visitor, which walks the method body and flags the ivars
92 /// referenced in it (either directly or via property).
93 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000094 /// The set of Ivars which need to be invalidated.
95 IvarSet &IVars;
96
Anna Zaks31f69cc2012-09-29 00:20:38 +000097 /// Flag is set as the result of a message send to another
98 /// invalidation method.
99 bool &CalledAnotherInvalidationMethod;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000100
Anna Zaks31f69cc2012-09-29 00:20:38 +0000101 /// Property setter to ivar mapping.
102 const MethToIvarMapTy &PropertySetterToIvarMap;
103
104 /// Property getter to ivar mapping.
105 const MethToIvarMapTy &PropertyGetterToIvarMap;
106
107 /// Property to ivar mapping.
108 const PropToIvarMapTy &PropertyToIvarMap;
109
110 /// The invalidation method being currently processed.
111 const ObjCMethodDecl *InvalidationMethod;
112
Anna Zaksb9733ac2012-10-01 20:33:58 +0000113 ASTContext &Ctx;
114
115 /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000116 const Expr *peel(const Expr *E) const;
117
118 /// Does this expression represent zero: '0'?
119 bool isZero(const Expr *E) const;
120
121 /// Mark the given ivar as invalidated.
122 void markInvalidated(const ObjCIvarDecl *Iv);
123
124 /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
125 /// invalidated.
126 void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
127
128 /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
129 /// it as invalidated.
130 void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
131
132 /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
133 /// if yes, marks it as invalidated.
134 void checkObjCMessageExpr(const ObjCMessageExpr *ME);
135
136 /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
137 void check(const Expr *E);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000138
139 public:
Anna Zaksbbff82f2012-10-01 20:34:04 +0000140 MethodCrawler(IvarSet &InIVars,
Anna Zaks31f69cc2012-09-29 00:20:38 +0000141 bool &InCalledAnotherInvalidationMethod,
142 const MethToIvarMapTy &InPropertySetterToIvarMap,
143 const MethToIvarMapTy &InPropertyGetterToIvarMap,
Anna Zaksb9733ac2012-10-01 20:33:58 +0000144 const PropToIvarMapTy &InPropertyToIvarMap,
145 ASTContext &InCtx)
Anna Zaksbbff82f2012-10-01 20:34:04 +0000146 : IVars(InIVars),
Anna Zaks31f69cc2012-09-29 00:20:38 +0000147 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
148 PropertySetterToIvarMap(InPropertySetterToIvarMap),
149 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
150 PropertyToIvarMap(InPropertyToIvarMap),
Anna Zaksb9733ac2012-10-01 20:33:58 +0000151 InvalidationMethod(0),
152 Ctx(InCtx) {}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000153
154 void VisitStmt(const Stmt *S) { VisitChildren(S); }
155
Anna Zaks31f69cc2012-09-29 00:20:38 +0000156 void VisitBinaryOperator(const BinaryOperator *BO);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000157
158 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
159
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000160 void VisitChildren(const Stmt *S) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000161 for (Stmt::const_child_range I = S->children(); I; ++I) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000162 if (*I)
Anna Zaksb087bbf2012-09-27 19:45:08 +0000163 this->Visit(*I);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000164 if (CalledAnotherInvalidationMethod)
165 return;
166 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000167 }
168 };
169
170 /// Check if the any of the methods inside the interface are annotated with
Anna Zaks31f69cc2012-09-29 00:20:38 +0000171 /// the invalidation annotation, update the IvarInfo accordingly.
172 static void containsInvalidationMethod(const ObjCContainerDecl *D,
173 IvarInfo &Out);
Anna Zaks377945c2012-09-27 21:57:14 +0000174
175 /// Check if ivar should be tracked and add to TrackedIvars if positive.
176 /// Returns true if ivar should be tracked.
177 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000178
179 /// Given the property declaration, and the list of tracked ivars, finds
180 /// the ivar backing the property when possible. Returns '0' when no such
181 /// ivar could be found.
182 static const ObjCIvarDecl *findPropertyBackingIvar(
183 const ObjCPropertyDecl *Prop,
184 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000185 IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000186
187public:
188 void checkASTDecl(const ObjCMethodDecl *D, AnalysisManager& Mgr,
189 BugReporter &BR) const;
190
Anna Zaksb087bbf2012-09-27 19:45:08 +0000191 // TODO: We are currently ignoring the ivars coming from class extensions.
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000192};
193
Anna Zaks31f69cc2012-09-29 00:20:38 +0000194static bool isInvalidationMethod(const ObjCMethodDecl *M) {
Anna Zaksb087bbf2012-09-27 19:45:08 +0000195 for (specific_attr_iterator<AnnotateAttr>
196 AI = M->specific_attr_begin<AnnotateAttr>(),
197 AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
198 const AnnotateAttr *Ann = *AI;
199 if (Ann->getAnnotation() == "objc_instance_variable_invalidator")
200 return true;
201 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000202 return false;
203}
204
Anna Zaks31f69cc2012-09-29 00:20:38 +0000205void IvarInvalidationChecker::containsInvalidationMethod(
206 const ObjCContainerDecl *D, IvarInfo &OutInfo) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000207
208 // TODO: Cache the results.
209
210 if (!D)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000211 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000212
213 // Check all methods.
214 for (ObjCContainerDecl::method_iterator
215 I = D->meth_begin(),
216 E = D->meth_end(); I != E; ++I) {
217 const ObjCMethodDecl *MDI = *I;
218 if (isInvalidationMethod(MDI))
Anna Zaks31f69cc2012-09-29 00:20:38 +0000219 OutInfo.addInvalidationMethod(
220 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000221 }
222
223 // If interface, check all parent protocols and super.
224 // TODO: Visit all categories in case the invalidation method is declared in
225 // a category.
226 if (const ObjCInterfaceDecl *InterfaceD = dyn_cast<ObjCInterfaceDecl>(D)) {
227 for (ObjCInterfaceDecl::protocol_iterator
228 I = InterfaceD->protocol_begin(),
229 E = InterfaceD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000230 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000231 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000232 containsInvalidationMethod(InterfaceD->getSuperClass(), OutInfo);
233 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000234 }
235
236 // If protocol, check all parent protocols.
237 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
238 for (ObjCInterfaceDecl::protocol_iterator
239 I = ProtD->protocol_begin(),
240 E = ProtD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000241 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000242 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000243 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000244 }
245
246 llvm_unreachable("One of the casts above should have succeeded.");
247}
248
Anna Zaks377945c2012-09-27 21:57:14 +0000249bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
250 IvarSet &TrackedIvars) {
251 QualType IvQTy = Iv->getType();
252 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
253 if (!IvTy)
254 return false;
255 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks31f69cc2012-09-29 00:20:38 +0000256
257 IvarInfo Info;
258 containsInvalidationMethod(IvInterf, Info);
259 if (Info.needsInvalidation()) {
260 TrackedIvars[cast<ObjCIvarDecl>(Iv->getCanonicalDecl())] = Info;
Anna Zaks377945c2012-09-27 21:57:14 +0000261 return true;
262 }
263 return false;
264}
265
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000266const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
267 const ObjCPropertyDecl *Prop,
268 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000269 IvarSet &TrackedIvars) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000270 const ObjCIvarDecl *IvarD = 0;
271
272 // Lookup for the synthesized case.
273 IvarD = Prop->getPropertyIvarDecl();
Anna Zaks377945c2012-09-27 21:57:14 +0000274 if (IvarD) {
275 if (TrackedIvars.count(IvarD)) {
276 return IvarD;
277 }
278 // If the ivar is synthesized we still want to track it.
279 if (trackIvar(IvarD, TrackedIvars))
280 return IvarD;
281 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000282
283 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
284 StringRef PropName = Prop->getIdentifier()->getName();
285 for (IvarSet::const_iterator I = TrackedIvars.begin(),
286 E = TrackedIvars.end(); I != E; ++I) {
287 const ObjCIvarDecl *Iv = I->first;
288 StringRef IvarName = Iv->getName();
289
290 if (IvarName == PropName)
291 return Iv;
292
293 SmallString<128> PropNameWithUnderscore;
294 {
295 llvm::raw_svector_ostream os(PropNameWithUnderscore);
296 os << '_' << PropName;
297 }
298 if (IvarName == PropNameWithUnderscore.str())
299 return Iv;
300 }
301
302 // Note, this is a possible source of false positives. We could look at the
303 // getter implementation to find the ivar when its name is not derived from
304 // the property name.
305 return 0;
306}
307
308void IvarInvalidationChecker::checkASTDecl(const ObjCMethodDecl *D,
309 AnalysisManager& Mgr,
310 BugReporter &BR) const {
311 // We are only interested in checking the cleanup methods.
312 if (!D->hasBody() || !isInvalidationMethod(D))
313 return;
314
315 // Collect all ivars that need cleanup.
316 IvarSet Ivars;
317 const ObjCInterfaceDecl *InterfaceD = D->getClassInterface();
Anna Zakse0c50fa2012-10-16 19:36:37 +0000318
319 // Collect ivars declared in this class, its extensions and its implementation
320 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
321 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
322 Iv= Iv->getNextIvar())
Anna Zaks377945c2012-09-27 21:57:14 +0000323 trackIvar(Iv, Ivars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000324
Anna Zaks377945c2012-09-27 21:57:14 +0000325 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000326 // ivar which is backing a property has been reset.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000327 MethToIvarMapTy PropSetterToIvarMap;
328 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000329 PropToIvarMapTy PropertyToIvarMap;
Anna Zaks377945c2012-09-27 21:57:14 +0000330 IvarToPropMapTy IvarToPopertyMap;
Anna Zaksc3c26b72012-10-18 19:17:57 +0000331
332 ObjCInterfaceDecl::PropertyMap PropMap;
333 InterfaceD->collectPropertiesToImplement(PropMap);
334
335 for (ObjCInterfaceDecl::PropertyMap::iterator
336 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
337 const ObjCPropertyDecl *PD = I->second;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000338
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;
Anna Zaksbbff82f2012-10-01 20:34:04 +0000366 MethodCrawler(Ivars,
Anna Zaks31f69cc2012-09-29 00:20:38 +0000367 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?");
Anna Zaksc3c26b72012-10-18 19:17:57 +0000393 os << "Property "<< PD->getName() <<
394 " needs to be invalidated or set to nil";
Anna Zaks377945c2012-09-27 21:57:14 +0000395 } else {
396 os << "Instance variable "<< IvarDecl->getName()
Anna Zaks51431dc2012-10-15 22:48:17 +0000397 << " needs to be invalidated or set to nil";
Anna Zaks377945c2012-09-27 21:57:14 +0000398 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000399
Anna Zaksb087bbf2012-09-27 19:45:08 +0000400 BR.EmitBasicReport(D,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000401 "Incomplete invalidation",
402 categories::CoreFoundationObjectiveC, os.str(),
403 IvarDecLocation);
404 }
405 }
406}
407
Anna Zaks31f69cc2012-09-29 00:20:38 +0000408void IvarInvalidationChecker::MethodCrawler::markInvalidated(
409 const ObjCIvarDecl *Iv) {
410 IvarSet::iterator I = IVars.find(Iv);
411 if (I != IVars.end()) {
412 // If InvalidationMethod is present, we are processing the message send and
413 // should ensure we are invalidating with the appropriate method,
414 // otherwise, we are processing setting to 'nil'.
415 if (InvalidationMethod)
416 I->second.markInvalidated(InvalidationMethod);
417 else
418 I->second.markInvalidated();
419 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000420}
421
Anna Zaks31f69cc2012-09-29 00:20:38 +0000422const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
423 E = E->IgnoreParenCasts();
424 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
425 E = POE->getSyntacticForm()->IgnoreParenCasts();
426 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
427 E = OVE->getSourceExpr()->IgnoreParenCasts();
428 return E;
429}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000430
Anna Zaks31f69cc2012-09-29 00:20:38 +0000431void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
432 const ObjCIvarRefExpr *IvarRef) {
433 if (const Decl *D = IvarRef->getDecl())
434 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
435}
436
437void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000438 const ObjCMessageExpr *ME) {
439 const ObjCMethodDecl *MD = ME->getMethodDecl();
440 if (MD) {
441 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000442 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
443 if (IvI != PropertyGetterToIvarMap.end())
444 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000445 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000446}
447
Anna Zaks31f69cc2012-09-29 00:20:38 +0000448void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000449 const ObjCPropertyRefExpr *PA) {
450
451 if (PA->isExplicitProperty()) {
452 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
453 if (PD) {
454 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000455 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
456 if (IvI != PropertyToIvarMap.end())
457 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000458 return;
459 }
460 }
461
462 if (PA->isImplicitProperty()) {
463 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
464 if (MD) {
465 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000466 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
467 if (IvI != PropertyGetterToIvarMap.end())
468 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000469 return;
470 }
471 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000472}
473
474bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
475 E = peel(E);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000476
Anna Zaksb9733ac2012-10-01 20:33:58 +0000477 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
478 != Expr::NPCK_NotNull);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000479}
480
481void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
482 E = peel(E);
483
Anna Zaks31f69cc2012-09-29 00:20:38 +0000484 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
485 checkObjCIvarRefExpr(IvarRef);
486 return;
487 }
488
489 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
490 checkObjCPropertyRefExpr(PropRef);
491 return;
492 }
493
494 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
495 checkObjCMessageExpr(MsgExpr);
496 return;
497 }
498}
499
500void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
501 const BinaryOperator *BO) {
Anna Zaksb9733ac2012-10-01 20:33:58 +0000502 VisitStmt(BO);
503
504 if (BO->getOpcode() != BO_Assign)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000505 return;
506
507 // Do we assign zero?
508 if (!isZero(BO->getRHS()))
509 return;
510
511 // Check the variable we are assigning to.
512 check(BO->getLHS());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000513}
514
515void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
516 const ObjCMessageExpr *ME) {
517 const ObjCMethodDecl *MD = ME->getMethodDecl();
518 const Expr *Receiver = ME->getInstanceReceiver();
519
520 // Stop if we are calling '[self invalidate]'.
521 if (Receiver && isInvalidationMethod(MD))
Anna Zaksbbff82f2012-10-01 20:34:04 +0000522 if (Receiver->isObjCSelfExpr()) {
523 CalledAnotherInvalidationMethod = true;
524 return;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000525 }
526
527 // Check if we call a setter and set the property to 'nil'.
528 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
529 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
530 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
531 if (IvI != PropertySetterToIvarMap.end()) {
532 markInvalidated(IvI->second);
533 return;
534 }
535 }
536
537 // Check if we call the 'invalidation' routine on the ivar.
538 if (Receiver) {
539 InvalidationMethod = MD;
540 check(Receiver->IgnoreParenCasts());
541 InvalidationMethod = 0;
542 }
543
544 VisitStmt(ME);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000545}
546}
547
548// Register the checker.
549void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
550 mgr.registerChecker<IvarInvalidationChecker>();
551}