Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 1 | //==- CheckObjCUnusedIVars.cpp - Check for unused ivars ----------*- 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 CheckObjCUnusedIvars, a checker that |
| 11 | // analyzes an Objective-C class's interface/implementation to determine if it |
| 12 | // has any ivars that are never accessed. |
| 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" |
| 22 | #include "clang/Basic/LangOptions.h" |
| 23 | #include <sstream> |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | enum IVarState { Unused, Used }; |
| 28 | typedef llvm::DenseMap<ObjCIvarDecl*,IVarState> IvarUsageMap; |
| 29 | |
| 30 | static void Scan(IvarUsageMap& M, Stmt* S) { |
| 31 | if (!S) |
| 32 | return; |
| 33 | |
| 34 | if (ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 35 | ObjCIvarDecl* D = Ex->getDecl(); |
| 36 | IvarUsageMap::iterator I = M.find(D); |
| 37 | if (I != M.end()) I->second = Used; |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 38 | return; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 39 | } |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 40 | |
| 41 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E;++I) |
| 42 | Scan(M, *I); |
| 43 | } |
| 44 | |
| 45 | static void Scan(IvarUsageMap& M, ObjCPropertyImplDecl* D) { |
| 46 | if (!D) |
| 47 | return; |
| 48 | |
| 49 | ObjCIvarDecl* ID = D->getPropertyIvarDecl(); |
| 50 | |
| 51 | if (!ID) |
| 52 | return; |
| 53 | |
| 54 | IvarUsageMap::iterator I = M.find(ID); |
| 55 | if (I != M.end()) I->second = Used; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) { |
| 59 | |
| 60 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
| 61 | IvarUsageMap M; |
| 62 | |
| 63 | |
| 64 | |
| 65 | // Iterate over the ivars. |
| 66 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 67 | I!=E; ++I) { |
| 68 | |
| 69 | ObjCIvarDecl* ID = *I; |
| 70 | |
| 71 | // Ignore ivars that aren't private. |
Ted Kremenek | 6678f7f | 2008-07-23 17:14:39 +0000 | [diff] [blame] | 72 | if (ID->getAccessControl() != ObjCIvarDecl::Private) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 73 | continue; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 74 | |
| 75 | // Skip IB Outlets. |
| 76 | if (ID->getAttr<IBOutletAttr>()) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 77 | continue; |
| 78 | |
| 79 | M[ID] = Unused; |
| 80 | } |
| 81 | |
| 82 | if (M.empty()) |
| 83 | return; |
| 84 | |
| 85 | // Now scan the methods for accesses. |
| 86 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 87 | E = D->instmeth_end(); I!=E; ++I) |
| 88 | Scan(M, (*I)->getBody()); |
| 89 | |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 90 | // Scan for @synthesized property methods that act as setters/getters |
| 91 | // to an ivar. |
| 92 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 93 | E = D->propimpl_end(); I!=E; ++I) |
| 94 | Scan(M, *I); |
| 95 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 96 | // Find ivars that are unused. |
| 97 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 98 | if (I->second == Unused) { |
| 99 | |
| 100 | std::ostringstream os; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 101 | os << "Instance variable '" << I->first->getName() |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 102 | << "' in class '" << ID->getName() |
| 103 | << "' is never used by the methods in its @implementation " |
| 104 | "(although it may be used by category methods)."; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 0b00785 | 2008-09-21 18:58:22 +0000 | [diff] [blame] | 106 | BR.EmitBasicReport("unused ivar", "Optimization", |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 107 | os.str().c_str(), I->first->getLocation()); |
| 108 | } |
| 109 | } |
| 110 | |