blob: b0454433c143ba51b03c5f1076642e3a3de95158 [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.
178 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000179
180 /// Given the property declaration, and the list of tracked ivars, finds
181 /// the ivar backing the property when possible. Returns '0' when no such
182 /// ivar could be found.
183 static const ObjCIvarDecl *findPropertyBackingIvar(
184 const ObjCPropertyDecl *Prop,
185 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000186 IvarSet &TrackedIvars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000187
Anna Zaksb1fc6732013-01-10 20:59:51 +0000188 /// Print ivar name or the property if the given ivar backs a property.
189 static void printIvar(llvm::raw_svector_ostream &os,
190 const ObjCIvarDecl *IvarDecl,
191 IvarToPropMapTy &IvarToPopertyMap);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000192public:
Anna Zaksb1fc6732013-01-10 20:59:51 +0000193 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000194 BugReporter &BR) const;
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(
Anna Zaksb1fc6732013-01-10 20:59:51 +0000209 const ObjCContainerDecl *D, InvalidationInfo &OutInfo) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000210
211 if (!D)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000212 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000213
Anna Zaksb1fc6732013-01-10 20:59:51 +0000214 assert(!isa<ObjCImplementationDecl>(D));
215 // TODO: Cache the results.
216
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000217 // Check all methods.
218 for (ObjCContainerDecl::method_iterator
219 I = D->meth_begin(),
220 E = D->meth_end(); I != E; ++I) {
221 const ObjCMethodDecl *MDI = *I;
222 if (isInvalidationMethod(MDI))
Anna Zaks31f69cc2012-09-29 00:20:38 +0000223 OutInfo.addInvalidationMethod(
224 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000225 }
226
227 // If interface, check all parent protocols and super.
228 // TODO: Visit all categories in case the invalidation method is declared in
229 // a category.
230 if (const ObjCInterfaceDecl *InterfaceD = dyn_cast<ObjCInterfaceDecl>(D)) {
231 for (ObjCInterfaceDecl::protocol_iterator
232 I = InterfaceD->protocol_begin(),
233 E = InterfaceD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000234 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000235 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000236 containsInvalidationMethod(InterfaceD->getSuperClass(), OutInfo);
237 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000238 }
239
240 // If protocol, check all parent protocols.
241 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
242 for (ObjCInterfaceDecl::protocol_iterator
243 I = ProtD->protocol_begin(),
244 E = ProtD->protocol_end(); I != E; ++I) {
Anna Zaks31f69cc2012-09-29 00:20:38 +0000245 containsInvalidationMethod(*I, OutInfo);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000246 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000247 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000248 }
249
250 llvm_unreachable("One of the casts above should have succeeded.");
251}
252
Anna Zaks377945c2012-09-27 21:57:14 +0000253bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
254 IvarSet &TrackedIvars) {
255 QualType IvQTy = Iv->getType();
256 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
257 if (!IvTy)
258 return false;
259 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks31f69cc2012-09-29 00:20:38 +0000260
Anna Zaksb1fc6732013-01-10 20:59:51 +0000261 InvalidationInfo Info;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000262 containsInvalidationMethod(IvInterf, Info);
263 if (Info.needsInvalidation()) {
264 TrackedIvars[cast<ObjCIvarDecl>(Iv->getCanonicalDecl())] = Info;
Anna Zaks377945c2012-09-27 21:57:14 +0000265 return true;
266 }
267 return false;
268}
269
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000270const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
271 const ObjCPropertyDecl *Prop,
272 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks377945c2012-09-27 21:57:14 +0000273 IvarSet &TrackedIvars) {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000274 const ObjCIvarDecl *IvarD = 0;
275
276 // Lookup for the synthesized case.
277 IvarD = Prop->getPropertyIvarDecl();
Anna Zaks5879fb32013-01-07 19:12:56 +0000278 // We only track the ivars/properties that are defined in the current
279 // class (not the parent).
280 if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
Anna Zaks377945c2012-09-27 21:57:14 +0000281 if (TrackedIvars.count(IvarD)) {
282 return IvarD;
283 }
284 // If the ivar is synthesized we still want to track it.
285 if (trackIvar(IvarD, TrackedIvars))
286 return IvarD;
287 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000288
289 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
290 StringRef PropName = Prop->getIdentifier()->getName();
291 for (IvarSet::const_iterator I = TrackedIvars.begin(),
292 E = TrackedIvars.end(); I != E; ++I) {
293 const ObjCIvarDecl *Iv = I->first;
294 StringRef IvarName = Iv->getName();
295
296 if (IvarName == PropName)
297 return Iv;
298
299 SmallString<128> PropNameWithUnderscore;
300 {
301 llvm::raw_svector_ostream os(PropNameWithUnderscore);
302 os << '_' << PropName;
303 }
304 if (IvarName == PropNameWithUnderscore.str())
305 return Iv;
306 }
307
308 // Note, this is a possible source of false positives. We could look at the
309 // getter implementation to find the ivar when its name is not derived from
310 // the property name.
311 return 0;
312}
313
Anna Zaksb1fc6732013-01-10 20:59:51 +0000314void IvarInvalidationChecker::printIvar(llvm::raw_svector_ostream &os,
315 const ObjCIvarDecl *IvarDecl,
316 IvarToPropMapTy &IvarToPopertyMap) {
317 if (IvarDecl->getSynthesize()) {
318 const ObjCPropertyDecl *PD = IvarToPopertyMap[IvarDecl];
319 assert(PD &&"Do we synthesize ivars for something other than properties?");
320 os << "Property "<< PD->getName() << " ";
321 } else {
322 os << "Instance variable "<< IvarDecl->getName() << " ";
323 }
324}
325
326// Check that the invalidatable interfaces with ivars/properties implement the
327// invalidation methods.
328void IvarInvalidationChecker::checkASTDecl(const ObjCImplementationDecl *ImplD,
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000329 AnalysisManager& Mgr,
330 BugReporter &BR) const {
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000331 // Collect all ivars that need cleanup.
332 IvarSet Ivars;
Anna Zaksb1fc6732013-01-10 20:59:51 +0000333 const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
Anna Zakse0c50fa2012-10-16 19:36:37 +0000334
335 // Collect ivars declared in this class, its extensions and its implementation
336 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
337 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
338 Iv= Iv->getNextIvar())
Anna Zaks377945c2012-09-27 21:57:14 +0000339 trackIvar(Iv, Ivars);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000340
Anna Zaks377945c2012-09-27 21:57:14 +0000341 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000342 // ivar which is backing a property has been reset.
Anna Zaks31f69cc2012-09-29 00:20:38 +0000343 MethToIvarMapTy PropSetterToIvarMap;
344 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000345 PropToIvarMapTy PropertyToIvarMap;
Anna Zaks377945c2012-09-27 21:57:14 +0000346 IvarToPropMapTy IvarToPopertyMap;
Anna Zaksc3c26b72012-10-18 19:17:57 +0000347
348 ObjCInterfaceDecl::PropertyMap PropMap;
349 InterfaceD->collectPropertiesToImplement(PropMap);
350
351 for (ObjCInterfaceDecl::PropertyMap::iterator
352 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
353 const ObjCPropertyDecl *PD = I->second;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000354
355 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars);
356 if (!ID) {
357 continue;
358 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000359
360 // Store the mappings.
361 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000362 PropertyToIvarMap[PD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000363 IvarToPopertyMap[ID] = PD;
364
365 // Find the setter and the getter.
366 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
367 if (SetterD) {
368 SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000369 PropSetterToIvarMap[SetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000370 }
371
372 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
373 if (GetterD) {
374 GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000375 PropGetterToIvarMap[GetterD] = ID;
Anna Zaks377945c2012-09-27 21:57:14 +0000376 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000377 }
378
Anna Zaksb1fc6732013-01-10 20:59:51 +0000379 // If no ivars need invalidation, there is nothing to check here.
380 if (Ivars.empty())
Anna Zaks31f69cc2012-09-29 00:20:38 +0000381 return;
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000382
Anna Zaksb1fc6732013-01-10 20:59:51 +0000383 // Find all invalidation methods in this @interface declaration and parents.
384 InvalidationInfo Info;
385 containsInvalidationMethod(InterfaceD, Info);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000386
Anna Zaksb1fc6732013-01-10 20:59:51 +0000387 // Report an error in case none of the invalidation methods are declared.
388 if (!Info.needsInvalidation()) {
389 SmallString<128> sbuf;
390 llvm::raw_svector_ostream os(sbuf);
391 os << "No invalidation method declared in the @interface for "
392 << InterfaceD->getName() << "; ";
393 const ObjCIvarDecl *IvarDecl = Ivars.begin()->first;
394 printIvar(os, IvarDecl, IvarToPopertyMap);
395 os << "needs to be invalidated";
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000396
Anna Zaksb1fc6732013-01-10 20:59:51 +0000397 PathDiagnosticLocation IvarDecLocation =
398 PathDiagnosticLocation::createBegin(IvarDecl, BR.getSourceManager());
Anna Zaks377945c2012-09-27 21:57:14 +0000399
Anna Zaksb1fc6732013-01-10 20:59:51 +0000400 BR.EmitBasicReport(IvarDecl, "Incomplete invalidation",
401 categories::CoreFoundationObjectiveC, os.str(),
402 IvarDecLocation);
403 return;
404 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000405
Anna Zaksb1fc6732013-01-10 20:59:51 +0000406 // Check that all ivars are invalidated by the invalidation methods.
407 bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
408 for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
409 E = Info.InvalidationMethods.end(); I != E; ++I) {
410 const ObjCMethodDecl *InterfD = *I;
411
412 // Get the corresponding method in the @implementation.
413 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
414 InterfD->isInstanceMethod());
415 if (D && D->hasBody()) {
416 AtImplementationContainsAtLeastOneInvalidationMethod = true;
417
418 // Get a copy of ivars needing invalidation.
419 IvarSet IvarsI = Ivars;
420
421 bool CalledAnotherInvalidationMethod = false;
422 MethodCrawler(IvarsI,
423 CalledAnotherInvalidationMethod,
424 PropSetterToIvarMap,
425 PropGetterToIvarMap,
426 PropertyToIvarMap,
427 BR.getContext()).VisitStmt(D->getBody());
428 // If another invalidation method was called, trust that full invalidation
429 // has occurred.
430 if (CalledAnotherInvalidationMethod)
431 continue;
432
433 // Warn on the ivars that were not invalidated by the method.
434 for (IvarSet::const_iterator I = IvarsI.begin(),
435 E = IvarsI.end(); I != E; ++I)
436 if (!I->second.isInvalidated()) {
437 SmallString<128> sbuf;
438 llvm::raw_svector_ostream os(sbuf);
439 printIvar(os, I->first, IvarToPopertyMap);
440 os << "needs to be invalidated or set to nil";
441 PathDiagnosticLocation MethodDecLocation =
442 PathDiagnosticLocation::createEnd(D->getBody(),
443 BR.getSourceManager(),
444 Mgr.getAnalysisDeclContext(D));
445 BR.EmitBasicReport(D, "Incomplete invalidation",
446 categories::CoreFoundationObjectiveC, os.str(),
447 MethodDecLocation);
448 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000449 }
450 }
Anna Zaksb1fc6732013-01-10 20:59:51 +0000451
452 // Report an error in case none of the invalidation methods are implemented.
453 if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
454 SmallString<128> sbuf;
455 llvm::raw_svector_ostream os(sbuf);
456 os << "No invalidation method defined in the @implementation for "
457 << InterfaceD->getName() << "; ";
458 const ObjCIvarDecl *IvarDecl = Ivars.begin()->first;
459 printIvar(os, IvarDecl, IvarToPopertyMap);
460 os << "needs to be invalidated";
461
462 PathDiagnosticLocation IvarDecLocation =
463 PathDiagnosticLocation::createBegin(IvarDecl,
464 BR.getSourceManager());
465 BR.EmitBasicReport(IvarDecl, "Incomplete invalidation",
466 categories::CoreFoundationObjectiveC, os.str(),
467 IvarDecLocation);
468 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000469}
470
Anna Zaks31f69cc2012-09-29 00:20:38 +0000471void IvarInvalidationChecker::MethodCrawler::markInvalidated(
472 const ObjCIvarDecl *Iv) {
473 IvarSet::iterator I = IVars.find(Iv);
474 if (I != IVars.end()) {
475 // If InvalidationMethod is present, we are processing the message send and
476 // should ensure we are invalidating with the appropriate method,
477 // otherwise, we are processing setting to 'nil'.
478 if (InvalidationMethod)
479 I->second.markInvalidated(InvalidationMethod);
480 else
481 I->second.markInvalidated();
482 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000483}
484
Anna Zaks31f69cc2012-09-29 00:20:38 +0000485const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
486 E = E->IgnoreParenCasts();
487 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
488 E = POE->getSyntacticForm()->IgnoreParenCasts();
489 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
490 E = OVE->getSourceExpr()->IgnoreParenCasts();
491 return E;
492}
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000493
Anna Zaks31f69cc2012-09-29 00:20:38 +0000494void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
495 const ObjCIvarRefExpr *IvarRef) {
496 if (const Decl *D = IvarRef->getDecl())
497 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
498}
499
500void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000501 const ObjCMessageExpr *ME) {
502 const ObjCMethodDecl *MD = ME->getMethodDecl();
503 if (MD) {
504 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000505 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
506 if (IvI != PropertyGetterToIvarMap.end())
507 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000508 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000509}
510
Anna Zaks31f69cc2012-09-29 00:20:38 +0000511void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000512 const ObjCPropertyRefExpr *PA) {
513
514 if (PA->isExplicitProperty()) {
515 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
516 if (PD) {
517 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000518 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
519 if (IvI != PropertyToIvarMap.end())
520 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000521 return;
522 }
523 }
524
525 if (PA->isImplicitProperty()) {
526 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
527 if (MD) {
528 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000529 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
530 if (IvI != PropertyGetterToIvarMap.end())
531 markInvalidated(IvI->second);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000532 return;
533 }
534 }
Anna Zaks31f69cc2012-09-29 00:20:38 +0000535}
536
537bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
538 E = peel(E);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000539
Anna Zaksb9733ac2012-10-01 20:33:58 +0000540 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
541 != Expr::NPCK_NotNull);
Anna Zaks31f69cc2012-09-29 00:20:38 +0000542}
543
544void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
545 E = peel(E);
546
Anna Zaks31f69cc2012-09-29 00:20:38 +0000547 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
548 checkObjCIvarRefExpr(IvarRef);
549 return;
550 }
551
552 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
553 checkObjCPropertyRefExpr(PropRef);
554 return;
555 }
556
557 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
558 checkObjCMessageExpr(MsgExpr);
559 return;
560 }
561}
562
563void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
564 const BinaryOperator *BO) {
Anna Zaksb9733ac2012-10-01 20:33:58 +0000565 VisitStmt(BO);
566
567 if (BO->getOpcode() != BO_Assign)
Anna Zaks31f69cc2012-09-29 00:20:38 +0000568 return;
569
570 // Do we assign zero?
571 if (!isZero(BO->getRHS()))
572 return;
573
574 // Check the variable we are assigning to.
575 check(BO->getLHS());
Anna Zaks31f69cc2012-09-29 00:20:38 +0000576}
577
578void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
579 const ObjCMessageExpr *ME) {
580 const ObjCMethodDecl *MD = ME->getMethodDecl();
581 const Expr *Receiver = ME->getInstanceReceiver();
582
583 // Stop if we are calling '[self invalidate]'.
584 if (Receiver && isInvalidationMethod(MD))
Anna Zaksbbff82f2012-10-01 20:34:04 +0000585 if (Receiver->isObjCSelfExpr()) {
586 CalledAnotherInvalidationMethod = true;
587 return;
Anna Zaks31f69cc2012-09-29 00:20:38 +0000588 }
589
590 // Check if we call a setter and set the property to 'nil'.
591 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
592 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
593 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
594 if (IvI != PropertySetterToIvarMap.end()) {
595 markInvalidated(IvI->second);
596 return;
597 }
598 }
599
600 // Check if we call the 'invalidation' routine on the ivar.
601 if (Receiver) {
602 InvalidationMethod = MD;
603 check(Receiver->IgnoreParenCasts());
604 InvalidationMethod = 0;
605 }
606
607 VisitStmt(ME);
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000608}
609}
610
611// Register the checker.
612void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
613 mgr.registerChecker<IvarInvalidationChecker>();
614}