blob: 6c942aaafc08682cb032597326178c3a9aac610c [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);
77 }
78}
79
Ted Kremenek23760022009-08-21 23:58:43 +000080void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D,
81 BugReporter &BR) {
Ted Kremenek395aaf22008-07-23 00:45:26 +000082
Ted Kremenek0b2dd772009-08-07 20:55:20 +000083 const ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek395aaf22008-07-23 00:45:26 +000084 IvarUsageMap M;
85
Ted Kremenek395aaf22008-07-23 00:45:26 +000086 // Iterate over the ivars.
Ted Kremenek0b2dd772009-08-07 20:55:20 +000087 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
88 E=ID->ivar_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremenek0b2dd772009-08-07 20:55:20 +000090 const ObjCIvarDecl* ID = *I;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenek395aaf22008-07-23 00:45:26 +000092 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +000093 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +000094 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +000095
96 // Skip IB Outlets.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000097 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +000098 continue;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek395aaf22008-07-23 00:45:26 +0000100 M[ID] = Unused;
101 }
102
103 if (M.empty())
104 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +0000106 // Now scan the implementation declaration.
107 Scan(M, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek395aaf22008-07-23 00:45:26 +0000109 // Find ivars that are unused.
110 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
111 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000112 std::string sbuf;
113 llvm::raw_string_ostream os(sbuf);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000114 os << "Instance variable '" << I->first->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000115 << "' in class '" << ID->getNameAsString()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000116 << "' is never used by the methods in its @implementation "
117 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000118
Ted Kremenek13493ea2009-04-02 02:44:03 +0000119 BR.EmitBasicReport("Unused instance variable", "Optimization",
Ted Kremenek395aaf22008-07-23 00:45:26 +0000120 os.str().c_str(), I->first->getLocation());
121 }
122}