blob: ebd2ed47b1a544c16407412774c36bc2cd00c33b [file] [log] [blame]
Ted Kremenekd98d22b2010-02-25 03:26:55 +00001//==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==//
Ted Kremenek3b28f492008-07-23 00:45:26 +00002//
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 Kyrtzidisaf45aca2011-02-17 21:39:33 +000016#include "ClangSACheckers.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000017#include "clang/AST/Attr.h"
Ted Kremenek3b28f492008-07-23 00:45:26 +000018#include "clang/AST/DeclObjC.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/ExprObjC.h"
Ted Kremenek3b28f492008-07-23 00:45:26 +000021#include "clang/Basic/LangOptions.h"
Benjamin Kramer4e75cd02009-11-28 10:50:44 +000022#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
24#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
25#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek3b28f492008-07-23 00:45:26 +000026
27using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000028using namespace ento;
Ted Kremenek3b28f492008-07-23 00:45:26 +000029
30enum IVarState { Unused, Used };
Ted Kremenekd99f2862009-08-07 20:55:20 +000031typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
Ted Kremenek3b28f492008-07-23 00:45:26 +000032
Ted Kremenek5ef32db2011-08-12 23:37:29 +000033static void Scan(IvarUsageMap& M, const Stmt *S) {
Ted Kremenek3b28f492008-07-23 00:45:26 +000034 if (!S)
35 return;
Mike Stump11289f42009-09-09 15:08:12 +000036
Ted Kremenek0e8e1fd2009-08-07 21:13:23 +000037 if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
38 const ObjCIvarDecl *D = Ex->getDecl();
Ted Kremenek3b28f492008-07-23 00:45:26 +000039 IvarUsageMap::iterator I = M.find(D);
Ted Kremenekd99f2862009-08-07 20:55:20 +000040 if (I != M.end())
41 I->second = Used;
Ted Kremenekd074ce42008-07-25 20:28:02 +000042 return;
Ted Kremenek3b28f492008-07-23 00:45:26 +000043 }
Mike Stump11289f42009-09-09 15:08:12 +000044
Ted Kremenek0e8e1fd2009-08-07 21:13:23 +000045 // 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 Zaks58d986c2012-05-15 22:31:56 +000051 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 Kremenekd99f2862009-08-07 20:55:20 +000060 for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
Ted Kremenekd074ce42008-07-25 20:28:02 +000061 Scan(M, *I);
62}
63
Ted Kremenek5ef32db2011-08-12 23:37:29 +000064static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl *D) {
Ted Kremenekd074ce42008-07-25 20:28:02 +000065 if (!D)
66 return;
Mike Stump11289f42009-09-09 15:08:12 +000067
Ted Kremenek5ef32db2011-08-12 23:37:29 +000068 const ObjCIvarDecl *ID = D->getPropertyIvarDecl();
Ted Kremenekd074ce42008-07-25 20:28:02 +000069
70 if (!ID)
71 return;
Mike Stump11289f42009-09-09 15:08:12 +000072
Ted Kremenekd074ce42008-07-25 20:28:02 +000073 IvarUsageMap::iterator I = M.find(ID);
Ted Kremenekd99f2862009-08-07 20:55:20 +000074 if (I != M.end())
75 I->second = Used;
Ted Kremenek3b28f492008-07-23 00:45:26 +000076}
77
Ted Kremenek5ef32db2011-08-12 23:37:29 +000078static void Scan(IvarUsageMap& M, const ObjCContainerDecl *D) {
Ted Kremenekfaba9fe2009-10-28 20:37:47 +000079 // Scan the methods for accesses.
Aaron Ballmanf26acce2014-03-13 19:50:17 +000080 for (const auto *I : D->instance_methods())
81 Scan(M, I->getBody());
Ted Kremenekd98d22b2010-02-25 03:26:55 +000082
83 if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenekfaba9fe2009-10-28 20:37:47 +000084 // 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 Blaikie40ed2972012-06-06 20:45:41 +000088 Scan(M, *I);
Ted Kremenekd98d22b2010-02-25 03:26:55 +000089
Ted Kremenek1c9401e2009-10-28 22:18:22 +000090 // Scan the associated categories as well.
Douglas Gregor048fbfa2013-01-16 23:00:23 +000091 for (ObjCInterfaceDecl::visible_categories_iterator
92 Cat = ID->getClassInterface()->visible_categories_begin(),
93 CatEnd = ID->getClassInterface()->visible_categories_end();
94 Cat != CatEnd; ++Cat) {
95 if (const ObjCCategoryImplDecl *CID = Cat->getImplementation())
Ted Kremenek1c9401e2009-10-28 22:18:22 +000096 Scan(M, CID);
97 }
Ted Kremenekfaba9fe2009-10-28 20:37:47 +000098 }
99}
100
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000101static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
102 SourceManager &SM) {
Aaron Ballman629afae2014-03-07 19:56:05 +0000103 for (const auto *I : C->decls())
104 if (const auto *FD = dyn_cast<FunctionDecl>(I)) {
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000105 SourceLocation L = FD->getLocStart();
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000106 if (SM.getFileID(L) == FID)
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000107 Scan(M, FD->getBody());
108 }
109}
110
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000111static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000112 BugReporter &BR,
113 const CheckerBase *Checker) {
Ted Kremenek3b28f492008-07-23 00:45:26 +0000114
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000115 const ObjCInterfaceDecl *ID = D->getClassInterface();
Ted Kremenek3b28f492008-07-23 00:45:26 +0000116 IvarUsageMap M;
117
Ted Kremenek3b28f492008-07-23 00:45:26 +0000118 // Iterate over the ivars.
Aaron Ballman59abbd42014-03-13 21:09:43 +0000119 for (const auto *Ivar : ID->ivars()) {
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000120 // Ignore ivars that...
121 // (a) aren't private
122 // (b) explicitly marked unused
123 // (c) are iboutlets
Ted Kremenek0abd85c2010-10-28 02:16:22 +0000124 // (d) are unnamed bitfields
Aaron Ballman59abbd42014-03-13 21:09:43 +0000125 if (Ivar->getAccessControl() != ObjCIvarDecl::Private ||
126 Ivar->hasAttr<UnusedAttr>() || Ivar->hasAttr<IBOutletAttr>() ||
127 Ivar->hasAttr<IBOutletCollectionAttr>() ||
128 Ivar->isUnnamedBitfield())
Ted Kremenek3b28f492008-07-23 00:45:26 +0000129 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Aaron Ballman59abbd42014-03-13 21:09:43 +0000131 M[Ivar] = Unused;
Ted Kremenek3b28f492008-07-23 00:45:26 +0000132 }
133
134 if (M.empty())
135 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000136
Ted Kremenekfaba9fe2009-10-28 20:37:47 +0000137 // Now scan the implementation declaration.
138 Scan(M, D);
Mike Stump11289f42009-09-09 15:08:12 +0000139
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000140 // Any potentially unused ivars?
141 bool hasUnused = false;
142 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
143 if (I->second == Unused) {
144 hasUnused = true;
145 break;
146 }
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000147
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000148 if (!hasUnused)
149 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000150
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000151 // We found some potentially unused ivars. Scan the entire translation unit
152 // for functions inside the @implementation that reference these ivars.
153 // FIXME: In the future hopefully we can just use the lexical DeclContext
154 // to go from the ObjCImplementationDecl to the lexically "nested"
155 // C functions.
156 SourceManager &SM = BR.getSourceManager();
157 Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
158
Ted Kremenek3b28f492008-07-23 00:45:26 +0000159 // Find ivars that are unused.
160 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
161 if (I->second == Unused) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000162 std::string sbuf;
163 llvm::raw_string_ostream os(sbuf);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000164 os << "Instance variable '" << *I->first << "' in class '" << *ID
Ted Kremenekd074ce42008-07-25 20:28:02 +0000165 << "' is never used by the methods in its @implementation "
166 "(although it may be used by category methods).";
Ted Kremenek46abc7d2008-07-23 18:21:36 +0000167
Anna Zaksc29bed32011-09-20 21:38:35 +0000168 PathDiagnosticLocation L =
169 PathDiagnosticLocation::create(I->first, BR.getSourceManager());
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000170 BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization",
Anna Zaksc29bed32011-09-20 21:38:35 +0000171 os.str(), L);
Ted Kremenek3b28f492008-07-23 00:45:26 +0000172 }
173}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000174
175//===----------------------------------------------------------------------===//
176// ObjCUnusedIvarsChecker
177//===----------------------------------------------------------------------===//
178
179namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000180class ObjCUnusedIvarsChecker : public Checker<
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000181 check::ASTDecl<ObjCImplementationDecl> > {
182public:
183 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
184 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000185 checkObjCUnusedIvar(D, BR, this);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000186 }
187};
188}
189
190void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
191 mgr.registerChecker<ObjCUnusedIvarsChecker>();
192}