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