blob: 3e21f016df208d26c9135701093ffe29c99356e3 [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");
86 Selector S = Ctx.Selectors.getSelector(0, &II);
87
88 ObjCMethodDecl* MD = 0;
89
90 // Scan the instance methods for "dealloc".
91 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
92 E = D->instmeth_end(); I!=E; ++I) {
93
94 if ((*I)->getSelector() == S) {
95 MD = *I;
96 break;
97 }
98 }
99
100 if (!MD) { // No dealloc found.
101
102 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000103 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
104 ? "missing -dealloc"
105 : "missing -dealloc (Hybrid MM, non-GC)");
106
Ted Kremenek724133b2008-07-03 04:29:21 +0000107 DiagCollector C(BT);
108
109 std::ostringstream os;
110 os << "Objective-C class '" << D->getName()
111 << "' lacks a 'dealloc' instance method";
112
113 Diagnostic& Diag = BR.getDiagnostic();
114 Diag.Report(&C,
115 Ctx.getFullLoc(D->getLocStart()),
116 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
117 NULL, 0, NULL, 0);
118
119 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
120 BR.EmitWarning(*I);
121
122 return;
123 }
124
125 // dealloc found. Scan for missing [super dealloc].
126 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
127
128 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000129 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
130 ? "missing [super dealloc]"
131 : "missing [super dealloc] (Hybrid MM, non-GC)");
132
Ted Kremenek724133b2008-07-03 04:29:21 +0000133 DiagCollector C(BT);
134
135 std::ostringstream os;
136 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000137 << "' does not send a 'dealloc' message to its super class"
Ted Kremenek724133b2008-07-03 04:29:21 +0000138 " (missing [super dealloc])";
139
140 Diagnostic& Diag = BR.getDiagnostic();
141 Diag.Report(&C,
142 Ctx.getFullLoc(MD->getLocStart()),
143 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
144 NULL, 0, NULL, 0);
145
146 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
147 BR.EmitWarning(*I);
148 }
149}
150