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 | // |
Ted Kremenek | a521db6 | 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 | 724133b | 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 | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | 724133b | 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 | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 45 | void clang::CheckObjCDealloc(ObjCImplementationDecl* D, |
| 46 | const LangOptions& LOpts, BugReporter& BR) { |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 48 | assert (LOpts.getGCMode() != LangOptions::GCOnly); |
| 49 | |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 50 | ASTContext& Ctx = BR.getContext(); |
Ted Kremenek | f7ff2ce | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 51 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | ea042f9 | 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 | f7ff2ce | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | ea042f9 | 2008-07-07 06:36:08 +0000 | [diff] [blame] | 58 | bool containsPointerIvar = false; |
Ted Kremenek | f7ff2ce | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | ea042f9 | 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 | |
| 63 | QualType T = (*I)->getType(); |
| 64 | |
| 65 | if (T->isPointerType() || T->isObjCQualifiedIdType()) { |
| 66 | containsPointerIvar = true; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (!containsPointerIvar) |
| 72 | return; |
Ted Kremenek | f7ff2ce | 2008-07-03 15:37:02 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 74 | // Determine if the class subclasses NSObject. |
| 75 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 76 | |
| 77 | for ( ; ID ; ID = ID->getSuperClass()) |
| 78 | if (ID->getIdentifier() == NSObjectII) |
| 79 | break; |
| 80 | |
| 81 | if (!ID) |
| 82 | return; |
| 83 | |
| 84 | // Get the "dealloc" selector. |
| 85 | IdentifierInfo* II = &Ctx.Idents.get("dealloc"); |
| 86 | Selector S = Ctx.Selectors.getSelector(0, &II); |
| 87 | |
| 88 | ObjCMethodDecl* MD = 0; |
| 89 | |
| 90 | // Scan the instance methods for "dealloc". |
| 91 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 92 | E = D->instmeth_end(); I!=E; ++I) { |
| 93 | |
| 94 | if ((*I)->getSelector() == S) { |
| 95 | MD = *I; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (!MD) { // No dealloc found. |
| 101 | |
| 102 | // FIXME: This code should be reduced to three lines if possible (Refactor). |
Ted Kremenek | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 103 | SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC |
| 104 | ? "missing -dealloc" |
| 105 | : "missing -dealloc (Hybrid MM, non-GC)"); |
| 106 | |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 107 | DiagCollector C(BT); |
| 108 | |
| 109 | std::ostringstream os; |
| 110 | os << "Objective-C class '" << D->getName() |
| 111 | << "' lacks a 'dealloc' instance method"; |
| 112 | |
| 113 | Diagnostic& Diag = BR.getDiagnostic(); |
| 114 | Diag.Report(&C, |
| 115 | Ctx.getFullLoc(D->getLocStart()), |
| 116 | Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()), |
| 117 | NULL, 0, NULL, 0); |
| 118 | |
| 119 | for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) |
| 120 | BR.EmitWarning(*I); |
| 121 | |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // dealloc found. Scan for missing [super dealloc]. |
| 126 | if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { |
| 127 | |
| 128 | // FIXME: This code should be reduced to three lines if possible (Refactor). |
Ted Kremenek | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 129 | SimpleBugType BT(LOpts.getGCMode() == LangOptions::NonGC |
| 130 | ? "missing [super dealloc]" |
| 131 | : "missing [super dealloc] (Hybrid MM, non-GC)"); |
| 132 | |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 133 | DiagCollector C(BT); |
| 134 | |
| 135 | std::ostringstream os; |
| 136 | os << "The 'dealloc' instance method in Objective-C class '" << D->getName() |
Ted Kremenek | fd32dbf | 2008-07-03 14:35:01 +0000 | [diff] [blame] | 137 | << "' does not send a 'dealloc' message to its super class" |
Ted Kremenek | 724133b | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 138 | " (missing [super dealloc])"; |
| 139 | |
| 140 | Diagnostic& Diag = BR.getDiagnostic(); |
| 141 | Diag.Report(&C, |
| 142 | Ctx.getFullLoc(MD->getLocStart()), |
| 143 | Diag.getCustomDiagID(Diagnostic::Warning, os.str().c_str()), |
| 144 | NULL, 0, NULL, 0); |
| 145 | |
| 146 | for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) |
| 147 | BR.EmitWarning(*I); |
| 148 | } |
| 149 | } |
| 150 | |