Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 1 | //==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==// |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 9705309 | 2010-01-26 22:59:55 +0000 | [diff] [blame] | 16 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/PathDiagnostic.h" |
| 18 | #include "clang/Checker/BugReporter/BugReporter.h" |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
| 20 | #include "clang/AST/Expr.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
| 22 | #include "clang/Basic/LangOptions.h" |
Benjamin Kramer | 5865288 | 2009-11-28 10:50:44 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | enum IVarState { Unused, Used }; |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 28 | typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 30 | static void Scan(IvarUsageMap& M, const Stmt* S) { |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 31 | if (!S) |
| 32 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 35ffcf3 | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 34 | if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 35 | const ObjCIvarDecl *D = Ex->getDecl(); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 36 | IvarUsageMap::iterator I = M.find(D); |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 37 | if (I != M.end()) |
| 38 | I->second = Used; |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 39 | return; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 40 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 35ffcf3 | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 42 | // Blocks can reference an instance variable of a class. |
| 43 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 44 | Scan(M, BE->getBody()); |
| 45 | return; |
| 46 | } |
| 47 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 48 | 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] | 49 | Scan(M, *I); |
| 50 | } |
| 51 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 52 | static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) { |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 53 | if (!D) |
| 54 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 56 | const ObjCIvarDecl* ID = D->getPropertyIvarDecl(); |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 57 | |
| 58 | if (!ID) |
| 59 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 61 | IvarUsageMap::iterator I = M.find(ID); |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 62 | if (I != M.end()) |
| 63 | I->second = Used; |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 66 | static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) { |
| 67 | // Scan the methods for accesses. |
| 68 | for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(), |
| 69 | E = D->instmeth_end(); I!=E; ++I) |
| 70 | Scan(M, (*I)->getBody()); |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 71 | |
| 72 | if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 73 | // Scan for @synthesized property methods that act as setters/getters |
| 74 | // to an ivar. |
| 75 | for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(), |
| 76 | E = ID->propimpl_end(); I!=E; ++I) |
| 77 | Scan(M, *I); |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | e8ec699 | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 79 | // Scan the associated categories as well. |
| 80 | for (const ObjCCategoryDecl *CD = |
| 81 | ID->getClassInterface()->getCategoryList(); CD ; |
| 82 | CD = CD->getNextClassCategory()) { |
| 83 | if (const ObjCCategoryImplDecl *CID = CD->getImplementation()) |
| 84 | Scan(M, CID); |
| 85 | } |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Ted Kremenek | b221e4f | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 89 | static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID, |
| 90 | SourceManager &SM) { |
| 91 | for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end(); |
| 92 | I!=E; ++I) |
| 93 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 94 | SourceLocation L = FD->getLocStart(); |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 95 | if (SM.getFileID(L) == FID) |
Ted Kremenek | b221e4f | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 96 | Scan(M, FD->getBody()); |
| 97 | } |
| 98 | } |
| 99 | |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 100 | void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D, |
| 101 | BugReporter &BR) { |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 103 | const ObjCInterfaceDecl* ID = D->getClassInterface(); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 104 | IvarUsageMap M; |
| 105 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 106 | // Iterate over the ivars. |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 107 | for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), |
| 108 | E=ID->ivar_end(); I!=E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | 0b2dd77 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 110 | const ObjCIvarDecl* ID = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 112 | // Ignore ivars that... |
| 113 | // (a) aren't private |
| 114 | // (b) explicitly marked unused |
| 115 | // (c) are iboutlets |
Ted Kremenek | ed50a8a | 2010-10-28 02:16:22 +0000 | [diff] [blame] | 116 | // (d) are unnamed bitfields |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 117 | if (ID->getAccessControl() != ObjCIvarDecl::Private || |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 118 | ID->getAttr<UnusedAttr>() || ID->getAttr<IBOutletAttr>() || |
Ted Kremenek | ed50a8a | 2010-10-28 02:16:22 +0000 | [diff] [blame] | 119 | ID->getAttr<IBOutletCollectionAttr>() || |
| 120 | ID->isUnnamedBitfield()) |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 121 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 123 | M[ID] = Unused; |
| 124 | } |
| 125 | |
| 126 | if (M.empty()) |
| 127 | return; |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 8fa3a1a | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 129 | // Now scan the implementation declaration. |
| 130 | Scan(M, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | b221e4f | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 132 | // Any potentially unused ivars? |
| 133 | bool hasUnused = false; |
| 134 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 135 | if (I->second == Unused) { |
| 136 | hasUnused = true; |
| 137 | break; |
| 138 | } |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | b221e4f | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 140 | if (!hasUnused) |
| 141 | return; |
Ted Kremenek | e3972a9 | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | b221e4f | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 143 | // We found some potentially unused ivars. Scan the entire translation unit |
| 144 | // for functions inside the @implementation that reference these ivars. |
| 145 | // FIXME: In the future hopefully we can just use the lexical DeclContext |
| 146 | // to go from the ObjCImplementationDecl to the lexically "nested" |
| 147 | // C functions. |
| 148 | SourceManager &SM = BR.getSourceManager(); |
| 149 | Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM); |
| 150 | |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 151 | // Find ivars that are unused. |
| 152 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 153 | if (I->second == Unused) { |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 154 | std::string sbuf; |
| 155 | llvm::raw_string_ostream os(sbuf); |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 156 | os << "Instance variable '" << I->first << "' in class '" << ID |
Ted Kremenek | 694eefb | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 157 | << "' is never used by the methods in its @implementation " |
| 158 | "(although it may be used by category methods)."; |
Ted Kremenek | cc87ba2 | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 13493ea | 2009-04-02 02:44:03 +0000 | [diff] [blame] | 160 | BR.EmitBasicReport("Unused instance variable", "Optimization", |
Benjamin Kramer | f017173 | 2009-11-29 18:27:55 +0000 | [diff] [blame] | 161 | os.str(), I->first->getLocation()); |
Ted Kremenek | 395aaf2 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 162 | } |
| 163 | } |