blob: 0c9100951ce1983ba2e0284d1d19435fc280e3fc [file] [log] [blame]
Ted Kremenek724133b2008-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 Kremeneka521db62008-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 Kremenek724133b2008-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 Kremenekfd32dbf2008-07-03 14:35:01 +000022#include "clang/Basic/LangOptions.h"
Ted Kremenek724133b2008-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 Kremenekfd32dbf2008-07-03 14:35:01 +000045void clang::CheckObjCDealloc(ObjCImplementationDecl* D,
46 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenek724133b2008-07-03 04:29:21 +000047
Ted Kremenekfd32dbf2008-07-03 14:35:01 +000048 assert (LOpts.getGCMode() != LangOptions::GCOnly);
49
Ted Kremenek724133b2008-07-03 04:29:21 +000050 ASTContext& Ctx = BR.getContext();
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000051 ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenekea042f92008-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 Kremenekf7ff2ce2008-07-03 15:37:02 +000057
Ted Kremenekea042f92008-07-07 06:36:08 +000058 bool containsPointerIvar = false;
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000059
Ted Kremenekea042f92008-07-07 06:36:08 +000060 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
61 I!=E; ++I) {
62
63 QualType T = (*I)->getType();
64
65 if (T->isPointerType() || T->isObjCQualifiedIdType()) {
66 containsPointerIvar = true;
67 break;
68 }
69 }
70
71 if (!containsPointerIvar)
72 return;
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000073
Ted Kremenek724133b2008-07-03 04:29:21 +000074 // Determine if the class subclasses NSObject.
75 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek724133b2008-07-03 04:29:21 +000076
77 for ( ; ID ; ID = ID->getSuperClass())
78 if (ID->getIdentifier() == NSObjectII)
79 break;
80
81 if (!ID)
82 return;
83
84 // Get the "dealloc" selector.
85 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
Ted Kremenek39c766a2008-07-14 17:40:50 +000086 Selector S = Ctx.Selectors.getSelector(0, &II);
Ted Kremenek724133b2008-07-03 04:29:21 +000087 ObjCMethodDecl* MD = 0;
88
89 // Scan the instance methods for "dealloc".
90 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
91 E = D->instmeth_end(); I!=E; ++I) {
92
93 if ((*I)->getSelector() == S) {
94 MD = *I;
95 break;
96 }
97 }
98
99 if (!MD) { // No dealloc found.
100
Ted Kremenek39c766a2008-07-14 17:40:50 +0000101 const char* name = LOpts.getGCMode() == LangOptions::NonGC
102 ? "missing -dealloc"
103 : "missing -dealloc (Hybrid MM, non-GC)";
Ted Kremenek724133b2008-07-03 04:29:21 +0000104
105 std::ostringstream os;
106 os << "Objective-C class '" << D->getName()
107 << "' lacks a 'dealloc' instance method";
108
Ted Kremenek39c766a2008-07-14 17:40:50 +0000109 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
Ted Kremenek724133b2008-07-03 04:29:21 +0000110 return;
111 }
112
113 // dealloc found. Scan for missing [super dealloc].
114 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
115
Ted Kremenek39c766a2008-07-14 17:40:50 +0000116 const char* name = LOpts.getGCMode() == LangOptions::NonGC
117 ? "missing [super dealloc]"
118 : "missing [super dealloc] (Hybrid MM, non-GC)";
Ted Kremenek724133b2008-07-03 04:29:21 +0000119
120 std::ostringstream os;
121 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000122 << "' does not send a 'dealloc' message to its super class"
Ted Kremenek724133b2008-07-03 04:29:21 +0000123 " (missing [super dealloc])";
124
Ted Kremenek39c766a2008-07-14 17:40:50 +0000125 BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart());
126 return;
127 }
Ted Kremenek724133b2008-07-03 04:29:21 +0000128}
129