blob: ebfc03e0eb73d31bce066f6ae88240105920dc9a [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.
Aaron Ballman3fe486a2014-03-13 21:23:55 +000091 for (const auto *Cat : ID->getClassInterface()->visible_categories()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +000092 if (const ObjCCategoryImplDecl *CID = Cat->getImplementation())
Ted Kremenek1c9401e2009-10-28 22:18:22 +000093 Scan(M, CID);
94 }
Ted Kremenekfaba9fe2009-10-28 20:37:47 +000095 }
96}
97
Ted Kremenekc1f161c2009-11-20 04:31:57 +000098static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
99 SourceManager &SM) {
Aaron Ballman629afae2014-03-07 19:56:05 +0000100 for (const auto *I : C->decls())
101 if (const auto *FD = dyn_cast<FunctionDecl>(I)) {
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000102 SourceLocation L = FD->getLocStart();
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000103 if (SM.getFileID(L) == FID)
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000104 Scan(M, FD->getBody());
105 }
106}
107
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000108static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000109 BugReporter &BR,
110 const CheckerBase *Checker) {
Ted Kremenek3b28f492008-07-23 00:45:26 +0000111
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000112 const ObjCInterfaceDecl *ID = D->getClassInterface();
Ted Kremenek3b28f492008-07-23 00:45:26 +0000113 IvarUsageMap M;
114
Ted Kremenek3b28f492008-07-23 00:45:26 +0000115 // Iterate over the ivars.
Aaron Ballman59abbd42014-03-13 21:09:43 +0000116 for (const auto *Ivar : ID->ivars()) {
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000117 // Ignore ivars that...
118 // (a) aren't private
119 // (b) explicitly marked unused
120 // (c) are iboutlets
Ted Kremenek0abd85c2010-10-28 02:16:22 +0000121 // (d) are unnamed bitfields
Aaron Ballman59abbd42014-03-13 21:09:43 +0000122 if (Ivar->getAccessControl() != ObjCIvarDecl::Private ||
123 Ivar->hasAttr<UnusedAttr>() || Ivar->hasAttr<IBOutletAttr>() ||
124 Ivar->hasAttr<IBOutletCollectionAttr>() ||
125 Ivar->isUnnamedBitfield())
Ted Kremenek3b28f492008-07-23 00:45:26 +0000126 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000127
Aaron Ballman59abbd42014-03-13 21:09:43 +0000128 M[Ivar] = Unused;
Ted Kremenek3b28f492008-07-23 00:45:26 +0000129 }
130
131 if (M.empty())
132 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000133
Ted Kremenekfaba9fe2009-10-28 20:37:47 +0000134 // Now scan the implementation declaration.
135 Scan(M, D);
Mike Stump11289f42009-09-09 15:08:12 +0000136
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000137 // 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 Kremenekd98d22b2010-02-25 03:26:55 +0000144
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000145 if (!hasUnused)
146 return;
Ted Kremenekd98d22b2010-02-25 03:26:55 +0000147
Ted Kremenekc1f161c2009-11-20 04:31:57 +0000148 // 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 Kremenek3b28f492008-07-23 00:45:26 +0000156 // Find ivars that are unused.
157 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
158 if (I->second == Unused) {
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000159 std::string sbuf;
160 llvm::raw_string_ostream os(sbuf);
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000161 os << "Instance variable '" << *I->first << "' in class '" << *ID
Ted Kremenekd074ce42008-07-25 20:28:02 +0000162 << "' is never used by the methods in its @implementation "
163 "(although it may be used by category methods).";
Ted Kremenek46abc7d2008-07-23 18:21:36 +0000164
Anna Zaksc29bed32011-09-20 21:38:35 +0000165 PathDiagnosticLocation L =
166 PathDiagnosticLocation::create(I->first, BR.getSourceManager());
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000167 BR.EmitBasicReport(D, Checker, "Unused instance variable", "Optimization",
Anna Zaksc29bed32011-09-20 21:38:35 +0000168 os.str(), L);
Ted Kremenek3b28f492008-07-23 00:45:26 +0000169 }
170}
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000171
172//===----------------------------------------------------------------------===//
173// ObjCUnusedIvarsChecker
174//===----------------------------------------------------------------------===//
175
176namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000177class ObjCUnusedIvarsChecker : public Checker<
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000178 check::ASTDecl<ObjCImplementationDecl> > {
179public:
180 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
181 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000182 checkObjCUnusedIvar(D, BR, this);
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +0000183 }
184};
185}
186
187void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
188 mgr.registerChecker<ObjCUnusedIvarsChecker>();
189}