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 }; |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 27 | typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 29 | static void Scan(IvarUsageMap& M, const Stmt* S) { |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 30 | if (!S) |
| 31 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 35ffcf3 | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 33 | if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 34 | const ObjCIvarDecl *D = Ex->getDecl(); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 35 | IvarUsageMap::iterator I = M.find(D); |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 36 | if (I != M.end()) |
| 37 | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | 35ffcf3 | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 41 | // Blocks can reference an instance variable of a class. |
| 42 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 43 | Scan(M, BE->getBody()); |
| 44 | return; |
| 45 | } |
| 46 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 47 | for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I) |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 48 | Scan(M, *I); |
| 49 | } |
| 50 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 51 | static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) { |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 52 | if (!D) |
| 53 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 55 | const ObjCIvarDecl* ID = D->getPropertyIvarDecl(); |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 56 | |
| 57 | if (!ID) |
| 58 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 60 | IvarUsageMap::iterator I = M.find(ID); |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 61 | if (I != M.end()) |
| 62 | I->second = Used; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 65 | static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) { |
| 66 | // Scan the methods for accesses. |
| 67 | for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(), |
| 68 | E = D->instmeth_end(); I!=E; ++I) |
| 69 | Scan(M, (*I)->getBody()); |
| 70 | |
| 71 | if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) { |
| 72 | // Scan for @synthesized property methods that act as setters/getters |
| 73 | // to an ivar. |
| 74 | for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(), |
| 75 | E = ID->propimpl_end(); I!=E; ++I) |
| 76 | Scan(M, *I); |
Ted Kremenek | e8ec699 | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 77 | |
| 78 | // Scan the associated categories as well. |
| 79 | for (const ObjCCategoryDecl *CD = |
| 80 | ID->getClassInterface()->getCategoryList(); CD ; |
| 81 | CD = CD->getNextClassCategory()) { |
| 82 | if (const ObjCCategoryImplDecl *CID = CD->getImplementation()) |
| 83 | Scan(M, CID); |
| 84 | } |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 88 | void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D, |
| 89 | BugReporter &BR) { |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 91 | const ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 92 | IvarUsageMap M; |
| 93 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 94 | // Iterate over the ivars. |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 95 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), |
| 96 | E=ID->ivar_end(); I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 98 | const ObjCIvarDecl* ID = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 100 | // Ignore ivars that aren't private. |
Ted Kremenek | 6678f7f | 2008-07-23 17:14:39 +0000 | [diff] [blame] | 101 | if (ID->getAccessControl() != ObjCIvarDecl::Private) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 102 | continue; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 103 | |
| 104 | // Skip IB Outlets. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 105 | if (ID->getAttr<IBOutletAttr>()) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 106 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 108 | M[ID] = Unused; |
| 109 | } |
| 110 | |
| 111 | if (M.empty()) |
| 112 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 114 | // Now scan the implementation declaration. |
| 115 | Scan(M, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 117 | // Find ivars that are unused. |
| 118 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 119 | if (I->second == Unused) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 120 | std::string sbuf; |
| 121 | llvm::raw_string_ostream os(sbuf); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 122 | os << "Instance variable '" << I->first->getNameAsString() |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | << "' in class '" << ID->getNameAsString() |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 124 | << "' is never used by the methods in its @implementation " |
| 125 | "(although it may be used by category methods)."; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 13493ea | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 127 | BR.EmitBasicReport("Unused instance variable", "Optimization", |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 128 | os.str().c_str(), I->first->getLocation()); |
| 129 | } |
| 130 | } |