blob: c663ccc1169d33b363df3937bcd3a77354cb0c48 [file] [log] [blame]
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001//===- IvarInvalidationChecker.cpp ------------------------------*- C++ -*-===//
Anna Zaks9802f9f2012-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 Zaks0353aad2012-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 Zaks9802f9f2012-09-26 18:55:16 +000022//
Ted Kremenek3a0678e2015-09-08 03:50:52 +000023// Partial invalidor annotation allows to addess cases when ivars are
24// invalidated by other methods, which might or might not be called from
Anna Zaksa5096f62013-02-08 23:55:43 +000025// 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 Zaks9802f9f2012-09-26 18:55:16 +000029//===----------------------------------------------------------------------===//
30
31#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000032#include "clang/AST/Attr.h"
Anna Zaks9802f9f2012-09-26 18:55:16 +000033#include "clang/AST/DeclObjC.h"
34#include "clang/AST/StmtVisitor.h"
Chandler Carruth3a022472012-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 Zaks9802f9f2012-09-26 18:55:16 +000038#include "llvm/ADT/DenseMap.h"
Anna Zaks0aeb60d2013-01-10 20:59:51 +000039#include "llvm/ADT/SetVector.h"
Anna Zaks9802f9f2012-09-26 18:55:16 +000040#include "llvm/ADT/SmallString.h"
41
42using namespace clang;
43using namespace ento;
44
45namespace {
Anna Zaks91a5fdf2013-02-08 23:55:47 +000046struct ChecksFilter {
47 /// Check for missing invalidation method declarations.
48 DefaultBool check_MissingInvalidationMethod;
49 /// Check that all ivars are invalidated.
50 DefaultBool check_InstanceVariableInvalidation;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000051
52 CheckName checkName_MissingInvalidationMethod;
53 CheckName checkName_InstanceVariableInvalidation;
Anna Zaks91a5fdf2013-02-08 23:55:47 +000054};
55
Anna Zaks7811c3e2013-02-09 01:09:27 +000056class IvarInvalidationCheckerImpl {
Anna Zaks0aeb60d2013-01-10 20:59:51 +000057 typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
Anna Zaks9802f9f2012-09-26 18:55:16 +000058 typedef llvm::DenseMap<const ObjCMethodDecl*,
59 const ObjCIvarDecl*> MethToIvarMapTy;
60 typedef llvm::DenseMap<const ObjCPropertyDecl*,
61 const ObjCIvarDecl*> PropToIvarMapTy;
Anna Zaksa0c83312012-09-27 21:57:14 +000062 typedef llvm::DenseMap<const ObjCIvarDecl*,
63 const ObjCPropertyDecl*> IvarToPropMapTy;
Anna Zaks9802f9f2012-09-26 18:55:16 +000064
Anna Zaks0aeb60d2013-01-10 20:59:51 +000065 struct InvalidationInfo {
Anna Zaks0353aad2012-09-29 00:20:38 +000066 /// Has the ivar been invalidated?
67 bool IsInvalidated;
68
69 /// The methods which can be used to invalidate the ivar.
70 MethodSet InvalidationMethods;
71
Anna Zaks0aeb60d2013-01-10 20:59:51 +000072 InvalidationInfo() : IsInvalidated(false) {}
Anna Zaks0353aad2012-09-29 00:20:38 +000073 void addInvalidationMethod(const ObjCMethodDecl *MD) {
74 InvalidationMethods.insert(MD);
75 }
76
77 bool needsInvalidation() const {
78 return !InvalidationMethods.empty();
79 }
80
Anna Zaksa5096f62013-02-08 23:55:43 +000081 bool hasMethod(const ObjCMethodDecl *MD) {
Anna Zaks0353aad2012-09-29 00:20:38 +000082 if (IsInvalidated)
83 return true;
84 for (MethodSet::iterator I = InvalidationMethods.begin(),
85 E = InvalidationMethods.end(); I != E; ++I) {
86 if (*I == MD) {
87 IsInvalidated = true;
88 return true;
89 }
90 }
91 return false;
92 }
Anna Zaks0353aad2012-09-29 00:20:38 +000093 };
94
Anna Zaks0aeb60d2013-01-10 20:59:51 +000095 typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
Anna Zaks0353aad2012-09-29 00:20:38 +000096
Anna Zaks9802f9f2012-09-26 18:55:16 +000097 /// Statement visitor, which walks the method body and flags the ivars
98 /// referenced in it (either directly or via property).
99 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
Anna Zaks9802f9f2012-09-26 18:55:16 +0000100 /// The set of Ivars which need to be invalidated.
101 IvarSet &IVars;
102
Anna Zaks0353aad2012-09-29 00:20:38 +0000103 /// Flag is set as the result of a message send to another
104 /// invalidation method.
105 bool &CalledAnotherInvalidationMethod;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000106
Anna Zaks0353aad2012-09-29 00:20:38 +0000107 /// Property setter to ivar mapping.
108 const MethToIvarMapTy &PropertySetterToIvarMap;
109
110 /// Property getter to ivar mapping.
111 const MethToIvarMapTy &PropertyGetterToIvarMap;
112
113 /// Property to ivar mapping.
114 const PropToIvarMapTy &PropertyToIvarMap;
115
116 /// The invalidation method being currently processed.
117 const ObjCMethodDecl *InvalidationMethod;
118
Anna Zaksbfacf172012-10-01 20:33:58 +0000119 ASTContext &Ctx;
120
121 /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
Anna Zaks0353aad2012-09-29 00:20:38 +0000122 const Expr *peel(const Expr *E) const;
123
124 /// Does this expression represent zero: '0'?
125 bool isZero(const Expr *E) const;
126
127 /// Mark the given ivar as invalidated.
128 void markInvalidated(const ObjCIvarDecl *Iv);
129
130 /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
131 /// invalidated.
132 void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
133
134 /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
135 /// it as invalidated.
136 void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
137
138 /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
139 /// if yes, marks it as invalidated.
140 void checkObjCMessageExpr(const ObjCMessageExpr *ME);
141
142 /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
143 void check(const Expr *E);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000144
145 public:
Anna Zaks97c7ce32012-10-01 20:34:04 +0000146 MethodCrawler(IvarSet &InIVars,
Anna Zaks0353aad2012-09-29 00:20:38 +0000147 bool &InCalledAnotherInvalidationMethod,
148 const MethToIvarMapTy &InPropertySetterToIvarMap,
149 const MethToIvarMapTy &InPropertyGetterToIvarMap,
Anna Zaksbfacf172012-10-01 20:33:58 +0000150 const PropToIvarMapTy &InPropertyToIvarMap,
151 ASTContext &InCtx)
Anna Zaks97c7ce32012-10-01 20:34:04 +0000152 : IVars(InIVars),
Anna Zaks0353aad2012-09-29 00:20:38 +0000153 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
154 PropertySetterToIvarMap(InPropertySetterToIvarMap),
155 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
156 PropertyToIvarMap(InPropertyToIvarMap),
Craig Topper0dbb7832014-05-27 02:45:47 +0000157 InvalidationMethod(nullptr),
Anna Zaksbfacf172012-10-01 20:33:58 +0000158 Ctx(InCtx) {}
Anna Zaks9802f9f2012-09-26 18:55:16 +0000159
160 void VisitStmt(const Stmt *S) { VisitChildren(S); }
161
Anna Zaks0353aad2012-09-29 00:20:38 +0000162 void VisitBinaryOperator(const BinaryOperator *BO);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000163
164 void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
165
Anna Zaks9802f9f2012-09-26 18:55:16 +0000166 void VisitChildren(const Stmt *S) {
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000167 for (const auto *Child : S->children()) {
Benjamin Kramer642f1732015-07-02 21:03:14 +0000168 if (Child)
169 this->Visit(Child);
Anna Zaks0353aad2012-09-29 00:20:38 +0000170 if (CalledAnotherInvalidationMethod)
171 return;
172 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000173 }
174 };
175
176 /// Check if the any of the methods inside the interface are annotated with
Anna Zaks0353aad2012-09-29 00:20:38 +0000177 /// the invalidation annotation, update the IvarInfo accordingly.
Anna Zaksa5096f62013-02-08 23:55:43 +0000178 /// \param LookForPartial is set when we are searching for partial
179 /// invalidators.
Anna Zaks0353aad2012-09-29 00:20:38 +0000180 static void containsInvalidationMethod(const ObjCContainerDecl *D,
Anna Zaksa5096f62013-02-08 23:55:43 +0000181 InvalidationInfo &Out,
182 bool LookForPartial);
Anna Zaksa0c83312012-09-27 21:57:14 +0000183
184 /// Check if ivar should be tracked and add to TrackedIvars if positive.
185 /// Returns true if ivar should be tracked.
Anna Zaks640123d2013-01-10 22:44:16 +0000186 static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
187 const ObjCIvarDecl **FirstIvarDecl);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000188
189 /// Given the property declaration, and the list of tracked ivars, finds
190 /// the ivar backing the property when possible. Returns '0' when no such
191 /// ivar could be found.
192 static const ObjCIvarDecl *findPropertyBackingIvar(
193 const ObjCPropertyDecl *Prop,
194 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks640123d2013-01-10 22:44:16 +0000195 IvarSet &TrackedIvars,
196 const ObjCIvarDecl **FirstIvarDecl);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000197
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000198 /// Print ivar name or the property if the given ivar backs a property.
199 static void printIvar(llvm::raw_svector_ostream &os,
200 const ObjCIvarDecl *IvarDecl,
Anna Zaks470543b2013-02-08 23:55:45 +0000201 const IvarToPropMapTy &IvarToPopertyMap);
202
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000203 void reportNoInvalidationMethod(CheckName CheckName,
204 const ObjCIvarDecl *FirstIvarDecl,
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000205 const IvarToPropMapTy &IvarToPopertyMap,
206 const ObjCInterfaceDecl *InterfaceD,
207 bool MissingDeclaration) const;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000208
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000209 void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
210 const IvarToPropMapTy &IvarToPopertyMap,
211 const ObjCMethodDecl *MethodD) const;
Anna Zaks470543b2013-02-08 23:55:45 +0000212
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000213 AnalysisManager& Mgr;
214 BugReporter &BR;
215 /// Filter on the checks performed.
216 const ChecksFilter &Filter;
Anna Zaksa5096f62013-02-08 23:55:43 +0000217
Anna Zaks9802f9f2012-09-26 18:55:16 +0000218public:
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000219 IvarInvalidationCheckerImpl(AnalysisManager& InMgr,
220 BugReporter &InBR,
221 const ChecksFilter &InFilter) :
222 Mgr (InMgr), BR(InBR), Filter(InFilter) {}
223
224 void visit(const ObjCImplementationDecl *D) const;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000225};
226
Anna Zaksa5096f62013-02-08 23:55:43 +0000227static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) {
Aaron Ballmanbe22bcb2014-03-10 17:08:28 +0000228 for (const auto *Ann : M->specific_attrs<AnnotateAttr>()) {
Anna Zaksa5096f62013-02-08 23:55:43 +0000229 if (!LookForPartial &&
230 Ann->getAnnotation() == "objc_instance_variable_invalidator")
231 return true;
232 if (LookForPartial &&
233 Ann->getAnnotation() == "objc_instance_variable_invalidator_partial")
Anna Zaks8c0dd362012-09-27 19:45:08 +0000234 return true;
235 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000236 return false;
237}
238
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000239void IvarInvalidationCheckerImpl::containsInvalidationMethod(
Anna Zaksa5096f62013-02-08 23:55:43 +0000240 const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) {
Anna Zaks9802f9f2012-09-26 18:55:16 +0000241
242 if (!D)
Anna Zaks0353aad2012-09-29 00:20:38 +0000243 return;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000244
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000245 assert(!isa<ObjCImplementationDecl>(D));
246 // TODO: Cache the results.
247
Anna Zaks9802f9f2012-09-26 18:55:16 +0000248 // Check all methods.
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000249 for (const auto *MDI : D->methods())
250 if (isInvalidationMethod(MDI, Partial))
251 OutInfo.addInvalidationMethod(
252 cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
Anna Zaks9802f9f2012-09-26 18:55:16 +0000253
254 // If interface, check all parent protocols and super.
Anna Zaks2975cf22013-01-11 03:52:37 +0000255 if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
256
257 // Visit all protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000258 for (const auto *I : InterfD->protocols())
259 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
Anna Zaks2975cf22013-01-11 03:52:37 +0000260
261 // Visit all categories in case the invalidation method is declared in
262 // a category.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +0000263 for (const auto *Ext : InterfD->visible_extensions())
264 containsInvalidationMethod(Ext, OutInfo, Partial);
Anna Zaks2975cf22013-01-11 03:52:37 +0000265
Anna Zaksa5096f62013-02-08 23:55:43 +0000266 containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
Anna Zaks0353aad2012-09-29 00:20:38 +0000267 return;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000268 }
269
270 // If protocol, check all parent protocols.
271 if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000272 for (const auto *I : ProtD->protocols()) {
273 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000274 }
Anna Zaks0353aad2012-09-29 00:20:38 +0000275 return;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000276 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000277}
278
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000279bool IvarInvalidationCheckerImpl::trackIvar(const ObjCIvarDecl *Iv,
Anna Zaks640123d2013-01-10 22:44:16 +0000280 IvarSet &TrackedIvars,
281 const ObjCIvarDecl **FirstIvarDecl) {
Anna Zaksa0c83312012-09-27 21:57:14 +0000282 QualType IvQTy = Iv->getType();
283 const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
284 if (!IvTy)
285 return false;
286 const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
Anna Zaks0353aad2012-09-29 00:20:38 +0000287
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000288 InvalidationInfo Info;
Anna Zaksa5096f62013-02-08 23:55:43 +0000289 containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false);
Anna Zaks0353aad2012-09-29 00:20:38 +0000290 if (Info.needsInvalidation()) {
Anna Zaks640123d2013-01-10 22:44:16 +0000291 const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
292 TrackedIvars[I] = Info;
293 if (!*FirstIvarDecl)
294 *FirstIvarDecl = I;
Anna Zaksa0c83312012-09-27 21:57:14 +0000295 return true;
296 }
297 return false;
298}
299
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000300const ObjCIvarDecl *IvarInvalidationCheckerImpl::findPropertyBackingIvar(
Anna Zaks9802f9f2012-09-26 18:55:16 +0000301 const ObjCPropertyDecl *Prop,
302 const ObjCInterfaceDecl *InterfaceD,
Anna Zaks640123d2013-01-10 22:44:16 +0000303 IvarSet &TrackedIvars,
304 const ObjCIvarDecl **FirstIvarDecl) {
Craig Topper0dbb7832014-05-27 02:45:47 +0000305 const ObjCIvarDecl *IvarD = nullptr;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000306
307 // Lookup for the synthesized case.
308 IvarD = Prop->getPropertyIvarDecl();
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000309 // We only track the ivars/properties that are defined in the current
Anna Zaks5f376432013-01-07 19:12:56 +0000310 // class (not the parent).
311 if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
Anna Zaksa0c83312012-09-27 21:57:14 +0000312 if (TrackedIvars.count(IvarD)) {
313 return IvarD;
314 }
315 // If the ivar is synthesized we still want to track it.
Anna Zaks640123d2013-01-10 22:44:16 +0000316 if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
Anna Zaksa0c83312012-09-27 21:57:14 +0000317 return IvarD;
318 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000319
320 // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
321 StringRef PropName = Prop->getIdentifier()->getName();
322 for (IvarSet::const_iterator I = TrackedIvars.begin(),
323 E = TrackedIvars.end(); I != E; ++I) {
324 const ObjCIvarDecl *Iv = I->first;
325 StringRef IvarName = Iv->getName();
326
327 if (IvarName == PropName)
328 return Iv;
329
330 SmallString<128> PropNameWithUnderscore;
331 {
332 llvm::raw_svector_ostream os(PropNameWithUnderscore);
333 os << '_' << PropName;
334 }
Yaron Keren92e1b622015-03-18 10:17:07 +0000335 if (IvarName == PropNameWithUnderscore)
Anna Zaks9802f9f2012-09-26 18:55:16 +0000336 return Iv;
337 }
338
339 // Note, this is a possible source of false positives. We could look at the
340 // getter implementation to find the ivar when its name is not derived from
341 // the property name.
Craig Topper0dbb7832014-05-27 02:45:47 +0000342 return nullptr;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000343}
344
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000345void IvarInvalidationCheckerImpl::printIvar(llvm::raw_svector_ostream &os,
Anna Zaks470543b2013-02-08 23:55:45 +0000346 const ObjCIvarDecl *IvarDecl,
347 const IvarToPropMapTy &IvarToPopertyMap) {
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000348 if (IvarDecl->getSynthesize()) {
Anna Zaks470543b2013-02-08 23:55:45 +0000349 const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000350 assert(PD &&"Do we synthesize ivars for something other than properties?");
351 os << "Property "<< PD->getName() << " ";
352 } else {
353 os << "Instance variable "<< IvarDecl->getName() << " ";
354 }
355}
356
357// Check that the invalidatable interfaces with ivars/properties implement the
358// invalidation methods.
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000359void IvarInvalidationCheckerImpl::
360visit(const ObjCImplementationDecl *ImplD) const {
Anna Zaks9802f9f2012-09-26 18:55:16 +0000361 // Collect all ivars that need cleanup.
362 IvarSet Ivars;
Anna Zaks640123d2013-01-10 22:44:16 +0000363 // Record the first Ivar needing invalidation; used in reporting when only
364 // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
365 // deterministic output.
Craig Topper0dbb7832014-05-27 02:45:47 +0000366 const ObjCIvarDecl *FirstIvarDecl = nullptr;
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000367 const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
Anna Zaksb642fc52012-10-16 19:36:37 +0000368
369 // Collect ivars declared in this class, its extensions and its implementation
370 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
371 for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
372 Iv= Iv->getNextIvar())
Anna Zaks640123d2013-01-10 22:44:16 +0000373 trackIvar(Iv, Ivars, &FirstIvarDecl);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000374
Anna Zaksa0c83312012-09-27 21:57:14 +0000375 // Construct Property/Property Accessor to Ivar maps to assist checking if an
Anna Zaks9802f9f2012-09-26 18:55:16 +0000376 // ivar which is backing a property has been reset.
Anna Zaks0353aad2012-09-29 00:20:38 +0000377 MethToIvarMapTy PropSetterToIvarMap;
378 MethToIvarMapTy PropGetterToIvarMap;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000379 PropToIvarMapTy PropertyToIvarMap;
Anna Zaksa0c83312012-09-27 21:57:14 +0000380 IvarToPropMapTy IvarToPopertyMap;
Anna Zaks92898a72012-10-18 19:17:57 +0000381
382 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +0000383 ObjCInterfaceDecl::PropertyDeclOrder PropOrder;
384 InterfaceD->collectPropertiesToImplement(PropMap, PropOrder);
Anna Zaks92898a72012-10-18 19:17:57 +0000385
386 for (ObjCInterfaceDecl::PropertyMap::iterator
387 I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
388 const ObjCPropertyDecl *PD = I->second;
Manman Ren494ee5b2016-01-28 23:36:05 +0000389 if (PD->isClassProperty())
390 continue;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000391
Anna Zaks640123d2013-01-10 22:44:16 +0000392 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
393 &FirstIvarDecl);
Anna Zaks470543b2013-02-08 23:55:45 +0000394 if (!ID)
Anna Zaks9802f9f2012-09-26 18:55:16 +0000395 continue;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000396
397 // Store the mappings.
398 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks9802f9f2012-09-26 18:55:16 +0000399 PropertyToIvarMap[PD] = ID;
Anna Zaksa0c83312012-09-27 21:57:14 +0000400 IvarToPopertyMap[ID] = PD;
401
402 // Find the setter and the getter.
403 const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
404 if (SetterD) {
405 SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
Anna Zaks0353aad2012-09-29 00:20:38 +0000406 PropSetterToIvarMap[SetterD] = ID;
Anna Zaksa0c83312012-09-27 21:57:14 +0000407 }
408
409 const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
410 if (GetterD) {
411 GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
Anna Zaks0353aad2012-09-29 00:20:38 +0000412 PropGetterToIvarMap[GetterD] = ID;
Anna Zaksa0c83312012-09-27 21:57:14 +0000413 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000414 }
415
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000416 // If no ivars need invalidation, there is nothing to check here.
417 if (Ivars.empty())
Anna Zaks0353aad2012-09-29 00:20:38 +0000418 return;
Anna Zaks9802f9f2012-09-26 18:55:16 +0000419
Anna Zaksa5096f62013-02-08 23:55:43 +0000420 // Find all partial invalidation methods.
421 InvalidationInfo PartialInfo;
422 containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true);
423
424 // Remove ivars invalidated by the partial invalidation methods. They do not
425 // need to be invalidated in the regular invalidation methods.
Anna Zaks7712f382013-04-24 02:49:16 +0000426 bool AtImplementationContainsAtLeastOnePartialInvalidationMethod = false;
Anna Zaksa5096f62013-02-08 23:55:43 +0000427 for (MethodSet::iterator
428 I = PartialInfo.InvalidationMethods.begin(),
429 E = PartialInfo.InvalidationMethods.end(); I != E; ++I) {
430 const ObjCMethodDecl *InterfD = *I;
431
432 // Get the corresponding method in the @implementation.
433 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
434 InterfD->isInstanceMethod());
435 if (D && D->hasBody()) {
Anna Zaks7712f382013-04-24 02:49:16 +0000436 AtImplementationContainsAtLeastOnePartialInvalidationMethod = true;
437
Anna Zaksa5096f62013-02-08 23:55:43 +0000438 bool CalledAnotherInvalidationMethod = false;
439 // The MethodCrowler is going to remove the invalidated ivars.
440 MethodCrawler(Ivars,
441 CalledAnotherInvalidationMethod,
442 PropSetterToIvarMap,
443 PropGetterToIvarMap,
444 PropertyToIvarMap,
445 BR.getContext()).VisitStmt(D->getBody());
446 // If another invalidation method was called, trust that full invalidation
447 // has occurred.
448 if (CalledAnotherInvalidationMethod)
449 Ivars.clear();
450 }
451 }
452
453 // If all ivars have been invalidated by partial invalidators, there is
454 // nothing to check here.
455 if (Ivars.empty())
456 return;
457
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000458 // Find all invalidation methods in this @interface declaration and parents.
459 InvalidationInfo Info;
Anna Zaksa5096f62013-02-08 23:55:43 +0000460 containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000461
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000462 // Report an error in case none of the invalidation methods are declared.
Anna Zaks7712f382013-04-24 02:49:16 +0000463 if (!Info.needsInvalidation() && !PartialInfo.needsInvalidation()) {
Anna Zaks7811c3e2013-02-09 01:09:27 +0000464 if (Filter.check_MissingInvalidationMethod)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000465 reportNoInvalidationMethod(Filter.checkName_MissingInvalidationMethod,
466 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
Anna Zaks7811c3e2013-02-09 01:09:27 +0000467 /*MissingDeclaration*/ true);
468 // If there are no invalidation methods, there is no ivar validation work
469 // to be done.
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000470 return;
471 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000472
Anna Zaks7811c3e2013-02-09 01:09:27 +0000473 // Only check if Ivars are invalidated when InstanceVariableInvalidation
474 // has been requested.
475 if (!Filter.check_InstanceVariableInvalidation)
476 return;
477
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000478 // Check that all ivars are invalidated by the invalidation methods.
479 bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
480 for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
481 E = Info.InvalidationMethods.end(); I != E; ++I) {
482 const ObjCMethodDecl *InterfD = *I;
483
484 // Get the corresponding method in the @implementation.
485 const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
486 InterfD->isInstanceMethod());
487 if (D && D->hasBody()) {
488 AtImplementationContainsAtLeastOneInvalidationMethod = true;
489
490 // Get a copy of ivars needing invalidation.
491 IvarSet IvarsI = Ivars;
492
493 bool CalledAnotherInvalidationMethod = false;
494 MethodCrawler(IvarsI,
495 CalledAnotherInvalidationMethod,
496 PropSetterToIvarMap,
497 PropGetterToIvarMap,
498 PropertyToIvarMap,
499 BR.getContext()).VisitStmt(D->getBody());
500 // If another invalidation method was called, trust that full invalidation
501 // has occurred.
502 if (CalledAnotherInvalidationMethod)
503 continue;
504
505 // Warn on the ivars that were not invalidated by the method.
Anna Zaks470543b2013-02-08 23:55:45 +0000506 for (IvarSet::const_iterator
507 I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000508 reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000509 }
510 }
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000511
512 // Report an error in case none of the invalidation methods are implemented.
Anna Zaks7712f382013-04-24 02:49:16 +0000513 if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
514 if (AtImplementationContainsAtLeastOnePartialInvalidationMethod) {
515 // Warn on the ivars that were not invalidated by the prrtial
516 // invalidation methods.
517 for (IvarSet::const_iterator
518 I = Ivars.begin(), E = Ivars.end(); I != E; ++I)
Craig Topper0dbb7832014-05-27 02:45:47 +0000519 reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, nullptr);
Anna Zaks7712f382013-04-24 02:49:16 +0000520 } else {
521 // Otherwise, no invalidation methods were implemented.
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000522 reportNoInvalidationMethod(Filter.checkName_InstanceVariableInvalidation,
523 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
Anna Zaks7712f382013-04-24 02:49:16 +0000524 /*MissingDeclaration*/ false);
525 }
526 }
Anna Zaks470543b2013-02-08 23:55:45 +0000527}
Anna Zaks0aeb60d2013-01-10 20:59:51 +0000528
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000529void IvarInvalidationCheckerImpl::reportNoInvalidationMethod(
530 CheckName CheckName, const ObjCIvarDecl *FirstIvarDecl,
531 const IvarToPropMapTy &IvarToPopertyMap,
532 const ObjCInterfaceDecl *InterfaceD, bool MissingDeclaration) const {
Anna Zaks470543b2013-02-08 23:55:45 +0000533 SmallString<128> sbuf;
534 llvm::raw_svector_ostream os(sbuf);
535 assert(FirstIvarDecl);
536 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
537 os << "needs to be invalidated; ";
538 if (MissingDeclaration)
539 os << "no invalidation method is declared for ";
540 else
541 os << "no invalidation method is defined in the @implementation for ";
542 os << InterfaceD->getName();
543
544 PathDiagnosticLocation IvarDecLocation =
545 PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
546
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000547 BR.EmitBasicReport(FirstIvarDecl, CheckName, "Incomplete invalidation",
Anna Zaks470543b2013-02-08 23:55:45 +0000548 categories::CoreFoundationObjectiveC, os.str(),
549 IvarDecLocation);
550}
551
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000552void IvarInvalidationCheckerImpl::
Anna Zaks470543b2013-02-08 23:55:45 +0000553reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
Anna Zaks7712f382013-04-24 02:49:16 +0000554 const IvarToPropMapTy &IvarToPopertyMap,
555 const ObjCMethodDecl *MethodD) const {
Anna Zaks470543b2013-02-08 23:55:45 +0000556 SmallString<128> sbuf;
557 llvm::raw_svector_ostream os(sbuf);
558 printIvar(os, IvarD, IvarToPopertyMap);
559 os << "needs to be invalidated or set to nil";
Anna Zaks7712f382013-04-24 02:49:16 +0000560 if (MethodD) {
561 PathDiagnosticLocation MethodDecLocation =
562 PathDiagnosticLocation::createEnd(MethodD->getBody(),
563 BR.getSourceManager(),
564 Mgr.getAnalysisDeclContext(MethodD));
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000565 BR.EmitBasicReport(MethodD, Filter.checkName_InstanceVariableInvalidation,
566 "Incomplete invalidation",
Anna Zaks7712f382013-04-24 02:49:16 +0000567 categories::CoreFoundationObjectiveC, os.str(),
568 MethodDecLocation);
569 } else {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000570 BR.EmitBasicReport(
571 IvarD, Filter.checkName_InstanceVariableInvalidation,
572 "Incomplete invalidation", categories::CoreFoundationObjectiveC,
573 os.str(),
574 PathDiagnosticLocation::createBegin(IvarD, BR.getSourceManager()));
Anna Zaks7712f382013-04-24 02:49:16 +0000575 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000576}
577
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000578void IvarInvalidationCheckerImpl::MethodCrawler::markInvalidated(
Anna Zaks0353aad2012-09-29 00:20:38 +0000579 const ObjCIvarDecl *Iv) {
580 IvarSet::iterator I = IVars.find(Iv);
581 if (I != IVars.end()) {
582 // If InvalidationMethod is present, we are processing the message send and
583 // should ensure we are invalidating with the appropriate method,
584 // otherwise, we are processing setting to 'nil'.
Anna Zaksa5096f62013-02-08 23:55:43 +0000585 if (!InvalidationMethod ||
586 (InvalidationMethod && I->second.hasMethod(InvalidationMethod)))
587 IVars.erase(I);
Anna Zaks0353aad2012-09-29 00:20:38 +0000588 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000589}
590
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000591const Expr *IvarInvalidationCheckerImpl::MethodCrawler::peel(const Expr *E) const {
Anna Zaks0353aad2012-09-29 00:20:38 +0000592 E = E->IgnoreParenCasts();
593 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
594 E = POE->getSyntacticForm()->IgnoreParenCasts();
595 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
596 E = OVE->getSourceExpr()->IgnoreParenCasts();
597 return E;
598}
Anna Zaks9802f9f2012-09-26 18:55:16 +0000599
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000600void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCIvarRefExpr(
Anna Zaks0353aad2012-09-29 00:20:38 +0000601 const ObjCIvarRefExpr *IvarRef) {
602 if (const Decl *D = IvarRef->getDecl())
603 markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
604}
605
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000606void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCMessageExpr(
Anna Zaks9802f9f2012-09-26 18:55:16 +0000607 const ObjCMessageExpr *ME) {
608 const ObjCMethodDecl *MD = ME->getMethodDecl();
609 if (MD) {
610 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks0353aad2012-09-29 00:20:38 +0000611 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
612 if (IvI != PropertyGetterToIvarMap.end())
613 markInvalidated(IvI->second);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000614 }
Anna Zaks9802f9f2012-09-26 18:55:16 +0000615}
616
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000617void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCPropertyRefExpr(
Anna Zaks9802f9f2012-09-26 18:55:16 +0000618 const ObjCPropertyRefExpr *PA) {
619
620 if (PA->isExplicitProperty()) {
621 const ObjCPropertyDecl *PD = PA->getExplicitProperty();
622 if (PD) {
623 PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
Anna Zaks0353aad2012-09-29 00:20:38 +0000624 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
625 if (IvI != PropertyToIvarMap.end())
626 markInvalidated(IvI->second);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000627 return;
628 }
629 }
630
631 if (PA->isImplicitProperty()) {
632 const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
633 if (MD) {
634 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
Anna Zaks0353aad2012-09-29 00:20:38 +0000635 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
636 if (IvI != PropertyGetterToIvarMap.end())
637 markInvalidated(IvI->second);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000638 return;
639 }
640 }
Anna Zaks0353aad2012-09-29 00:20:38 +0000641}
642
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000643bool IvarInvalidationCheckerImpl::MethodCrawler::isZero(const Expr *E) const {
Anna Zaks0353aad2012-09-29 00:20:38 +0000644 E = peel(E);
Anna Zaks0353aad2012-09-29 00:20:38 +0000645
Anna Zaksbfacf172012-10-01 20:33:58 +0000646 return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
647 != Expr::NPCK_NotNull);
Anna Zaks0353aad2012-09-29 00:20:38 +0000648}
649
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000650void IvarInvalidationCheckerImpl::MethodCrawler::check(const Expr *E) {
Anna Zaks0353aad2012-09-29 00:20:38 +0000651 E = peel(E);
652
Anna Zaks0353aad2012-09-29 00:20:38 +0000653 if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
654 checkObjCIvarRefExpr(IvarRef);
655 return;
656 }
657
658 if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
659 checkObjCPropertyRefExpr(PropRef);
660 return;
661 }
662
663 if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
664 checkObjCMessageExpr(MsgExpr);
665 return;
666 }
667}
668
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000669void IvarInvalidationCheckerImpl::MethodCrawler::VisitBinaryOperator(
Anna Zaks0353aad2012-09-29 00:20:38 +0000670 const BinaryOperator *BO) {
Anna Zaksbfacf172012-10-01 20:33:58 +0000671 VisitStmt(BO);
672
Anna Zaksa96a9ef2013-01-10 23:34:16 +0000673 // Do we assign/compare against zero? If yes, check the variable we are
674 // assigning to.
675 BinaryOperatorKind Opcode = BO->getOpcode();
676 if (Opcode != BO_Assign &&
677 Opcode != BO_EQ &&
678 Opcode != BO_NE)
Anna Zaks0353aad2012-09-29 00:20:38 +0000679 return;
680
Anna Zaksa96a9ef2013-01-10 23:34:16 +0000681 if (isZero(BO->getRHS())) {
682 check(BO->getLHS());
683 return;
684 }
Anna Zaks0353aad2012-09-29 00:20:38 +0000685
Anna Zaksa96a9ef2013-01-10 23:34:16 +0000686 if (Opcode != BO_Assign && isZero(BO->getLHS())) {
687 check(BO->getRHS());
688 return;
689 }
Anna Zaks0353aad2012-09-29 00:20:38 +0000690}
691
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000692void IvarInvalidationCheckerImpl::MethodCrawler::VisitObjCMessageExpr(
Anna Zaks640123d2013-01-10 22:44:16 +0000693 const ObjCMessageExpr *ME) {
Anna Zaks0353aad2012-09-29 00:20:38 +0000694 const ObjCMethodDecl *MD = ME->getMethodDecl();
695 const Expr *Receiver = ME->getInstanceReceiver();
696
697 // Stop if we are calling '[self invalidate]'.
Anna Zaksa5096f62013-02-08 23:55:43 +0000698 if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false))
Anna Zaks97c7ce32012-10-01 20:34:04 +0000699 if (Receiver->isObjCSelfExpr()) {
700 CalledAnotherInvalidationMethod = true;
701 return;
Anna Zaks0353aad2012-09-29 00:20:38 +0000702 }
703
704 // Check if we call a setter and set the property to 'nil'.
705 if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
706 MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
707 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
708 if (IvI != PropertySetterToIvarMap.end()) {
709 markInvalidated(IvI->second);
710 return;
711 }
712 }
713
714 // Check if we call the 'invalidation' routine on the ivar.
715 if (Receiver) {
716 InvalidationMethod = MD;
717 check(Receiver->IgnoreParenCasts());
Craig Topper0dbb7832014-05-27 02:45:47 +0000718 InvalidationMethod = nullptr;
Anna Zaks0353aad2012-09-29 00:20:38 +0000719 }
720
721 VisitStmt(ME);
Anna Zaks9802f9f2012-09-26 18:55:16 +0000722}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000723} // end anonymous namespace
Anna Zaks9802f9f2012-09-26 18:55:16 +0000724
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000725// Register the checkers.
726namespace {
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000727class IvarInvalidationChecker :
728 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
729public:
730 ChecksFilter Filter;
731public:
732 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
733 BugReporter &BR) const {
734 IvarInvalidationCheckerImpl Walker(Mgr, BR, Filter);
735 Walker.visit(D);
736 }
737};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000738} // end anonymous namespace
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000739
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000740#define REGISTER_CHECKER(name) \
741 void ento::register##name(CheckerManager &mgr) { \
742 IvarInvalidationChecker *checker = \
743 mgr.registerChecker<IvarInvalidationChecker>(); \
744 checker->Filter.check_##name = true; \
745 checker->Filter.checkName_##name = mgr.getCurrentCheckName(); \
746 }
Anna Zaks91a5fdf2013-02-08 23:55:47 +0000747
748REGISTER_CHECKER(InstanceVariableInvalidation)
749REGISTER_CHECKER(MissingInvalidationMethod)