blob: 2d9b53163f6adf220a0dd19d2aeca9b48c43546a [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 Kremenek23760022009-08-21 23:58:43 +000088void clang::CheckObjCUnusedIvar(const ObjCImplementationDecl *D,
89 BugReporter &BR) {
Ted Kremenek395aaf22008-07-23 00:45:26 +000090
Ted Kremenek0b2dd772009-08-07 20:55:20 +000091 const ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek395aaf22008-07-23 00:45:26 +000092 IvarUsageMap M;
93
Ted Kremenek395aaf22008-07-23 00:45:26 +000094 // Iterate over the ivars.
Ted Kremenek0b2dd772009-08-07 20:55:20 +000095 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
96 E=ID->ivar_end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenek0b2dd772009-08-07 20:55:20 +000098 const ObjCIvarDecl* ID = *I;
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek395aaf22008-07-23 00:45:26 +0000100 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +0000101 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +0000102 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000103
104 // Skip IB Outlets.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000105 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +0000106 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek395aaf22008-07-23 00:45:26 +0000108 M[ID] = Unused;
109 }
110
111 if (M.empty())
112 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek8fa3a1a2009-10-28 20:37:47 +0000114 // Now scan the implementation declaration.
115 Scan(M, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremenek395aaf22008-07-23 00:45:26 +0000117 // Find ivars that are unused.
118 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
119 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000120 std::string sbuf;
121 llvm::raw_string_ostream os(sbuf);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000122 os << "Instance variable '" << I->first->getNameAsString()
Mike Stump1eb44332009-09-09 15:08:12 +0000123 << "' in class '" << ID->getNameAsString()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000124 << "' is never used by the methods in its @implementation "
125 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000126
Ted Kremenek13493ea2009-04-02 02:44:03 +0000127 BR.EmitBasicReport("Unused instance variable", "Optimization",
Ted Kremenek395aaf22008-07-23 00:45:26 +0000128 os.str().c_str(), I->first->getLocation());
129 }
130}