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. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 86 | for (const auto *I : ID->property_impls()) |
| 87 | Scan(M, I); |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 1c9401e | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 89 | // Scan the associated categories as well. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 90 | for (const auto *Cat : ID->getClassInterface()->visible_categories()) { |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 91 | if (const ObjCCategoryImplDecl *CID = Cat->getImplementation()) |
Ted Kremenek | 1c9401e | 2009-10-28 22:18:22 +0000 | [diff] [blame] | 92 | Scan(M, CID); |
| 93 | } |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 97 | static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID, |
| 98 | SourceManager &SM) { |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 99 | for (const auto *I : C->decls()) |
| 100 | if (const auto *FD = dyn_cast<FunctionDecl>(I)) { |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 101 | SourceLocation L = FD->getLocStart(); |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 102 | if (SM.getFileID(L) == FID) |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 103 | Scan(M, FD->getBody()); |
| 104 | } |
| 105 | } |
| 106 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 107 | static void checkObjCUnusedIvar(const ObjCImplementationDecl *D, |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 108 | BugReporter &BR, |
| 109 | const CheckerBase *Checker) { |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 111 | const ObjCInterfaceDecl *ID = D->getClassInterface(); |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 112 | IvarUsageMap M; |
| 113 | |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 114 | // Iterate over the ivars. |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 115 | for (const auto *Ivar : ID->ivars()) { |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 116 | // Ignore ivars that... |
| 117 | // (a) aren't private |
| 118 | // (b) explicitly marked unused |
| 119 | // (c) are iboutlets |
Ted Kremenek | 0abd85c | 2010-10-28 02:16:22 +0000 | [diff] [blame] | 120 | // (d) are unnamed bitfields |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 121 | if (Ivar->getAccessControl() != ObjCIvarDecl::Private || |
| 122 | Ivar->hasAttr<UnusedAttr>() || Ivar->hasAttr<IBOutletAttr>() || |
| 123 | Ivar->hasAttr<IBOutletCollectionAttr>() || |
| 124 | Ivar->isUnnamedBitfield()) |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 125 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 127 | M[Ivar] = Unused; |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (M.empty()) |
| 131 | return; |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | faba9fe | 2009-10-28 20:37:47 +0000 | [diff] [blame] | 133 | // Now scan the implementation declaration. |
| 134 | Scan(M, D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 136 | // Any potentially unused ivars? |
| 137 | bool hasUnused = false; |
| 138 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 139 | if (I->second == Unused) { |
| 140 | hasUnused = true; |
| 141 | break; |
| 142 | } |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 144 | if (!hasUnused) |
| 145 | return; |
Ted Kremenek | d98d22b | 2010-02-25 03:26:55 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | c1f161c | 2009-11-20 04:31:57 +0000 | [diff] [blame] | 147 | // We found some potentially unused ivars. Scan the entire translation unit |
| 148 | // for functions inside the @implementation that reference these ivars. |
| 149 | // FIXME: In the future hopefully we can just use the lexical DeclContext |
| 150 | // to go from the ObjCImplementationDecl to the lexically "nested" |
| 151 | // C functions. |
| 152 | SourceManager &SM = BR.getSourceManager(); |
| 153 | Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM); |
| 154 | |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 155 | // Find ivars that are unused. |
| 156 | for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I) |
| 157 | if (I->second == Unused) { |
Ted Kremenek | 799bb6e | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 158 | std::string sbuf; |
| 159 | llvm::raw_string_ostream os(sbuf); |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 160 | os << "Instance variable '" << *I->first << "' in class '" << *ID |
Ted Kremenek | d074ce4 | 2008-07-25 20:28:02 +0000 | [diff] [blame] | 161 | << "' is never used by the methods in its @implementation " |
| 162 | "(although it may be used by category methods)."; |
Ted Kremenek | 46abc7d | 2008-07-23 18:21:36 +0000 | [diff] [blame] | 163 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 164 | PathDiagnosticLocation L = |
| 165 | PathDiagnosticLocation::create(I->first, BR.getSourceManager()); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 166 | BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization", |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 167 | os.str(), L); |
Ted Kremenek | 3b28f49 | 2008-07-23 00:45:26 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 170 | |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | // ObjCUnusedIvarsChecker |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
| 175 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 176 | class ObjCUnusedIvarsChecker : public Checker< |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 177 | check::ASTDecl<ObjCImplementationDecl> > { |
| 178 | public: |
| 179 | void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr, |
| 180 | BugReporter &BR) const { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 181 | checkObjCUnusedIvar(D, BR, this); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 182 | } |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) { |
| 187 | mgr.registerChecker<ObjCUnusedIvarsChecker>(); |
| 188 | } |