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