1 //==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "clang/GR/Checkers/LocalCheckers.h"
17 #include "clang/GR/BugReporter/PathDiagnostic.h"
18 #include "clang/GR/BugReporter/BugReporter.h"
19 #include "clang/AST/ExprObjC.h"
20 #include "clang/AST/Expr.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/Basic/LangOptions.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang
;
27 static bool scan_dealloc(Stmt
* S
, Selector Dealloc
) {
29 if (ObjCMessageExpr
* ME
= dyn_cast
<ObjCMessageExpr
>(S
))
30 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;
39 // Recurse to children.
41 for (Stmt::child_iterator I
= S
->child_begin(), E
= S
->child_end(); I
!=E
; ++I
)
42 if (*I
&& scan_dealloc(*I
, Dealloc
))
48 static bool scan_ivar_release(Stmt
* S
, ObjCIvarDecl
* ID
,
49 const ObjCPropertyDecl
* PD
,
51 IdentifierInfo
* SelfII
,
55 if (ObjCMessageExpr
* ME
= dyn_cast
<ObjCMessageExpr
>(S
))
56 if (ME
->getSelector() == Release
)
57 if (ME
->getInstanceReceiver())
58 if (Expr
* Receiver
= ME
->getInstanceReceiver()->IgnoreParenCasts())
59 if (ObjCIvarRefExpr
* E
= dyn_cast
<ObjCIvarRefExpr
>(Receiver
))
60 if (E
->getDecl() == ID
)
63 // [self setMyIvar:nil];
64 if (ObjCMessageExpr
* ME
= dyn_cast
<ObjCMessageExpr
>(S
))
65 if (ME
->getInstanceReceiver())
66 if (Expr
* Receiver
= ME
->getInstanceReceiver()->IgnoreParenCasts())
67 if (DeclRefExpr
* E
= dyn_cast
<DeclRefExpr
>(Receiver
))
68 if (E
->getDecl()->getIdentifier() == SelfII
)
69 if (ME
->getMethodDecl() == PD
->getSetterMethodDecl() &&
70 ME
->getNumArgs() == 1 &&
71 ME
->getArg(0)->isNullPointerConstant(Ctx
,
72 Expr::NPC_ValueDependentIsNull
))
76 if (BinaryOperator
* BO
= dyn_cast
<BinaryOperator
>(S
))
77 if (BO
->isAssignmentOp())
78 if (ObjCPropertyRefExpr
* PRE
=
79 dyn_cast
<ObjCPropertyRefExpr
>(BO
->getLHS()->IgnoreParenCasts()))
80 if (PRE
->isExplicitProperty() && PRE
->getExplicitProperty() == PD
)
81 if (BO
->getRHS()->isNullPointerConstant(Ctx
,
82 Expr::NPC_ValueDependentIsNull
)) {
83 // This is only a 'release' if the property kind is not
85 return PD
->getSetterKind() != ObjCPropertyDecl::Assign
;;
88 // Recurse to children.
89 for (Stmt::child_iterator I
= S
->child_begin(), E
= S
->child_end(); I
!=E
; ++I
)
90 if (*I
&& scan_ivar_release(*I
, ID
, PD
, Release
, SelfII
, Ctx
))
96 void clang::CheckObjCDealloc(const ObjCImplementationDecl
* D
,
97 const LangOptions
& LOpts
, BugReporter
& BR
) {
99 assert (LOpts
.getGCMode() != LangOptions::GCOnly
);
101 ASTContext
& Ctx
= BR
.getContext();
102 const ObjCInterfaceDecl
* ID
= D
->getClassInterface();
104 // 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
109 bool containsPointerIvar
= false;
111 for (ObjCInterfaceDecl::ivar_iterator I
=ID
->ivar_begin(), E
=ID
->ivar_end();
114 ObjCIvarDecl
* ID
= *I
;
115 QualType T
= ID
->getType();
117 if (!T
->isObjCObjectPointerType() ||
118 ID
->getAttr
<IBOutletAttr
>() || // Skip IBOutlets.
119 ID
->getAttr
<IBOutletCollectionAttr
>()) // Skip IBOutletCollections.
122 containsPointerIvar
= true;
126 if (!containsPointerIvar
)
129 // Determine if the class subclasses NSObject.
130 IdentifierInfo
* NSObjectII
= &Ctx
.Idents
.get("NSObject");
131 IdentifierInfo
* SenTestCaseII
= &Ctx
.Idents
.get("SenTestCase");
134 for ( ; ID
; ID
= ID
->getSuperClass()) {
135 IdentifierInfo
*II
= ID
->getIdentifier();
137 if (II
== NSObjectII
)
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
)
151 // Get the "dealloc" selector.
152 IdentifierInfo
* II
= &Ctx
.Idents
.get("dealloc");
153 Selector S
= Ctx
.Selectors
.getSelector(0, &II
);
154 ObjCMethodDecl
* MD
= 0;
156 // Scan the instance methods for "dealloc".
157 for (ObjCImplementationDecl::instmeth_iterator I
= D
->instmeth_begin(),
158 E
= D
->instmeth_end(); I
!=E
; ++I
) {
160 if ((*I
)->getSelector() == S
) {
166 if (!MD
) { // No dealloc found.
168 const char* name
= LOpts
.getGCMode() == LangOptions::NonGC
170 : "missing -dealloc (Hybrid MM, non-GC)";
173 llvm::raw_string_ostream
os(buf
);
174 os
<< "Objective-C class '" << D
<< "' lacks a 'dealloc' instance method";
176 BR
.EmitBasicReport(name
, os
.str(), D
->getLocStart());
180 // dealloc found. Scan for missing [super dealloc].
181 if (MD
->getBody() && !scan_dealloc(MD
->getBody(), S
)) {
183 const char* name
= LOpts
.getGCMode() == LangOptions::NonGC
184 ? "missing [super dealloc]"
185 : "missing [super dealloc] (Hybrid MM, non-GC)";
188 llvm::raw_string_ostream
os(buf
);
189 os
<< "The 'dealloc' instance method in Objective-C class '" << D
190 << "' does not send a 'dealloc' message to its super class"
191 " (missing [super dealloc])";
193 BR
.EmitBasicReport(name
, os
.str(), D
->getLocStart());
197 // Get the "release" selector.
198 IdentifierInfo
* RII
= &Ctx
.Idents
.get("release");
199 Selector RS
= Ctx
.Selectors
.getSelector(0, &RII
);
201 // Get the "self" identifier
202 IdentifierInfo
* SelfII
= &Ctx
.Idents
.get("self");
204 // Scan for missing and extra releases of ivars used by implementations
205 // of synthesized properties
206 for (ObjCImplementationDecl::propimpl_iterator I
= D
->propimpl_begin(),
207 E
= D
->propimpl_end(); I
!=E
; ++I
) {
209 // We can only check the synthesized properties
210 if ((*I
)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize
)
213 ObjCIvarDecl
* ID
= (*I
)->getPropertyIvarDecl();
217 QualType T
= ID
->getType();
218 if (!T
->isObjCObjectPointerType()) // Skip non-pointer ivars
221 const ObjCPropertyDecl
* PD
= (*I
)->getPropertyDecl();
225 // ivars cannot be set via read-only properties, so we'll skip them
226 if (PD
->isReadOnly())
229 // ivar must be released if and only if the kind of setter was not 'assign'
230 bool requiresRelease
= PD
->getSetterKind() != ObjCPropertyDecl::Assign
;
231 if (scan_ivar_release(MD
->getBody(), ID
, PD
, RS
, SelfII
, Ctx
)
232 != requiresRelease
) {
234 const char* category
= "Memory (Core Foundation/Objective-C)";
237 llvm::raw_string_ostream
os(buf
);
239 if (requiresRelease
) {
240 name
= LOpts
.getGCMode() == LangOptions::NonGC
241 ? "missing ivar release (leak)"
242 : "missing ivar release (Hybrid MM, non-GC)";
245 << "' instance variable was retained by a synthesized property but "
246 "wasn't released in 'dealloc'";
248 name
= LOpts
.getGCMode() == LangOptions::NonGC
249 ? "extra ivar release (use-after-release)"
250 : "extra ivar release (Hybrid MM, non-GC)";
253 << "' instance variable was not retained by a synthesized property "
254 "but was released in 'dealloc'";
257 BR
.EmitBasicReport(name
, category
, os
.str(), (*I
)->getLocation());