blob: d3b17534fdac87c622712f11e9e6f9431dbe4d4d [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.
Aaron Ballmand85eff42014-03-14 15:02:45 +000086 for (const auto *I : ID->property_impls())
87 Scan(M, I);
Ted Kremenekd98d22b2010-02-25 03:26:55 +000088
Ted Kremenek1c9401e2009-10-28 22:18:22 +000089 // Scan the associated categories as well.
Aaron Ballman3fe486a2014-03-13 21:23:55 +000090 for (const auto *Cat : ID->getClassInterface()->visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +000091 if (const ObjCCategoryImplDecl *CID = Cat->getImplementation())
Ted Kremenek1c9401e2009-10-28 22:18:22 +000092 Scan(M, CID);
93 }
Ted Kremenekfaba9fe2009-10-28 20:37:47 +000094 }
95}
96
Ted Kremenekc1f161c2009-11-20 04:31:57 +000097static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
98 SourceManager &SM) {
Aaron Ballman629afae2014-03-07 19:56:05 +000099 for (const auto *I : C->decls())
100 if (const auto *FD = dyn_cast<FunctionDecl>(I)) {
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000101 SourceLocation L = FD->getLocStart();
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000102 if (SM.getFileID(L) == FID)
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000103 Scan(M, FD->getBody());
104 }
105}
106
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000107static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000108 BugReporter &BR,
109 const CheckerBase *Checker) {
Ted Kremenek3b28f492008-07-23 00:45:26 +0000110
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000111 const ObjCInterfaceDecl *ID = D->getClassInterface();
Ted Kremenek3b28f492008-07-23 00:45:26 +0000112 IvarUsageMap M;
113
Ted Kremenek3b28f492008-07-23 00:45:26 +0000114 // Iterate over the ivars.
Aaron Ballman59abbd42014-03-13 21:09:43 +0000115 for (const auto *Ivar : ID->ivars()) {
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000116 // Ignore ivars that...
117 // (a) aren't private
118 // (b) explicitly marked unused
119 // (c) are iboutlets
Ted Kremenek0abd85c2010-10-28 02:16:22 +0000120 // (d) are unnamed bitfields
Aaron Ballman59abbd42014-03-13 21:09:43 +0000121 if (Ivar->getAccessControl() != ObjCIvarDecl::Private ||
122 Ivar->hasAttr<UnusedAttr>() || Ivar->hasAttr<IBOutletAttr>() ||
123 Ivar->hasAttr<IBOutletCollectionAttr>() ||
124 Ivar->isUnnamedBitfield())
Ted Kremenek3b28f492008-07-23 00:45:26 +0000125 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000126
Aaron Ballman59abbd42014-03-13 21:09:43 +0000127 M[Ivar] = Unused;
Ted Kremenek3b28f492008-07-23 00:45:26 +0000128 }
129
130 if (M.empty())
131 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000132
Ted Kremenekfaba9fe2009-10-28 20:37:47 +0000133 // Now scan the implementation declaration.
134 Scan(M, D);
Mike Stump11289f42009-09-09 15:08:12 +0000135
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000136 // 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 Kremenekd98d22b2010-02-25 03:26:55 +0000143
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000144 if (!hasUnused)
145 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000146
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000147 // 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 Kremenek3b28f492008-07-23 00:45:26 +0000155 // Find ivars that are unused.
156 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
157 if (I->second == Unused) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000158 std::string sbuf;
159 llvm::raw_string_ostream os(sbuf);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000160 os << "Instance variable '" << *I->first << "' in class '" << *ID
Ted Kremenekd074ce42008-07-25 20:28:02 +0000161 << "' is never used by the methods in its @implementation "
162 "(although it may be used by category methods).";
Ted Kremenek46abc7d2008-07-23 18:21:36 +0000163
Anna Zaksc29bed32011-09-20 21:38:35 +0000164 PathDiagnosticLocation L =
165 PathDiagnosticLocation::create(I->first, BR.getSourceManager());
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000166 BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization",
Anna Zaksc29bed32011-09-20 21:38:35 +0000167 os.str(), L);
Ted Kremenek3b28f492008-07-23 00:45:26 +0000168 }
169}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000170
171//===----------------------------------------------------------------------===//
172// ObjCUnusedIvarsChecker
173//===----------------------------------------------------------------------===//
174
175namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000176class ObjCUnusedIvarsChecker : public Checker<
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000177 check::ASTDecl<ObjCImplementationDecl> > {
178public:
179 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
180 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000181 checkObjCUnusedIvar(D, BR, this);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000182 }
183};
184}
185
186void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
187 mgr.registerChecker<ObjCUnusedIvarsChecker>();
188}