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