Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 1 | //==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==// |
| 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 | // |
Ted Kremenek | 078c0bc | 2008-07-11 20:53:14 +0000 | [diff] [blame] | 10 | // This file defines a CheckObjCDealloc, a checker that |
| 11 | // analyzes an Objective-C class's implementation to determine if it |
| 12 | // correctly implements -dealloc. |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "clang/Analysis/LocalCheckers.h" |
| 17 | #include "clang/Analysis/PathDiagnostic.h" |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static bool scan_dealloc(Stmt* S, Selector Dealloc) { |
| 28 | |
| 29 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 30 | if (ME->getSelector() == Dealloc) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 31 | if(ME->getReceiver()) |
| 32 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 33 | return isa<ObjCSuperExpr>(Receiver); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 34 | |
| 35 | // Recurse to children. |
| 36 | |
| 37 | for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I) |
| 38 | if (*I && scan_dealloc(*I, Dealloc)) |
| 39 | return true; |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 44 | static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID, |
| 45 | const ObjCPropertyDecl* PD, |
| 46 | Selector Release, |
| 47 | IdentifierInfo* SelfII, |
| 48 | ASTContext& Ctx) { |
| 49 | |
| 50 | // [mMyIvar release] |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 51 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 52 | if (ME->getSelector() == Release) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 53 | if(ME->getReceiver()) |
| 54 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 55 | if (ObjCIvarRefExpr* E = dyn_cast<ObjCIvarRefExpr>(Receiver)) |
| 56 | if (E->getDecl() == ID) |
| 57 | return true; |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 59 | // [self setMyIvar:nil]; |
| 60 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 61 | if(ME->getReceiver()) |
| 62 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 63 | if (DeclRefExpr* E = dyn_cast<DeclRefExpr>(Receiver)) |
| 64 | if (E->getDecl()->getIdentifier() == SelfII) |
| 65 | if (ME->getMethodDecl() == PD->getSetterMethodDecl() && |
| 66 | ME->getNumArgs() == 1 && |
| 67 | ME->getArg(0)->isNullPointerConstant(Ctx)) |
| 68 | return true; |
| 69 | |
| 70 | // self.myIvar = nil; |
| 71 | if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S)) |
| 72 | if (BO->isAssignmentOp()) |
| 73 | if(ObjCPropertyRefExpr* PRE = |
| 74 | dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParenCasts())) |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 75 | if(PRE->getProperty() == PD) |
Ted Kremenek | 2c61566 | 2008-12-08 21:44:15 +0000 | [diff] [blame] | 76 | if(BO->getRHS()->isNullPointerConstant(Ctx)) { |
| 77 | // This is only a 'release' if the property kind is not |
| 78 | // 'assign'. |
| 79 | return PD->getSetterKind() != ObjCPropertyDecl::Assign;; |
| 80 | } |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 82 | // Recurse to children. |
| 83 | for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 84 | if (*I && scan_ivar_release(*I, ID, PD, Release, SelfII, Ctx)) |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 85 | return true; |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 90 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 91 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 93 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 94 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 95 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 96 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 97 | |
| 98 | // Does the class contain any ivars that are pointers (or id<...>)? |
| 99 | // If not, skip the check entirely. |
| 100 | // NOTE: This is motivated by PR 2517: |
| 101 | // http://llvm.org/bugs/show_bug.cgi?id=2517 |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 103 | bool containsPointerIvar = false; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 105 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 106 | I!=E; ++I) { |
| 107 | |
Ted Kremenek | f4ebf42 | 2008-07-15 23:04:27 +0000 | [diff] [blame] | 108 | ObjCIvarDecl* ID = *I; |
| 109 | QualType T = ID->getType(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 110 | |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 111 | if (!T->isObjCObjectPointerType() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 112 | ID->getAttr<IBOutletAttr>()) // Skip IBOutlets. |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 113 | continue; |
| 114 | |
| 115 | containsPointerIvar = true; |
| 116 | break; |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (!containsPointerIvar) |
| 120 | return; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 122 | // Determine if the class subclasses NSObject. |
| 123 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 124 | IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase"); |
| 125 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 127 | for ( ; ID ; ID = ID->getSuperClass()) { |
| 128 | IdentifierInfo *II = ID->getIdentifier(); |
| 129 | |
| 130 | if (II == NSObjectII) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 131 | break; |
Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 132 | |
| 133 | // FIXME: For now, ignore classes that subclass SenTestCase, as these don't |
| 134 | // need to implement -dealloc. They implement tear down in another way, |
| 135 | // which we should try and catch later. |
| 136 | // http://llvm.org/bugs/show_bug.cgi?id=3187 |
| 137 | if (II == SenTestCaseII) |
| 138 | return; |
| 139 | } |
| 140 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 141 | if (!ID) |
| 142 | return; |
| 143 | |
| 144 | // Get the "dealloc" selector. |
| 145 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 146 | Selector S = Ctx.Selectors.getSelector(0, &II); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 147 | ObjCMethodDecl* MD = 0; |
| 148 | |
| 149 | // Scan the instance methods for "dealloc". |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 150 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 151 | E = D->instmeth_end(); I!=E; ++I) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 152 | |
| 153 | if ((*I)->getSelector() == S) { |
| 154 | MD = *I; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (!MD) { // No dealloc found. |
| 160 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 161 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 162 | ? "missing -dealloc" |
| 163 | : "missing -dealloc (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 165 | std::string buf; |
| 166 | llvm::raw_string_ostream os(buf); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 167 | os << "Objective-C class '" << D->getNameAsString() |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 168 | << "' lacks a 'dealloc' instance method"; |
| 169 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 170 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
| 174 | // dealloc found. Scan for missing [super dealloc]. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 175 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 177 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 178 | ? "missing [super dealloc]" |
| 179 | : "missing [super dealloc] (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 181 | std::string buf; |
| 182 | llvm::raw_string_ostream os(buf); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 183 | os << "The 'dealloc' instance method in Objective-C class '" |
| 184 | << D->getNameAsString() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 185 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 186 | " (missing [super dealloc])"; |
| 187 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 188 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
| 189 | return; |
| 190 | } |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 191 | |
| 192 | // Get the "release" selector. |
| 193 | IdentifierInfo* RII = &Ctx.Idents.get("release"); |
| 194 | Selector RS = Ctx.Selectors.getSelector(0, &RII); |
| 195 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 196 | // Get the "self" identifier |
| 197 | IdentifierInfo* SelfII = &Ctx.Idents.get("self"); |
| 198 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 199 | // Scan for missing and extra releases of ivars used by implementations |
| 200 | // of synthesized properties |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 201 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 202 | E = D->propimpl_end(); I!=E; ++I) { |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 203 | |
| 204 | // We can only check the synthesized properties |
| 205 | if((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 206 | continue; |
| 207 | |
| 208 | ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl(); |
| 209 | if (!ID) |
| 210 | continue; |
| 211 | |
| 212 | QualType T = ID->getType(); |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 213 | if (!T->isObjCObjectPointerType()) // Skip non-pointer ivars |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 214 | continue; |
| 215 | |
| 216 | const ObjCPropertyDecl* PD = (*I)->getPropertyDecl(); |
| 217 | if(!PD) |
| 218 | continue; |
| 219 | |
| 220 | // ivars cannot be set via read-only properties, so we'll skip them |
| 221 | if(PD->isReadOnly()) |
| 222 | continue; |
| 223 | |
| 224 | // ivar must be released if and only if the kind of setter was not 'assign' |
| 225 | bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 226 | if(scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 227 | != requiresRelease) { |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 228 | const char *name; |
| 229 | const char* category = "Memory (Core Foundation/Objective-C)"; |
| 230 | |
| 231 | std::string buf; |
| 232 | llvm::raw_string_ostream os(buf); |
| 233 | |
| 234 | if(requiresRelease) { |
| 235 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 236 | ? "missing ivar release (leak)" |
| 237 | : "missing ivar release (Hybrid MM, non-GC)"; |
| 238 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 239 | os << "The '" << ID->getNameAsString() |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 240 | << "' instance variable was retained by a synthesized property but " |
| 241 | "wasn't released in 'dealloc'"; |
| 242 | } else { |
| 243 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 244 | ? "extra ivar release (use-after-release)" |
| 245 | : "extra ivar release (Hybrid MM, non-GC)"; |
| 246 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 247 | os << "The '" << ID->getNameAsString() |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 248 | << "' instance variable was not retained by a synthesized property " |
| 249 | "but was released in 'dealloc'"; |
| 250 | } |
| 251 | |
| 252 | BR.EmitBasicReport(name, category, |
| 253 | os.str().c_str(), (*I)->getLocation()); |
| 254 | } |
| 255 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 256 | } |
| 257 | |