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" |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | enum IVarState { Unused, Used }; |
| 27 | typedef llvm::DenseMap<ObjCIvarDecl*,IVarState> IvarUsageMap; |
| 28 | |
| 29 | static void Scan(IvarUsageMap& M, Stmt* S) { |
| 30 | if (!S) |
| 31 | return; |
| 32 | |
| 33 | if (ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 34 | ObjCIvarDecl* D = Ex->getDecl(); |
| 35 | IvarUsageMap::iterator I = M.find(D); |
| 36 | if (I != M.end()) I->second = Used; |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 37 | return; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 38 | } |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 39 | |
| 40 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E;++I) |
| 41 | Scan(M, *I); |
| 42 | } |
| 43 | |
| 44 | static void Scan(IvarUsageMap& M, ObjCPropertyImplDecl* D) { |
| 45 | if (!D) |
| 46 | return; |
| 47 | |
| 48 | ObjCIvarDecl* ID = D->getPropertyIvarDecl(); |
| 49 | |
| 50 | if (!ID) |
| 51 | return; |
| 52 | |
| 53 | IvarUsageMap::iterator I = M.find(ID); |
| 54 | if (I != M.end()) I->second = Used; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) { |
| 58 | |
| 59 | ObjCInterfaceDecl* ID = D->getClassInterface(); |
| 60 | IvarUsageMap M; |
| 61 | |
| 62 | |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 63 | ASTContext &Ctx = BR.getContext(); |
| 64 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 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. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 86 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(Ctx), |
| 87 | E = D->instmeth_end(Ctx); I!=E; ++I) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame^] | 88 | Scan(M, (*I)->getBody()); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 92 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(Ctx), |
| 93 | E = D->propimpl_end(Ctx); I!=E; ++I) |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 99 | std::string sbuf; |
| 100 | llvm::raw_string_ostream os(sbuf); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 101 | os << "Instance variable '" << I->first->getNameAsString() |
| 102 | << "' in class '" << ID->getNameAsString() |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 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 | 13493ea | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 106 | BR.EmitBasicReport("Unused instance variable", "Optimization", |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 107 | os.str().c_str(), I->first->getLocation()); |
| 108 | } |
| 109 | } |
| 110 | |