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