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 | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 23 | #include <sstream> |
| 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()) |
| 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 45 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 46 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 48 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 49 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 50 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 51 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 52 | |
| 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 Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 58 | bool containsPointerIvar = false; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 60 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 61 | I!=E; ++I) { |
| 62 | |
Ted Kremenek | f4ebf42 | 2008-07-15 23:04:27 +0000 | [diff] [blame] | 63 | ObjCIvarDecl* ID = *I; |
| 64 | QualType T = ID->getType(); |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 66 | if (!Ctx.isObjCObjectPointerType(T) || |
Ted Kremenek | a95acd6 | 2008-07-25 18:17:35 +0000 | [diff] [blame^] | 67 | ID->getAttr<IBOutletAttr>()) // Skip IBOutlets. |
Ted Kremenek | 684b9d2 | 2008-07-25 17:04:49 +0000 | [diff] [blame] | 68 | continue; |
| 69 | |
| 70 | containsPointerIvar = true; |
| 71 | break; |
Ted Kremenek | 00fed8d | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (!containsPointerIvar) |
| 75 | return; |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 77 | // Determine if the class subclasses NSObject. |
| 78 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 79 | |
| 80 | for ( ; ID ; ID = ID->getSuperClass()) |
| 81 | if (ID->getIdentifier() == NSObjectII) |
| 82 | break; |
| 83 | |
| 84 | if (!ID) |
| 85 | return; |
| 86 | |
| 87 | // Get the "dealloc" selector. |
| 88 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 89 | Selector S = Ctx.Selectors.getSelector(0, &II); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 90 | ObjCMethodDecl* MD = 0; |
| 91 | |
| 92 | // Scan the instance methods for "dealloc". |
| 93 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 94 | E = D->instmeth_end(); I!=E; ++I) { |
| 95 | |
| 96 | if ((*I)->getSelector() == S) { |
| 97 | MD = *I; |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (!MD) { // No dealloc found. |
| 103 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 104 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 105 | ? "missing -dealloc" |
| 106 | : "missing -dealloc (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 107 | |
| 108 | std::ostringstream os; |
| 109 | os << "Objective-C class '" << D->getName() |
| 110 | << "' lacks a 'dealloc' instance method"; |
| 111 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 112 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | // dealloc found. Scan for missing [super dealloc]. |
| 117 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 118 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 119 | const char* name = LOpts.getGCMode() == LangOptions::NonGC |
| 120 | ? "missing [super dealloc]" |
| 121 | : "missing [super dealloc] (Hybrid MM, non-GC)"; |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 122 | |
| 123 | std::ostringstream os; |
| 124 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 125 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 126 | " (missing [super dealloc])"; |
| 127 | |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 128 | BR.EmitBasicReport(name, os.str().c_str(), D->getLocStart()); |
| 129 | return; |
| 130 | } |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 131 | } |
| 132 | |