blob: 8ee6daa5c8a0fbe0c097a93711065805a9108267 [file] [log] [blame]
Ted Kremenekdb09a4d2008-07-03 04:29:21 +00001//==- 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 Kremenek078c0bc2008-07-11 20:53:14 +000010// 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 Kremenekdb09a4d2008-07-03 04:29:21 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek97053092010-01-26 22:59:55 +000016#include "clang/Checker/Checkers/LocalCheckers.h"
Ted Kremenek6b676302010-01-25 17:10:22 +000017#include "clang/Checker/BugReporter/PathDiagnostic.h"
18#include "clang/Checker/BugReporter/BugReporter.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000019#include "clang/AST/ExprObjC.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/DeclObjC.h"
Ted Kremenek3cd483c2008-07-03 14:35:01 +000022#include "clang/Basic/LangOptions.h"
Ted Kremenek6f2bb362008-10-29 04:30:28 +000023#include "llvm/Support/raw_ostream.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000024
25using namespace clang;
26
Mike Stump1eb44332009-09-09 15:08:12 +000027static bool scan_dealloc(Stmt* S, Selector Dealloc) {
28
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000029 if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S))
Douglas Gregor04badcf2010-04-21 00:45:42 +000030 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 Kremenekdb09a4d2008-07-03 04:29:21 +000038
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 Stump1eb44332009-09-09 15:08:12 +000044
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000045 return false;
46}
47
Mike Stump1eb44332009-09-09 15:08:12 +000048static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID,
49 const ObjCPropertyDecl* PD,
50 Selector Release,
Ted Kremenekd3b25c52008-10-30 15:13:43 +000051 IdentifierInfo* SelfII,
Mike Stump1eb44332009-09-09 15:08:12 +000052 ASTContext& Ctx) {
53
Ted Kremenekd3b25c52008-10-30 15:13:43 +000054 // [mMyIvar release]
Ted Kremenek6f2bb362008-10-29 04:30:28 +000055 if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S))
56 if (ME->getSelector() == Release)
Douglas Gregor04badcf2010-04-21 00:45:42 +000057 if (ME->getInstanceReceiver())
58 if (Expr* Receiver = ME->getInstanceReceiver()->IgnoreParenCasts())
Ted Kremenekd3b25c52008-10-30 15:13:43 +000059 if (ObjCIvarRefExpr* E = dyn_cast<ObjCIvarRefExpr>(Receiver))
60 if (E->getDecl() == ID)
61 return true;
Ted Kremenek6f2bb362008-10-29 04:30:28 +000062
Ted Kremenekd3b25c52008-10-30 15:13:43 +000063 // [self setMyIvar:nil];
64 if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S))
Douglas Gregor04badcf2010-04-21 00:45:42 +000065 if (ME->getInstanceReceiver())
66 if (Expr* Receiver = ME->getInstanceReceiver()->IgnoreParenCasts())
Ted Kremenekd3b25c52008-10-30 15:13:43 +000067 if (DeclRefExpr* E = dyn_cast<DeclRefExpr>(Receiver))
68 if (E->getDecl()->getIdentifier() == SelfII)
69 if (ME->getMethodDecl() == PD->getSetterMethodDecl() &&
70 ME->getNumArgs() == 1 &&
Douglas Gregorce940492009-09-25 04:25:58 +000071 ME->getArg(0)->isNullPointerConstant(Ctx,
72 Expr::NPC_ValueDependentIsNull))
Ted Kremenekd3b25c52008-10-30 15:13:43 +000073 return true;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremenekd3b25c52008-10-30 15:13:43 +000075 // self.myIvar = nil;
76 if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S))
77 if (BO->isAssignmentOp())
Mike Stump1eb44332009-09-09 15:08:12 +000078 if (ObjCPropertyRefExpr* PRE =
John McCall12f78a62010-12-02 01:19:52 +000079 dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParenCasts()))
80 if (PRE->isExplicitProperty() && PRE->getExplicitProperty() == PD)
Douglas Gregorce940492009-09-25 04:25:58 +000081 if (BO->getRHS()->isNullPointerConstant(Ctx,
82 Expr::NPC_ValueDependentIsNull)) {
Ted Kremenek2c615662008-12-08 21:44:15 +000083 // This is only a 'release' if the property kind is not
84 // 'assign'.
85 return PD->getSetterKind() != ObjCPropertyDecl::Assign;;
86 }
Mike Stump1eb44332009-09-09 15:08:12 +000087
Ted Kremenek6f2bb362008-10-29 04:30:28 +000088 // Recurse to children.
89 for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I)
Ted Kremenekd3b25c52008-10-30 15:13:43 +000090 if (*I && scan_ivar_release(*I, ID, PD, Release, SelfII, Ctx))
Ted Kremenek6f2bb362008-10-29 04:30:28 +000091 return true;
92
93 return false;
94}
95
Ted Kremenek23760022009-08-21 23:58:43 +000096void clang::CheckObjCDealloc(const ObjCImplementationDecl* D,
Ted Kremenek3cd483c2008-07-03 14:35:01 +000097 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000098
Ted Kremenek3cd483c2008-07-03 14:35:01 +000099 assert (LOpts.getGCMode() != LangOptions::GCOnly);
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000101 ASTContext& Ctx = BR.getContext();
Ted Kremenek23760022009-08-21 23:58:43 +0000102 const ObjCInterfaceDecl* ID = D->getClassInterface();
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek00fed8d2008-07-07 06:36:08 +0000104 // 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 Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek00fed8d2008-07-07 06:36:08 +0000109 bool containsPointerIvar = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek00fed8d2008-07-07 06:36:08 +0000111 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
112 I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenekf4ebf422008-07-15 23:04:27 +0000114 ObjCIvarDecl* ID = *I;
115 QualType T = ID->getType();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Steve Narofff4954562009-07-16 15:41:00 +0000117 if (!T->isObjCObjectPointerType() ||
Ted Kremenek857e9182010-05-19 17:38:06 +0000118 ID->getAttr<IBOutletAttr>() || // Skip IBOutlets.
119 ID->getAttr<IBOutletCollectionAttr>()) // Skip IBOutletCollections.
Ted Kremenek684b9d22008-07-25 17:04:49 +0000120 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenek684b9d22008-07-25 17:04:49 +0000122 containsPointerIvar = true;
123 break;
Ted Kremenek00fed8d2008-07-07 06:36:08 +0000124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek00fed8d2008-07-07 06:36:08 +0000126 if (!containsPointerIvar)
127 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000129 // Determine if the class subclasses NSObject.
130 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek8cb6fb32009-02-11 07:10:07 +0000131 IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase");
132
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek8cb6fb32009-02-11 07:10:07 +0000134 for ( ; ID ; ID = ID->getSuperClass()) {
135 IdentifierInfo *II = ID->getIdentifier();
136
137 if (II == NSObjectII)
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000138 break;
Ted Kremenek8cb6fb32009-02-11 07:10:07 +0000139
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 Stump1eb44332009-09-09 15:08:12 +0000147
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000148 if (!ID)
149 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000151 // Get the "dealloc" selector.
152 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
Mike Stump1eb44332009-09-09 15:08:12 +0000153 Selector S = Ctx.Selectors.getSelector(0, &II);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000154 ObjCMethodDecl* MD = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000156 // Scan the instance methods for "dealloc".
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000157 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
158 E = D->instmeth_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000160 if ((*I)->getSelector() == S) {
161 MD = *I;
162 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000163 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000164 }
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000166 if (!MD) { // No dealloc found.
Mike Stump1eb44332009-09-09 15:08:12 +0000167
168 const char* name = LOpts.getGCMode() == LangOptions::NonGC
169 ? "missing -dealloc"
Ted Kremenek57202072008-07-14 17:40:50 +0000170 : "missing -dealloc (Hybrid MM, non-GC)";
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000172 std::string buf;
173 llvm::raw_string_ostream os(buf);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000174 os << "Objective-C class '" << D << "' lacks a 'dealloc' instance method";
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Benjamin Kramerf0171732009-11-29 18:27:55 +0000176 BR.EmitBasicReport(name, os.str(), D->getLocStart());
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000177 return;
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000180 // dealloc found. Scan for missing [super dealloc].
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000181 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek57202072008-07-14 17:40:50 +0000183 const char* name = LOpts.getGCMode() == LangOptions::NonGC
184 ? "missing [super dealloc]"
185 : "missing [super dealloc] (Hybrid MM, non-GC)";
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000187 std::string buf;
188 llvm::raw_string_ostream os(buf);
Benjamin Kramer900fc632010-04-17 09:33:03 +0000189 os << "The 'dealloc' instance method in Objective-C class '" << D
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000190 << "' does not send a 'dealloc' message to its super class"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000191 " (missing [super dealloc])";
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Benjamin Kramerf0171732009-11-29 18:27:55 +0000193 BR.EmitBasicReport(name, os.str(), D->getLocStart());
Ted Kremenek57202072008-07-14 17:40:50 +0000194 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000195 }
196
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000197 // Get the "release" selector.
198 IdentifierInfo* RII = &Ctx.Idents.get("release");
Mike Stump1eb44332009-09-09 15:08:12 +0000199 Selector RS = Ctx.Selectors.getSelector(0, &RII);
200
Ted Kremenekd3b25c52008-10-30 15:13:43 +0000201 // Get the "self" identifier
202 IdentifierInfo* SelfII = &Ctx.Idents.get("self");
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000204 // Scan for missing and extra releases of ivars used by implementations
205 // of synthesized properties
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000206 for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
207 E = D->propimpl_end(); I!=E; ++I) {
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000208
209 // We can only check the synthesized properties
Mike Stump1eb44332009-09-09 15:08:12 +0000210 if ((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000211 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000213 ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl();
214 if (!ID)
215 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000217 QualType T = ID->getType();
Steve Narofff4954562009-07-16 15:41:00 +0000218 if (!T->isObjCObjectPointerType()) // Skip non-pointer ivars
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000219 continue;
220
221 const ObjCPropertyDecl* PD = (*I)->getPropertyDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000222 if (!PD)
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000223 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000225 // ivars cannot be set via read-only properties, so we'll skip them
Mike Stump1eb44332009-09-09 15:08:12 +0000226 if (PD->isReadOnly())
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000227 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000229 // ivar must be released if and only if the kind of setter was not 'assign'
230 bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign;
Mike Stump1eb44332009-09-09 15:08:12 +0000231 if (scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx)
Ted Kremenekd3b25c52008-10-30 15:13:43 +0000232 != requiresRelease) {
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000233 const char *name;
234 const char* category = "Memory (Core Foundation/Objective-C)";
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000236 std::string buf;
237 llvm::raw_string_ostream os(buf);
238
Mike Stump1eb44332009-09-09 15:08:12 +0000239 if (requiresRelease) {
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000240 name = LOpts.getGCMode() == LangOptions::NonGC
241 ? "missing ivar release (leak)"
242 : "missing ivar release (Hybrid MM, non-GC)";
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Benjamin Kramer900fc632010-04-17 09:33:03 +0000244 os << "The '" << ID
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000245 << "' instance variable was retained by a synthesized property but "
Mike Stump1eb44332009-09-09 15:08:12 +0000246 "wasn't released in 'dealloc'";
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000247 } else {
248 name = LOpts.getGCMode() == LangOptions::NonGC
249 ? "extra ivar release (use-after-release)"
250 : "extra ivar release (Hybrid MM, non-GC)";
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Benjamin Kramer900fc632010-04-17 09:33:03 +0000252 os << "The '" << ID
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000253 << "' instance variable was not retained by a synthesized property "
254 "but was released in 'dealloc'";
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Benjamin Kramerf0171732009-11-29 18:27:55 +0000257 BR.EmitBasicReport(name, category, os.str(), (*I)->getLocation());
Ted Kremenek6f2bb362008-10-29 04:30:28 +0000258 }
259 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000260}
261