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, |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 194 | const IvarToPropMapTy &IvarToPopertyMap); |
| 195 | |
| 196 | static void reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl, |
| 197 | const IvarToPropMapTy &IvarToPopertyMap, |
| 198 | const ObjCInterfaceDecl *InterfaceD, |
| 199 | BugReporter &BR, |
| 200 | bool MissingDeclaration); |
| 201 | static void reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD, |
| 202 | const IvarToPropMapTy &IvarToPopertyMap, |
| 203 | const ObjCMethodDecl *MethodD, |
| 204 | AnalysisManager& Mgr, |
| 205 | BugReporter &BR); |
| 206 | |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 207 | |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 208 | public: |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 209 | void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr, |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 210 | BugReporter &BR) const; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 213 | static bool isInvalidationMethod(const ObjCMethodDecl *M, bool LookForPartial) { |
Anna Zaks | b087bbf | 2012-09-27 19:45:08 +0000 | [diff] [blame] | 214 | for (specific_attr_iterator<AnnotateAttr> |
| 215 | AI = M->specific_attr_begin<AnnotateAttr>(), |
| 216 | AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) { |
| 217 | const AnnotateAttr *Ann = *AI; |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 218 | if (!LookForPartial && |
| 219 | Ann->getAnnotation() == "objc_instance_variable_invalidator") |
| 220 | return true; |
| 221 | if (LookForPartial && |
| 222 | Ann->getAnnotation() == "objc_instance_variable_invalidator_partial") |
Anna Zaks | b087bbf | 2012-09-27 19:45:08 +0000 | [diff] [blame] | 223 | return true; |
| 224 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 228 | void IvarInvalidationChecker::containsInvalidationMethod( |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 229 | const ObjCContainerDecl *D, InvalidationInfo &OutInfo, bool Partial) { |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 230 | |
| 231 | if (!D) |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 232 | return; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 233 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 234 | assert(!isa<ObjCImplementationDecl>(D)); |
| 235 | // TODO: Cache the results. |
| 236 | |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 237 | // Check all methods. |
| 238 | for (ObjCContainerDecl::method_iterator |
| 239 | I = D->meth_begin(), |
| 240 | E = D->meth_end(); I != E; ++I) { |
| 241 | const ObjCMethodDecl *MDI = *I; |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 242 | if (isInvalidationMethod(MDI, Partial)) |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 243 | OutInfo.addInvalidationMethod( |
| 244 | cast<ObjCMethodDecl>(MDI->getCanonicalDecl())); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // If interface, check all parent protocols and super. |
Anna Zaks | ae81e17 | 2013-01-11 03:52:37 +0000 | [diff] [blame] | 248 | if (const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 249 | |
| 250 | // Visit all protocols. |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 251 | for (ObjCInterfaceDecl::protocol_iterator |
Anna Zaks | ae81e17 | 2013-01-11 03:52:37 +0000 | [diff] [blame] | 252 | I = InterfD->protocol_begin(), |
| 253 | E = InterfD->protocol_end(); I != E; ++I) { |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 254 | containsInvalidationMethod((*I)->getDefinition(), OutInfo, Partial); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 255 | } |
Anna Zaks | ae81e17 | 2013-01-11 03:52:37 +0000 | [diff] [blame] | 256 | |
| 257 | // Visit all categories in case the invalidation method is declared in |
| 258 | // a category. |
Douglas Gregor | d329724 | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 259 | for (ObjCInterfaceDecl::visible_extensions_iterator |
| 260 | Ext = InterfD->visible_extensions_begin(), |
| 261 | ExtEnd = InterfD->visible_extensions_end(); |
| 262 | Ext != ExtEnd; ++Ext) { |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 263 | containsInvalidationMethod(*Ext, OutInfo, Partial); |
Anna Zaks | ae81e17 | 2013-01-11 03:52:37 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 266 | containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 267 | return; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // If protocol, check all parent protocols. |
| 271 | if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) { |
| 272 | for (ObjCInterfaceDecl::protocol_iterator |
| 273 | I = ProtD->protocol_begin(), |
| 274 | E = ProtD->protocol_end(); I != E; ++I) { |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 275 | containsInvalidationMethod((*I)->getDefinition(), OutInfo, Partial); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 276 | } |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 277 | return; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Anna Zaks | ae81e17 | 2013-01-11 03:52:37 +0000 | [diff] [blame] | 280 | return; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 283 | bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv, |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 284 | IvarSet &TrackedIvars, |
| 285 | const ObjCIvarDecl **FirstIvarDecl) { |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 286 | QualType IvQTy = Iv->getType(); |
| 287 | const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>(); |
| 288 | if (!IvTy) |
| 289 | return false; |
| 290 | const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl(); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 291 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 292 | InvalidationInfo Info; |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 293 | containsInvalidationMethod(IvInterf, Info, /*LookForPartial*/ false); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 294 | if (Info.needsInvalidation()) { |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 295 | const ObjCIvarDecl *I = cast<ObjCIvarDecl>(Iv->getCanonicalDecl()); |
| 296 | TrackedIvars[I] = Info; |
| 297 | if (!*FirstIvarDecl) |
| 298 | *FirstIvarDecl = I; |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 299 | return true; |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 304 | const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar( |
| 305 | const ObjCPropertyDecl *Prop, |
| 306 | const ObjCInterfaceDecl *InterfaceD, |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 307 | IvarSet &TrackedIvars, |
| 308 | const ObjCIvarDecl **FirstIvarDecl) { |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 309 | const ObjCIvarDecl *IvarD = 0; |
| 310 | |
| 311 | // Lookup for the synthesized case. |
| 312 | IvarD = Prop->getPropertyIvarDecl(); |
Anna Zaks | 5879fb3 | 2013-01-07 19:12:56 +0000 | [diff] [blame] | 313 | // We only track the ivars/properties that are defined in the current |
| 314 | // class (not the parent). |
| 315 | if (IvarD && IvarD->getContainingInterface() == InterfaceD) { |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 316 | if (TrackedIvars.count(IvarD)) { |
| 317 | return IvarD; |
| 318 | } |
| 319 | // If the ivar is synthesized we still want to track it. |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 320 | if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl)) |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 321 | return IvarD; |
| 322 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 323 | |
| 324 | // Lookup IVars named "_PropName"or "PropName" among the tracked Ivars. |
| 325 | StringRef PropName = Prop->getIdentifier()->getName(); |
| 326 | for (IvarSet::const_iterator I = TrackedIvars.begin(), |
| 327 | E = TrackedIvars.end(); I != E; ++I) { |
| 328 | const ObjCIvarDecl *Iv = I->first; |
| 329 | StringRef IvarName = Iv->getName(); |
| 330 | |
| 331 | if (IvarName == PropName) |
| 332 | return Iv; |
| 333 | |
| 334 | SmallString<128> PropNameWithUnderscore; |
| 335 | { |
| 336 | llvm::raw_svector_ostream os(PropNameWithUnderscore); |
| 337 | os << '_' << PropName; |
| 338 | } |
| 339 | if (IvarName == PropNameWithUnderscore.str()) |
| 340 | return Iv; |
| 341 | } |
| 342 | |
| 343 | // Note, this is a possible source of false positives. We could look at the |
| 344 | // getter implementation to find the ivar when its name is not derived from |
| 345 | // the property name. |
| 346 | return 0; |
| 347 | } |
| 348 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 349 | void IvarInvalidationChecker::printIvar(llvm::raw_svector_ostream &os, |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 350 | const ObjCIvarDecl *IvarDecl, |
| 351 | const IvarToPropMapTy &IvarToPopertyMap) { |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 352 | if (IvarDecl->getSynthesize()) { |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 353 | const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl); |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 354 | assert(PD &&"Do we synthesize ivars for something other than properties?"); |
| 355 | os << "Property "<< PD->getName() << " "; |
| 356 | } else { |
| 357 | os << "Instance variable "<< IvarDecl->getName() << " "; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // Check that the invalidatable interfaces with ivars/properties implement the |
| 362 | // invalidation methods. |
| 363 | void IvarInvalidationChecker::checkASTDecl(const ObjCImplementationDecl *ImplD, |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 364 | AnalysisManager& Mgr, |
| 365 | BugReporter &BR) const { |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 366 | // Collect all ivars that need cleanup. |
| 367 | IvarSet Ivars; |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 368 | // Record the first Ivar needing invalidation; used in reporting when only |
| 369 | // one ivar is sufficient. Cannot grab the first on the Ivars set to ensure |
| 370 | // deterministic output. |
| 371 | const ObjCIvarDecl *FirstIvarDecl = 0; |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 372 | const ObjCInterfaceDecl *InterfaceD = ImplD->getClassInterface(); |
Anna Zaks | e0c50fa | 2012-10-16 19:36:37 +0000 | [diff] [blame] | 373 | |
| 374 | // Collect ivars declared in this class, its extensions and its implementation |
| 375 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD); |
| 376 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
| 377 | Iv= Iv->getNextIvar()) |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 378 | trackIvar(Iv, Ivars, &FirstIvarDecl); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 379 | |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 380 | // Construct Property/Property Accessor to Ivar maps to assist checking if an |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 381 | // ivar which is backing a property has been reset. |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 382 | MethToIvarMapTy PropSetterToIvarMap; |
| 383 | MethToIvarMapTy PropGetterToIvarMap; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 384 | PropToIvarMapTy PropertyToIvarMap; |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 385 | IvarToPropMapTy IvarToPopertyMap; |
Anna Zaks | c3c26b7 | 2012-10-18 19:17:57 +0000 | [diff] [blame] | 386 | |
| 387 | ObjCInterfaceDecl::PropertyMap PropMap; |
| 388 | InterfaceD->collectPropertiesToImplement(PropMap); |
| 389 | |
| 390 | for (ObjCInterfaceDecl::PropertyMap::iterator |
| 391 | I = PropMap.begin(), E = PropMap.end(); I != E; ++I) { |
| 392 | const ObjCPropertyDecl *PD = I->second; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 393 | |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 394 | const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars, |
| 395 | &FirstIvarDecl); |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 396 | if (!ID) |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 397 | continue; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 398 | |
| 399 | // Store the mappings. |
| 400 | PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl()); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 401 | PropertyToIvarMap[PD] = ID; |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 402 | IvarToPopertyMap[ID] = PD; |
| 403 | |
| 404 | // Find the setter and the getter. |
| 405 | const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl(); |
| 406 | if (SetterD) { |
| 407 | SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl()); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 408 | PropSetterToIvarMap[SetterD] = ID; |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl(); |
| 412 | if (GetterD) { |
| 413 | GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl()); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 414 | PropGetterToIvarMap[GetterD] = ID; |
Anna Zaks | 377945c | 2012-09-27 21:57:14 +0000 | [diff] [blame] | 415 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 418 | // If no ivars need invalidation, there is nothing to check here. |
| 419 | if (Ivars.empty()) |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 420 | return; |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 421 | |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 422 | // Find all partial invalidation methods. |
| 423 | InvalidationInfo PartialInfo; |
| 424 | containsInvalidationMethod(InterfaceD, PartialInfo, /*LookForPartial*/ true); |
| 425 | |
| 426 | // Remove ivars invalidated by the partial invalidation methods. They do not |
| 427 | // need to be invalidated in the regular invalidation methods. |
| 428 | for (MethodSet::iterator |
| 429 | I = PartialInfo.InvalidationMethods.begin(), |
| 430 | E = PartialInfo.InvalidationMethods.end(); I != E; ++I) { |
| 431 | const ObjCMethodDecl *InterfD = *I; |
| 432 | |
| 433 | // Get the corresponding method in the @implementation. |
| 434 | const ObjCMethodDecl *D = ImplD->getMethod(InterfD->getSelector(), |
| 435 | InterfD->isInstanceMethod()); |
| 436 | if (D && D->hasBody()) { |
| 437 | bool CalledAnotherInvalidationMethod = false; |
| 438 | // The MethodCrowler is going to remove the invalidated ivars. |
| 439 | MethodCrawler(Ivars, |
| 440 | CalledAnotherInvalidationMethod, |
| 441 | PropSetterToIvarMap, |
| 442 | PropGetterToIvarMap, |
| 443 | PropertyToIvarMap, |
| 444 | BR.getContext()).VisitStmt(D->getBody()); |
| 445 | // If another invalidation method was called, trust that full invalidation |
| 446 | // has occurred. |
| 447 | if (CalledAnotherInvalidationMethod) |
| 448 | Ivars.clear(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // If all ivars have been invalidated by partial invalidators, there is |
| 453 | // nothing to check here. |
| 454 | if (Ivars.empty()) |
| 455 | return; |
| 456 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 457 | // Find all invalidation methods in this @interface declaration and parents. |
| 458 | InvalidationInfo Info; |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 459 | containsInvalidationMethod(InterfaceD, Info, /*LookForPartial*/ false); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 460 | |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 461 | // Report an error in case none of the invalidation methods are declared. |
| 462 | if (!Info.needsInvalidation()) { |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 463 | reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR, |
| 464 | /*MissingDeclaration*/ true); |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 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. |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 496 | for (IvarSet::const_iterator |
| 497 | I = IvarsI.begin(), E = IvarsI.end(); I != E; ++I) |
| 498 | reportIvarNeedsInvalidation(I->first, IvarToPopertyMap, D, Mgr, BR); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 501 | |
| 502 | // Report an error in case none of the invalidation methods are implemented. |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 503 | if (!AtImplementationContainsAtLeastOneInvalidationMethod) |
| 504 | reportNoInvalidationMethod(FirstIvarDecl, IvarToPopertyMap, InterfaceD, BR, |
| 505 | /*MissingDeclaration*/ false); |
| 506 | } |
Anna Zaks | b1fc673 | 2013-01-10 20:59:51 +0000 | [diff] [blame] | 507 | |
Anna Zaks | 2b174c3 | 2013-02-08 23:55:45 +0000 | [diff] [blame^] | 508 | void IvarInvalidationChecker:: |
| 509 | reportNoInvalidationMethod(const ObjCIvarDecl *FirstIvarDecl, |
| 510 | const IvarToPropMapTy &IvarToPopertyMap, |
| 511 | const ObjCInterfaceDecl *InterfaceD, |
| 512 | BugReporter &BR, |
| 513 | bool MissingDeclaration) { |
| 514 | SmallString<128> sbuf; |
| 515 | llvm::raw_svector_ostream os(sbuf); |
| 516 | assert(FirstIvarDecl); |
| 517 | printIvar(os, FirstIvarDecl, IvarToPopertyMap); |
| 518 | os << "needs to be invalidated; "; |
| 519 | if (MissingDeclaration) |
| 520 | os << "no invalidation method is declared for "; |
| 521 | else |
| 522 | os << "no invalidation method is defined in the @implementation for "; |
| 523 | os << InterfaceD->getName(); |
| 524 | |
| 525 | PathDiagnosticLocation IvarDecLocation = |
| 526 | PathDiagnosticLocation::createBegin(FirstIvarDecl, BR.getSourceManager()); |
| 527 | |
| 528 | BR.EmitBasicReport(FirstIvarDecl, "Incomplete invalidation", |
| 529 | categories::CoreFoundationObjectiveC, os.str(), |
| 530 | IvarDecLocation); |
| 531 | } |
| 532 | |
| 533 | void IvarInvalidationChecker:: |
| 534 | reportIvarNeedsInvalidation(const ObjCIvarDecl *IvarD, |
| 535 | const IvarToPropMapTy &IvarToPopertyMap, |
| 536 | const ObjCMethodDecl *MethodD, |
| 537 | AnalysisManager& Mgr, |
| 538 | BugReporter &BR) { |
| 539 | SmallString<128> sbuf; |
| 540 | llvm::raw_svector_ostream os(sbuf); |
| 541 | printIvar(os, IvarD, IvarToPopertyMap); |
| 542 | os << "needs to be invalidated or set to nil"; |
| 543 | PathDiagnosticLocation MethodDecLocation = |
| 544 | PathDiagnosticLocation::createEnd(MethodD->getBody(), |
| 545 | BR.getSourceManager(), |
| 546 | Mgr.getAnalysisDeclContext(MethodD)); |
| 547 | BR.EmitBasicReport(MethodD, "Incomplete invalidation", |
| 548 | categories::CoreFoundationObjectiveC, os.str(), |
| 549 | MethodDecLocation); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 552 | void IvarInvalidationChecker::MethodCrawler::markInvalidated( |
| 553 | const ObjCIvarDecl *Iv) { |
| 554 | IvarSet::iterator I = IVars.find(Iv); |
| 555 | if (I != IVars.end()) { |
| 556 | // If InvalidationMethod is present, we are processing the message send and |
| 557 | // should ensure we are invalidating with the appropriate method, |
| 558 | // otherwise, we are processing setting to 'nil'. |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 559 | if (!InvalidationMethod || |
| 560 | (InvalidationMethod && I->second.hasMethod(InvalidationMethod))) |
| 561 | IVars.erase(I); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 562 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 565 | const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const { |
| 566 | E = E->IgnoreParenCasts(); |
| 567 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 568 | E = POE->getSyntacticForm()->IgnoreParenCasts(); |
| 569 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E)) |
| 570 | E = OVE->getSourceExpr()->IgnoreParenCasts(); |
| 571 | return E; |
| 572 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 573 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 574 | void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr( |
| 575 | const ObjCIvarRefExpr *IvarRef) { |
| 576 | if (const Decl *D = IvarRef->getDecl()) |
| 577 | markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl())); |
| 578 | } |
| 579 | |
| 580 | void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr( |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 581 | const ObjCMessageExpr *ME) { |
| 582 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 583 | if (MD) { |
| 584 | MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl()); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 585 | MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD); |
| 586 | if (IvI != PropertyGetterToIvarMap.end()) |
| 587 | markInvalidated(IvI->second); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 588 | } |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 591 | void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr( |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 592 | const ObjCPropertyRefExpr *PA) { |
| 593 | |
| 594 | if (PA->isExplicitProperty()) { |
| 595 | const ObjCPropertyDecl *PD = PA->getExplicitProperty(); |
| 596 | if (PD) { |
| 597 | PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl()); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 598 | PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD); |
| 599 | if (IvI != PropertyToIvarMap.end()) |
| 600 | markInvalidated(IvI->second); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 601 | return; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (PA->isImplicitProperty()) { |
| 606 | const ObjCMethodDecl *MD = PA->getImplicitPropertySetter(); |
| 607 | if (MD) { |
| 608 | MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl()); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 609 | MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD); |
| 610 | if (IvI != PropertyGetterToIvarMap.end()) |
| 611 | markInvalidated(IvI->second); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | } |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const { |
| 618 | E = peel(E); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 619 | |
Anna Zaks | b9733ac | 2012-10-01 20:33:58 +0000 | [diff] [blame] | 620 | return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull) |
| 621 | != Expr::NPCK_NotNull); |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) { |
| 625 | E = peel(E); |
| 626 | |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 627 | if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) { |
| 628 | checkObjCIvarRefExpr(IvarRef); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) { |
| 633 | checkObjCPropertyRefExpr(PropRef); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) { |
| 638 | checkObjCMessageExpr(MsgExpr); |
| 639 | return; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator( |
| 644 | const BinaryOperator *BO) { |
Anna Zaks | b9733ac | 2012-10-01 20:33:58 +0000 | [diff] [blame] | 645 | VisitStmt(BO); |
| 646 | |
Anna Zaks | 6503255 | 2013-01-10 23:34:16 +0000 | [diff] [blame] | 647 | // Do we assign/compare against zero? If yes, check the variable we are |
| 648 | // assigning to. |
| 649 | BinaryOperatorKind Opcode = BO->getOpcode(); |
| 650 | if (Opcode != BO_Assign && |
| 651 | Opcode != BO_EQ && |
| 652 | Opcode != BO_NE) |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 653 | return; |
| 654 | |
Anna Zaks | 6503255 | 2013-01-10 23:34:16 +0000 | [diff] [blame] | 655 | if (isZero(BO->getRHS())) { |
| 656 | check(BO->getLHS()); |
| 657 | return; |
| 658 | } |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 659 | |
Anna Zaks | 6503255 | 2013-01-10 23:34:16 +0000 | [diff] [blame] | 660 | if (Opcode != BO_Assign && isZero(BO->getLHS())) { |
| 661 | check(BO->getRHS()); |
| 662 | return; |
| 663 | } |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr( |
Anna Zaks | 664566c | 2013-01-10 22:44:16 +0000 | [diff] [blame] | 667 | const ObjCMessageExpr *ME) { |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 668 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 669 | const Expr *Receiver = ME->getInstanceReceiver(); |
| 670 | |
| 671 | // Stop if we are calling '[self invalidate]'. |
Anna Zaks | 26db7db | 2013-02-08 23:55:43 +0000 | [diff] [blame] | 672 | if (Receiver && isInvalidationMethod(MD, /*LookForPartial*/ false)) |
Anna Zaks | bbff82f | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 673 | if (Receiver->isObjCSelfExpr()) { |
| 674 | CalledAnotherInvalidationMethod = true; |
| 675 | return; |
Anna Zaks | 31f69cc | 2012-09-29 00:20:38 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | // Check if we call a setter and set the property to 'nil'. |
| 679 | if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) { |
| 680 | MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl()); |
| 681 | MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD); |
| 682 | if (IvI != PropertySetterToIvarMap.end()) { |
| 683 | markInvalidated(IvI->second); |
| 684 | return; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // Check if we call the 'invalidation' routine on the ivar. |
| 689 | if (Receiver) { |
| 690 | InvalidationMethod = MD; |
| 691 | check(Receiver->IgnoreParenCasts()); |
| 692 | InvalidationMethod = 0; |
| 693 | } |
| 694 | |
| 695 | VisitStmt(ME); |
Anna Zaks | 5bf5c2e | 2012-09-26 18:55:16 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | // Register the checker. |
| 700 | void ento::registerIvarInvalidationChecker(CheckerManager &mgr) { |
| 701 | mgr.registerChecker<IvarInvalidationChecker>(); |
| 702 | } |