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