blob: 4718dc7c7aa96415231e4e427fee37d40ff55dcb [file] [log] [blame]
Ted Kremeneke3972a92010-02-25 03:26:55 +00001//==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==//
Ted Kremenek395aaf22008-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 Kyrtzidis7dd445e2011-02-17 21:39:33 +000016#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000017#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000018#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Ted Kremenek395aaf22008-07-23 00:45:26 +000020#include "clang/AST/ExprObjC.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/DeclObjC.h"
23#include "clang/Basic/LangOptions.h"
Benjamin Kramer58652882009-11-28 10:50:44 +000024#include "clang/Basic/SourceManager.h"
Ted Kremenek395aaf22008-07-23 00:45:26 +000025
26using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000027using namespace ento;
Ted Kremenek395aaf22008-07-23 00:45:26 +000028
29enum IVarState { Unused, Used };
Ted Kremenek0b2dd772009-08-07 20:55:20 +000030typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
Ted Kremenek395aaf22008-07-23 00:45:26 +000031
Ted Kremenek9c378f72011-08-12 23:37:29 +000032static void Scan(IvarUsageMap& M, const Stmt *S) {
Ted Kremenek395aaf22008-07-23 00:45:26 +000033 if (!S)
34 return;
Mike Stump1eb44332009-09-09 15:08:12 +000035
Ted Kremenek35ffcf32009-08-07 21:13:23 +000036 if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
37 const ObjCIvarDecl *D = Ex->getDecl();
Ted Kremenek395aaf22008-07-23 00:45:26 +000038 IvarUsageMap::iterator I = M.find(D);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000039 if (I != M.end())
40 I->second = Used;
Ted Kremenek694eefb2008-07-25 20:28:02 +000041 return;
Ted Kremenek395aaf22008-07-23 00:45:26 +000042 }
Mike Stump1eb44332009-09-09 15:08:12 +000043
Ted Kremenek35ffcf32009-08-07 21:13:23 +000044 // Blocks can reference an instance variable of a class.
45 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
46 Scan(M, BE->getBody());
47 return;
48 }
49
Ted Kremenek0b2dd772009-08-07 20:55:20 +000050 for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
Ted Kremenek694eefb2008-07-25 20:28:02 +000051 Scan(M, *I);
52}
53
Ted Kremenek9c378f72011-08-12 23:37:29 +000054static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl *D) {
Ted Kremenek694eefb2008-07-25 20:28:02 +000055 if (!D)
56 return;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Ted Kremenek9c378f72011-08-12 23:37:29 +000058 const ObjCIvarDecl *ID = D->getPropertyIvarDecl();
Ted Kremenek694eefb2008-07-25 20:28:02 +000059
60 if (!ID)
61 return;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek694eefb2008-07-25 20:28:02 +000063 IvarUsageMap::iterator I = M.find(ID);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000064 if (I != M.end())
65 I->second = Used;
Ted Kremenek395aaf22008-07-23 00:45:26 +000066}
67
Ted Kremenek9c378f72011-08-12 23:37:29 +000068static void Scan(IvarUsageMap& M, const ObjCContainerDecl *D) {
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000069 // Scan the methods for accesses.
70 for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(),
71 E = D->instmeth_end(); I!=E; ++I)
72 Scan(M, (*I)->getBody());
Ted Kremeneke3972a92010-02-25 03:26:55 +000073
74 if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) {
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000075 // Scan for @synthesized property methods that act as setters/getters
76 // to an ivar.
77 for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(),
78 E = ID->propimpl_end(); I!=E; ++I)
79 Scan(M, *I);
Ted Kremeneke3972a92010-02-25 03:26:55 +000080
Ted Kremeneke8ec6992009-10-28 22:18:22 +000081 // Scan the associated categories as well.
82 for (const ObjCCategoryDecl *CD =
83 ID->getClassInterface()->getCategoryList(); CD ;
84 CD = CD->getNextClassCategory()) {
85 if (const ObjCCategoryImplDecl *CID = CD->getImplementation())
86 Scan(M, CID);
87 }
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000088 }
89}
90
Ted Kremenekb221e4f2009-11-20 04:31:57 +000091static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
92 SourceManager &SM) {
93 for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end();
94 I!=E; ++I)
95 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
96 SourceLocation L = FD->getLocStart();
Ted Kremeneke3972a92010-02-25 03:26:55 +000097 if (SM.getFileID(L) == FID)
Ted Kremenekb221e4f2009-11-20 04:31:57 +000098 Scan(M, FD->getBody());
99 }
100}
101
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000102static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
Ted Kremenek23760022009-08-21 23:58:43 +0000103 BugReporter &BR) {
Ted Kremenek395aaf22008-07-23 00:45:26 +0000104
Ted Kremenek9c378f72011-08-12 23:37:29 +0000105 const ObjCInterfaceDecl *ID = D->getClassInterface();
Ted Kremenek395aaf22008-07-23 00:45:26 +0000106 IvarUsageMap M;
107
Ted Kremenek395aaf22008-07-23 00:45:26 +0000108 // Iterate over the ivars.
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000109 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
110 E=ID->ivar_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek9c378f72011-08-12 23:37:29 +0000112 const ObjCIvarDecl *ID = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremeneke3972a92010-02-25 03:26:55 +0000114 // Ignore ivars that...
115 // (a) aren't private
116 // (b) explicitly marked unused
117 // (c) are iboutlets
Ted Kremeneked50a8a2010-10-28 02:16:22 +0000118 // (d) are unnamed bitfields
Ted Kremeneke3972a92010-02-25 03:26:55 +0000119 if (ID->getAccessControl() != ObjCIvarDecl::Private ||
Ted Kremenek857e9182010-05-19 17:38:06 +0000120 ID->getAttr<UnusedAttr>() || ID->getAttr<IBOutletAttr>() ||
Ted Kremeneked50a8a2010-10-28 02:16:22 +0000121 ID->getAttr<IBOutletCollectionAttr>() ||
122 ID->isUnnamedBitfield())
Ted Kremenek395aaf22008-07-23 00:45:26 +0000123 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek395aaf22008-07-23 00:45:26 +0000125 M[ID] = Unused;
126 }
127
128 if (M.empty())
129 return;
Ted Kremeneke3972a92010-02-25 03:26:55 +0000130
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +0000131 // Now scan the implementation declaration.
132 Scan(M, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000134 // Any potentially unused ivars?
135 bool hasUnused = false;
136 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
137 if (I->second == Unused) {
138 hasUnused = true;
139 break;
140 }
Ted Kremeneke3972a92010-02-25 03:26:55 +0000141
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000142 if (!hasUnused)
143 return;
Ted Kremeneke3972a92010-02-25 03:26:55 +0000144
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000145 // We found some potentially unused ivars. Scan the entire translation unit
146 // for functions inside the @implementation that reference these ivars.
147 // FIXME: In the future hopefully we can just use the lexical DeclContext
148 // to go from the ObjCImplementationDecl to the lexically "nested"
149 // C functions.
150 SourceManager &SM = BR.getSourceManager();
151 Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
152
Ted Kremenek395aaf22008-07-23 00:45:26 +0000153 // Find ivars that are unused.
154 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
155 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000156 std::string sbuf;
157 llvm::raw_string_ostream os(sbuf);
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000158 os << "Instance variable '" << *I->first << "' in class '" << *ID
Ted Kremenek694eefb2008-07-25 20:28:02 +0000159 << "' is never used by the methods in its @implementation "
160 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000161
Anna Zaks590dd8e2011-09-20 21:38:35 +0000162 PathDiagnosticLocation L =
163 PathDiagnosticLocation::create(I->first, BR.getSourceManager());
Ted Kremenek07189522012-04-04 18:11:35 +0000164 BR.EmitBasicReport(D, "Unused instance variable", "Optimization",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000165 os.str(), L);
Ted Kremenek395aaf22008-07-23 00:45:26 +0000166 }
167}
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000168
169//===----------------------------------------------------------------------===//
170// ObjCUnusedIvarsChecker
171//===----------------------------------------------------------------------===//
172
173namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000174class ObjCUnusedIvarsChecker : public Checker<
Argyrios Kyrtzidis7dd445e2011-02-17 21:39:33 +0000175 check::ASTDecl<ObjCImplementationDecl> > {
176public:
177 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
178 BugReporter &BR) const {
179 checkObjCUnusedIvar(D, BR);
180 }
181};
182}
183
184void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
185 mgr.registerChecker<ObjCUnusedIvarsChecker>();
186}