blob: 6da52baed2fe881ded66c22d264dc54d2d09f917 [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
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 Kremenek3cd483c2008-07-03 14:35:01 +000022#include "clang/Basic/LangOptions.h"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000023#include <sstream>
24
25using namespace clang;
26
27static bool scan_dealloc(Stmt* S, Selector Dealloc) {
28
29 if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S))
30 if (ME->getSelector() == Dealloc)
31 if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts())
32 if (PreDefinedExpr* E = dyn_cast<PreDefinedExpr>(Receiver))
33 if (E->getIdentType() == PreDefinedExpr::ObjCSuper)
34 return true;
35
36 // Recurse to children.
37
38 for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I)
39 if (*I && scan_dealloc(*I, Dealloc))
40 return true;
41
42 return false;
43}
44
Ted Kremenekf071e182008-07-24 17:45:56 +000045static bool isSEL(QualType T, IdentifierInfo* SelII) {
46 if (const TypedefType* Ty = T->getAsTypedefType())
47 return Ty->getDecl()->getIdentifier() == SelII;
48
49 return false;
50}
51
Ted Kremenek3cd483c2008-07-03 14:35:01 +000052void clang::CheckObjCDealloc(ObjCImplementationDecl* D,
53 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000054
Ted Kremenek3cd483c2008-07-03 14:35:01 +000055 assert (LOpts.getGCMode() != LangOptions::GCOnly);
56
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000057 ASTContext& Ctx = BR.getContext();
Ted Kremenekaeca9632008-07-03 15:37:02 +000058 ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek00fed8d2008-07-07 06:36:08 +000059
60 // Does the class contain any ivars that are pointers (or id<...>)?
61 // If not, skip the check entirely.
62 // NOTE: This is motivated by PR 2517:
63 // http://llvm.org/bugs/show_bug.cgi?id=2517
Ted Kremenekaeca9632008-07-03 15:37:02 +000064
Ted Kremenek00fed8d2008-07-07 06:36:08 +000065 bool containsPointerIvar = false;
Ted Kremenekf071e182008-07-24 17:45:56 +000066 IdentifierInfo* SelII = &Ctx.Idents.get("SEL");
Ted Kremenekaeca9632008-07-03 15:37:02 +000067
Ted Kremenek00fed8d2008-07-07 06:36:08 +000068 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
69 I!=E; ++I) {
70
Ted Kremenekf4ebf422008-07-15 23:04:27 +000071 ObjCIvarDecl* ID = *I;
72 QualType T = ID->getType();
Ted Kremenek00fed8d2008-07-07 06:36:08 +000073
Ted Kremenekf4ebf422008-07-15 23:04:27 +000074 if ((T->isPointerType() || T->isObjCQualifiedIdType()) &&
Ted Kremenekf071e182008-07-24 17:45:56 +000075 (ID->getAttr<IBOutletAttr>() == 0 && // Skip IBOutlets.
76 !isSEL(T, SelII))) { // Skip SEL ivars.
Ted Kremenek00fed8d2008-07-07 06:36:08 +000077 containsPointerIvar = true;
78 break;
79 }
80 }
81
82 if (!containsPointerIvar)
83 return;
Ted Kremenekaeca9632008-07-03 15:37:02 +000084
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000085 // Determine if the class subclasses NSObject.
86 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000087
88 for ( ; ID ; ID = ID->getSuperClass())
89 if (ID->getIdentifier() == NSObjectII)
90 break;
91
92 if (!ID)
93 return;
94
95 // Get the "dealloc" selector.
96 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
Ted Kremenek57202072008-07-14 17:40:50 +000097 Selector S = Ctx.Selectors.getSelector(0, &II);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000098 ObjCMethodDecl* MD = 0;
99
100 // Scan the instance methods for "dealloc".
101 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
102 E = D->instmeth_end(); I!=E; ++I) {
103
104 if ((*I)->getSelector() == S) {
105 MD = *I;
106 break;
107 }
108 }
109
110 if (!MD) { // No dealloc found.
111
Ted Kremenek57202072008-07-14 17:40:50 +0000112 const char* name = LOpts.getGCMode() == LangOptions::NonGC
113 ? "missing -dealloc"
114 : "missing -dealloc (Hybrid MM, non-GC)";
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000115
116 std::ostringstream os;
117 os << "Objective-C class '" << D->getName()
118 << "' lacks a 'dealloc' instance method";
119
Ted Kremenek57202072008-07-14 17:40:50 +0000120 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000121 return;
122 }
123
124 // dealloc found. Scan for missing [super dealloc].
125 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
126
Ted Kremenek57202072008-07-14 17:40:50 +0000127 const char* name = LOpts.getGCMode() == LangOptions::NonGC
128 ? "missing [super dealloc]"
129 : "missing [super dealloc] (Hybrid MM, non-GC)";
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000130
131 std::ostringstream os;
132 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000133 << "' does not send a 'dealloc' message to its super class"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000134 " (missing [super dealloc])";
135
Ted Kremenek57202072008-07-14 17:40:50 +0000136 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
137 return;
138 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000139}
140