Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 1 | //==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==// |
Ted Kremenek | 3b28f49 | 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 | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 16 | #include "ClangSACheckers.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/Attr.h" |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
Benjamin Kramer | 4e75cd0 | 2009-11-28 10:50:44 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 24 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
| 25 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 28 | using namespace ento; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 29 | |
| 30 | enum IVarState { Unused, Used }; |
Ted Kremenek | d99f286 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 31 | typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 33 | static void Scan(IvarUsageMap& M, const Stmt *S) { |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 34 | if (!S) |
| 35 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 0e8e1fd | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 37 | if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) { |
| 38 | const ObjCIvarDecl *D = Ex->getDecl(); |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 39 | IvarUsageMap::iterator I = M.find(D); |
Ted Kremenek | d99f286 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 40 | if (I != M.end()) |
| 41 | I->second = Used; |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 42 | return; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 43 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | 0e8e1fd | 2009-08-07 21:13:23 +0000 | [diff] [blame] | 45 | // Blocks can reference an instance variable of a class. |
| 46 | if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) { |
| 47 | Scan(M, BE->getBody()); |
| 48 | return; |
| 49 | } |
| 50 | |
Anna Zaks | 58d986c | 2012-05-15 22:31:56 +0000 | [diff] [blame] | 51 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(S)) |
| 52 | for (PseudoObjectExpr::const_semantics_iterator |
| 53 | i = POE->semantics_begin(), e = POE->semantics_end(); i != e; ++i) { |
| 54 | const Expr *sub = *i; |
| 55 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(sub)) |
| 56 | sub = OVE->getSourceExpr(); |
| 57 | Scan(M, sub); |
| 58 | } |
| 59 | |
Ted Kremenek | d99f286 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 60 | for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I) |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 61 | Scan(M, *I); |
| 62 | } |
| 63 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 64 | static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl *D) { |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 65 | if (!D) |
| 66 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 68 | const ObjCIvarDecl *ID = D->getPropertyIvarDecl(); |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 69 | |
| 70 | if (!ID) |
| 71 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 73 | IvarUsageMap::iterator I = M.find(ID); |
Ted Kremenek | d99f286 | 2009-08-07 20:55:20 +0000 | [diff] [blame] | 74 | if (I != M.end()) |
| 75 | I->second = Used; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 78 | static void Scan(IvarUsageMap& M, const ObjCContainerDecl *D) { |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 79 | // Scan the methods for accesses. |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 80 | for (const auto *I : D->instance_methods()) |
| 81 | Scan(M, I->getBody()); |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 82 | |
| 83 | if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) { |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 84 | // Scan for @synthesized property methods that act as setters/getters |
| 85 | // to an ivar. |
| 86 | for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(), |
| 87 | E = ID->propimpl_end(); I!=E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 88 | Scan(M, *I); |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 1c9401e | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 90 | // Scan the associated categories as well. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame^] | 91 | for (const auto *Cat : ID->getClassInterface()->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 92 | if (const ObjCCategoryImplDecl *CID = Cat->getImplementation()) |
Ted Kremenek | 1c9401e | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 93 | Scan(M, CID); |
| 94 | } |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 98 | static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID, |
| 99 | SourceManager &SM) { |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 100 | for (const auto *I : C->decls()) |
| 101 | if (const auto *FD = dyn_cast<FunctionDecl>(I)) { |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 102 | SourceLocation L = FD->getLocStart(); |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 103 | if (SM.getFileID(L) == FID) |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 104 | Scan(M, FD->getBody()); |
| 105 | } |
| 106 | } |
| 107 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 108 | static void checkObjCUnusedIvar(const ObjCImplementationDecl *D, |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 109 | BugReporter &BR, |
| 110 | const CheckerBase *Checker) { |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 112 | const ObjCInterfaceDecl *ID = D->getClassInterface(); |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 113 | IvarUsageMap M; |
| 114 | |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 115 | // Iterate over the ivars. |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 116 | for (const auto *Ivar : ID->ivars()) { |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 117 | // Ignore ivars that... |
| 118 | // (a) aren't private |
| 119 | // (b) explicitly marked unused |
| 120 | // (c) are iboutlets |
Ted Kremenek | 0abd85c | 2010-10-28 02:16:22 +0000 | [diff] [blame] | 121 | // (d) are unnamed bitfields |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 122 | if (Ivar->getAccessControl() != ObjCIvarDecl::Private || |
| 123 | Ivar->hasAttr<UnusedAttr>() || Ivar->hasAttr<IBOutletAttr>() || |
| 124 | Ivar->hasAttr<IBOutletCollectionAttr>() || |
| 125 | Ivar->isUnnamedBitfield()) |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 126 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 128 | M[Ivar] = Unused; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (M.empty()) |
| 132 | return; |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 134 | // Now scan the implementation declaration. |
| 135 | Scan(M, D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 137 | // Any potentially unused ivars? |
| 138 | bool hasUnused = false; |
| 139 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 140 | if (I->second == Unused) { |
| 141 | hasUnused = true; |
| 142 | break; |
| 143 | } |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 145 | if (!hasUnused) |
| 146 | return; |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 148 | // We found some potentially unused ivars. Scan the entire translation unit |
| 149 | // for functions inside the @implementation that reference these ivars. |
| 150 | // FIXME: In the future hopefully we can just use the lexical DeclContext |
| 151 | // to go from the ObjCImplementationDecl to the lexically "nested" |
| 152 | // C functions. |
| 153 | SourceManager &SM = BR.getSourceManager(); |
| 154 | Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM); |
| 155 | |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 156 | // Find ivars that are unused. |
| 157 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 158 | if (I->second == Unused) { |
Ted Kremenek | 799bb6e | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 159 | std::string sbuf; |
| 160 | llvm::raw_string_ostream os(sbuf); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 161 | os << "Instance variable '" << *I->first << "' in class '" << *ID |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 162 | << "' is never used by the methods in its @implementation " |
| 163 | "(although it may be used by category methods)."; |
Ted Kremenek | 46abc7d | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 164 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 165 | PathDiagnosticLocation L = |
| 166 | PathDiagnosticLocation::create(I->first, BR.getSourceManager()); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 167 | BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization", |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 168 | os.str(), L); |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 171 | |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | // ObjCUnusedIvarsChecker |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
| 176 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 177 | class ObjCUnusedIvarsChecker : public Checker< |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 178 | check::ASTDecl<ObjCImplementationDecl> > { |
| 179 | public: |
| 180 | void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr, |
| 181 | BugReporter &BR) const { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 182 | checkObjCUnusedIvar(D, BR, this); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 183 | } |
| 184 | }; |
| 185 | } |
| 186 | |
| 187 | void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) { |
| 188 | mgr.registerChecker<ObjCUnusedIvarsChecker>(); |
| 189 | } |