blob: dbc90d65c30a47eb10bf1607600085ee60832178 [file] [log] [blame]
Ted Kremenek69ea7852008-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 Kremenek69ea7852008-07-23 00:45:26 +000023
24using namespace clang;
25
26enum IVarState { Unused, Used };
Ted Kremenek923c5c32009-08-07 20:55:20 +000027typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
Ted Kremenek69ea7852008-07-23 00:45:26 +000028
Ted Kremenek923c5c32009-08-07 20:55:20 +000029static void Scan(IvarUsageMap& M, const Stmt* S) {
Ted Kremenek69ea7852008-07-23 00:45:26 +000030 if (!S)
31 return;
32
Ted Kremenek923c5c32009-08-07 20:55:20 +000033 if (const ObjCIvarRefExpr* Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
34 const ObjCIvarDecl* D = Ex->getDecl();
Ted Kremenek69ea7852008-07-23 00:45:26 +000035 IvarUsageMap::iterator I = M.find(D);
Ted Kremenek923c5c32009-08-07 20:55:20 +000036 if (I != M.end())
37 I->second = Used;
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000038 return;
Ted Kremenek69ea7852008-07-23 00:45:26 +000039 }
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000040
Ted Kremenek923c5c32009-08-07 20:55:20 +000041 for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000042 Scan(M, *I);
43}
44
Ted Kremenek923c5c32009-08-07 20:55:20 +000045static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) {
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000046 if (!D)
47 return;
48
Ted Kremenek923c5c32009-08-07 20:55:20 +000049 const ObjCIvarDecl* ID = D->getPropertyIvarDecl();
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000050
51 if (!ID)
52 return;
53
54 IvarUsageMap::iterator I = M.find(ID);
Ted Kremenek923c5c32009-08-07 20:55:20 +000055 if (I != M.end())
56 I->second = Used;
Ted Kremenek69ea7852008-07-23 00:45:26 +000057}
58
59void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) {
60
Ted Kremenek923c5c32009-08-07 20:55:20 +000061 const ObjCInterfaceDecl* ID = D->getClassInterface();
Ted Kremenek69ea7852008-07-23 00:45:26 +000062 IvarUsageMap M;
63
Ted Kremenek69ea7852008-07-23 00:45:26 +000064 // Iterate over the ivars.
Ted Kremenek923c5c32009-08-07 20:55:20 +000065 for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
66 E=ID->ivar_end(); I!=E; ++I) {
Ted Kremenek69ea7852008-07-23 00:45:26 +000067
Ted Kremenek923c5c32009-08-07 20:55:20 +000068 const ObjCIvarDecl* ID = *I;
Ted Kremenek69ea7852008-07-23 00:45:26 +000069
70 // Ignore ivars that aren't private.
Ted Kremenek4b8805a2008-07-23 17:14:39 +000071 if (ID->getAccessControl() != ObjCIvarDecl::Private)
Ted Kremenek69ea7852008-07-23 00:45:26 +000072 continue;
Ted Kremenek083541a2008-07-23 18:21:36 +000073
74 // Skip IB Outlets.
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +000075 if (ID->getAttr<IBOutletAttr>())
Ted Kremenek69ea7852008-07-23 00:45:26 +000076 continue;
77
78 M[ID] = Unused;
79 }
80
81 if (M.empty())
82 return;
83
84 // Now scan the methods for accesses.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000085 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
Ted Kremenek923c5c32009-08-07 20:55:20 +000086 E = D->instmeth_end(); I!=E; ++I)
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +000087 Scan(M, (*I)->getBody());
Ted Kremenek69ea7852008-07-23 00:45:26 +000088
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000089 // Scan for @synthesized property methods that act as setters/getters
90 // to an ivar.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +000091 for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
92 E = D->propimpl_end(); I!=E; ++I)
Ted Kremenek0e8cfa42008-07-25 20:28:02 +000093 Scan(M, *I);
94
Ted Kremenek69ea7852008-07-23 00:45:26 +000095 // Find ivars that are unused.
96 for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
97 if (I->second == Unused) {
Ted Kremenekdd04ed62009-06-24 23:06:47 +000098 std::string sbuf;
99 llvm::raw_string_ostream os(sbuf);
Chris Lattner271d4c22008-11-24 05:29:24 +0000100 os << "Instance variable '" << I->first->getNameAsString()
101 << "' in class '" << ID->getNameAsString()
Ted Kremenek0e8cfa42008-07-25 20:28:02 +0000102 << "' is never used by the methods in its @implementation "
103 "(although it may be used by category methods).";
Ted Kremenek083541a2008-07-23 18:21:36 +0000104
Ted Kremenek2f3a21e2009-04-02 02:44:03 +0000105 BR.EmitBasicReport("Unused instance variable", "Optimization",
Ted Kremenek69ea7852008-07-23 00:45:26 +0000106 os.str().c_str(), I->first->getLocation());
107 }
108}