blob: 7a2a0b65f02b8b6240a9d17fd70a6c5ea62cf3e7 [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//
10// This file defines a DeadStores, a flow-sensitive checker that looks for
11// stores to variables that are no longer live.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/LocalCheckers.h"
16#include "clang/Analysis/PathDiagnostic.h"
17#include "clang/Analysis/PathSensitive/BugReporter.h"
18#include "clang/AST/ExprObjC.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/DeclObjC.h"
Ted Kremenekfd32dbf2008-07-03 14:35:01 +000021#include "clang/Basic/LangOptions.h"
Ted Kremenek724133b2008-07-03 04:29:21 +000022#include <sstream>
23
24using namespace clang;
25
26static bool scan_dealloc(Stmt* S, Selector Dealloc) {
27
28 if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S))
29 if (ME->getSelector() == Dealloc)
30 if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts())
31 if (PreDefinedExpr* E = dyn_cast<PreDefinedExpr>(Receiver))
32 if (E->getIdentType() == PreDefinedExpr::ObjCSuper)
33 return true;
34
35 // Recurse to children.
36
37 for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I)
38 if (*I && scan_dealloc(*I, Dealloc))
39 return true;
40
41 return false;
42}
43
Ted Kremenekfd32dbf2008-07-03 14:35:01 +000044void clang::CheckObjCDealloc(ObjCImplementationDecl* D,
45 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenek724133b2008-07-03 04:29:21 +000046
Ted Kremenekfd32dbf2008-07-03 14:35:01 +000047 assert (LOpts.getGCMode() != LangOptions::GCOnly);
48
Ted Kremenek724133b2008-07-03 04:29:21 +000049 ASTContext& Ctx = BR.getContext();
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000050 ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenekea042f92008-07-07 06:36:08 +000051
52 // Does the class contain any ivars that are pointers (or id<...>)?
53 // If not, skip the check entirely.
54 // NOTE: This is motivated by PR 2517:
55 // http://llvm.org/bugs/show_bug.cgi?id=2517
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000056
Ted Kremenekea042f92008-07-07 06:36:08 +000057 bool containsPointerIvar = false;
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000058
Ted Kremenekea042f92008-07-07 06:36:08 +000059 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
60 I!=E; ++I) {
61
62 QualType T = (*I)->getType();
63
64 if (T->isPointerType() || T->isObjCQualifiedIdType()) {
65 containsPointerIvar = true;
66 break;
67 }
68 }
69
70 if (!containsPointerIvar)
71 return;
Ted Kremenekf7ff2ce2008-07-03 15:37:02 +000072
Ted Kremenek724133b2008-07-03 04:29:21 +000073 // Determine if the class subclasses NSObject.
74 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek724133b2008-07-03 04:29:21 +000075
76 for ( ; ID ; ID = ID->getSuperClass())
77 if (ID->getIdentifier() == NSObjectII)
78 break;
79
80 if (!ID)
81 return;
82
83 // Get the "dealloc" selector.
84 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
85 Selector S = Ctx.Selectors.getSelector(0, &II);
86
87 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
101 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000102 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
103 ? "missing -dealloc"
104 : "missing -dealloc (Hybrid MM, non-GC)");
105
Ted Kremenek724133b2008-07-03 04:29:21 +0000106 DiagCollector C(BT);
107
108 std::ostringstream os;
109 os << "Objective-C class '" << D->getName()
110 << "' lacks a 'dealloc' instance method";
111
112 Diagnostic& Diag = BR.getDiagnostic();
113 Diag.Report(&C,
114 Ctx.getFullLoc(D->getLocStart()),
115 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
116 NULL, 0, NULL, 0);
117
118 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
119 BR.EmitWarning(*I);
120
121 return;
122 }
123
124 // dealloc found. Scan for missing [super dealloc].
125 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
126
127 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000128 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
129 ? "missing [super dealloc]"
130 : "missing [super dealloc] (Hybrid MM, non-GC)");
131
Ted Kremenek724133b2008-07-03 04:29:21 +0000132 DiagCollector C(BT);
133
134 std::ostringstream os;
135 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenekfd32dbf2008-07-03 14:35:01 +0000136 << "' does not send a 'dealloc' message to its super class"
Ted Kremenek724133b2008-07-03 04:29:21 +0000137 " (missing [super dealloc])";
138
139 Diagnostic& Diag = BR.getDiagnostic();
140 Diag.Report(&C,
141 Ctx.getFullLoc(MD->getLocStart()),
142 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
143 NULL, 0, NULL, 0);
144
145 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
146 BR.EmitWarning(*I);
147 }
148}
149