blob: b3bad6130108483a5e73a4fdc58bd63af327b096 [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"
Anna Zaksb1fc6732013-01-10 20:59:51 +000033#include "llvm/ADT/SetVector.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000034#include "llvm/ADT/SmallString.h"
35
36using namespace clang;
37using namespace ento;
38
39namespace {
40class IvarInvalidationChecker :
Anna Zaksb1fc6732013-01-10 20:59:51 +000041 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000042
Anna Zaksb1fc6732013-01-10 20:59:51 +000043 typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000044 typedef llvm::DenseMap<const ObjCMethodDecl*,
45 const ObjCIvarDecl*> MethToIvarMapTy;
46 typedef llvm::DenseMap<const ObjCPropertyDecl*,
47 const ObjCIvarDecl*> PropToIvarMapTy;
Anna Zaks377945c2012-09-27 21:57:14 +000048 typedef llvm::DenseMap<const ObjCIvarDecl*,
49 const ObjCPropertyDecl*> IvarToPropMapTy;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000050
Anna Zaks31f69cc2012-09-29 00:20:38 +000051
Anna Zaksb1fc6732013-01-10 20:59:51 +000052 struct InvalidationInfo {
Anna Zaks31f69cc2012-09-29 00:20:38 +000053 /// Has the ivar been invalidated?
54 bool IsInvalidated;
55
56 /// The methods which can be used to invalidate the ivar.
57 MethodSet InvalidationMethods;
58
Anna Zaksb1fc6732013-01-10 20:59:51 +000059 InvalidationInfo() : IsInvalidated(false) {}
Anna Zaks31f69cc2012-09-29 00:20:38 +000060 void addInvalidationMethod(const ObjCMethodDecl *MD) {
61 InvalidationMethods.insert(MD);
62 }
63
64 bool needsInvalidation() const {
65 return !InvalidationMethods.empty();
66 }
67
68 void markInvalidated() {
69 IsInvalidated = true;
70 }
71
72 bool markInvalidated(const ObjCMethodDecl *MD) {
73 if (IsInvalidated)
74 return true;
75 for (MethodSet::iterator I = InvalidationMethods.begin(),
76 E = InvalidationMethods.end(); I != E; ++I) {
77 if (*I == MD) {
78 IsInvalidated = true;
79 return true;
80 }
81 }
82 return false;
83 }
84
85 bool isInvalidated() const {
86 return IsInvalidated;
87 }
88 };
89
Anna Zaksb1fc6732013-01-10 20:59:51 +000090 typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
Anna Zaks31f69cc2012-09-29 00:20:38 +000091
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000092 /// Statement visitor, which walks the method body and flags the ivars
93 /// referenced in it (either directly or via property).
94 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000095 /// 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 Zaksbbff82f2012-10-01 20:34:04 +0000141 MethodCrawler(IvarSet &InIVars,
Anna Zaks31f69cc2012-09-29 00:20:38 +0000142 bool &InCalledAnotherInvalidationMethod,
143 const MethToIvarMapTy &InPropertySetterToIvarMap,
144 const MethToIvarMapTy &InPropertyGetterToIvarMap,
Anna Zaksb9733ac2012-10-01 20:33:58 +0000145 const PropToIvarMapTy &InPropertyToIvarMap,
146 ASTContext &InCtx)
Anna Zaksbbff82f2012-10-01 20:34:04 +0000147 : IVars(InIVars),
Anna Zaks31f69cc2012-09-29 00:20:38 +0000148 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
149 PropertySetterToIvarMap(InPropertySetterToIvarMap),
150 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
151 PropertyToIvarMap(InPropertyToIvarMap),
Anna Zaksb9733ac2012-10-01 20:33:58 +0000152 InvalidationMethod(0),
153 Ctx(InCtx) {}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000154
155 void VisitStmt(const Stmt *S) { VisitChildren(S); }
156
Anna Zaks31f69cc2012-09-29 00:20:38 +0000157 void VisitBinaryOperator(const BinaryOperator *BO);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000158
159 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
160
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000161 void VisitChildren(const Stmt *S) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000162 for (Stmt::const_child_range I = S->children(); I; ++I) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000163 if (*I)
Anna Zaksb087bbf2012-09-27 19:45:08 +0000164 this->Visit(*I);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000165 if (CalledAnotherInvalidationMethod)
166 return;
167 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000168 }
169 };
170
171 /// Check if the any of the methods inside the interface are annotated with
Anna Zaks31f69cc2012-09-29 00:20:38 +0000172 /// the invalidation annotation, update the IvarInfo accordingly.
173 static void containsInvalidationMethod(const ObjCContainerDecl *D,
Anna Zaksb1fc6732013-01-10 20:59:51 +0000174 InvalidationInfo &Out);
Anna Zaks377945c2012-09-27 21:57:14 +0000175
176 /// Check if ivar should be tracked and add to TrackedIvars if positive.
177 /// Returns true if ivar should be tracked.
Anna Zaks664566c2013-01-10 22:44:16 +0000178 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
179 const ObjCIvarDecl **FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000180
181 /// Given the property declaration, and the list of tracked ivars, finds
182 /// the ivar backing the property when possible. Returns '0' when no such
183 /// ivar could be found.
184 static const ObjCIvarDecl *findPropertyBackingIvar(
185 const ObjCPropertyDecl *Prop,
186 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks664566c2013-01-10 22:44:16 +0000187 IvarSet &TrackedIvars,
188 const ObjCIvarDecl **FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000189
Anna Zaksb1fc6732013-01-10 20:59:51 +0000190 /// Print ivar name or the property if the given ivar backs a property.
191 static void printIvar(llvm::raw_svector_ostream &os,
192 const ObjCIvarDecl *IvarDecl,
193 IvarToPropMapTy &IvarToPopertyMap);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000194public:
Anna Zaksb1fc6732013-01-10 20:59:51 +0000195 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000196 BugReporter &BR) const;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000197};
198
Anna Zaks31f69cc2012-09-29 00:20:38 +0000199static bool isInvalidationMethod(const ObjCMethodDecl *M) {
Anna Zaksb087bbf2012-09-27 19:45:08 +0000200 for (specific_attr_iterator<AnnotateAttr>
201 AI = M->specific_attr_begin<AnnotateAttr>(),
202 AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
203 const AnnotateAttr *Ann = *AI;
204 if (Ann->getAnnotation() == "objc_instance_variable_invalidator")
205 return true;
206 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000207 return false;
208}
209
Anna Zaks31f69cc2012-09-29 00:20:38 +0000210void IvarInvalidationChecker::containsInvalidationMethod(
Anna Zaksb1fc6732013-01-10 20:59:51 +0000211 const ObjCContainerDecl *D, InvalidationInfo &OutInfo) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000212
213 if (!D)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000214 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000215
Anna Zaksb1fc6732013-01-10 20:59:51 +0000216 assert(!isa<ObjCImplementationDecl>(D));
217 // TODO: Cache the results.
218
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000219 // Check all methods.
220 for (ObjCContainerDecl::method_iterator
221 I = D->meth_begin(),
222 E = D->meth_end(); I != E; ++I) {
223 const ObjCMethodDecl *MDI = *I;
224 if (isInvalidationMethod(MDI))
Anna Zaks31f69cc2012-09-29 00:20:38 +0000225 OutInfo.addInvalidationMethod(
226 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000227 }
228
229 // If interface, check all parent protocols and super.
Anna Zaksae81e172013-01-11 03:52:37 +0000230 if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
231
232 // Visit all protocols.
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000233 for (ObjCInterfaceDecl::protocol_iterator
Anna Zaksae81e172013-01-11 03:52:37 +0000234 I = InterfD->protocol_begin(),
235 E = InterfD->protocol_end(); I != E; ++I) {
Anna Zaksb8f66782013-01-11 03:52:40 +0000236 containsInvalidationMethod((*I)->getDefinition(), OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000237 }
Anna Zaksae81e172013-01-11 03:52:37 +0000238
239 // Visit all categories in case the invalidation method is declared in
240 // a category.
241 for (const ObjCCategoryDecl *I = InterfD->getFirstClassExtension(); I;
242 I = I->getNextClassExtension()) {
243 containsInvalidationMethod(I, OutInfo);
244 }
245
246 containsInvalidationMethod(InterfD->getSuperClass(), OutInfo);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000247 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000248 }
249
250 // If protocol, check all parent protocols.
251 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
252 for (ObjCInterfaceDecl::protocol_iterator
253 I = ProtD->protocol_begin(),
254 E = ProtD->protocol_end(); I != E; ++I) {
Anna Zaksb8f66782013-01-11 03:52:40 +0000255 containsInvalidationMethod((*I)->getDefinition(), OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000256 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000257 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000258 }
259
Anna Zaksae81e172013-01-11 03:52:37 +0000260 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000261}
262
Anna Zaks377945c2012-09-27 21:57:14 +0000263bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
Anna Zaks664566c2013-01-10 22:44:16 +0000264 IvarSet &TrackedIvars,
265 const ObjCIvarDecl **FirstIvarDecl) {
Anna Zaks377945c2012-09-27 21:57:14 +0000266 QualType IvQTy = Iv->getType();
267 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
268 if (!IvTy)
269 return false;
270 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks31f69cc2012-09-29 00:20:38 +0000271
Anna Zaksb1fc6732013-01-10 20:59:51 +0000272 InvalidationInfo Info;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000273 containsInvalidationMethod(IvInterf, Info);
274 if (Info.needsInvalidation()) {
Anna Zaks664566c2013-01-10 22:44:16 +0000275 const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
276 TrackedIvars[I] = Info;
277 if (!*FirstIvarDecl)
278 *FirstIvarDecl = I;
Anna Zaks377945c2012-09-27 21:57:14 +0000279 return true;
280 }
281 return false;
282}
283
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000284const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
285 const ObjCPropertyDecl *Prop,
286 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks664566c2013-01-10 22:44:16 +0000287 IvarSet &TrackedIvars,
288 const ObjCIvarDecl **FirstIvarDecl) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000289 const ObjCIvarDecl *IvarD = 0;
290
291 // Lookup for the synthesized case.
292 IvarD = Prop->getPropertyIvarDecl();
Anna Zaks5879fb32013-01-07 19:12:56 +0000293 // We only track the ivars/properties that are defined in the current
294 // class (not the parent).
295 if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
Anna Zaks377945c2012-09-27 21:57:14 +0000296 if (TrackedIvars.count(IvarD)) {
297 return IvarD;
298 }
299 // If the ivar is synthesized we still want to track it.
Anna Zaks664566c2013-01-10 22:44:16 +0000300 if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
Anna Zaks377945c2012-09-27 21:57:14 +0000301 return IvarD;
302 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000303
304 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
305 StringRef PropName = Prop->getIdentifier()->getName();
306 for (IvarSet::const_iterator I = TrackedIvars.begin(),
307 E = TrackedIvars.end(); I != E; ++I) {
308 const ObjCIvarDecl *Iv = I->first;
309 StringRef IvarName = Iv->getName();
310
311 if (IvarName == PropName)
312 return Iv;
313
314 SmallString<128> PropNameWithUnderscore;
315 {
316 llvm::raw_svector_ostream os(PropNameWithUnderscore);
317 os << '_' << PropName;
318 }
319 if (IvarName == PropNameWithUnderscore.str())
320 return Iv;
321 }
322
323 // Note, this is a possible source of false positives. We could look at the
324 // getter implementation to find the ivar when its name is not derived from
325 // the property name.
326 return 0;
327}
328
Anna Zaksb1fc6732013-01-10 20:59:51 +0000329void IvarInvalidationChecker::printIvar(llvm::raw_svector_ostream &os,
330 const ObjCIvarDecl *IvarDecl,
331 IvarToPropMapTy &IvarToPopertyMap) {
332 if (IvarDecl->getSynthesize()) {
333 const ObjCPropertyDecl *PD = IvarToPopertyMap[IvarDecl];
334 assert(PD &&"Do we synthesize ivars for something other than properties?");
335 os << "Property "<< PD->getName() << " ";
336 } else {
337 os << "Instance variable "<< IvarDecl->getName() << " ";
338 }
339}
340
341// Check that the invalidatable interfaces with ivars/properties implement the
342// invalidation methods.
343void IvarInvalidationChecker::checkASTDecl(const ObjCImplementationDecl *ImplD,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000344 AnalysisManager& Mgr,
345 BugReporter &BR) const {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000346 // Collect all ivars that need cleanup.
347 IvarSet Ivars;
Anna Zaks664566c2013-01-10 22:44:16 +0000348 // Record the first Ivar needing invalidation; used in reporting when only
349 // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
350 // deterministic output.
351 const ObjCIvarDecl *FirstIvarDecl = 0;
Anna Zaksb1fc6732013-01-10 20:59:51 +0000352 const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
Anna Zakse0c50fa2012-10-16 19:36:37 +0000353
354 // Collect ivars declared in this class, its extensions and its implementation
355 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
356 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
357 Iv= Iv->getNextIvar())
Anna Zaks664566c2013-01-10 22:44:16 +0000358 trackIvar(Iv, Ivars, &FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000359
Anna Zaks377945c2012-09-27 21:57:14 +0000360 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000361 // ivar which is backing a property has been reset.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000362 MethToIvarMapTy PropSetterToIvarMap;
363 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000364 PropToIvarMapTy PropertyToIvarMap;
Anna Zaks377945c2012-09-27 21:57:14 +0000365 IvarToPropMapTy IvarToPopertyMap;
Anna Zaksc3c26b72012-10-18 19:17:57 +0000366
367 ObjCInterfaceDecl::PropertyMap PropMap;
368 InterfaceD->collectPropertiesToImplement(PropMap);
369
370 for (ObjCInterfaceDecl::PropertyMap::iterator
371 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
372 const ObjCPropertyDecl *PD = I->second;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000373
Anna Zaks664566c2013-01-10 22:44:16 +0000374 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
375 &FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000376 if (!ID) {
377 continue;
378 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000379
380 // Store the mappings.
381 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000382 PropertyToIvarMap[PD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000383 IvarToPopertyMap[ID] = PD;
384
385 // Find the setter and the getter.
386 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
387 if (SetterD) {
388 SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000389 PropSetterToIvarMap[SetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000390 }
391
392 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
393 if (GetterD) {
394 GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000395 PropGetterToIvarMap[GetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000396 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000397 }
398
Anna Zaksb1fc6732013-01-10 20:59:51 +0000399 // If no ivars need invalidation, there is nothing to check here.
400 if (Ivars.empty())
Anna Zaks31f69cc2012-09-29 00:20:38 +0000401 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000402
Anna Zaksb1fc6732013-01-10 20:59:51 +0000403 // Find all invalidation methods in this @interface declaration and parents.
404 InvalidationInfo Info;
405 containsInvalidationMethod(InterfaceD, Info);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000406
Anna Zaksb1fc6732013-01-10 20:59:51 +0000407 // Report an error in case none of the invalidation methods are declared.
408 if (!Info.needsInvalidation()) {
409 SmallString<128> sbuf;
410 llvm::raw_svector_ostream os(sbuf);
Anna Zaks664566c2013-01-10 22:44:16 +0000411 assert(FirstIvarDecl);
412 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
Anna Zaks6de7daa2013-01-11 03:52:44 +0000413 os << "needs to be invalidated; ";
Anna Zaks79ccd562013-01-16 01:35:57 +0000414 os << "no invalidation method is declared for " << InterfaceD->getName();
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000415
Anna Zaksb1fc6732013-01-10 20:59:51 +0000416 PathDiagnosticLocation IvarDecLocation =
Anna Zaks664566c2013-01-10 22:44:16 +0000417 PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
Anna Zaks377945c2012-09-27 21:57:14 +0000418
Anna Zaks664566c2013-01-10 22:44:16 +0000419 BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
Anna Zaksb1fc6732013-01-10 20:59:51 +0000420 categories::CoreFoundationObjectiveC, os.str(),
421 IvarDecLocation);
422 return;
423 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000424
Anna Zaksb1fc6732013-01-10 20:59:51 +0000425 // Check that all ivars are invalidated by the invalidation methods.
426 bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
427 for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
428 E = Info.InvalidationMethods.end(); I != E; ++I) {
429 const ObjCMethodDecl *InterfD = *I;
430
431 // Get the corresponding method in the @implementation.
432 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
433 InterfD->isInstanceMethod());
434 if (D && D->hasBody()) {
435 AtImplementationContainsAtLeastOneInvalidationMethod = true;
436
437 // Get a copy of ivars needing invalidation.
438 IvarSet IvarsI = Ivars;
439
440 bool CalledAnotherInvalidationMethod = false;
441 MethodCrawler(IvarsI,
442 CalledAnotherInvalidationMethod,
443 PropSetterToIvarMap,
444 PropGetterToIvarMap,
445 PropertyToIvarMap,
446 BR.getContext()).VisitStmt(D->getBody());
447 // If another invalidation method was called, trust that full invalidation
448 // has occurred.
449 if (CalledAnotherInvalidationMethod)
450 continue;
451
452 // Warn on the ivars that were not invalidated by the method.
453 for (IvarSet::const_iterator I = IvarsI.begin(),
454 E = IvarsI.end(); I != E; ++I)
455 if (!I->second.isInvalidated()) {
456 SmallString<128> sbuf;
457 llvm::raw_svector_ostream os(sbuf);
458 printIvar(os, I->first, IvarToPopertyMap);
459 os << "needs to be invalidated or set to nil";
460 PathDiagnosticLocation MethodDecLocation =
461 PathDiagnosticLocation::createEnd(D->getBody(),
462 BR.getSourceManager(),
463 Mgr.getAnalysisDeclContext(D));
464 BR.EmitBasicReport(D, "Incomplete invalidation",
465 categories::CoreFoundationObjectiveC, os.str(),
466 MethodDecLocation);
467 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000468 }
469 }
Anna Zaksb1fc6732013-01-10 20:59:51 +0000470
471 // Report an error in case none of the invalidation methods are implemented.
472 if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
473 SmallString<128> sbuf;
474 llvm::raw_svector_ostream os(sbuf);
Anna Zaks664566c2013-01-10 22:44:16 +0000475 assert(FirstIvarDecl);
476 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
Anna Zaks6de7daa2013-01-11 03:52:44 +0000477 os << "needs to be invalidated; ";
Anna Zaks79ccd562013-01-16 01:35:57 +0000478 os << "no invalidation method is defined in the @implementation for "
Anna Zaks6de7daa2013-01-11 03:52:44 +0000479 << InterfaceD->getName();
Anna Zaksb1fc6732013-01-10 20:59:51 +0000480
481 PathDiagnosticLocation IvarDecLocation =
Anna Zaks664566c2013-01-10 22:44:16 +0000482 PathDiagnosticLocation::createBegin(FirstIvarDecl,
Anna Zaksb1fc6732013-01-10 20:59:51 +0000483 BR.getSourceManager());
Anna Zaks664566c2013-01-10 22:44:16 +0000484 BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
Anna Zaksb1fc6732013-01-10 20:59:51 +0000485 categories::CoreFoundationObjectiveC, os.str(),
486 IvarDecLocation);
487 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000488}
489
Anna Zaks31f69cc2012-09-29 00:20:38 +0000490void IvarInvalidationChecker::MethodCrawler::markInvalidated(
491 const ObjCIvarDecl *Iv) {
492 IvarSet::iterator I = IVars.find(Iv);
493 if (I != IVars.end()) {
494 // If InvalidationMethod is present, we are processing the message send and
495 // should ensure we are invalidating with the appropriate method,
496 // otherwise, we are processing setting to 'nil'.
497 if (InvalidationMethod)
498 I->second.markInvalidated(InvalidationMethod);
499 else
500 I->second.markInvalidated();
501 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000502}
503
Anna Zaks31f69cc2012-09-29 00:20:38 +0000504const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
505 E = E->IgnoreParenCasts();
506 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
507 E = POE->getSyntacticForm()->IgnoreParenCasts();
508 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
509 E = OVE->getSourceExpr()->IgnoreParenCasts();
510 return E;
511}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000512
Anna Zaks31f69cc2012-09-29 00:20:38 +0000513void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
514 const ObjCIvarRefExpr *IvarRef) {
515 if (const Decl *D = IvarRef->getDecl())
516 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
517}
518
519void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000520 const ObjCMessageExpr *ME) {
521 const ObjCMethodDecl *MD = ME->getMethodDecl();
522 if (MD) {
523 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000524 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
525 if (IvI != PropertyGetterToIvarMap.end())
526 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000527 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000528}
529
Anna Zaks31f69cc2012-09-29 00:20:38 +0000530void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000531 const ObjCPropertyRefExpr *PA) {
532
533 if (PA->isExplicitProperty()) {
534 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
535 if (PD) {
536 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000537 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
538 if (IvI != PropertyToIvarMap.end())
539 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000540 return;
541 }
542 }
543
544 if (PA->isImplicitProperty()) {
545 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
546 if (MD) {
547 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000548 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
549 if (IvI != PropertyGetterToIvarMap.end())
550 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000551 return;
552 }
553 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000554}
555
556bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
557 E = peel(E);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000558
Anna Zaksb9733ac2012-10-01 20:33:58 +0000559 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
560 != Expr::NPCK_NotNull);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000561}
562
563void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
564 E = peel(E);
565
Anna Zaks31f69cc2012-09-29 00:20:38 +0000566 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
567 checkObjCIvarRefExpr(IvarRef);
568 return;
569 }
570
571 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
572 checkObjCPropertyRefExpr(PropRef);
573 return;
574 }
575
576 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
577 checkObjCMessageExpr(MsgExpr);
578 return;
579 }
580}
581
582void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
583 const BinaryOperator *BO) {
Anna Zaksb9733ac2012-10-01 20:33:58 +0000584 VisitStmt(BO);
585
Anna Zaks65032552013-01-10 23:34:16 +0000586 // Do we assign/compare against zero? If yes, check the variable we are
587 // assigning to.
588 BinaryOperatorKind Opcode = BO->getOpcode();
589 if (Opcode != BO_Assign &&
590 Opcode != BO_EQ &&
591 Opcode != BO_NE)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000592 return;
593
Anna Zaks65032552013-01-10 23:34:16 +0000594 if (isZero(BO->getRHS())) {
595 check(BO->getLHS());
596 return;
597 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000598
Anna Zaks65032552013-01-10 23:34:16 +0000599 if (Opcode != BO_Assign && isZero(BO->getLHS())) {
600 check(BO->getRHS());
601 return;
602 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000603}
604
605void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
Anna Zaks664566c2013-01-10 22:44:16 +0000606 const ObjCMessageExpr *ME) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000607 const ObjCMethodDecl *MD = ME->getMethodDecl();
608 const Expr *Receiver = ME->getInstanceReceiver();
609
610 // Stop if we are calling '[self invalidate]'.
611 if (Receiver && isInvalidationMethod(MD))
Anna Zaksbbff82f2012-10-01 20:34:04 +0000612 if (Receiver->isObjCSelfExpr()) {
613 CalledAnotherInvalidationMethod = true;
614 return;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000615 }
616
617 // Check if we call a setter and set the property to 'nil'.
618 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
619 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
620 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
621 if (IvI != PropertySetterToIvarMap.end()) {
622 markInvalidated(IvI->second);
623 return;
624 }
625 }
626
627 // Check if we call the 'invalidation' routine on the ivar.
628 if (Receiver) {
629 InvalidationMethod = MD;
630 check(Receiver->IgnoreParenCasts());
631 InvalidationMethod = 0;
632 }
633
634 VisitStmt(ME);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000635}
636}
637
638// Register the checker.
639void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
640 mgr.registerChecker<IvarInvalidationChecker>();
641}