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 | // |
| 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 22 | #include <sstream> |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 44 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 45 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 47 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 48 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 49 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | aeca963 | 2008-07-03 15:37:02 +0000 | [diff] [blame^] | 50 | 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 Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 57 | // Determine if the class subclasses NSObject. |
| 58 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 59 | |
| 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 86 | SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC |
| 87 | ? "missing -dealloc" |
| 88 | : "missing -dealloc (Hybrid MM, non-GC)"); |
| 89 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 90 | 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 Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 112 | SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC |
| 113 | ? "missing [super dealloc]" |
| 114 | : "missing [super dealloc] (Hybrid MM, non-GC)"); |
| 115 | |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 116 | DiagCollector C(BT); |
| 117 | |
| 118 | std::ostringstream os; |
| 119 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | 3cd483c | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 120 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | db09a4d | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 121 | " (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 | |