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()) |
| 33 | if (PredefinedExpr* E = dyn_cast<PredefinedExpr>(Receiver)) |
| 34 | if (E->getIdentType() == PredefinedExpr::ObjCSuper) |
| 35 | return true; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 36 | |
| 37 | // Recurse to children. |
| 38 | |
| 39 | for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I) |
| 40 | if (*I && scan_dealloc(*I, Dealloc)) |
| 41 | return true; |
| 42 | |
| 43 | return false; |
| 44 | } |
| 45 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 46 | static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID, |
| 47 | const ObjCPropertyDecl* PD, |
| 48 | Selector Release, |
| 49 | IdentifierInfo* SelfII, |
| 50 | ASTContext& Ctx) { |
| 51 | |
| 52 | // [mMyIvar release] |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 53 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 54 | if (ME->getSelector() == Release) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 55 | if(ME->getReceiver()) |
| 56 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 57 | if (ObjCIvarRefExpr* E = dyn_cast<ObjCIvarRefExpr>(Receiver)) |
| 58 | if (E->getDecl() == ID) |
| 59 | return true; |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 61 | // [self setMyIvar:nil]; |
| 62 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 63 | if(ME->getReceiver()) |
| 64 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 65 | if (DeclRefExpr* E = dyn_cast<DeclRefExpr>(Receiver)) |
| 66 | if (E->getDecl()->getIdentifier() == SelfII) |
| 67 | if (ME->getMethodDecl() == PD->getSetterMethodDecl() && |
| 68 | ME->getNumArgs() == 1 && |
| 69 | ME->getArg(0)->isNullPointerConstant(Ctx)) |
| 70 | return true; |
| 71 | |
| 72 | // self.myIvar = nil; |
| 73 | if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S)) |
| 74 | if (BO->isAssignmentOp()) |
| 75 | if(ObjCPropertyRefExpr* PRE = |
| 76 | dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParenCasts())) |
Ted Kremenek | 8bdf9b9 | 2008-10-30 22:28:48 +0000 | [diff] [blame^] | 77 | if(PRE->getKind() == ObjCPropertyRefExpr::PropertyRef && |
| 78 | PRE->getProperty() == PD) |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 79 | if(BO->getRHS()->isNullPointerConstant(Ctx)) |
| 80 | return true; |
| 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 | |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 111 | if (!Ctx.isObjCObjectPointerType(T) || |
Ted Kremenek | a95acd6 | 2008-07-25 18:17:35 +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 | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 124 | |
| 125 | for ( ; ID ; ID = ID->getSuperClass()) |
| 126 | if (ID->getIdentifier() == NSObjectII) |
| 127 | break; |
| 128 | |
| 129 | if (!ID) |
| 130 | return; |
| 131 | |
| 132 | // Get the "dealloc" selector. |
| 133 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 134 | Selector S = Ctx.Selectors.getSelector(0, &II); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 135 | ObjCMethodDecl* MD = 0; |
| 136 | |
| 137 | // Scan the instance methods for "dealloc". |
| 138 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 139 | E = D->instmeth_end(); I!=E; ++I) { |
| 140 | |
| 141 | if ((*I)->getSelector() == S) { |
| 142 | MD = *I; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (!MD) { // No dealloc found. |
| 148 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 149 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 150 | ? "missing -dealloc" |
| 151 | : "missing -dealloc (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 153 | std::string buf; |
| 154 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 155 | os << "Objective-C class '" << D->getName() |
| 156 | << "' lacks a 'dealloc' instance method"; |
| 157 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 158 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | // dealloc found. Scan for missing [super dealloc]. |
| 163 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 164 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 165 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 166 | ? "missing [super dealloc]" |
| 167 | : "missing [super dealloc] (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 169 | std::string buf; |
| 170 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 171 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 172 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 173 | " (missing [super dealloc])"; |
| 174 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 175 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
| 176 | return; |
| 177 | } |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 178 | |
| 179 | // Get the "release" selector. |
| 180 | IdentifierInfo* RII = &Ctx.Idents.get("release"); |
| 181 | Selector RS = Ctx.Selectors.getSelector(0, &RII); |
| 182 | |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 183 | // Get the "self" identifier |
| 184 | IdentifierInfo* SelfII = &Ctx.Idents.get("self"); |
| 185 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 186 | // Scan for missing and extra releases of ivars used by implementations |
| 187 | // of synthesized properties |
| 188 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 189 | E = D->propimpl_end(); I!=E; ++I) { |
| 190 | |
| 191 | // We can only check the synthesized properties |
| 192 | if((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 193 | continue; |
| 194 | |
| 195 | ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl(); |
| 196 | if (!ID) |
| 197 | continue; |
| 198 | |
| 199 | QualType T = ID->getType(); |
| 200 | if (!Ctx.isObjCObjectPointerType(T)) // Skip non-pointer ivars |
| 201 | continue; |
| 202 | |
| 203 | const ObjCPropertyDecl* PD = (*I)->getPropertyDecl(); |
| 204 | if(!PD) |
| 205 | continue; |
| 206 | |
| 207 | // ivars cannot be set via read-only properties, so we'll skip them |
| 208 | if(PD->isReadOnly()) |
| 209 | continue; |
| 210 | |
| 211 | // ivar must be released if and only if the kind of setter was not 'assign' |
| 212 | bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; |
Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 213 | if(scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx) |
| 214 | != requiresRelease) { |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 215 | const char *name; |
| 216 | const char* category = "Memory (Core Foundation/Objective-C)"; |
| 217 | |
| 218 | std::string buf; |
| 219 | llvm::raw_string_ostream os(buf); |
| 220 | |
| 221 | if(requiresRelease) { |
| 222 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 223 | ? "missing ivar release (leak)" |
| 224 | : "missing ivar release (Hybrid MM, non-GC)"; |
| 225 | |
| 226 | os << "The '" << ID->getName() |
| 227 | << "' instance variable was retained by a synthesized property but " |
| 228 | "wasn't released in 'dealloc'"; |
| 229 | } else { |
| 230 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 231 | ? "extra ivar release (use-after-release)" |
| 232 | : "extra ivar release (Hybrid MM, non-GC)"; |
| 233 | |
| 234 | os << "The '" << ID->getName() |
| 235 | << "' instance variable was not retained by a synthesized property " |
| 236 | "but was released in 'dealloc'"; |
| 237 | } |
| 238 | |
| 239 | BR.EmitBasicReport(name, category, |
| 240 | os.str().c_str(), (*I)->getLocation()); |
| 241 | } |
| 242 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 243 | } |
| 244 | |