blob: 38736dff3deb978740d8bccd0615f0e29452b77e [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();
50
51 // Determine if the class subclasses NSObject.
52 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
53 ObjCInterfaceDecl* ID = D->getClassInterface();
54
55 for ( ; ID ; ID = ID->getSuperClass())
56 if (ID->getIdentifier() == NSObjectII)
57 break;
58
59 if (!ID)
60 return;
61
62 // Get the "dealloc" selector.
63 IdentifierInfo* II = &Ctx.Idents.get("dealloc");
64 Selector S = Ctx.Selectors.getSelector(0, &II);
65
66 ObjCMethodDecl* MD = 0;
67
68 // Scan the instance methods for "dealloc".
69 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
70 E = D->instmeth_end(); I!=E; ++I) {
71
72 if ((*I)->getSelector() == S) {
73 MD = *I;
74 break;
75 }
76 }
77
78 if (!MD) { // No dealloc found.
79
80 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenek3cd483c2008-07-03 14:35:01 +000081 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
82 ? "missing -dealloc"
83 : "missing -dealloc (Hybrid MM, non-GC)");
84
Ted Kremenekdb09a4d2008-07-03 04:29:21 +000085 DiagCollector C(BT);
86
87 std::ostringstream os;
88 os << "Objective-C class '" << D->getName()
89 << "' lacks a 'dealloc' instance method";
90
91 Diagnostic& Diag = BR.getDiagnostic();
92 Diag.Report(&C,
93 Ctx.getFullLoc(D->getLocStart()),
94 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
95 NULL, 0, NULL, 0);
96
97 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
98 BR.EmitWarning(*I);
99
100 return;
101 }
102
103 // dealloc found. Scan for missing [super dealloc].
104 if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
105
106 // FIXME: This code should be reduced to three lines if possible (Refactor).
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000107 SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC
108 ? "missing [super dealloc]"
109 : "missing [super dealloc] (Hybrid MM, non-GC)");
110
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000111 DiagCollector C(BT);
112
113 std::ostringstream os;
114 os << "The 'dealloc' instance method in Objective-C class '" << D->getName()
Ted Kremenek3cd483c2008-07-03 14:35:01 +0000115 << "' does not send a 'dealloc' message to its super class"
Ted Kremenekdb09a4d2008-07-03 04:29:21 +0000116 " (missing [super dealloc])";
117
118 Diagnostic& Diag = BR.getDiagnostic();
119 Diag.Report(&C,
120 Ctx.getFullLoc(MD->getLocStart()),
121 Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()),
122 NULL, 0, NULL, 0);
123
124 for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I)
125 BR.EmitWarning(*I);
126 }
127}
128