blob: 0063c40482a0e38ec290420440ff2dc73f8f19da [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 };
27typedef llvm::DenseMap<ObjCIvarDecl*,IVarState> IvarUsageMap;
28
29static void Scan(IvarUsageMap& M, Stmt* S) {
30 if (!S)
31 return;
32
33 if (ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
34 ObjCIvarDecl* D = Ex->getDecl();
35 IvarUsageMap::iterator I = M.find(D);
36 if (I != M.end()) I->second = Used;
Ted Kremenek694eefb2008-07-25 20:28:02 +000037 return;
Ted Kremenek395aaf22008-07-23 00:45:26 +000038 }
Ted Kremenek694eefb2008-07-25 20:28:02 +000039
40 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E;++I)
41 Scan(M, *I);
42}
43
44static void Scan(IvarUsageMap& M, ObjCPropertyImplDecl* D) {
45 if (!D)
46 return;
47
48 ObjCIvarDecl* ID = D->getPropertyIvarDecl();
49
50 if (!ID)
51 return;
52
53 IvarUsageMap::iterator I = M.find(ID);
54 if (I != M.end()) I->second = Used;
Ted Kremenek395aaf22008-07-23 00:45:26 +000055}
56
57void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) {
58
59 ObjCInterfaceDecl* ID = D->getClassInterface();
60 IvarUsageMap M;
61
Ted Kremenek395aaf22008-07-23 00:45:26 +000062 // Iterate over the ivars.
63 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
64 I!=E; ++I) {
65
66 ObjCIvarDecl* ID = *I;
67
68 // Ignore ivars that aren't private.
Ted Kremenek6678f7f2008-07-23 17:14:39 +000069 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek395aaf22008-07-23 00:45:26 +000070 continue;
Ted Kremenekcc87ba22008-07-23 18:21:36 +000071
72 // Skip IB Outlets.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000073 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek395aaf22008-07-23 00:45:26 +000074 continue;
75
76 M[ID] = Unused;
77 }
78
79 if (M.empty())
80 return;
81
82 // Now scan the methods for accesses.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000083 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
84 E = D->instmeth_end(); I!=E; ++I)
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +000085 Scan(M, (*I)->getBody());
Ted Kremenek395aaf22008-07-23 00:45:26 +000086
Ted Kremenek694eefb2008-07-25 20:28:02 +000087 // Scan for @synthesized property methods that act as setters/getters
88 // to an ivar.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +000089 for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
90 E = D->propimpl_end(); I!=E; ++I)
Ted Kremenek694eefb2008-07-25 20:28:02 +000091 Scan(M, *I);
92
Ted Kremenek395aaf22008-07-23 00:45:26 +000093 // Find ivars that are unused.
94 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
95 if (I->second == Unused) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +000096 std::string sbuf;
97 llvm::raw_string_ostream os(sbuf);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +000098 os << "Instance variable '" << I->first->getNameAsString()
99 << "' in class '" << ID->getNameAsString()
Ted Kremenek694eefb2008-07-25 20:28:02 +0000100 << "' is never used by the methods in its @implementation "
101 "(although it may be used by category methods).";
Ted Kremenekcc87ba22008-07-23 18:21:36 +0000102
Ted Kremenek13493ea2009-04-02 02:44:03 +0000103 BR.EmitBasicReport("Unused instance variable", "Optimization",
Ted Kremenek395aaf22008-07-23 00:45:26 +0000104 os.str().c_str(), I->first->getLocation());
105 }
106}
107