blob: 194847a6d0d0366c39927644d835aef1b0e734f7 [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//
Anna Zaks26db7db2013-02-08 23:55:43 +000023// Partial invalidor annotation allows to addess cases when ivars are
24// invalidated by other methods, which might or might not be called from
25// the invalidation method. The checker checks that each invalidation
26// method and all the partial methods cumulatively invalidate all ivars.
27// __attribute__((annotate("objc_instance_variable_invalidator_partial")));
28//
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000029//===----------------------------------------------------------------------===//
30
31#include "ClangSACheckers.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000032#include "clang/AST/Attr.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000033#include "clang/AST/DeclObjC.h"
34#include "clang/AST/StmtVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000035#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36#include "clang/StaticAnalyzer/Core/Checker.h"
37#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000038#include "llvm/ADT/DenseMap.h"
Anna Zaksb1fc6732013-01-10 20:59:51 +000039#include "llvm/ADT/SetVector.h"
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000040#include "llvm/ADT/SmallString.h"
41
42using namespace clang;
43using namespace ento;
44
45namespace {
46class IvarInvalidationChecker :
Anna Zaksb1fc6732013-01-10 20:59:51 +000047 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000048
Anna Zaksb1fc6732013-01-10 20:59:51 +000049 typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000050 typedef llvm::DenseMap<const ObjCMethodDecl*,
51 const ObjCIvarDecl*> MethToIvarMapTy;
52 typedef llvm::DenseMap<const ObjCPropertyDecl*,
53 const ObjCIvarDecl*> PropToIvarMapTy;
Anna Zaks377945c2012-09-27 21:57:14 +000054 typedef llvm::DenseMap<const ObjCIvarDecl*,
55 const ObjCPropertyDecl*> IvarToPropMapTy;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000056
Anna Zaks31f69cc2012-09-29 00:20:38 +000057
Anna Zaksb1fc6732013-01-10 20:59:51 +000058 struct InvalidationInfo {
Anna Zaks31f69cc2012-09-29 00:20:38 +000059 /// Has the ivar been invalidated?
60 bool IsInvalidated;
61
62 /// The methods which can be used to invalidate the ivar.
63 MethodSet InvalidationMethods;
64
Anna Zaksb1fc6732013-01-10 20:59:51 +000065 InvalidationInfo() : IsInvalidated(false) {}
Anna Zaks31f69cc2012-09-29 00:20:38 +000066 void addInvalidationMethod(const ObjCMethodDecl *MD) {
67 InvalidationMethods.insert(MD);
68 }
69
70 bool needsInvalidation() const {
71 return !InvalidationMethods.empty();
72 }
73
Anna Zaks26db7db2013-02-08 23:55:43 +000074 bool hasMethod(const ObjCMethodDecl *MD) {
Anna Zaks31f69cc2012-09-29 00:20:38 +000075 if (IsInvalidated)
76 return true;
77 for (MethodSet::iterator I = InvalidationMethods.begin(),
78 E = InvalidationMethods.end(); I != E; ++I) {
79 if (*I == MD) {
80 IsInvalidated = true;
81 return true;
82 }
83 }
84 return false;
85 }
Anna Zaks31f69cc2012-09-29 00:20:38 +000086 };
87
Anna Zaksb1fc6732013-01-10 20:59:51 +000088 typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
Anna Zaks31f69cc2012-09-29 00:20:38 +000089
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 Zaks5bf5c2e2012-09-26 18:55:16 +000093 /// The set of Ivars which need to be invalidated.
94 IvarSet &IVars;
95
Anna Zaks31f69cc2012-09-29 00:20:38 +000096 /// Flag is set as the result of a message send to another
97 /// invalidation method.
98 bool &CalledAnotherInvalidationMethod;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +000099
Anna Zaks31f69cc2012-09-29 00:20:38 +0000100 /// Property setter to ivar mapping.
101 const MethToIvarMapTy &PropertySetterToIvarMap;
102
103 /// Property getter to ivar mapping.
104 const MethToIvarMapTy &PropertyGetterToIvarMap;
105
106 /// Property to ivar mapping.
107 const PropToIvarMapTy &PropertyToIvarMap;
108
109 /// The invalidation method being currently processed.
110 const ObjCMethodDecl *InvalidationMethod;
111
Anna Zaksb9733ac2012-10-01 20:33:58 +0000112 ASTContext &Ctx;
113
114 /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000115 const Expr *peel(const Expr *E) const;
116
117 /// Does this expression represent zero: '0'?
118 bool isZero(const Expr *E) const;
119
120 /// Mark the given ivar as invalidated.
121 void markInvalidated(const ObjCIvarDecl *Iv);
122
123 /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
124 /// invalidated.
125 void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
126
127 /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
128 /// it as invalidated.
129 void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
130
131 /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
132 /// if yes, marks it as invalidated.
133 void checkObjCMessageExpr(const ObjCMessageExpr *ME);
134
135 /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
136 void check(const Expr *E);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000137
138 public:
Anna Zaksbbff82f2012-10-01 20:34:04 +0000139 MethodCrawler(IvarSet &InIVars,
Anna Zaks31f69cc2012-09-29 00:20:38 +0000140 bool &InCalledAnotherInvalidationMethod,
141 const MethToIvarMapTy &InPropertySetterToIvarMap,
142 const MethToIvarMapTy &InPropertyGetterToIvarMap,
Anna Zaksb9733ac2012-10-01 20:33:58 +0000143 const PropToIvarMapTy &InPropertyToIvarMap,
144 ASTContext &InCtx)
Anna Zaksbbff82f2012-10-01 20:34:04 +0000145 : IVars(InIVars),
Anna Zaks31f69cc2012-09-29 00:20:38 +0000146 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
147 PropertySetterToIvarMap(InPropertySetterToIvarMap),
148 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
149 PropertyToIvarMap(InPropertyToIvarMap),
Anna Zaksb9733ac2012-10-01 20:33:58 +0000150 InvalidationMethod(0),
151 Ctx(InCtx) {}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000152
153 void VisitStmt(const Stmt *S) { VisitChildren(S); }
154
Anna Zaks31f69cc2012-09-29 00:20:38 +0000155 void VisitBinaryOperator(const BinaryOperator *BO);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000156
157 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
158
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000159 void VisitChildren(const Stmt *S) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000160 for (Stmt::const_child_range I = S->children(); I; ++I) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000161 if (*I)
Anna Zaksb087bbf2012-09-27 19:45:08 +0000162 this->Visit(*I);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000163 if (CalledAnotherInvalidationMethod)
164 return;
165 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000166 }
167 };
168
169 /// Check if the any of the methods inside the interface are annotated with
Anna Zaks31f69cc2012-09-29 00:20:38 +0000170 /// the invalidation annotation, update the IvarInfo accordingly.
Anna Zaks26db7db2013-02-08 23:55:43 +0000171 /// \param LookForPartial is set when we are searching for partial
172 /// invalidators.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000173 static void containsInvalidationMethod(const ObjCContainerDecl *D,
Anna Zaks26db7db2013-02-08 23:55:43 +0000174 InvalidationInfo &Out,
175 bool LookForPartial);
Anna Zaks377945c2012-09-27 21:57:14 +0000176
177 /// Check if ivar should be tracked and add to TrackedIvars if positive.
178 /// Returns true if ivar should be tracked.
Anna Zaks664566c2013-01-10 22:44:16 +0000179 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
180 const ObjCIvarDecl **FirstIvarDecl);
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 Zaks664566c2013-01-10 22:44:16 +0000188 IvarSet &TrackedIvars,
189 const ObjCIvarDecl **FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000190
Anna Zaksb1fc6732013-01-10 20:59:51 +0000191 /// Print ivar name or the property if the given ivar backs a property.
192 static void printIvar(llvm::raw_svector_ostream &os,
193 const ObjCIvarDecl *IvarDecl,
Anna Zaks2b174c32013-02-08 23:55:45 +0000194 const IvarToPropMapTy &IvarToPopertyMap);
195
196 static void reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl,
197 const IvarToPropMapTy &IvarToPopertyMap,
198 const ObjCInterfaceDecl *InterfaceD,
199 BugReporter &BR,
200 bool MissingDeclaration);
201 static void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
202 const IvarToPropMapTy &IvarToPopertyMap,
203 const ObjCMethodDecl *MethodD,
204 AnalysisManager& Mgr,
205 BugReporter &BR);
206
Anna Zaks26db7db2013-02-08 23:55:43 +0000207
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000208public:
Anna Zaksb1fc6732013-01-10 20:59:51 +0000209 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000210 BugReporter &BR) const;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000211};
212
Anna Zaks26db7db2013-02-08 23:55:43 +0000213static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) {
Anna Zaksb087bbf2012-09-27 19:45:08 +0000214 for (specific_attr_iterator<AnnotateAttr>
215 AI = M->specific_attr_begin<AnnotateAttr>(),
216 AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
217 const AnnotateAttr *Ann = *AI;
Anna Zaks26db7db2013-02-08 23:55:43 +0000218 if (!LookForPartial &&
219 Ann->getAnnotation() == "objc_instance_variable_invalidator")
220 return true;
221 if (LookForPartial &&
222 Ann->getAnnotation() == "objc_instance_variable_invalidator_partial")
Anna Zaksb087bbf2012-09-27 19:45:08 +0000223 return true;
224 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000225 return false;
226}
227
Anna Zaks31f69cc2012-09-29 00:20:38 +0000228void IvarInvalidationChecker::containsInvalidationMethod(
Anna Zaks26db7db2013-02-08 23:55:43 +0000229 const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000230
231 if (!D)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000232 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000233
Anna Zaksb1fc6732013-01-10 20:59:51 +0000234 assert(!isa<ObjCImplementationDecl>(D));
235 // TODO: Cache the results.
236
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000237 // Check all methods.
238 for (ObjCContainerDecl::method_iterator
239 I = D->meth_begin(),
240 E = D->meth_end(); I != E; ++I) {
241 const ObjCMethodDecl *MDI = *I;
Anna Zaks26db7db2013-02-08 23:55:43 +0000242 if (isInvalidationMethod(MDI, Partial))
Anna Zaks31f69cc2012-09-29 00:20:38 +0000243 OutInfo.addInvalidationMethod(
244 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000245 }
246
247 // If interface, check all parent protocols and super.
Anna Zaksae81e172013-01-11 03:52:37 +0000248 if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
249
250 // Visit all protocols.
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000251 for (ObjCInterfaceDecl::protocol_iterator
Anna Zaksae81e172013-01-11 03:52:37 +0000252 I = InterfD->protocol_begin(),
253 E = InterfD->protocol_end(); I != E; ++I) {
Anna Zaks26db7db2013-02-08 23:55:43 +0000254 containsInvalidationMethod((*I)->getDefinition(), OutInfo, Partial);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000255 }
Anna Zaksae81e172013-01-11 03:52:37 +0000256
257 // Visit all categories in case the invalidation method is declared in
258 // a category.
Douglas Gregord3297242013-01-16 23:00:23 +0000259 for (ObjCInterfaceDecl::visible_extensions_iterator
260 Ext = InterfD->visible_extensions_begin(),
261 ExtEnd = InterfD->visible_extensions_end();
262 Ext != ExtEnd; ++Ext) {
Anna Zaks26db7db2013-02-08 23:55:43 +0000263 containsInvalidationMethod(*Ext, OutInfo, Partial);
Anna Zaksae81e172013-01-11 03:52:37 +0000264 }
265
Anna Zaks26db7db2013-02-08 23:55:43 +0000266 containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000267 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000268 }
269
270 // If protocol, check all parent protocols.
271 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
272 for (ObjCInterfaceDecl::protocol_iterator
273 I = ProtD->protocol_begin(),
274 E = ProtD->protocol_end(); I != E; ++I) {
Anna Zaks26db7db2013-02-08 23:55:43 +0000275 containsInvalidationMethod((*I)->getDefinition(), OutInfo, Partial);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000276 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000277 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000278 }
279
Anna Zaksae81e172013-01-11 03:52:37 +0000280 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000281}
282
Anna Zaks377945c2012-09-27 21:57:14 +0000283bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
Anna Zaks664566c2013-01-10 22:44:16 +0000284 IvarSet &TrackedIvars,
285 const ObjCIvarDecl **FirstIvarDecl) {
Anna Zaks377945c2012-09-27 21:57:14 +0000286 QualType IvQTy = Iv->getType();
287 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
288 if (!IvTy)
289 return false;
290 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks31f69cc2012-09-29 00:20:38 +0000291
Anna Zaksb1fc6732013-01-10 20:59:51 +0000292 InvalidationInfo Info;
Anna Zaks26db7db2013-02-08 23:55:43 +0000293 containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000294 if (Info.needsInvalidation()) {
Anna Zaks664566c2013-01-10 22:44:16 +0000295 const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
296 TrackedIvars[I] = Info;
297 if (!*FirstIvarDecl)
298 *FirstIvarDecl = I;
Anna Zaks377945c2012-09-27 21:57:14 +0000299 return true;
300 }
301 return false;
302}
303
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000304const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
305 const ObjCPropertyDecl *Prop,
306 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks664566c2013-01-10 22:44:16 +0000307 IvarSet &TrackedIvars,
308 const ObjCIvarDecl **FirstIvarDecl) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000309 const ObjCIvarDecl *IvarD = 0;
310
311 // Lookup for the synthesized case.
312 IvarD = Prop->getPropertyIvarDecl();
Anna Zaks5879fb32013-01-07 19:12:56 +0000313 // We only track the ivars/properties that are defined in the current
314 // class (not the parent).
315 if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
Anna Zaks377945c2012-09-27 21:57:14 +0000316 if (TrackedIvars.count(IvarD)) {
317 return IvarD;
318 }
319 // If the ivar is synthesized we still want to track it.
Anna Zaks664566c2013-01-10 22:44:16 +0000320 if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
Anna Zaks377945c2012-09-27 21:57:14 +0000321 return IvarD;
322 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000323
324 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
325 StringRef PropName = Prop->getIdentifier()->getName();
326 for (IvarSet::const_iterator I = TrackedIvars.begin(),
327 E = TrackedIvars.end(); I != E; ++I) {
328 const ObjCIvarDecl *Iv = I->first;
329 StringRef IvarName = Iv->getName();
330
331 if (IvarName == PropName)
332 return Iv;
333
334 SmallString<128> PropNameWithUnderscore;
335 {
336 llvm::raw_svector_ostream os(PropNameWithUnderscore);
337 os << '_' << PropName;
338 }
339 if (IvarName == PropNameWithUnderscore.str())
340 return Iv;
341 }
342
343 // Note, this is a possible source of false positives. We could look at the
344 // getter implementation to find the ivar when its name is not derived from
345 // the property name.
346 return 0;
347}
348
Anna Zaksb1fc6732013-01-10 20:59:51 +0000349void IvarInvalidationChecker::printIvar(llvm::raw_svector_ostream &os,
Anna Zaks2b174c32013-02-08 23:55:45 +0000350 const ObjCIvarDecl *IvarDecl,
351 const IvarToPropMapTy &IvarToPopertyMap) {
Anna Zaksb1fc6732013-01-10 20:59:51 +0000352 if (IvarDecl->getSynthesize()) {
Anna Zaks2b174c32013-02-08 23:55:45 +0000353 const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
Anna Zaksb1fc6732013-01-10 20:59:51 +0000354 assert(PD &&"Do we synthesize ivars for something other than properties?");
355 os << "Property "<< PD->getName() << " ";
356 } else {
357 os << "Instance variable "<< IvarDecl->getName() << " ";
358 }
359}
360
361// Check that the invalidatable interfaces with ivars/properties implement the
362// invalidation methods.
363void IvarInvalidationChecker::checkASTDecl(const ObjCImplementationDecl *ImplD,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000364 AnalysisManager& Mgr,
365 BugReporter &BR) const {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000366 // Collect all ivars that need cleanup.
367 IvarSet Ivars;
Anna Zaks664566c2013-01-10 22:44:16 +0000368 // Record the first Ivar needing invalidation; used in reporting when only
369 // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
370 // deterministic output.
371 const ObjCIvarDecl *FirstIvarDecl = 0;
Anna Zaksb1fc6732013-01-10 20:59:51 +0000372 const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
Anna Zakse0c50fa2012-10-16 19:36:37 +0000373
374 // Collect ivars declared in this class, its extensions and its implementation
375 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
376 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
377 Iv= Iv->getNextIvar())
Anna Zaks664566c2013-01-10 22:44:16 +0000378 trackIvar(Iv, Ivars, &FirstIvarDecl);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000379
Anna Zaks377945c2012-09-27 21:57:14 +0000380 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000381 // ivar which is backing a property has been reset.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000382 MethToIvarMapTy PropSetterToIvarMap;
383 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000384 PropToIvarMapTy PropertyToIvarMap;
Anna Zaks377945c2012-09-27 21:57:14 +0000385 IvarToPropMapTy IvarToPopertyMap;
Anna Zaksc3c26b72012-10-18 19:17:57 +0000386
387 ObjCInterfaceDecl::PropertyMap PropMap;
388 InterfaceD->collectPropertiesToImplement(PropMap);
389
390 for (ObjCInterfaceDecl::PropertyMap::iterator
391 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
392 const ObjCPropertyDecl *PD = I->second;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000393
Anna Zaks664566c2013-01-10 22:44:16 +0000394 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
395 &FirstIvarDecl);
Anna Zaks2b174c32013-02-08 23:55:45 +0000396 if (!ID)
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000397 continue;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000398
399 // Store the mappings.
400 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000401 PropertyToIvarMap[PD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000402 IvarToPopertyMap[ID] = PD;
403
404 // Find the setter and the getter.
405 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
406 if (SetterD) {
407 SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000408 PropSetterToIvarMap[SetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000409 }
410
411 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
412 if (GetterD) {
413 GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000414 PropGetterToIvarMap[GetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000415 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000416 }
417
Anna Zaksb1fc6732013-01-10 20:59:51 +0000418 // If no ivars need invalidation, there is nothing to check here.
419 if (Ivars.empty())
Anna Zaks31f69cc2012-09-29 00:20:38 +0000420 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000421
Anna Zaks26db7db2013-02-08 23:55:43 +0000422 // Find all partial invalidation methods.
423 InvalidationInfo PartialInfo;
424 containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true);
425
426 // Remove ivars invalidated by the partial invalidation methods. They do not
427 // need to be invalidated in the regular invalidation methods.
428 for (MethodSet::iterator
429 I = PartialInfo.InvalidationMethods.begin(),
430 E = PartialInfo.InvalidationMethods.end(); I != E; ++I) {
431 const ObjCMethodDecl *InterfD = *I;
432
433 // Get the corresponding method in the @implementation.
434 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
435 InterfD->isInstanceMethod());
436 if (D && D->hasBody()) {
437 bool CalledAnotherInvalidationMethod = false;
438 // The MethodCrowler is going to remove the invalidated ivars.
439 MethodCrawler(Ivars,
440 CalledAnotherInvalidationMethod,
441 PropSetterToIvarMap,
442 PropGetterToIvarMap,
443 PropertyToIvarMap,
444 BR.getContext()).VisitStmt(D->getBody());
445 // If another invalidation method was called, trust that full invalidation
446 // has occurred.
447 if (CalledAnotherInvalidationMethod)
448 Ivars.clear();
449 }
450 }
451
452 // If all ivars have been invalidated by partial invalidators, there is
453 // nothing to check here.
454 if (Ivars.empty())
455 return;
456
Anna Zaksb1fc6732013-01-10 20:59:51 +0000457 // Find all invalidation methods in this @interface declaration and parents.
458 InvalidationInfo Info;
Anna Zaks26db7db2013-02-08 23:55:43 +0000459 containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000460
Anna Zaksb1fc6732013-01-10 20:59:51 +0000461 // Report an error in case none of the invalidation methods are declared.
462 if (!Info.needsInvalidation()) {
Anna Zaks2b174c32013-02-08 23:55:45 +0000463 reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR,
464 /*MissingDeclaration*/ true);
Anna Zaksb1fc6732013-01-10 20:59:51 +0000465 return;
466 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000467
Anna Zaksb1fc6732013-01-10 20:59:51 +0000468 // Check that all ivars are invalidated by the invalidation methods.
469 bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
470 for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
471 E = Info.InvalidationMethods.end(); I != E; ++I) {
472 const ObjCMethodDecl *InterfD = *I;
473
474 // Get the corresponding method in the @implementation.
475 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
476 InterfD->isInstanceMethod());
477 if (D && D->hasBody()) {
478 AtImplementationContainsAtLeastOneInvalidationMethod = true;
479
480 // Get a copy of ivars needing invalidation.
481 IvarSet IvarsI = Ivars;
482
483 bool CalledAnotherInvalidationMethod = false;
484 MethodCrawler(IvarsI,
485 CalledAnotherInvalidationMethod,
486 PropSetterToIvarMap,
487 PropGetterToIvarMap,
488 PropertyToIvarMap,
489 BR.getContext()).VisitStmt(D->getBody());
490 // If another invalidation method was called, trust that full invalidation
491 // has occurred.
492 if (CalledAnotherInvalidationMethod)
493 continue;
494
495 // Warn on the ivars that were not invalidated by the method.
Anna Zaks2b174c32013-02-08 23:55:45 +0000496 for (IvarSet::const_iterator
497 I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
498 reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D, Mgr, BR);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000499 }
500 }
Anna Zaksb1fc6732013-01-10 20:59:51 +0000501
502 // Report an error in case none of the invalidation methods are implemented.
Anna Zaks2b174c32013-02-08 23:55:45 +0000503 if (!AtImplementationContainsAtLeastOneInvalidationMethod)
504 reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR,
505 /*MissingDeclaration*/ false);
506}
Anna Zaksb1fc6732013-01-10 20:59:51 +0000507
Anna Zaks2b174c32013-02-08 23:55:45 +0000508void IvarInvalidationChecker::
509reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl,
510 const IvarToPropMapTy &IvarToPopertyMap,
511 const ObjCInterfaceDecl *InterfaceD,
512 BugReporter &BR,
513 bool MissingDeclaration) {
514 SmallString<128> sbuf;
515 llvm::raw_svector_ostream os(sbuf);
516 assert(FirstIvarDecl);
517 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
518 os << "needs to be invalidated; ";
519 if (MissingDeclaration)
520 os << "no invalidation method is declared for ";
521 else
522 os << "no invalidation method is defined in the @implementation for ";
523 os << InterfaceD->getName();
524
525 PathDiagnosticLocation IvarDecLocation =
526 PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
527
528 BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation",
529 categories::CoreFoundationObjectiveC, os.str(),
530 IvarDecLocation);
531}
532
533void IvarInvalidationChecker::
534reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
535 const IvarToPropMapTy &IvarToPopertyMap,
536 const ObjCMethodDecl *MethodD,
537 AnalysisManager& Mgr,
538 BugReporter &BR) {
539 SmallString<128> sbuf;
540 llvm::raw_svector_ostream os(sbuf);
541 printIvar(os, IvarD, IvarToPopertyMap);
542 os << "needs to be invalidated or set to nil";
543 PathDiagnosticLocation MethodDecLocation =
544 PathDiagnosticLocation::createEnd(MethodD->getBody(),
545 BR.getSourceManager(),
546 Mgr.getAnalysisDeclContext(MethodD));
547 BR.EmitBasicReport(MethodD, "Incomplete invalidation",
548 categories::CoreFoundationObjectiveC, os.str(),
549 MethodDecLocation);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000550}
551
Anna Zaks31f69cc2012-09-29 00:20:38 +0000552void IvarInvalidationChecker::MethodCrawler::markInvalidated(
553 const ObjCIvarDecl *Iv) {
554 IvarSet::iterator I = IVars.find(Iv);
555 if (I != IVars.end()) {
556 // If InvalidationMethod is present, we are processing the message send and
557 // should ensure we are invalidating with the appropriate method,
558 // otherwise, we are processing setting to 'nil'.
Anna Zaks26db7db2013-02-08 23:55:43 +0000559 if (!InvalidationMethod ||
560 (InvalidationMethod && I->second.hasMethod(InvalidationMethod)))
561 IVars.erase(I);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000562 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000563}
564
Anna Zaks31f69cc2012-09-29 00:20:38 +0000565const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
566 E = E->IgnoreParenCasts();
567 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
568 E = POE->getSyntacticForm()->IgnoreParenCasts();
569 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
570 E = OVE->getSourceExpr()->IgnoreParenCasts();
571 return E;
572}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000573
Anna Zaks31f69cc2012-09-29 00:20:38 +0000574void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
575 const ObjCIvarRefExpr *IvarRef) {
576 if (const Decl *D = IvarRef->getDecl())
577 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
578}
579
580void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000581 const ObjCMessageExpr *ME) {
582 const ObjCMethodDecl *MD = ME->getMethodDecl();
583 if (MD) {
584 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000585 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
586 if (IvI != PropertyGetterToIvarMap.end())
587 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000588 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000589}
590
Anna Zaks31f69cc2012-09-29 00:20:38 +0000591void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000592 const ObjCPropertyRefExpr *PA) {
593
594 if (PA->isExplicitProperty()) {
595 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
596 if (PD) {
597 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000598 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
599 if (IvI != PropertyToIvarMap.end())
600 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000601 return;
602 }
603 }
604
605 if (PA->isImplicitProperty()) {
606 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
607 if (MD) {
608 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000609 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
610 if (IvI != PropertyGetterToIvarMap.end())
611 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000612 return;
613 }
614 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000615}
616
617bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
618 E = peel(E);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000619
Anna Zaksb9733ac2012-10-01 20:33:58 +0000620 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
621 != Expr::NPCK_NotNull);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000622}
623
624void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
625 E = peel(E);
626
Anna Zaks31f69cc2012-09-29 00:20:38 +0000627 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
628 checkObjCIvarRefExpr(IvarRef);
629 return;
630 }
631
632 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
633 checkObjCPropertyRefExpr(PropRef);
634 return;
635 }
636
637 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
638 checkObjCMessageExpr(MsgExpr);
639 return;
640 }
641}
642
643void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
644 const BinaryOperator *BO) {
Anna Zaksb9733ac2012-10-01 20:33:58 +0000645 VisitStmt(BO);
646
Anna Zaks65032552013-01-10 23:34:16 +0000647 // Do we assign/compare against zero? If yes, check the variable we are
648 // assigning to.
649 BinaryOperatorKind Opcode = BO->getOpcode();
650 if (Opcode != BO_Assign &&
651 Opcode != BO_EQ &&
652 Opcode != BO_NE)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000653 return;
654
Anna Zaks65032552013-01-10 23:34:16 +0000655 if (isZero(BO->getRHS())) {
656 check(BO->getLHS());
657 return;
658 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000659
Anna Zaks65032552013-01-10 23:34:16 +0000660 if (Opcode != BO_Assign && isZero(BO->getLHS())) {
661 check(BO->getRHS());
662 return;
663 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000664}
665
666void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
Anna Zaks664566c2013-01-10 22:44:16 +0000667 const ObjCMessageExpr *ME) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000668 const ObjCMethodDecl *MD = ME->getMethodDecl();
669 const Expr *Receiver = ME->getInstanceReceiver();
670
671 // Stop if we are calling '[self invalidate]'.
Anna Zaks26db7db2013-02-08 23:55:43 +0000672 if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false))
Anna Zaksbbff82f2012-10-01 20:34:04 +0000673 if (Receiver->isObjCSelfExpr()) {
674 CalledAnotherInvalidationMethod = true;
675 return;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000676 }
677
678 // Check if we call a setter and set the property to 'nil'.
679 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
680 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
681 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
682 if (IvI != PropertySetterToIvarMap.end()) {
683 markInvalidated(IvI->second);
684 return;
685 }
686 }
687
688 // Check if we call the 'invalidation' routine on the ivar.
689 if (Receiver) {
690 InvalidationMethod = MD;
691 check(Receiver->IgnoreParenCasts());
692 InvalidationMethod = 0;
693 }
694
695 VisitStmt(ME);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000696}
697}
698
699// Register the checker.
700void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
701 mgr.registerChecker<IvarInvalidationChecker>();
702}