Ted Kremenek | 724133b | 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" |
| 21 | #include <sstream> |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static bool scan_dealloc(Stmt* S, Selector Dealloc) { |
| 26 | |
| 27 | if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) |
| 28 | if (ME->getSelector() == Dealloc) |
| 29 | if (Expr* Receiver = ME->getReceiver()->IgnoreParenCasts()) |
| 30 | if (PreDefinedExpr* E = dyn_cast<PreDefinedExpr>(Receiver)) |
| 31 | if (E->getIdentType() == PreDefinedExpr::ObjCSuper) |
| 32 | return true; |
| 33 | |
| 34 | // Recurse to children. |
| 35 | |
| 36 | for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I) |
| 37 | if (*I && scan_dealloc(*I, Dealloc)) |
| 38 | return true; |
| 39 | |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, BugReporter& BR) { |
| 44 | |
| 45 | ASTContext& Ctx = BR.getContext(); |
| 46 | |
| 47 | // Determine if the class subclasses NSObject. |
| 48 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 49 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
| 50 | |
| 51 | for ( ; ID ; ID = ID->getSuperClass()) |
| 52 | if (ID->getIdentifier() == NSObjectII) |
| 53 | break; |
| 54 | |
| 55 | if (!ID) |
| 56 | return; |
| 57 | |
| 58 | // Get the "dealloc" selector. |
| 59 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
| 60 | Selector S = Ctx.Selectors.getSelector(0, &II); |
| 61 | |
| 62 | ObjCMethodDecl* MD = 0; |
| 63 | |
| 64 | // Scan the instance methods for "dealloc". |
| 65 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 66 | E = D->instmeth_end(); I!=E; ++I) { |
| 67 | |
| 68 | if ((*I)->getSelector() == S) { |
| 69 | MD = *I; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (!MD) { // No dealloc found. |
| 75 | |
| 76 | // FIXME: This code should be reduced to three lines if possible (Refactor). |
| 77 | SimpleBugType BT("missing -dealloc"); |
| 78 | DiagCollector C(BT); |
| 79 | |
| 80 | std::ostringstream os; |
| 81 | os << "Objective-C class '" << D->getName() |
| 82 | << "' lacks a 'dealloc' instance method"; |
| 83 | |
| 84 | Diagnostic& Diag = BR.getDiagnostic(); |
| 85 | Diag.Report(&C, |
| 86 | Ctx.getFullLoc(D->getLocStart()), |
| 87 | Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()), |
| 88 | NULL, 0, NULL, 0); |
| 89 | |
| 90 | for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) |
| 91 | BR.EmitWarning(*I); |
| 92 | |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | // dealloc found. Scan for missing [super dealloc]. |
| 97 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 98 | |
| 99 | // FIXME: This code should be reduced to three lines if possible (Refactor). |
| 100 | SimpleBugType BT("missing [super dealloc]"); |
| 101 | DiagCollector C(BT); |
| 102 | |
| 103 | std::ostringstream os; |
| 104 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
| 105 | << "' does not send a 'dealloc' message to it super class" |
| 106 | " (missing [super dealloc])"; |
| 107 | |
| 108 | Diagnostic& Diag = BR.getDiagnostic(); |
| 109 | Diag.Report(&C, |
| 110 | Ctx.getFullLoc(MD->getLocStart()), |
| 111 | Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()), |
| 112 | NULL, 0, NULL, 0); |
| 113 | |
| 114 | for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) |
| 115 | BR.EmitWarning(*I); |
| 116 | } |
| 117 | } |
| 118 | |