2008-07-23 08:45:26 +08:00
|
|
|
//==- CheckObjCUnusedIVars.cpp - Check for unused ivars ----------*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a CheckObjCUnusedIvars, a checker that
|
|
|
|
// analyzes an Objective-C class's interface/implementation to determine if it
|
|
|
|
// has any ivars that are never accessed.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
enum IVarState { Unused, Used };
|
2009-08-08 04:55:20 +08:00
|
|
|
typedef llvm::DenseMap<const ObjCIvarDecl*,IVarState> IvarUsageMap;
|
2008-07-23 08:45:26 +08:00
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
static void Scan(IvarUsageMap& M, const Stmt* S) {
|
2008-07-23 08:45:26 +08:00
|
|
|
if (!S)
|
|
|
|
return;
|
|
|
|
|
2009-08-08 05:13:23 +08:00
|
|
|
if (const ObjCIvarRefExpr *Ex = dyn_cast<ObjCIvarRefExpr>(S)) {
|
|
|
|
const ObjCIvarDecl *D = Ex->getDecl();
|
2008-07-23 08:45:26 +08:00
|
|
|
IvarUsageMap::iterator I = M.find(D);
|
2009-08-08 04:55:20 +08:00
|
|
|
if (I != M.end())
|
|
|
|
I->second = Used;
|
2008-07-26 04:28:02 +08:00
|
|
|
return;
|
2008-07-23 08:45:26 +08:00
|
|
|
}
|
2008-07-26 04:28:02 +08:00
|
|
|
|
2009-08-08 05:13:23 +08:00
|
|
|
// Blocks can reference an instance variable of a class.
|
|
|
|
if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
|
|
|
|
Scan(M, BE->getBody());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
for (Stmt::const_child_iterator I=S->child_begin(),E=S->child_end(); I!=E;++I)
|
2008-07-26 04:28:02 +08:00
|
|
|
Scan(M, *I);
|
|
|
|
}
|
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
static void Scan(IvarUsageMap& M, const ObjCPropertyImplDecl* D) {
|
2008-07-26 04:28:02 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
const ObjCIvarDecl* ID = D->getPropertyIvarDecl();
|
2008-07-26 04:28:02 +08:00
|
|
|
|
|
|
|
if (!ID)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IvarUsageMap::iterator I = M.find(ID);
|
2009-08-08 04:55:20 +08:00
|
|
|
if (I != M.end())
|
|
|
|
I->second = Used;
|
2008-07-23 08:45:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void clang::CheckObjCUnusedIvar(ObjCImplementationDecl* D, BugReporter& BR) {
|
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
const ObjCInterfaceDecl* ID = D->getClassInterface();
|
2008-07-23 08:45:26 +08:00
|
|
|
IvarUsageMap M;
|
|
|
|
|
|
|
|
// Iterate over the ivars.
|
2009-08-08 04:55:20 +08:00
|
|
|
for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(),
|
|
|
|
E=ID->ivar_end(); I!=E; ++I) {
|
2008-07-23 08:45:26 +08:00
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
const ObjCIvarDecl* ID = *I;
|
2008-07-23 08:45:26 +08:00
|
|
|
|
|
|
|
// Ignore ivars that aren't private.
|
2008-07-24 01:14:39 +08:00
|
|
|
if (ID->getAccessControl() != ObjCIvarDecl::Private)
|
2008-07-23 08:45:26 +08:00
|
|
|
continue;
|
2008-07-24 02:21:36 +08:00
|
|
|
|
|
|
|
// Skip IB Outlets.
|
2009-06-30 10:34:44 +08:00
|
|
|
if (ID->getAttr<IBOutletAttr>())
|
2008-07-23 08:45:26 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
M[ID] = Unused;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Now scan the methods for accesses.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
|
2009-08-08 04:55:20 +08:00
|
|
|
E = D->instmeth_end(); I!=E; ++I)
|
2009-06-30 10:35:26 +08:00
|
|
|
Scan(M, (*I)->getBody());
|
2008-07-23 08:45:26 +08:00
|
|
|
|
2008-07-26 04:28:02 +08:00
|
|
|
// Scan for @synthesized property methods that act as setters/getters
|
|
|
|
// to an ivar.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
|
|
|
|
E = D->propimpl_end(); I!=E; ++I)
|
2008-07-26 04:28:02 +08:00
|
|
|
Scan(M, *I);
|
|
|
|
|
2008-07-23 08:45:26 +08:00
|
|
|
// Find ivars that are unused.
|
|
|
|
for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
|
|
|
|
if (I->second == Unused) {
|
2009-06-25 07:06:47 +08:00
|
|
|
std::string sbuf;
|
|
|
|
llvm::raw_string_ostream os(sbuf);
|
2008-11-24 13:29:24 +08:00
|
|
|
os << "Instance variable '" << I->first->getNameAsString()
|
|
|
|
<< "' in class '" << ID->getNameAsString()
|
2008-07-26 04:28:02 +08:00
|
|
|
<< "' is never used by the methods in its @implementation "
|
|
|
|
"(although it may be used by category methods).";
|
2008-07-24 02:21:36 +08:00
|
|
|
|
2009-04-02 10:44:03 +08:00
|
|
|
BR.EmitBasicReport("Unused instance variable", "Optimization",
|
2008-07-23 08:45:26 +08:00
|
|
|
os.str().c_str(), I->first->getLocation());
|
|
|
|
}
|
|
|
|
}
|