1f4a2713aSLionel Sambuc //=- IvarInvalidationChecker.cpp - -*- C++ -------------------------------*-==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This checker implements annotation driven invalidation checking. If a class
11f4a2713aSLionel Sambuc // contains a method annotated with 'objc_instance_variable_invalidator',
12f4a2713aSLionel Sambuc // - (void) foo
13f4a2713aSLionel Sambuc // __attribute__((annotate("objc_instance_variable_invalidator")));
14f4a2713aSLionel Sambuc // all the "ivalidatable" instance variables of this class should be
15f4a2713aSLionel Sambuc // invalidated. We call an instance variable ivalidatable if it is an object of
16f4a2713aSLionel Sambuc // a class which contains an invalidation method. There could be multiple
17f4a2713aSLionel Sambuc // methods annotated with such annotations per class, either one can be used
18f4a2713aSLionel Sambuc // to invalidate the ivar. An ivar or property are considered to be
19f4a2713aSLionel Sambuc // invalidated if they are being assigned 'nil' or an invalidation method has
20f4a2713aSLionel Sambuc // been called on them. An invalidation method should either invalidate all
21f4a2713aSLionel Sambuc // the ivars or call another invalidation method (on self).
22f4a2713aSLionel Sambuc //
23f4a2713aSLionel Sambuc // Partial invalidor annotation allows to addess cases when ivars are
24f4a2713aSLionel Sambuc // invalidated by other methods, which might or might not be called from
25f4a2713aSLionel Sambuc // the invalidation method. The checker checks that each invalidation
26f4a2713aSLionel Sambuc // method and all the partial methods cumulatively invalidate all ivars.
27f4a2713aSLionel Sambuc // __attribute__((annotate("objc_instance_variable_invalidator_partial")));
28f4a2713aSLionel Sambuc //
29f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc #include "ClangSACheckers.h"
32f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
33f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
34f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
35f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
36f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
37f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
38f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
39f4a2713aSLionel Sambuc #include "llvm/ADT/SetVector.h"
40f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc using namespace clang;
43f4a2713aSLionel Sambuc using namespace ento;
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc namespace {
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc struct ChecksFilter {
48f4a2713aSLionel Sambuc /// Check for missing invalidation method declarations.
49f4a2713aSLionel Sambuc DefaultBool check_MissingInvalidationMethod;
50f4a2713aSLionel Sambuc /// Check that all ivars are invalidated.
51f4a2713aSLionel Sambuc DefaultBool check_InstanceVariableInvalidation;
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc CheckName checkName_MissingInvalidationMethod;
54*0a6a1f1dSLionel Sambuc CheckName checkName_InstanceVariableInvalidation;
55f4a2713aSLionel Sambuc };
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc class IvarInvalidationCheckerImpl {
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
60f4a2713aSLionel Sambuc typedef llvm::DenseMap<const ObjCMethodDecl*,
61f4a2713aSLionel Sambuc const ObjCIvarDecl*> MethToIvarMapTy;
62f4a2713aSLionel Sambuc typedef llvm::DenseMap<const ObjCPropertyDecl*,
63f4a2713aSLionel Sambuc const ObjCIvarDecl*> PropToIvarMapTy;
64f4a2713aSLionel Sambuc typedef llvm::DenseMap<const ObjCIvarDecl*,
65f4a2713aSLionel Sambuc const ObjCPropertyDecl*> IvarToPropMapTy;
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc struct InvalidationInfo {
69f4a2713aSLionel Sambuc /// Has the ivar been invalidated?
70f4a2713aSLionel Sambuc bool IsInvalidated;
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc /// The methods which can be used to invalidate the ivar.
73f4a2713aSLionel Sambuc MethodSet InvalidationMethods;
74f4a2713aSLionel Sambuc
InvalidationInfo__anon5c2bd96a0111::IvarInvalidationCheckerImpl::InvalidationInfo75f4a2713aSLionel Sambuc InvalidationInfo() : IsInvalidated(false) {}
addInvalidationMethod__anon5c2bd96a0111::IvarInvalidationCheckerImpl::InvalidationInfo76f4a2713aSLionel Sambuc void addInvalidationMethod(const ObjCMethodDecl *MD) {
77f4a2713aSLionel Sambuc InvalidationMethods.insert(MD);
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
needsInvalidation__anon5c2bd96a0111::IvarInvalidationCheckerImpl::InvalidationInfo80f4a2713aSLionel Sambuc bool needsInvalidation() const {
81f4a2713aSLionel Sambuc return !InvalidationMethods.empty();
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc
hasMethod__anon5c2bd96a0111::IvarInvalidationCheckerImpl::InvalidationInfo84f4a2713aSLionel Sambuc bool hasMethod(const ObjCMethodDecl *MD) {
85f4a2713aSLionel Sambuc if (IsInvalidated)
86f4a2713aSLionel Sambuc return true;
87f4a2713aSLionel Sambuc for (MethodSet::iterator I = InvalidationMethods.begin(),
88f4a2713aSLionel Sambuc E = InvalidationMethods.end(); I != E; ++I) {
89f4a2713aSLionel Sambuc if (*I == MD) {
90f4a2713aSLionel Sambuc IsInvalidated = true;
91f4a2713aSLionel Sambuc return true;
92f4a2713aSLionel Sambuc }
93f4a2713aSLionel Sambuc }
94f4a2713aSLionel Sambuc return false;
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc };
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
99f4a2713aSLionel Sambuc
100f4a2713aSLionel Sambuc /// Statement visitor, which walks the method body and flags the ivars
101f4a2713aSLionel Sambuc /// referenced in it (either directly or via property).
102f4a2713aSLionel Sambuc class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
103f4a2713aSLionel Sambuc /// The set of Ivars which need to be invalidated.
104f4a2713aSLionel Sambuc IvarSet &IVars;
105f4a2713aSLionel Sambuc
106f4a2713aSLionel Sambuc /// Flag is set as the result of a message send to another
107f4a2713aSLionel Sambuc /// invalidation method.
108f4a2713aSLionel Sambuc bool &CalledAnotherInvalidationMethod;
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc /// Property setter to ivar mapping.
111f4a2713aSLionel Sambuc const MethToIvarMapTy &PropertySetterToIvarMap;
112f4a2713aSLionel Sambuc
113f4a2713aSLionel Sambuc /// Property getter to ivar mapping.
114f4a2713aSLionel Sambuc const MethToIvarMapTy &PropertyGetterToIvarMap;
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc /// Property to ivar mapping.
117f4a2713aSLionel Sambuc const PropToIvarMapTy &PropertyToIvarMap;
118f4a2713aSLionel Sambuc
119f4a2713aSLionel Sambuc /// The invalidation method being currently processed.
120f4a2713aSLionel Sambuc const ObjCMethodDecl *InvalidationMethod;
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc ASTContext &Ctx;
123f4a2713aSLionel Sambuc
124f4a2713aSLionel Sambuc /// Peel off parens, casts, OpaqueValueExpr, and PseudoObjectExpr.
125f4a2713aSLionel Sambuc const Expr *peel(const Expr *E) const;
126f4a2713aSLionel Sambuc
127f4a2713aSLionel Sambuc /// Does this expression represent zero: '0'?
128f4a2713aSLionel Sambuc bool isZero(const Expr *E) const;
129f4a2713aSLionel Sambuc
130f4a2713aSLionel Sambuc /// Mark the given ivar as invalidated.
131f4a2713aSLionel Sambuc void markInvalidated(const ObjCIvarDecl *Iv);
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc /// Checks if IvarRef refers to the tracked IVar, if yes, marks it as
134f4a2713aSLionel Sambuc /// invalidated.
135f4a2713aSLionel Sambuc void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc /// Checks if ObjCPropertyRefExpr refers to the tracked IVar, if yes, marks
138f4a2713aSLionel Sambuc /// it as invalidated.
139f4a2713aSLionel Sambuc void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
140f4a2713aSLionel Sambuc
141f4a2713aSLionel Sambuc /// Checks if ObjCMessageExpr refers to (is a getter for) the tracked IVar,
142f4a2713aSLionel Sambuc /// if yes, marks it as invalidated.
143f4a2713aSLionel Sambuc void checkObjCMessageExpr(const ObjCMessageExpr *ME);
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc /// Checks if the Expr refers to an ivar, if yes, marks it as invalidated.
146f4a2713aSLionel Sambuc void check(const Expr *E);
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc public:
MethodCrawler(IvarSet & InIVars,bool & InCalledAnotherInvalidationMethod,const MethToIvarMapTy & InPropertySetterToIvarMap,const MethToIvarMapTy & InPropertyGetterToIvarMap,const PropToIvarMapTy & InPropertyToIvarMap,ASTContext & InCtx)149f4a2713aSLionel Sambuc MethodCrawler(IvarSet &InIVars,
150f4a2713aSLionel Sambuc bool &InCalledAnotherInvalidationMethod,
151f4a2713aSLionel Sambuc const MethToIvarMapTy &InPropertySetterToIvarMap,
152f4a2713aSLionel Sambuc const MethToIvarMapTy &InPropertyGetterToIvarMap,
153f4a2713aSLionel Sambuc const PropToIvarMapTy &InPropertyToIvarMap,
154f4a2713aSLionel Sambuc ASTContext &InCtx)
155f4a2713aSLionel Sambuc : IVars(InIVars),
156f4a2713aSLionel Sambuc CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
157f4a2713aSLionel Sambuc PropertySetterToIvarMap(InPropertySetterToIvarMap),
158f4a2713aSLionel Sambuc PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
159f4a2713aSLionel Sambuc PropertyToIvarMap(InPropertyToIvarMap),
160*0a6a1f1dSLionel Sambuc InvalidationMethod(nullptr),
161f4a2713aSLionel Sambuc Ctx(InCtx) {}
162f4a2713aSLionel Sambuc
VisitStmt(const Stmt * S)163f4a2713aSLionel Sambuc void VisitStmt(const Stmt *S) { VisitChildren(S); }
164f4a2713aSLionel Sambuc
165f4a2713aSLionel Sambuc void VisitBinaryOperator(const BinaryOperator *BO);
166f4a2713aSLionel Sambuc
167f4a2713aSLionel Sambuc void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
168f4a2713aSLionel Sambuc
VisitChildren(const Stmt * S)169f4a2713aSLionel Sambuc void VisitChildren(const Stmt *S) {
170f4a2713aSLionel Sambuc for (Stmt::const_child_range I = S->children(); I; ++I) {
171f4a2713aSLionel Sambuc if (*I)
172f4a2713aSLionel Sambuc this->Visit(*I);
173f4a2713aSLionel Sambuc if (CalledAnotherInvalidationMethod)
174f4a2713aSLionel Sambuc return;
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc };
178f4a2713aSLionel Sambuc
179f4a2713aSLionel Sambuc /// Check if the any of the methods inside the interface are annotated with
180f4a2713aSLionel Sambuc /// the invalidation annotation, update the IvarInfo accordingly.
181f4a2713aSLionel Sambuc /// \param LookForPartial is set when we are searching for partial
182f4a2713aSLionel Sambuc /// invalidators.
183f4a2713aSLionel Sambuc static void containsInvalidationMethod(const ObjCContainerDecl *D,
184f4a2713aSLionel Sambuc InvalidationInfo &Out,
185f4a2713aSLionel Sambuc bool LookForPartial);
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc /// Check if ivar should be tracked and add to TrackedIvars if positive.
188f4a2713aSLionel Sambuc /// Returns true if ivar should be tracked.
189f4a2713aSLionel Sambuc static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
190f4a2713aSLionel Sambuc const ObjCIvarDecl **FirstIvarDecl);
191f4a2713aSLionel Sambuc
192f4a2713aSLionel Sambuc /// Given the property declaration, and the list of tracked ivars, finds
193f4a2713aSLionel Sambuc /// the ivar backing the property when possible. Returns '0' when no such
194f4a2713aSLionel Sambuc /// ivar could be found.
195f4a2713aSLionel Sambuc static const ObjCIvarDecl *findPropertyBackingIvar(
196f4a2713aSLionel Sambuc const ObjCPropertyDecl *Prop,
197f4a2713aSLionel Sambuc const ObjCInterfaceDecl *InterfaceD,
198f4a2713aSLionel Sambuc IvarSet &TrackedIvars,
199f4a2713aSLionel Sambuc const ObjCIvarDecl **FirstIvarDecl);
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc /// Print ivar name or the property if the given ivar backs a property.
202f4a2713aSLionel Sambuc static void printIvar(llvm::raw_svector_ostream &os,
203f4a2713aSLionel Sambuc const ObjCIvarDecl *IvarDecl,
204f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap);
205f4a2713aSLionel Sambuc
206*0a6a1f1dSLionel Sambuc void reportNoInvalidationMethod(CheckName CheckName,
207*0a6a1f1dSLionel Sambuc const ObjCIvarDecl *FirstIvarDecl,
208f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap,
209f4a2713aSLionel Sambuc const ObjCInterfaceDecl *InterfaceD,
210f4a2713aSLionel Sambuc bool MissingDeclaration) const;
211f4a2713aSLionel Sambuc void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
212f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap,
213f4a2713aSLionel Sambuc const ObjCMethodDecl *MethodD) const;
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc AnalysisManager& Mgr;
216f4a2713aSLionel Sambuc BugReporter &BR;
217f4a2713aSLionel Sambuc /// Filter on the checks performed.
218f4a2713aSLionel Sambuc const ChecksFilter &Filter;
219f4a2713aSLionel Sambuc
220f4a2713aSLionel Sambuc public:
IvarInvalidationCheckerImpl(AnalysisManager & InMgr,BugReporter & InBR,const ChecksFilter & InFilter)221f4a2713aSLionel Sambuc IvarInvalidationCheckerImpl(AnalysisManager& InMgr,
222f4a2713aSLionel Sambuc BugReporter &InBR,
223f4a2713aSLionel Sambuc const ChecksFilter &InFilter) :
224f4a2713aSLionel Sambuc Mgr (InMgr), BR(InBR), Filter(InFilter) {}
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc void visit(const ObjCImplementationDecl *D) const;
227f4a2713aSLionel Sambuc };
228f4a2713aSLionel Sambuc
isInvalidationMethod(const ObjCMethodDecl * M,bool LookForPartial)229f4a2713aSLionel Sambuc static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) {
230*0a6a1f1dSLionel Sambuc for (const auto *Ann : M->specific_attrs<AnnotateAttr>()) {
231f4a2713aSLionel Sambuc if (!LookForPartial &&
232f4a2713aSLionel Sambuc Ann->getAnnotation() == "objc_instance_variable_invalidator")
233f4a2713aSLionel Sambuc return true;
234f4a2713aSLionel Sambuc if (LookForPartial &&
235f4a2713aSLionel Sambuc Ann->getAnnotation() == "objc_instance_variable_invalidator_partial")
236f4a2713aSLionel Sambuc return true;
237f4a2713aSLionel Sambuc }
238f4a2713aSLionel Sambuc return false;
239f4a2713aSLionel Sambuc }
240f4a2713aSLionel Sambuc
containsInvalidationMethod(const ObjCContainerDecl * D,InvalidationInfo & OutInfo,bool Partial)241f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::containsInvalidationMethod(
242f4a2713aSLionel Sambuc const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) {
243f4a2713aSLionel Sambuc
244f4a2713aSLionel Sambuc if (!D)
245f4a2713aSLionel Sambuc return;
246f4a2713aSLionel Sambuc
247f4a2713aSLionel Sambuc assert(!isa<ObjCImplementationDecl>(D));
248f4a2713aSLionel Sambuc // TODO: Cache the results.
249f4a2713aSLionel Sambuc
250f4a2713aSLionel Sambuc // Check all methods.
251*0a6a1f1dSLionel Sambuc for (const auto *MDI : D->methods())
252f4a2713aSLionel Sambuc if (isInvalidationMethod(MDI, Partial))
253f4a2713aSLionel Sambuc OutInfo.addInvalidationMethod(
254f4a2713aSLionel Sambuc cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc // If interface, check all parent protocols and super.
257f4a2713aSLionel Sambuc if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc // Visit all protocols.
260*0a6a1f1dSLionel Sambuc for (const auto *I : InterfD->protocols())
261*0a6a1f1dSLionel Sambuc containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
262f4a2713aSLionel Sambuc
263f4a2713aSLionel Sambuc // Visit all categories in case the invalidation method is declared in
264f4a2713aSLionel Sambuc // a category.
265*0a6a1f1dSLionel Sambuc for (const auto *Ext : InterfD->visible_extensions())
266*0a6a1f1dSLionel Sambuc containsInvalidationMethod(Ext, OutInfo, Partial);
267f4a2713aSLionel Sambuc
268f4a2713aSLionel Sambuc containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
269f4a2713aSLionel Sambuc return;
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
272f4a2713aSLionel Sambuc // If protocol, check all parent protocols.
273f4a2713aSLionel Sambuc if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
274*0a6a1f1dSLionel Sambuc for (const auto *I : ProtD->protocols()) {
275*0a6a1f1dSLionel Sambuc containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
276f4a2713aSLionel Sambuc }
277f4a2713aSLionel Sambuc return;
278f4a2713aSLionel Sambuc }
279f4a2713aSLionel Sambuc
280f4a2713aSLionel Sambuc return;
281f4a2713aSLionel Sambuc }
282f4a2713aSLionel Sambuc
trackIvar(const ObjCIvarDecl * Iv,IvarSet & TrackedIvars,const ObjCIvarDecl ** FirstIvarDecl)283f4a2713aSLionel Sambuc bool IvarInvalidationCheckerImpl::trackIvar(const ObjCIvarDecl *Iv,
284f4a2713aSLionel Sambuc IvarSet &TrackedIvars,
285f4a2713aSLionel Sambuc const ObjCIvarDecl **FirstIvarDecl) {
286f4a2713aSLionel Sambuc QualType IvQTy = Iv->getType();
287f4a2713aSLionel Sambuc const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
288f4a2713aSLionel Sambuc if (!IvTy)
289f4a2713aSLionel Sambuc return false;
290f4a2713aSLionel Sambuc const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
291f4a2713aSLionel Sambuc
292f4a2713aSLionel Sambuc InvalidationInfo Info;
293f4a2713aSLionel Sambuc containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false);
294f4a2713aSLionel Sambuc if (Info.needsInvalidation()) {
295f4a2713aSLionel Sambuc const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl());
296f4a2713aSLionel Sambuc TrackedIvars[I] = Info;
297f4a2713aSLionel Sambuc if (!*FirstIvarDecl)
298f4a2713aSLionel Sambuc *FirstIvarDecl = I;
299f4a2713aSLionel Sambuc return true;
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc return false;
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc
findPropertyBackingIvar(const ObjCPropertyDecl * Prop,const ObjCInterfaceDecl * InterfaceD,IvarSet & TrackedIvars,const ObjCIvarDecl ** FirstIvarDecl)304f4a2713aSLionel Sambuc const ObjCIvarDecl *IvarInvalidationCheckerImpl::findPropertyBackingIvar(
305f4a2713aSLionel Sambuc const ObjCPropertyDecl *Prop,
306f4a2713aSLionel Sambuc const ObjCInterfaceDecl *InterfaceD,
307f4a2713aSLionel Sambuc IvarSet &TrackedIvars,
308f4a2713aSLionel Sambuc const ObjCIvarDecl **FirstIvarDecl) {
309*0a6a1f1dSLionel Sambuc const ObjCIvarDecl *IvarD = nullptr;
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc // Lookup for the synthesized case.
312f4a2713aSLionel Sambuc IvarD = Prop->getPropertyIvarDecl();
313f4a2713aSLionel Sambuc // We only track the ivars/properties that are defined in the current
314f4a2713aSLionel Sambuc // class (not the parent).
315f4a2713aSLionel Sambuc if (IvarD && IvarD->getContainingInterface() == InterfaceD) {
316f4a2713aSLionel Sambuc if (TrackedIvars.count(IvarD)) {
317f4a2713aSLionel Sambuc return IvarD;
318f4a2713aSLionel Sambuc }
319f4a2713aSLionel Sambuc // If the ivar is synthesized we still want to track it.
320f4a2713aSLionel Sambuc if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
321f4a2713aSLionel Sambuc return IvarD;
322f4a2713aSLionel Sambuc }
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars.
325f4a2713aSLionel Sambuc StringRef PropName = Prop->getIdentifier()->getName();
326f4a2713aSLionel Sambuc for (IvarSet::const_iterator I = TrackedIvars.begin(),
327f4a2713aSLionel Sambuc E = TrackedIvars.end(); I != E; ++I) {
328f4a2713aSLionel Sambuc const ObjCIvarDecl *Iv = I->first;
329f4a2713aSLionel Sambuc StringRef IvarName = Iv->getName();
330f4a2713aSLionel Sambuc
331f4a2713aSLionel Sambuc if (IvarName == PropName)
332f4a2713aSLionel Sambuc return Iv;
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc SmallString<128> PropNameWithUnderscore;
335f4a2713aSLionel Sambuc {
336f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(PropNameWithUnderscore);
337f4a2713aSLionel Sambuc os << '_' << PropName;
338f4a2713aSLionel Sambuc }
339f4a2713aSLionel Sambuc if (IvarName == PropNameWithUnderscore.str())
340f4a2713aSLionel Sambuc return Iv;
341f4a2713aSLionel Sambuc }
342f4a2713aSLionel Sambuc
343f4a2713aSLionel Sambuc // Note, this is a possible source of false positives. We could look at the
344f4a2713aSLionel Sambuc // getter implementation to find the ivar when its name is not derived from
345f4a2713aSLionel Sambuc // the property name.
346*0a6a1f1dSLionel Sambuc return nullptr;
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc
printIvar(llvm::raw_svector_ostream & os,const ObjCIvarDecl * IvarDecl,const IvarToPropMapTy & IvarToPopertyMap)349f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::printIvar(llvm::raw_svector_ostream &os,
350f4a2713aSLionel Sambuc const ObjCIvarDecl *IvarDecl,
351f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap) {
352f4a2713aSLionel Sambuc if (IvarDecl->getSynthesize()) {
353f4a2713aSLionel Sambuc const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
354f4a2713aSLionel Sambuc assert(PD &&"Do we synthesize ivars for something other than properties?");
355f4a2713aSLionel Sambuc os << "Property "<< PD->getName() << " ";
356f4a2713aSLionel Sambuc } else {
357f4a2713aSLionel Sambuc os << "Instance variable "<< IvarDecl->getName() << " ";
358f4a2713aSLionel Sambuc }
359f4a2713aSLionel Sambuc }
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc // Check that the invalidatable interfaces with ivars/properties implement the
362f4a2713aSLionel Sambuc // invalidation methods.
363f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::
visit(const ObjCImplementationDecl * ImplD) const364f4a2713aSLionel Sambuc visit(const ObjCImplementationDecl *ImplD) const {
365f4a2713aSLionel Sambuc // Collect all ivars that need cleanup.
366f4a2713aSLionel Sambuc IvarSet Ivars;
367f4a2713aSLionel Sambuc // Record the first Ivar needing invalidation; used in reporting when only
368f4a2713aSLionel Sambuc // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure
369f4a2713aSLionel Sambuc // deterministic output.
370*0a6a1f1dSLionel Sambuc const ObjCIvarDecl *FirstIvarDecl = nullptr;
371f4a2713aSLionel Sambuc const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface();
372f4a2713aSLionel Sambuc
373f4a2713aSLionel Sambuc // Collect ivars declared in this class, its extensions and its implementation
374f4a2713aSLionel Sambuc ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
375f4a2713aSLionel Sambuc for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
376f4a2713aSLionel Sambuc Iv= Iv->getNextIvar())
377f4a2713aSLionel Sambuc trackIvar(Iv, Ivars, &FirstIvarDecl);
378f4a2713aSLionel Sambuc
379f4a2713aSLionel Sambuc // Construct Property/Property Accessor to Ivar maps to assist checking if an
380f4a2713aSLionel Sambuc // ivar which is backing a property has been reset.
381f4a2713aSLionel Sambuc MethToIvarMapTy PropSetterToIvarMap;
382f4a2713aSLionel Sambuc MethToIvarMapTy PropGetterToIvarMap;
383f4a2713aSLionel Sambuc PropToIvarMapTy PropertyToIvarMap;
384f4a2713aSLionel Sambuc IvarToPropMapTy IvarToPopertyMap;
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc ObjCInterfaceDecl::PropertyMap PropMap;
387f4a2713aSLionel Sambuc ObjCInterfaceDecl::PropertyDeclOrder PropOrder;
388f4a2713aSLionel Sambuc InterfaceD->collectPropertiesToImplement(PropMap, PropOrder);
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc for (ObjCInterfaceDecl::PropertyMap::iterator
391f4a2713aSLionel Sambuc I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
392f4a2713aSLionel Sambuc const ObjCPropertyDecl *PD = I->second;
393f4a2713aSLionel Sambuc
394f4a2713aSLionel Sambuc const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
395f4a2713aSLionel Sambuc &FirstIvarDecl);
396f4a2713aSLionel Sambuc if (!ID)
397f4a2713aSLionel Sambuc continue;
398f4a2713aSLionel Sambuc
399f4a2713aSLionel Sambuc // Store the mappings.
400f4a2713aSLionel Sambuc PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
401f4a2713aSLionel Sambuc PropertyToIvarMap[PD] = ID;
402f4a2713aSLionel Sambuc IvarToPopertyMap[ID] = PD;
403f4a2713aSLionel Sambuc
404f4a2713aSLionel Sambuc // Find the setter and the getter.
405f4a2713aSLionel Sambuc const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
406f4a2713aSLionel Sambuc if (SetterD) {
407f4a2713aSLionel Sambuc SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
408f4a2713aSLionel Sambuc PropSetterToIvarMap[SetterD] = ID;
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc
411f4a2713aSLionel Sambuc const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
412f4a2713aSLionel Sambuc if (GetterD) {
413f4a2713aSLionel Sambuc GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
414f4a2713aSLionel Sambuc PropGetterToIvarMap[GetterD] = ID;
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc
418f4a2713aSLionel Sambuc // If no ivars need invalidation, there is nothing to check here.
419f4a2713aSLionel Sambuc if (Ivars.empty())
420f4a2713aSLionel Sambuc return;
421f4a2713aSLionel Sambuc
422f4a2713aSLionel Sambuc // Find all partial invalidation methods.
423f4a2713aSLionel Sambuc InvalidationInfo PartialInfo;
424f4a2713aSLionel Sambuc containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true);
425f4a2713aSLionel Sambuc
426f4a2713aSLionel Sambuc // Remove ivars invalidated by the partial invalidation methods. They do not
427f4a2713aSLionel Sambuc // need to be invalidated in the regular invalidation methods.
428f4a2713aSLionel Sambuc bool AtImplementationContainsAtLeastOnePartialInvalidationMethod = false;
429f4a2713aSLionel Sambuc for (MethodSet::iterator
430f4a2713aSLionel Sambuc I = PartialInfo.InvalidationMethods.begin(),
431f4a2713aSLionel Sambuc E = PartialInfo.InvalidationMethods.end(); I != E; ++I) {
432f4a2713aSLionel Sambuc const ObjCMethodDecl *InterfD = *I;
433f4a2713aSLionel Sambuc
434f4a2713aSLionel Sambuc // Get the corresponding method in the @implementation.
435f4a2713aSLionel Sambuc const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
436f4a2713aSLionel Sambuc InterfD->isInstanceMethod());
437f4a2713aSLionel Sambuc if (D && D->hasBody()) {
438f4a2713aSLionel Sambuc AtImplementationContainsAtLeastOnePartialInvalidationMethod = true;
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc bool CalledAnotherInvalidationMethod = false;
441f4a2713aSLionel Sambuc // The MethodCrowler is going to remove the invalidated ivars.
442f4a2713aSLionel Sambuc MethodCrawler(Ivars,
443f4a2713aSLionel Sambuc CalledAnotherInvalidationMethod,
444f4a2713aSLionel Sambuc PropSetterToIvarMap,
445f4a2713aSLionel Sambuc PropGetterToIvarMap,
446f4a2713aSLionel Sambuc PropertyToIvarMap,
447f4a2713aSLionel Sambuc BR.getContext()).VisitStmt(D->getBody());
448f4a2713aSLionel Sambuc // If another invalidation method was called, trust that full invalidation
449f4a2713aSLionel Sambuc // has occurred.
450f4a2713aSLionel Sambuc if (CalledAnotherInvalidationMethod)
451f4a2713aSLionel Sambuc Ivars.clear();
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc }
454f4a2713aSLionel Sambuc
455f4a2713aSLionel Sambuc // If all ivars have been invalidated by partial invalidators, there is
456f4a2713aSLionel Sambuc // nothing to check here.
457f4a2713aSLionel Sambuc if (Ivars.empty())
458f4a2713aSLionel Sambuc return;
459f4a2713aSLionel Sambuc
460f4a2713aSLionel Sambuc // Find all invalidation methods in this @interface declaration and parents.
461f4a2713aSLionel Sambuc InvalidationInfo Info;
462f4a2713aSLionel Sambuc containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false);
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambuc // Report an error in case none of the invalidation methods are declared.
465f4a2713aSLionel Sambuc if (!Info.needsInvalidation() && !PartialInfo.needsInvalidation()) {
466f4a2713aSLionel Sambuc if (Filter.check_MissingInvalidationMethod)
467*0a6a1f1dSLionel Sambuc reportNoInvalidationMethod(Filter.checkName_MissingInvalidationMethod,
468*0a6a1f1dSLionel Sambuc FirstIvarDecl, IvarToPopertyMap, InterfaceD,
469f4a2713aSLionel Sambuc /*MissingDeclaration*/ true);
470f4a2713aSLionel Sambuc // If there are no invalidation methods, there is no ivar validation work
471f4a2713aSLionel Sambuc // to be done.
472f4a2713aSLionel Sambuc return;
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc // Only check if Ivars are invalidated when InstanceVariableInvalidation
476f4a2713aSLionel Sambuc // has been requested.
477f4a2713aSLionel Sambuc if (!Filter.check_InstanceVariableInvalidation)
478f4a2713aSLionel Sambuc return;
479f4a2713aSLionel Sambuc
480f4a2713aSLionel Sambuc // Check that all ivars are invalidated by the invalidation methods.
481f4a2713aSLionel Sambuc bool AtImplementationContainsAtLeastOneInvalidationMethod = false;
482f4a2713aSLionel Sambuc for (MethodSet::iterator I = Info.InvalidationMethods.begin(),
483f4a2713aSLionel Sambuc E = Info.InvalidationMethods.end(); I != E; ++I) {
484f4a2713aSLionel Sambuc const ObjCMethodDecl *InterfD = *I;
485f4a2713aSLionel Sambuc
486f4a2713aSLionel Sambuc // Get the corresponding method in the @implementation.
487f4a2713aSLionel Sambuc const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(),
488f4a2713aSLionel Sambuc InterfD->isInstanceMethod());
489f4a2713aSLionel Sambuc if (D && D->hasBody()) {
490f4a2713aSLionel Sambuc AtImplementationContainsAtLeastOneInvalidationMethod = true;
491f4a2713aSLionel Sambuc
492f4a2713aSLionel Sambuc // Get a copy of ivars needing invalidation.
493f4a2713aSLionel Sambuc IvarSet IvarsI = Ivars;
494f4a2713aSLionel Sambuc
495f4a2713aSLionel Sambuc bool CalledAnotherInvalidationMethod = false;
496f4a2713aSLionel Sambuc MethodCrawler(IvarsI,
497f4a2713aSLionel Sambuc CalledAnotherInvalidationMethod,
498f4a2713aSLionel Sambuc PropSetterToIvarMap,
499f4a2713aSLionel Sambuc PropGetterToIvarMap,
500f4a2713aSLionel Sambuc PropertyToIvarMap,
501f4a2713aSLionel Sambuc BR.getContext()).VisitStmt(D->getBody());
502f4a2713aSLionel Sambuc // If another invalidation method was called, trust that full invalidation
503f4a2713aSLionel Sambuc // has occurred.
504f4a2713aSLionel Sambuc if (CalledAnotherInvalidationMethod)
505f4a2713aSLionel Sambuc continue;
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc // Warn on the ivars that were not invalidated by the method.
508f4a2713aSLionel Sambuc for (IvarSet::const_iterator
509f4a2713aSLionel Sambuc I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I)
510f4a2713aSLionel Sambuc reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D);
511f4a2713aSLionel Sambuc }
512f4a2713aSLionel Sambuc }
513f4a2713aSLionel Sambuc
514f4a2713aSLionel Sambuc // Report an error in case none of the invalidation methods are implemented.
515f4a2713aSLionel Sambuc if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
516f4a2713aSLionel Sambuc if (AtImplementationContainsAtLeastOnePartialInvalidationMethod) {
517f4a2713aSLionel Sambuc // Warn on the ivars that were not invalidated by the prrtial
518f4a2713aSLionel Sambuc // invalidation methods.
519f4a2713aSLionel Sambuc for (IvarSet::const_iterator
520f4a2713aSLionel Sambuc I = Ivars.begin(), E = Ivars.end(); I != E; ++I)
521*0a6a1f1dSLionel Sambuc reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, nullptr);
522f4a2713aSLionel Sambuc } else {
523f4a2713aSLionel Sambuc // Otherwise, no invalidation methods were implemented.
524*0a6a1f1dSLionel Sambuc reportNoInvalidationMethod(Filter.checkName_InstanceVariableInvalidation,
525*0a6a1f1dSLionel Sambuc FirstIvarDecl, IvarToPopertyMap, InterfaceD,
526f4a2713aSLionel Sambuc /*MissingDeclaration*/ false);
527f4a2713aSLionel Sambuc }
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc }
530f4a2713aSLionel Sambuc
reportNoInvalidationMethod(CheckName CheckName,const ObjCIvarDecl * FirstIvarDecl,const IvarToPropMapTy & IvarToPopertyMap,const ObjCInterfaceDecl * InterfaceD,bool MissingDeclaration) const531*0a6a1f1dSLionel Sambuc void IvarInvalidationCheckerImpl::reportNoInvalidationMethod(
532*0a6a1f1dSLionel Sambuc CheckName CheckName, const ObjCIvarDecl *FirstIvarDecl,
533f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap,
534*0a6a1f1dSLionel Sambuc const ObjCInterfaceDecl *InterfaceD, bool MissingDeclaration) const {
535f4a2713aSLionel Sambuc SmallString<128> sbuf;
536f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(sbuf);
537f4a2713aSLionel Sambuc assert(FirstIvarDecl);
538f4a2713aSLionel Sambuc printIvar(os, FirstIvarDecl, IvarToPopertyMap);
539f4a2713aSLionel Sambuc os << "needs to be invalidated; ";
540f4a2713aSLionel Sambuc if (MissingDeclaration)
541f4a2713aSLionel Sambuc os << "no invalidation method is declared for ";
542f4a2713aSLionel Sambuc else
543f4a2713aSLionel Sambuc os << "no invalidation method is defined in the @implementation for ";
544f4a2713aSLionel Sambuc os << InterfaceD->getName();
545f4a2713aSLionel Sambuc
546f4a2713aSLionel Sambuc PathDiagnosticLocation IvarDecLocation =
547f4a2713aSLionel Sambuc PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager());
548f4a2713aSLionel Sambuc
549*0a6a1f1dSLionel Sambuc BR.EmitBasicReport(FirstIvarDecl, CheckName, "Incomplete invalidation",
550f4a2713aSLionel Sambuc categories::CoreFoundationObjectiveC, os.str(),
551f4a2713aSLionel Sambuc IvarDecLocation);
552f4a2713aSLionel Sambuc }
553f4a2713aSLionel Sambuc
554f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::
reportIvarNeedsInvalidation(const ObjCIvarDecl * IvarD,const IvarToPropMapTy & IvarToPopertyMap,const ObjCMethodDecl * MethodD) const555f4a2713aSLionel Sambuc reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD,
556f4a2713aSLionel Sambuc const IvarToPropMapTy &IvarToPopertyMap,
557f4a2713aSLionel Sambuc const ObjCMethodDecl *MethodD) const {
558f4a2713aSLionel Sambuc SmallString<128> sbuf;
559f4a2713aSLionel Sambuc llvm::raw_svector_ostream os(sbuf);
560f4a2713aSLionel Sambuc printIvar(os, IvarD, IvarToPopertyMap);
561f4a2713aSLionel Sambuc os << "needs to be invalidated or set to nil";
562f4a2713aSLionel Sambuc if (MethodD) {
563f4a2713aSLionel Sambuc PathDiagnosticLocation MethodDecLocation =
564f4a2713aSLionel Sambuc PathDiagnosticLocation::createEnd(MethodD->getBody(),
565f4a2713aSLionel Sambuc BR.getSourceManager(),
566f4a2713aSLionel Sambuc Mgr.getAnalysisDeclContext(MethodD));
567*0a6a1f1dSLionel Sambuc BR.EmitBasicReport(MethodD, Filter.checkName_InstanceVariableInvalidation,
568*0a6a1f1dSLionel Sambuc "Incomplete invalidation",
569f4a2713aSLionel Sambuc categories::CoreFoundationObjectiveC, os.str(),
570f4a2713aSLionel Sambuc MethodDecLocation);
571f4a2713aSLionel Sambuc } else {
572*0a6a1f1dSLionel Sambuc BR.EmitBasicReport(
573*0a6a1f1dSLionel Sambuc IvarD, Filter.checkName_InstanceVariableInvalidation,
574*0a6a1f1dSLionel Sambuc "Incomplete invalidation", categories::CoreFoundationObjectiveC,
575*0a6a1f1dSLionel Sambuc os.str(),
576*0a6a1f1dSLionel Sambuc PathDiagnosticLocation::createBegin(IvarD, BR.getSourceManager()));
577f4a2713aSLionel Sambuc }
578f4a2713aSLionel Sambuc }
579f4a2713aSLionel Sambuc
markInvalidated(const ObjCIvarDecl * Iv)580f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::markInvalidated(
581f4a2713aSLionel Sambuc const ObjCIvarDecl *Iv) {
582f4a2713aSLionel Sambuc IvarSet::iterator I = IVars.find(Iv);
583f4a2713aSLionel Sambuc if (I != IVars.end()) {
584f4a2713aSLionel Sambuc // If InvalidationMethod is present, we are processing the message send and
585f4a2713aSLionel Sambuc // should ensure we are invalidating with the appropriate method,
586f4a2713aSLionel Sambuc // otherwise, we are processing setting to 'nil'.
587f4a2713aSLionel Sambuc if (!InvalidationMethod ||
588f4a2713aSLionel Sambuc (InvalidationMethod && I->second.hasMethod(InvalidationMethod)))
589f4a2713aSLionel Sambuc IVars.erase(I);
590f4a2713aSLionel Sambuc }
591f4a2713aSLionel Sambuc }
592f4a2713aSLionel Sambuc
peel(const Expr * E) const593f4a2713aSLionel Sambuc const Expr *IvarInvalidationCheckerImpl::MethodCrawler::peel(const Expr *E) const {
594f4a2713aSLionel Sambuc E = E->IgnoreParenCasts();
595f4a2713aSLionel Sambuc if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
596f4a2713aSLionel Sambuc E = POE->getSyntacticForm()->IgnoreParenCasts();
597f4a2713aSLionel Sambuc if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
598f4a2713aSLionel Sambuc E = OVE->getSourceExpr()->IgnoreParenCasts();
599f4a2713aSLionel Sambuc return E;
600f4a2713aSLionel Sambuc }
601f4a2713aSLionel Sambuc
checkObjCIvarRefExpr(const ObjCIvarRefExpr * IvarRef)602f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCIvarRefExpr(
603f4a2713aSLionel Sambuc const ObjCIvarRefExpr *IvarRef) {
604f4a2713aSLionel Sambuc if (const Decl *D = IvarRef->getDecl())
605f4a2713aSLionel Sambuc markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc
checkObjCMessageExpr(const ObjCMessageExpr * ME)608f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCMessageExpr(
609f4a2713aSLionel Sambuc const ObjCMessageExpr *ME) {
610f4a2713aSLionel Sambuc const ObjCMethodDecl *MD = ME->getMethodDecl();
611f4a2713aSLionel Sambuc if (MD) {
612f4a2713aSLionel Sambuc MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
613f4a2713aSLionel Sambuc MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
614f4a2713aSLionel Sambuc if (IvI != PropertyGetterToIvarMap.end())
615f4a2713aSLionel Sambuc markInvalidated(IvI->second);
616f4a2713aSLionel Sambuc }
617f4a2713aSLionel Sambuc }
618f4a2713aSLionel Sambuc
checkObjCPropertyRefExpr(const ObjCPropertyRefExpr * PA)619f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCPropertyRefExpr(
620f4a2713aSLionel Sambuc const ObjCPropertyRefExpr *PA) {
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc if (PA->isExplicitProperty()) {
623f4a2713aSLionel Sambuc const ObjCPropertyDecl *PD = PA->getExplicitProperty();
624f4a2713aSLionel Sambuc if (PD) {
625f4a2713aSLionel Sambuc PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
626f4a2713aSLionel Sambuc PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
627f4a2713aSLionel Sambuc if (IvI != PropertyToIvarMap.end())
628f4a2713aSLionel Sambuc markInvalidated(IvI->second);
629f4a2713aSLionel Sambuc return;
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc if (PA->isImplicitProperty()) {
634f4a2713aSLionel Sambuc const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
635f4a2713aSLionel Sambuc if (MD) {
636f4a2713aSLionel Sambuc MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
637f4a2713aSLionel Sambuc MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
638f4a2713aSLionel Sambuc if (IvI != PropertyGetterToIvarMap.end())
639f4a2713aSLionel Sambuc markInvalidated(IvI->second);
640f4a2713aSLionel Sambuc return;
641f4a2713aSLionel Sambuc }
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc
isZero(const Expr * E) const645f4a2713aSLionel Sambuc bool IvarInvalidationCheckerImpl::MethodCrawler::isZero(const Expr *E) const {
646f4a2713aSLionel Sambuc E = peel(E);
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
649f4a2713aSLionel Sambuc != Expr::NPCK_NotNull);
650f4a2713aSLionel Sambuc }
651f4a2713aSLionel Sambuc
check(const Expr * E)652f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::check(const Expr *E) {
653f4a2713aSLionel Sambuc E = peel(E);
654f4a2713aSLionel Sambuc
655f4a2713aSLionel Sambuc if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
656f4a2713aSLionel Sambuc checkObjCIvarRefExpr(IvarRef);
657f4a2713aSLionel Sambuc return;
658f4a2713aSLionel Sambuc }
659f4a2713aSLionel Sambuc
660f4a2713aSLionel Sambuc if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
661f4a2713aSLionel Sambuc checkObjCPropertyRefExpr(PropRef);
662f4a2713aSLionel Sambuc return;
663f4a2713aSLionel Sambuc }
664f4a2713aSLionel Sambuc
665f4a2713aSLionel Sambuc if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
666f4a2713aSLionel Sambuc checkObjCMessageExpr(MsgExpr);
667f4a2713aSLionel Sambuc return;
668f4a2713aSLionel Sambuc }
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc
VisitBinaryOperator(const BinaryOperator * BO)671f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::VisitBinaryOperator(
672f4a2713aSLionel Sambuc const BinaryOperator *BO) {
673f4a2713aSLionel Sambuc VisitStmt(BO);
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc // Do we assign/compare against zero? If yes, check the variable we are
676f4a2713aSLionel Sambuc // assigning to.
677f4a2713aSLionel Sambuc BinaryOperatorKind Opcode = BO->getOpcode();
678f4a2713aSLionel Sambuc if (Opcode != BO_Assign &&
679f4a2713aSLionel Sambuc Opcode != BO_EQ &&
680f4a2713aSLionel Sambuc Opcode != BO_NE)
681f4a2713aSLionel Sambuc return;
682f4a2713aSLionel Sambuc
683f4a2713aSLionel Sambuc if (isZero(BO->getRHS())) {
684f4a2713aSLionel Sambuc check(BO->getLHS());
685f4a2713aSLionel Sambuc return;
686f4a2713aSLionel Sambuc }
687f4a2713aSLionel Sambuc
688f4a2713aSLionel Sambuc if (Opcode != BO_Assign && isZero(BO->getLHS())) {
689f4a2713aSLionel Sambuc check(BO->getRHS());
690f4a2713aSLionel Sambuc return;
691f4a2713aSLionel Sambuc }
692f4a2713aSLionel Sambuc }
693f4a2713aSLionel Sambuc
VisitObjCMessageExpr(const ObjCMessageExpr * ME)694f4a2713aSLionel Sambuc void IvarInvalidationCheckerImpl::MethodCrawler::VisitObjCMessageExpr(
695f4a2713aSLionel Sambuc const ObjCMessageExpr *ME) {
696f4a2713aSLionel Sambuc const ObjCMethodDecl *MD = ME->getMethodDecl();
697f4a2713aSLionel Sambuc const Expr *Receiver = ME->getInstanceReceiver();
698f4a2713aSLionel Sambuc
699f4a2713aSLionel Sambuc // Stop if we are calling '[self invalidate]'.
700f4a2713aSLionel Sambuc if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false))
701f4a2713aSLionel Sambuc if (Receiver->isObjCSelfExpr()) {
702f4a2713aSLionel Sambuc CalledAnotherInvalidationMethod = true;
703f4a2713aSLionel Sambuc return;
704f4a2713aSLionel Sambuc }
705f4a2713aSLionel Sambuc
706f4a2713aSLionel Sambuc // Check if we call a setter and set the property to 'nil'.
707f4a2713aSLionel Sambuc if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
708f4a2713aSLionel Sambuc MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
709f4a2713aSLionel Sambuc MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
710f4a2713aSLionel Sambuc if (IvI != PropertySetterToIvarMap.end()) {
711f4a2713aSLionel Sambuc markInvalidated(IvI->second);
712f4a2713aSLionel Sambuc return;
713f4a2713aSLionel Sambuc }
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc
716f4a2713aSLionel Sambuc // Check if we call the 'invalidation' routine on the ivar.
717f4a2713aSLionel Sambuc if (Receiver) {
718f4a2713aSLionel Sambuc InvalidationMethod = MD;
719f4a2713aSLionel Sambuc check(Receiver->IgnoreParenCasts());
720*0a6a1f1dSLionel Sambuc InvalidationMethod = nullptr;
721f4a2713aSLionel Sambuc }
722f4a2713aSLionel Sambuc
723f4a2713aSLionel Sambuc VisitStmt(ME);
724f4a2713aSLionel Sambuc }
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc
727f4a2713aSLionel Sambuc // Register the checkers.
728f4a2713aSLionel Sambuc namespace {
729f4a2713aSLionel Sambuc
730f4a2713aSLionel Sambuc class IvarInvalidationChecker :
731f4a2713aSLionel Sambuc public Checker<check::ASTDecl<ObjCImplementationDecl> > {
732f4a2713aSLionel Sambuc public:
733f4a2713aSLionel Sambuc ChecksFilter Filter;
734f4a2713aSLionel Sambuc public:
checkASTDecl(const ObjCImplementationDecl * D,AnalysisManager & Mgr,BugReporter & BR) const735f4a2713aSLionel Sambuc void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
736f4a2713aSLionel Sambuc BugReporter &BR) const {
737f4a2713aSLionel Sambuc IvarInvalidationCheckerImpl Walker(Mgr, BR, Filter);
738f4a2713aSLionel Sambuc Walker.visit(D);
739f4a2713aSLionel Sambuc }
740f4a2713aSLionel Sambuc };
741f4a2713aSLionel Sambuc }
742f4a2713aSLionel Sambuc
743f4a2713aSLionel Sambuc #define REGISTER_CHECKER(name) \
744f4a2713aSLionel Sambuc void ento::register##name(CheckerManager &mgr) { \
745*0a6a1f1dSLionel Sambuc IvarInvalidationChecker *checker = \
746*0a6a1f1dSLionel Sambuc mgr.registerChecker<IvarInvalidationChecker>(); \
747*0a6a1f1dSLionel Sambuc checker->Filter.check_##name = true; \
748*0a6a1f1dSLionel Sambuc checker->Filter.checkName_##name = mgr.getCurrentCheckName(); \
749f4a2713aSLionel Sambuc }
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc REGISTER_CHECKER(InstanceVariableInvalidation)
752f4a2713aSLionel Sambuc REGISTER_CHECKER(MissingInvalidationMethod)
753f4a2713aSLionel Sambuc
754