Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 1 | //==- 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 Kremenek | 078c0bc | 2008-07-11 20:53:14 +0000 | [diff] [blame] | 10 | // 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 Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 13 | // |
| 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static 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()) |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 32 | if (PredefinedExpr* E = dyn_cast<PredefinedExpr>(Receiver)) |
| 33 | if (E->getIdentType() == PredefinedExpr::ObjCSuper) |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 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 Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 45 | static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID, Selector Release ) { |
| 46 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 47 | if (ME->getSelector() == Release) |
| 48 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 49 | if (ObjCIvarRefExpr* E = dyn_cast<ObjCIvarRefExpr>(Receiver)) |
| 50 | if (E->getDecl() == ID) |
| 51 | return true; |
| 52 | |
| 53 | // Recurse to children. |
| 54 | for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I) |
| 55 | if (*I && scan_ivar_release(*I, ID, Release)) |
| 56 | return true; |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 61 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 62 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 64 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 65 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 66 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 67 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 68 | |
| 69 | // Does the class contain any ivars that are pointers (or id<...>)? |
| 70 | // If not, skip the check entirely. |
| 71 | // NOTE: This is motivated by PR 2517: |
| 72 | // http://llvm.org/bugs/show_bug.cgi?id=2517 |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 74 | bool containsPointerIvar = false; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 76 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 77 | I!=E; ++I) { |
| 78 | |
Ted Kremenek | f4ebf42 | 2008-07-15 23:04:27 +0000 | [diff] [blame] | 79 | ObjCIvarDecl* ID = *I; |
| 80 | QualType T = ID->getType(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 82 | if (!Ctx.isObjCObjectPointerType(T) || |
Ted Kremenek | a95acd6 | 2008-07-25 18:17:35 +0000 | [diff] [blame] | 83 | ID->getAttr<IBOutletAttr>()) // Skip IBOutlets. |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 84 | continue; |
| 85 | |
| 86 | containsPointerIvar = true; |
| 87 | break; |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | if (!containsPointerIvar) |
| 91 | return; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 93 | // Determine if the class subclasses NSObject. |
| 94 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 95 | |
| 96 | for ( ; ID ; ID = ID->getSuperClass()) |
| 97 | if (ID->getIdentifier() == NSObjectII) |
| 98 | break; |
| 99 | |
| 100 | if (!ID) |
| 101 | return; |
| 102 | |
| 103 | // Get the "dealloc" selector. |
| 104 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 105 | Selector S = Ctx.Selectors.getSelector(0, &II); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 106 | ObjCMethodDecl* MD = 0; |
| 107 | |
| 108 | // Scan the instance methods for "dealloc". |
| 109 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 110 | E = D->instmeth_end(); I!=E; ++I) { |
| 111 | |
| 112 | if ((*I)->getSelector() == S) { |
| 113 | MD = *I; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (!MD) { // No dealloc found. |
| 119 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 120 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 121 | ? "missing -dealloc" |
| 122 | : "missing -dealloc (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 124 | std::string buf; |
| 125 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 126 | os << "Objective-C class '" << D->getName() |
| 127 | << "' lacks a 'dealloc' instance method"; |
| 128 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 129 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
| 133 | // dealloc found. Scan for missing [super dealloc]. |
| 134 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 135 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 136 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 137 | ? "missing [super dealloc]" |
| 138 | : "missing [super dealloc] (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 140 | std::string buf; |
| 141 | llvm::raw_string_ostream os(buf); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 142 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 143 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 144 | " (missing [super dealloc])"; |
| 145 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 146 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
| 147 | return; |
| 148 | } |
Ted Kremenek | 6f2bb36 | 2008-10-29 04:30:28 +0000 | [diff] [blame] | 149 | |
| 150 | // Get the "release" selector. |
| 151 | IdentifierInfo* RII = &Ctx.Idents.get("release"); |
| 152 | Selector RS = Ctx.Selectors.getSelector(0, &RII); |
| 153 | |
| 154 | // Scan for missing and extra releases of ivars used by implementations |
| 155 | // of synthesized properties |
| 156 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 157 | E = D->propimpl_end(); I!=E; ++I) { |
| 158 | |
| 159 | // We can only check the synthesized properties |
| 160 | if((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) |
| 161 | continue; |
| 162 | |
| 163 | ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl(); |
| 164 | if (!ID) |
| 165 | continue; |
| 166 | |
| 167 | QualType T = ID->getType(); |
| 168 | if (!Ctx.isObjCObjectPointerType(T)) // Skip non-pointer ivars |
| 169 | continue; |
| 170 | |
| 171 | const ObjCPropertyDecl* PD = (*I)->getPropertyDecl(); |
| 172 | if(!PD) |
| 173 | continue; |
| 174 | |
| 175 | // ivars cannot be set via read-only properties, so we'll skip them |
| 176 | if(PD->isReadOnly()) |
| 177 | continue; |
| 178 | |
| 179 | // ivar must be released if and only if the kind of setter was not 'assign' |
| 180 | bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; |
| 181 | if(scan_ivar_release(MD->getBody(), ID, RS) != requiresRelease) { |
| 182 | const char *name; |
| 183 | const char* category = "Memory (Core Foundation/Objective-C)"; |
| 184 | |
| 185 | std::string buf; |
| 186 | llvm::raw_string_ostream os(buf); |
| 187 | |
| 188 | if(requiresRelease) { |
| 189 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 190 | ? "missing ivar release (leak)" |
| 191 | : "missing ivar release (Hybrid MM, non-GC)"; |
| 192 | |
| 193 | os << "The '" << ID->getName() |
| 194 | << "' instance variable was retained by a synthesized property but " |
| 195 | "wasn't released in 'dealloc'"; |
| 196 | } else { |
| 197 | name = LOpts.getGCMode() == LangOptions::NonGC |
| 198 | ? "extra ivar release (use-after-release)" |
| 199 | : "extra ivar release (Hybrid MM, non-GC)"; |
| 200 | |
| 201 | os << "The '" << ID->getName() |
| 202 | << "' instance variable was not retained by a synthesized property " |
| 203 | "but was released in 'dealloc'"; |
| 204 | } |
| 205 | |
| 206 | BR.EmitBasicReport(name, category, |
| 207 | os.str().c_str(), (*I)->getLocation()); |
| 208 | } |
| 209 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 210 | } |
| 211 | |