blob: c2deeeff8fe49757ba61e6d0bd198be8987a7351 [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"
23#include <sstream>
24
25using namespace clang;
26
27enum IVarState { Unused, Used };
28typedef llvm::DenseMap<ObjCIvarDecl*,IVarState> IvarUsageMap;
29
30static void Scan(IvarUsageMap& M, Stmt* S) {
31 if (!S)
32 return;
33
34 if (ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
35 ObjCIvarDecl* D = Ex->getDecl();
36 IvarUsageMap::iterator I = M.find(D);
37 if (I != M.end()) I->second = Used;
Ted Kremenek694eefb2008-07-25 20:28:02 +000038 return;
Ted Kremenek395aaf22008-07-23 00:45:26 +000039 }
Ted Kremenek694eefb2008-07-25 20:28:02 +000040
41 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E;++I)
42 Scan(M, *I);
43}
44
45static void Scan(IvarUsageMap& M, ObjCPropertyImplDecl* D) {
46 if (!D)
47 return;
48
49 ObjCIvarDecl* ID = D->getPropertyIvarDecl();
50
51 if (!ID)
52 return;
53
54 IvarUsageMap::iterator I = M.find(ID);
55 if (I != M.end()) I->second = Used;
Ted Kremenek395aaf22008-07-23 00:45:26 +000056}
57
58void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) {
59
60 ObjCInterfaceDecl* ID = D->getClassInterface();
61 IvarUsageMap M;
62
63
64
65 // Iterate over the ivars.
66 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
67 I!=E; ++I) {
68
69 ObjCIvarDecl* ID = *I;
70
71 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +000072 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +000073 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +000074
75 // Skip IB Outlets.
76 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +000077 continue;
78
79 M[ID] = Unused;
80 }
81
82 if (M.empty())
83 return;
84
85 // Now scan the methods for accesses.
86 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
87 E = D->instmeth_end(); I!=E; ++I)
88 Scan(M, (*I)->getBody());
89
Ted Kremenek694eefb2008-07-25 20:28:02 +000090 // Scan for @synthesized property methods that act as setters/getters
91 // to an ivar.
92 for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
93 E = D->propimpl_end(); I!=E; ++I)
94 Scan(M, *I);
95
Ted Kremenek395aaf22008-07-23 00:45:26 +000096 // Find ivars that are unused.
97 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
98 if (I->second == Unused) {
99
100 std::ostringstream os;
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000101 os << "Instance variable '" << I->first->getName()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000102 << "' in class '" << ID->getName()
103 << "' is never used by the methods in its @implementation "
104 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000105
Ted Kremenek0b007852008-09-21 18:58:22 +0000106 BR.EmitBasicReport("unused ivar", "Optimization",
Ted Kremenek395aaf22008-07-23 00:45:26 +0000107 os.str().c_str(), I->first->getLocation());
108 }
109}
110