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 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 62 | // Iterate over the ivars. |
| 63 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end(); |
| 64 | I!=E; ++I) { |
| 65 | |
| 66 | ObjCIvarDecl* ID = *I; |
| 67 | |
| 68 | // Ignore ivars that aren't private. |
Ted Kremenek | 6678f7f | 2008-07-23 17:14:39 +0000 | [diff] [blame] | 69 | if (ID->getAccessControl() != ObjCIvarDecl::Private) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 70 | continue; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 71 | |
| 72 | // Skip IB Outlets. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 73 | if (ID->getAttr<IBOutletAttr>()) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 74 | continue; |
| 75 | |
| 76 | M[ID] = Unused; |
| 77 | } |
| 78 | |
| 79 | if (M.empty()) |
| 80 | return; |
| 81 | |
| 82 | // Now scan the methods for accesses. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 83 | for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), |
| 84 | E = D->instmeth_end(); I!=E; ++I) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 85 | Scan(M, (*I)->getBody()); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 87 | // Scan for @synthesized property methods that act as setters/getters |
| 88 | // to an ivar. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 89 | for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(), |
| 90 | E = D->propimpl_end(); I!=E; ++I) |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 91 | Scan(M, *I); |
| 92 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 93 | // Find ivars that are unused. |
| 94 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 95 | if (I->second == Unused) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 96 | std::string sbuf; |
| 97 | llvm::raw_string_ostream os(sbuf); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 98 | os << "Instance variable '" << I->first->getNameAsString() |
| 99 | << "' in class '" << ID->getNameAsString() |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 100 | << "' is never used by the methods in its @implementation " |
| 101 | "(although it may be used by category methods)."; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 13493ea | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 103 | BR.EmitBasicReport("Unused instance variable", "Optimization", |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 104 | os.str().c_str(), I->first->getLocation()); |
| 105 | } |
| 106 | } |
| 107 | |