blob: 1f627216ef5d45eb39c9a8f64492013b7ba48a3a [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//
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 Kremenek3cd483c2008-07-03 14:35:01 +000021#include "clang/Basic/LangOptions.h"
Ted Kremenekdb09a4d2008-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 Kremenek3cd483c2008-07-03 14:35:01 +000044void clang::CheckObjCDealloc(ObjCImplementationDecl* D,
45 const LangOptions& LOpts, BugReporter& BR) {
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000046
Ted Kremenek3cd483c2008-07-03 14:35:01 +000047 assert (LOpts.getGCMode() != LangOptions::GCOnly);
48
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000049 ASTContext& Ctx = BR.getContext();
Ted Kremenekaeca9632008-07-03 15:37:02 +000050 ObjCInterfaceDecl* ID = D->getClassInterface();
51
52 // Does the class contain any ivars? If not, skip the check entirely.
53
54 if (ID->ivar_empty())
55 return;
56
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000057 // Determine if the class subclasses NSObject.
58 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000059
60 for ( ; ID ; ID = ID->getSuperClass())
61 if (ID->getIdentifier() == NSObjectII)
62 break;
63
64 if (!ID)
65 return;
66
67 // Get the "dealloc" selector.
68 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
69 Selector S = Ctx.Selectors.getSelector(0, &II);
70
71 ObjCMethodDecl* MD = 0;
72
73 // Scan the instance methods for "dealloc".
74 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
75 E = D->instmeth_end(); I!=E; ++I) {
76
77 if ((*I)->getSelector() == S) {
78 MD = *I;
79 break;
80 }
81 }
82
83 if (!MD) { // No dealloc found.
84
85 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenek3cd483c2008-07-03 14:35:01 +000086 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
87 ? "missing -dealloc"
88 : "missing -dealloc (Hybrid MM, non-GC)");
89
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000090 DiagCollector C(BT);
91
92 std::ostringstream os;
93 os << "Objective-C class '" << D->getName()
94 << "' lacks a 'dealloc' instance method";
95
96 Diagnostic& Diag = BR.getDiagnostic();
97 Diag.Report(&C,
98 Ctx.getFullLoc(D->getLocStart()),
99 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
100 NULL, 0, NULL, 0);
101
102 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
103 BR.EmitWarning(*I);
104
105 return;
106 }
107
108 // dealloc found. Scan for missing [super dealloc].
109 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
110
111 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000112 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
113 ? "missing [super dealloc]"
114 : "missing [super dealloc] (Hybrid MM, non-GC)");
115
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000116 DiagCollector C(BT);
117
118 std::ostringstream os;
119 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000120 << "' does not send a 'dealloc' message to its super class"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000121 " (missing [super dealloc])";
122
123 Diagnostic& Diag = BR.getDiagnostic();
124 Diag.Report(&C,
125 Ctx.getFullLoc(MD->getLocStart()),
126 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
127 NULL, 0, NULL, 0);
128
129 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
130 BR.EmitWarning(*I);
131 }
132}
133