blob: 7bb0015462bb99b6a578841f9ffa1b7b3f2ae02c [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 Kremenek3cd483c2008-07-03 14:35:01 +000045void clang::CheckObjCDealloc(ObjCImplementationDecl* D,
46 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000047
Ted Kremenek3cd483c2008-07-03 14:35:01 +000048 assert (LOpts.getGCMode() != LangOptions::GCOnly);
49
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000050 ASTContext& Ctx = BR.getContext();
Ted Kremenekaeca9632008-07-03 15:37:02 +000051 ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek00fed8d2008-07-07 06:36:08 +000052
53 // Does the class contain any ivars that are pointers (or id<...>)?
54 // If not, skip the check entirely.
55 // NOTE: This is motivated by PR 2517:
56 // http://llvm.org/bugs/show_bug.cgi?id=2517
Ted Kremenekaeca9632008-07-03 15:37:02 +000057
Ted Kremenek00fed8d2008-07-07 06:36:08 +000058 bool containsPointerIvar = false;
Ted Kremenekaeca9632008-07-03 15:37:02 +000059
Ted Kremenek00fed8d2008-07-07 06:36:08 +000060 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
61 I!=E; ++I) {
62
Ted Kremenekf4ebf422008-07-15 23:04:27 +000063 ObjCIvarDecl* ID = *I;
64 QualType T = ID->getType();
Ted Kremenek00fed8d2008-07-07 06:36:08 +000065
Ted Kremenekf4ebf422008-07-15 23:04:27 +000066 if ((T->isPointerType() || T->isObjCQualifiedIdType()) &&
67 ID->getAttr<IBOutletAttr>() == 0) { // Skip IBOutlets.
Ted Kremenek00fed8d2008-07-07 06:36:08 +000068 containsPointerIvar = true;
69 break;
70 }
71 }
72
73 if (!containsPointerIvar)
74 return;
Ted Kremenekaeca9632008-07-03 15:37:02 +000075
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000076 // Determine if the class subclasses NSObject.
77 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000078
79 for ( ; ID ; ID = ID->getSuperClass())
80 if (ID->getIdentifier() == NSObjectII)
81 break;
82
83 if (!ID)
84 return;
85
86 // Get the "dealloc" selector.
87 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
Ted Kremenek57202072008-07-14 17:40:50 +000088 Selector S = Ctx.Selectors.getSelector(0, &II);
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000089 ObjCMethodDecl* MD = 0;
90
91 // Scan the instance methods for "dealloc".
92 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
93 E = D->instmeth_end(); I!=E; ++I) {
94
95 if ((*I)->getSelector() == S) {
96 MD = *I;
97 break;
98 }
99 }
100
101 if (!MD) { // No dealloc found.
102
Ted Kremenek57202072008-07-14 17:40:50 +0000103 const char* name = LOpts.getGCMode() == LangOptions::NonGC
104 ? "missing -dealloc"
105 : "missing -dealloc (Hybrid MM, non-GC)";
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000106
107 std::ostringstream os;
108 os << "Objective-C class '" << D->getName()
109 << "' lacks a 'dealloc' instance method";
110
Ted Kremenek57202072008-07-14 17:40:50 +0000111 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000112 return;
113 }
114
115 // dealloc found. Scan for missing [super dealloc].
116 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
117
Ted Kremenek57202072008-07-14 17:40:50 +0000118 const char* name = LOpts.getGCMode() == LangOptions::NonGC
119 ? "missing [super dealloc]"
120 : "missing [super dealloc] (Hybrid MM, non-GC)";
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000121
122 std::ostringstream os;
123 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000124 << "' does not send a 'dealloc' message to its super class"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000125 " (missing [super dealloc])";
126
Ted Kremenek57202072008-07-14 17:40:50 +0000127 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
128 return;
129 }
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000130}
131