blob: d4067c900f3fa136879df1bbebdc1ab05c794aaa [file] [log] [blame]
Ted Kremenek395aaf22008-07-23 00:45:26 +00001//==- CheckObjCUnusedIVars.cpp - Check for unused ivars ----------*- C++ -*-==//
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
16#include "clang/Analysis/LocalCheckers.h"
17#include "clang/Analysis/PathDiagnostic.h"
18#include "clang/Analysis/PathSensitive/BugReporter.h"
19#include "clang/AST/ExprObjC.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/Basic/LangOptions.h"
Benjamin Kramer58652882009-11-28 10:50:44 +000023#include "clang/Basic/SourceManager.h"
Ted Kremenek395aaf22008-07-23 00:45:26 +000024
25using namespace clang;
26
27enum IVarState { Unused, Used };
Ted Kremenek0b2dd772009-08-07 20:55:20 +000028typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
Ted Kremenek395aaf22008-07-23 00:45:26 +000029
Ted Kremenek0b2dd772009-08-07 20:55:20 +000030static void Scan(IvarUsageMap& M, const Stmt* S) {
Ted Kremenek395aaf22008-07-23 00:45:26 +000031 if (!S)
32 return;
Mike Stump1eb44332009-09-09 15:08:12 +000033
Ted Kremenek35ffcf32009-08-07 21:13:23 +000034 if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
35 const ObjCIvarDecl *D = Ex->getDecl();
Ted Kremenek395aaf22008-07-23 00:45:26 +000036 IvarUsageMap::iterator I = M.find(D);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000037 if (I != M.end())
38 I->second = Used;
Ted Kremenek694eefb2008-07-25 20:28:02 +000039 return;
Ted Kremenek395aaf22008-07-23 00:45:26 +000040 }
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenek35ffcf32009-08-07 21:13:23 +000042 // Blocks can reference an instance variable of a class.
43 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
44 Scan(M, BE->getBody());
45 return;
46 }
47
Ted Kremenek0b2dd772009-08-07 20:55:20 +000048 for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
Ted Kremenek694eefb2008-07-25 20:28:02 +000049 Scan(M, *I);
50}
51
Ted Kremenek0b2dd772009-08-07 20:55:20 +000052static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) {
Ted Kremenek694eefb2008-07-25 20:28:02 +000053 if (!D)
54 return;
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenek0b2dd772009-08-07 20:55:20 +000056 const ObjCIvarDecl* ID = D->getPropertyIvarDecl();
Ted Kremenek694eefb2008-07-25 20:28:02 +000057
58 if (!ID)
59 return;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenek694eefb2008-07-25 20:28:02 +000061 IvarUsageMap::iterator I = M.find(ID);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000062 if (I != M.end())
63 I->second = Used;
Ted Kremenek395aaf22008-07-23 00:45:26 +000064}
65
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000066static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) {
67 // Scan the methods for accesses.
68 for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(),
69 E = D->instmeth_end(); I!=E; ++I)
70 Scan(M, (*I)->getBody());
71
72 if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) {
73 // Scan for @synthesized property methods that act as setters/getters
74 // to an ivar.
75 for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(),
76 E = ID->propimpl_end(); I!=E; ++I)
77 Scan(M, *I);
Ted Kremeneke8ec6992009-10-28 22:18:22 +000078
79 // Scan the associated categories as well.
80 for (const ObjCCategoryDecl *CD =
81 ID->getClassInterface()->getCategoryList(); CD ;
82 CD = CD->getNextClassCategory()) {
83 if (const ObjCCategoryImplDecl *CID = CD->getImplementation())
84 Scan(M, CID);
85 }
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000086 }
87}
88
Ted Kremenekb221e4f2009-11-20 04:31:57 +000089static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
90 SourceManager &SM) {
91 for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end();
92 I!=E; ++I)
93 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
94 SourceLocation L = FD->getLocStart();
95 if (SM.getFileID(L) == FID)
96 Scan(M, FD->getBody());
97 }
98}
99
Ted Kremenek23760022009-08-21 23:58:43 +0000100void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D,
101 BugReporter &BR) {
Ted Kremenek395aaf22008-07-23 00:45:26 +0000102
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000103 const ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek395aaf22008-07-23 00:45:26 +0000104 IvarUsageMap M;
105
Ted Kremenek395aaf22008-07-23 00:45:26 +0000106 // Iterate over the ivars.
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000107 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
108 E=ID->ivar_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000110 const ObjCIvarDecl* ID = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek395aaf22008-07-23 00:45:26 +0000112 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +0000113 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +0000114 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000115
116 // Skip IB Outlets.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000117 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +0000118 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek395aaf22008-07-23 00:45:26 +0000120 M[ID] = Unused;
121 }
122
123 if (M.empty())
124 return;
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000125
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +0000126 // Now scan the implementation declaration.
127 Scan(M, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000129
130 // Any potentially unused ivars?
131 bool hasUnused = false;
132 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
133 if (I->second == Unused) {
134 hasUnused = true;
135 break;
136 }
137
138 if (!hasUnused)
139 return;
140
141 // We found some potentially unused ivars. Scan the entire translation unit
142 // for functions inside the @implementation that reference these ivars.
143 // FIXME: In the future hopefully we can just use the lexical DeclContext
144 // to go from the ObjCImplementationDecl to the lexically "nested"
145 // C functions.
146 SourceManager &SM = BR.getSourceManager();
147 Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
148
Ted Kremenek395aaf22008-07-23 00:45:26 +0000149 // Find ivars that are unused.
150 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
151 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000152 std::string sbuf;
153 llvm::raw_string_ostream os(sbuf);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000154 os << "Instance variable '" << I->first->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000155 << "' in class '" << ID->getNameAsString()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000156 << "' is never used by the methods in its @implementation "
157 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000158
Ted Kremenek13493ea2009-04-02 02:44:03 +0000159 BR.EmitBasicReport("Unused instance variable", "Optimization",
Benjamin Kramerf0171732009-11-29 18:27:55 +0000160 os.str(), I->first->getLocation());
Ted Kremenek395aaf22008-07-23 00:45:26 +0000161 }
162}