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())) |
Ted Kremenek | 8bdf9b9 | 2008-10-30 22:28:48 +0000 | [diff] [blame] | 75 | if(PRE->getKind() == ObjCPropertyRefExpr::PropertyRef && |
| 76 | PRE->getProperty() == PD) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 77 | if(BO->getRHS()->isNullPointerConstant(Ctx)) |
| 78 | return true; |
| 79 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 80 | // Recurse to children. |
| 81 | 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] | 82 | if (*I && scan_ivar_release(*I, ID, PD, Release, SelfII, Ctx)) |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 83 | return true; |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 88 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 89 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 91 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 92 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 93 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 94 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 95 | |
| 96 | // Does the class contain any ivars that are pointers (or id<...>)? |
| 97 | // If not, skip the check entirely. |
| 98 | // NOTE: This is motivated by PR 2517: |
| 99 | // http://llvm.org/bugs/show_bug.cgi?id=2517 |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 101 | bool containsPointerIvar = false; |
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 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 104 | I!=E; ++I) { |
| 105 | |
Ted Kremenek | f4ebf42 | 2008-07-15 23:04:27 +0000 | [diff] [blame] | 106 | ObjCIvarDecl* ID = *I; |
| 107 | QualType T = ID->getType(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 109 | if (!Ctx.isObjCObjectPointerType(T) || |
Ted Kremenek | a95acd6 | 2008-07-25 18:17:35 +0000 | [diff] [blame] | 110 | ID->getAttr<IBOutletAttr>()) // Skip IBOutlets. |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 111 | continue; |
| 112 | |
| 113 | containsPointerIvar = true; |
| 114 | break; |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (!containsPointerIvar) |
| 118 | return; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 120 | // Determine if the class subclasses NSObject. |
| 121 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 122 | |
| 123 | for ( ; ID ; ID = ID->getSuperClass()) |
| 124 | if (ID->getIdentifier() == NSObjectII) |
| 125 | break; |
| 126 | |
| 127 | if (!ID) |
| 128 | return; |
| 129 | |
| 130 | // Get the "dealloc" selector. |
| 131 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 132 | Selector S = Ctx.Selectors.getSelector(0, &II); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 133 | ObjCMethodDecl* MD = 0; |
| 134 | |
| 135 | // Scan the instance methods for "dealloc". |
| 136 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 137 | E = D->instmeth_end(); I!=E; ++I) { |
| 138 | |
| 139 | if ((*I)->getSelector() == S) { |
| 140 | MD = *I; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!MD) { // No dealloc found. |
| 146 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 147 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 148 | ? "missing -dealloc" |
| 149 | : "missing -dealloc (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 151 | std::string buf; |
| 152 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 153 | os << "Objective-C class '" << D->getName() |
| 154 | << "' lacks a 'dealloc' instance method"; |
| 155 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 156 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
| 160 | // dealloc found. Scan for missing [super dealloc]. |
| 161 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 162 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 163 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 164 | ? "missing [super dealloc]" |
| 165 | : "missing [super dealloc] (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 167 | std::string buf; |
| 168 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 169 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 170 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 171 | " (missing [super dealloc])"; |
| 172 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 173 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
| 174 | return; |
| 175 | } |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 176 | |
| 177 | // Get the "release" selector. |
| 178 | IdentifierInfo* RII = &Ctx.Idents.get("release"); |
| 179 | Selector RS = Ctx.Selectors.getSelector(0, &RII); |
| 180 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 181 | // Get the "self" identifier |
| 182 | IdentifierInfo* SelfII = &Ctx.Idents.get("self"); |
| 183 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 184 | // Scan for missing and extra releases of ivars used by implementations |
| 185 | // of synthesized properties |
| 186 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 187 | E = D->propimpl_end(); I!=E; ++I) { |
| 188 | |
| 189 | // We can only check the synthesized properties |
| 190 | if((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 191 | continue; |
| 192 | |
| 193 | ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl(); |
| 194 | if (!ID) |
| 195 | continue; |
| 196 | |
| 197 | QualType T = ID->getType(); |
| 198 | if (!Ctx.isObjCObjectPointerType(T)) // Skip non-pointer ivars |
| 199 | continue; |
| 200 | |
| 201 | const ObjCPropertyDecl* PD = (*I)->getPropertyDecl(); |
| 202 | if(!PD) |
| 203 | continue; |
| 204 | |
| 205 | // ivars cannot be set via read-only properties, so we'll skip them |
| 206 | if(PD->isReadOnly()) |
| 207 | continue; |
| 208 | |
| 209 | // ivar must be released if and only if the kind of setter was not 'assign' |
| 210 | bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 211 | if(scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx) |
| 212 | != requiresRelease) { |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 213 | const char *name; |
| 214 | const char* category = "Memory (Core Foundation/Objective-C)"; |
| 215 | |
| 216 | std::string buf; |
| 217 | llvm::raw_string_ostream os(buf); |
| 218 | |
| 219 | if(requiresRelease) { |
| 220 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 221 | ? "missing ivar release (leak)" |
| 222 | : "missing ivar release (Hybrid MM, non-GC)"; |
| 223 | |
| 224 | os << "The '" << ID->getName() |
| 225 | << "' instance variable was retained by a synthesized property but " |
| 226 | "wasn't released in 'dealloc'"; |
| 227 | } else { |
| 228 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 229 | ? "extra ivar release (use-after-release)" |
| 230 | : "extra ivar release (Hybrid MM, non-GC)"; |
| 231 | |
| 232 | os << "The '" << ID->getName() |
| 233 | << "' instance variable was not retained by a synthesized property " |
| 234 | "but was released in 'dealloc'"; |
| 235 | } |
| 236 | |
| 237 | BR.EmitBasicReport(name, category, |
| 238 | os.str().c_str(), (*I)->getLocation()); |
| 239 | } |
| 240 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 241 | } |
| 242 | |