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