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