blob: 3ee5cb12d95d00abcaa728e32fc2274ebd591374 [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"
Ted Kremenek395aaf22008-07-23 00:45:26 +000023
24using namespace clang;
25
26enum IVarState { Unused, Used };
Ted Kremenek0b2dd772009-08-07 20:55:20 +000027typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
Ted Kremenek395aaf22008-07-23 00:45:26 +000028
Ted Kremenek0b2dd772009-08-07 20:55:20 +000029static void Scan(IvarUsageMap& M, const Stmt* S) {
Ted Kremenek395aaf22008-07-23 00:45:26 +000030 if (!S)
31 return;
Mike Stump1eb44332009-09-09 15:08:12 +000032
Ted Kremenek35ffcf32009-08-07 21:13:23 +000033 if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
34 const ObjCIvarDecl *D = Ex->getDecl();
Ted Kremenek395aaf22008-07-23 00:45:26 +000035 IvarUsageMap::iterator I = M.find(D);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000036 if (I != M.end())
37 I->second = Used;
Ted Kremenek694eefb2008-07-25 20:28:02 +000038 return;
Ted Kremenek395aaf22008-07-23 00:45:26 +000039 }
Mike Stump1eb44332009-09-09 15:08:12 +000040
Ted Kremenek35ffcf32009-08-07 21:13:23 +000041 // Blocks can reference an instance variable of a class.
42 if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
43 Scan(M, BE->getBody());
44 return;
45 }
46
Ted Kremenek0b2dd772009-08-07 20:55:20 +000047 for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
Ted Kremenek694eefb2008-07-25 20:28:02 +000048 Scan(M, *I);
49}
50
Ted Kremenek0b2dd772009-08-07 20:55:20 +000051static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) {
Ted Kremenek694eefb2008-07-25 20:28:02 +000052 if (!D)
53 return;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek0b2dd772009-08-07 20:55:20 +000055 const ObjCIvarDecl* ID = D->getPropertyIvarDecl();
Ted Kremenek694eefb2008-07-25 20:28:02 +000056
57 if (!ID)
58 return;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Ted Kremenek694eefb2008-07-25 20:28:02 +000060 IvarUsageMap::iterator I = M.find(ID);
Ted Kremenek0b2dd772009-08-07 20:55:20 +000061 if (I != M.end())
62 I->second = Used;
Ted Kremenek395aaf22008-07-23 00:45:26 +000063}
64
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000065static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) {
66 // Scan the methods for accesses.
67 for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(),
68 E = D->instmeth_end(); I!=E; ++I)
69 Scan(M, (*I)->getBody());
70
71 if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) {
72 // Scan for @synthesized property methods that act as setters/getters
73 // to an ivar.
74 for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(),
75 E = ID->propimpl_end(); I!=E; ++I)
76 Scan(M, *I);
Ted Kremeneke8ec6992009-10-28 22:18:22 +000077
78 // Scan the associated categories as well.
79 for (const ObjCCategoryDecl *CD =
80 ID->getClassInterface()->getCategoryList(); CD ;
81 CD = CD->getNextClassCategory()) {
82 if (const ObjCCategoryImplDecl *CID = CD->getImplementation())
83 Scan(M, CID);
84 }
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +000085 }
86}
87
Ted Kremenekb221e4f2009-11-20 04:31:57 +000088static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
89 SourceManager &SM) {
90 for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end();
91 I!=E; ++I)
92 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
93 SourceLocation L = FD->getLocStart();
94 if (SM.getFileID(L) == FID)
95 Scan(M, FD->getBody());
96 }
97}
98
Ted Kremenek23760022009-08-21 23:58:43 +000099void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D,
100 BugReporter &BR) {
Ted Kremenek395aaf22008-07-23 00:45:26 +0000101
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000102 const ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek395aaf22008-07-23 00:45:26 +0000103 IvarUsageMap M;
104
Ted Kremenek395aaf22008-07-23 00:45:26 +0000105 // Iterate over the ivars.
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000106 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
107 E=ID->ivar_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek0b2dd772009-08-07 20:55:20 +0000109 const ObjCIvarDecl* ID = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek395aaf22008-07-23 00:45:26 +0000111 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +0000112 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +0000113 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000114
115 // Skip IB Outlets.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000116 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +0000117 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek395aaf22008-07-23 00:45:26 +0000119 M[ID] = Unused;
120 }
121
122 if (M.empty())
123 return;
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000124
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +0000125 // Now scan the implementation declaration.
126 Scan(M, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenekb221e4f2009-11-20 04:31:57 +0000128
129 // Any potentially unused ivars?
130 bool hasUnused = false;
131 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
132 if (I->second == Unused) {
133 hasUnused = true;
134 break;
135 }
136
137 if (!hasUnused)
138 return;
139
140 // We found some potentially unused ivars. Scan the entire translation unit
141 // for functions inside the @implementation that reference these ivars.
142 // FIXME: In the future hopefully we can just use the lexical DeclContext
143 // to go from the ObjCImplementationDecl to the lexically "nested"
144 // C functions.
145 SourceManager &SM = BR.getSourceManager();
146 Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
147
Ted Kremenek395aaf22008-07-23 00:45:26 +0000148 // Find ivars that are unused.
149 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
150 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000151 std::string sbuf;
152 llvm::raw_string_ostream os(sbuf);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000153 os << "Instance variable '" << I->first->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000154 << "' in class '" << ID->getNameAsString()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000155 << "' is never used by the methods in its @implementation "
156 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000157
Ted Kremenek13493ea2009-04-02 02:44:03 +0000158 BR.EmitBasicReport("Unused instance variable", "Optimization",
Ted Kremenek395aaf22008-07-23 00:45:26 +0000159 os.str().c_str(), I->first->getLocation());
160 }
161}