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