| 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 |  | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 100 | static void checkObjCDealloc(const CheckerBase *Checker, | 
|  | 101 | const ObjCImplementationDecl *D, | 
|  | 102 | const LangOptions &LOpts, BugReporter &BR) { | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 103 |  | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 104 | assert (LOpts.getGC() != LangOptions::GCOnly); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 |  | 
| Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 106 | ASTContext &Ctx = BR.getContext(); | 
|  | 107 | const ObjCInterfaceDecl *ID = D->getClassInterface(); | 
| 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 | // Does the class contain any ivars that are pointers (or id<...>)? | 
|  | 110 | // If not, skip the check entirely. | 
|  | 111 | // NOTE: This is motivated by PR 2517: | 
|  | 112 | //        http://llvm.org/bugs/show_bug.cgi?id=2517 | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 |  | 
| Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 114 | bool containsPointerIvar = false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 |  | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 116 | for (const auto *Ivar : ID->ivars()) { | 
|  | 117 | QualType T = Ivar->getType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 |  | 
| Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 119 | if (!T->isObjCObjectPointerType() || | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 120 | Ivar->hasAttr<IBOutletAttr>() || // Skip IBOutlets. | 
|  | 121 | Ivar->hasAttr<IBOutletCollectionAttr>()) // Skip IBOutletCollections. | 
| Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 122 | continue; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 |  | 
| Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 124 | containsPointerIvar = true; | 
|  | 125 | break; | 
| Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 126 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 |  | 
| Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 128 | if (!containsPointerIvar) | 
|  | 129 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 131 | // Determine if the class subclasses NSObject. | 
|  | 132 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); | 
| Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 133 | IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase"); | 
|  | 134 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 |  | 
| Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 136 | for ( ; ID ; ID = ID->getSuperClass()) { | 
|  | 137 | IdentifierInfo *II = ID->getIdentifier(); | 
|  | 138 |  | 
|  | 139 | if (II == NSObjectII) | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 140 | break; | 
| Ted Kremenek | 8cb6fb3 | 2009-02-11 07:10:07 +0000 | [diff] [blame] | 141 |  | 
|  | 142 | // FIXME: For now, ignore classes that subclass SenTestCase, as these don't | 
|  | 143 | // need to implement -dealloc.  They implement tear down in another way, | 
|  | 144 | // which we should try and catch later. | 
|  | 145 | //  http://llvm.org/bugs/show_bug.cgi?id=3187 | 
|  | 146 | if (II == SenTestCaseII) | 
|  | 147 | return; | 
|  | 148 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 150 | if (!ID) | 
|  | 151 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 153 | // Get the "dealloc" selector. | 
|  | 154 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | Selector S = Ctx.Selectors.getSelector(0, &II); | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 156 | const ObjCMethodDecl *MD = 0; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 158 | // Scan the instance methods for "dealloc". | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 159 | for (const auto *I : D->instance_methods()) { | 
|  | 160 | if (I->getSelector() == S) { | 
|  | 161 | MD = I; | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 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 |  | 
| Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 166 | PathDiagnosticLocation DLoc = | 
|  | 167 | PathDiagnosticLocation::createBegin(D, BR.getSourceManager()); | 
|  | 168 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 169 | if (!MD) { // No dealloc found. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 |  | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 171 | const char* name = LOpts.getGC() == LangOptions::NonGC | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | ? "missing -dealloc" | 
| Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 173 | : "missing -dealloc (Hybrid MM, non-GC)"; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 175 | std::string buf; | 
|  | 176 | llvm::raw_string_ostream os(buf); | 
| Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 177 | os << "Objective-C class '" << *D << "' lacks a 'dealloc' instance method"; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 |  | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 179 | BR.EmitBasicReport(D, Checker, name, categories::CoreFoundationObjectiveC, | 
| Ted Kremenek | 6fd4505 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 180 | os.str(), DLoc); | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 181 | return; | 
|  | 182 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 |  | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 184 | // dealloc found.  Scan for missing [super dealloc]. | 
| Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 185 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 |  | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 187 | const char* name = LOpts.getGC() == LangOptions::NonGC | 
| Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 188 | ? "missing [super dealloc]" | 
|  | 189 | : "missing [super dealloc] (Hybrid MM, non-GC)"; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 191 | std::string buf; | 
|  | 192 | llvm::raw_string_ostream os(buf); | 
| Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 193 | os << "The 'dealloc' instance method in Objective-C class '" << *D | 
| Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 194 | << "' does not send a 'dealloc' message to its super class" | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 195 | " (missing [super dealloc])"; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 |  | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 197 | BR.EmitBasicReport(MD, Checker, name, categories::CoreFoundationObjectiveC, | 
| Ted Kremenek | 6fd4505 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 198 | os.str(), DLoc); | 
| Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 199 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 202 | // Get the "release" selector. | 
|  | 203 | IdentifierInfo* RII = &Ctx.Idents.get("release"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | Selector RS = Ctx.Selectors.getSelector(0, &RII); | 
|  | 205 |  | 
| Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 206 | // Get the "self" identifier | 
|  | 207 | IdentifierInfo* SelfII = &Ctx.Idents.get("self"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 209 | // Scan for missing and extra releases of ivars used by implementations | 
|  | 210 | // of synthesized properties | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 211 | for (const auto *I : D->property_impls()) { | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 212 | // We can only check the synthesized properties | 
| David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 213 | if (I->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 214 | continue; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 |  | 
| David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 216 | ObjCIvarDecl *ID = I->getPropertyIvarDecl(); | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 217 | if (!ID) | 
|  | 218 | continue; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 220 | QualType T = ID->getType(); | 
| Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 221 | if (!T->isObjCObjectPointerType()) // Skip non-pointer ivars | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 222 | continue; | 
|  | 223 |  | 
| David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 224 | const ObjCPropertyDecl *PD = I->getPropertyDecl(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | if (!PD) | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 226 | continue; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 228 | // 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] | 229 | if (PD->isReadOnly()) | 
| Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 230 | continue; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 |  | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 232 | // ivar must be released if and only if the kind of setter was not 'assign' | 
|  | 233 | bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | if (scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx) | 
| Ted Kremenek | d3b25c5 | 2008-10-30 15:13:43 +0000 | [diff] [blame] | 235 | != requiresRelease) { | 
| Ted Kremenek | 6fd4505 | 2012-04-05 20:43:28 +0000 | [diff] [blame] | 236 | const char *name = 0; | 
| 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) { | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 241 | name = LOpts.getGC() == LangOptions::NonGC | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 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 | b8989f2 | 2011-10-14 18:45:37 +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 { | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 249 | name = LOpts.getGC() == LangOptions::NonGC | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 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 | b8989f2 | 2011-10-14 18:45:37 +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 |  | 
| Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 258 | PathDiagnosticLocation SDLoc = | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 259 | PathDiagnosticLocation::createBegin(I, BR.getSourceManager()); | 
| Anna Zaks | 590dd8e | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 260 |  | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 261 | BR.EmitBasicReport(MD, Checker, name, | 
|  | 262 | categories::CoreFoundationObjectiveC, os.str(), SDLoc); | 
| Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 263 | } | 
|  | 264 | } | 
| Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// | 
|  | 268 | // ObjCDeallocChecker | 
|  | 269 | //===----------------------------------------------------------------------===// | 
|  | 270 |  | 
|  | 271 | namespace { | 
| Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 272 | class ObjCDeallocChecker : public Checker< | 
| Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 273 | check::ASTDecl<ObjCImplementationDecl> > { | 
|  | 274 | public: | 
|  | 275 | void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr, | 
|  | 276 | BugReporter &BR) const { | 
| David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 277 | if (mgr.getLangOpts().getGC() == LangOptions::GCOnly) | 
| Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 278 | return; | 
| Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 279 | checkObjCDealloc(this, cast<ObjCImplementationDecl>(D), mgr.getLangOpts(), | 
|  | 280 | BR); | 
| Argyrios Kyrtzidis | 7dd445e | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 281 | } | 
|  | 282 | }; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | void ento::registerObjCDeallocChecker(CheckerManager &mgr) { | 
|  | 286 | mgr.registerChecker<ObjCDeallocChecker>(); | 
|  | 287 | } |