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