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