2010-02-25 11:26:55 +08:00
|
|
|
//==- ObjCUnusedIVarsChecker.cpp - Check for unused ivars --------*- C++ -*-==//
|
2008-07-23 08:45:26 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
#include "ClangSACheckers.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerV2.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2008-07-23 08:45:26 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
2009-11-28 18:50:44 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-07-23 08:45:26 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2008-07-23 08:45:26 +08:00
|
|
|
|
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +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-09-09 23:08:12 +08:00
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
const ObjCIvarDecl* ID = D->getPropertyIvarDecl();
|
2008-07-26 04:28:02 +08:00
|
|
|
|
|
|
|
if (!ID)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-26 04:28:02 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-10-29 04:37:47 +08:00
|
|
|
static void Scan(IvarUsageMap& M, const ObjCContainerDecl* D) {
|
|
|
|
// Scan the methods for accesses.
|
|
|
|
for (ObjCContainerDecl::instmeth_iterator I = D->instmeth_begin(),
|
|
|
|
E = D->instmeth_end(); I!=E; ++I)
|
|
|
|
Scan(M, (*I)->getBody());
|
2010-02-25 11:26:55 +08:00
|
|
|
|
|
|
|
if (const ObjCImplementationDecl *ID = dyn_cast<ObjCImplementationDecl>(D)) {
|
2009-10-29 04:37:47 +08:00
|
|
|
// Scan for @synthesized property methods that act as setters/getters
|
|
|
|
// to an ivar.
|
|
|
|
for (ObjCImplementationDecl::propimpl_iterator I = ID->propimpl_begin(),
|
|
|
|
E = ID->propimpl_end(); I!=E; ++I)
|
|
|
|
Scan(M, *I);
|
2010-02-25 11:26:55 +08:00
|
|
|
|
2009-10-29 06:18:22 +08:00
|
|
|
// Scan the associated categories as well.
|
|
|
|
for (const ObjCCategoryDecl *CD =
|
|
|
|
ID->getClassInterface()->getCategoryList(); CD ;
|
|
|
|
CD = CD->getNextClassCategory()) {
|
|
|
|
if (const ObjCCategoryImplDecl *CID = CD->getImplementation())
|
|
|
|
Scan(M, CID);
|
|
|
|
}
|
2009-10-29 04:37:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-20 12:31:57 +08:00
|
|
|
static void Scan(IvarUsageMap &M, const DeclContext *C, const FileID FID,
|
|
|
|
SourceManager &SM) {
|
|
|
|
for (DeclContext::decl_iterator I=C->decls_begin(), E=C->decls_end();
|
|
|
|
I!=E; ++I)
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
|
|
|
|
SourceLocation L = FD->getLocStart();
|
2010-02-25 11:26:55 +08:00
|
|
|
if (SM.getFileID(L) == FID)
|
2009-11-20 12:31:57 +08:00
|
|
|
Scan(M, FD->getBody());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
static void checkObjCUnusedIvar(const ObjCImplementationDecl *D,
|
2009-08-22 07:58:43 +08:00
|
|
|
BugReporter &BR) {
|
2008-07-23 08:45:26 +08:00
|
|
|
|
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) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-08 04:55:20 +08:00
|
|
|
const ObjCIvarDecl* ID = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-25 11:26:55 +08:00
|
|
|
// Ignore ivars that...
|
|
|
|
// (a) aren't private
|
|
|
|
// (b) explicitly marked unused
|
|
|
|
// (c) are iboutlets
|
2010-10-28 10:16:22 +08:00
|
|
|
// (d) are unnamed bitfields
|
2010-02-25 11:26:55 +08:00
|
|
|
if (ID->getAccessControl() != ObjCIvarDecl::Private ||
|
2010-05-20 01:38:06 +08:00
|
|
|
ID->getAttr<UnusedAttr>() || ID->getAttr<IBOutletAttr>() ||
|
2010-10-28 10:16:22 +08:00
|
|
|
ID->getAttr<IBOutletCollectionAttr>() ||
|
|
|
|
ID->isUnnamedBitfield())
|
2008-07-23 08:45:26 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-23 08:45:26 +08:00
|
|
|
M[ID] = Unused;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M.empty())
|
|
|
|
return;
|
2010-02-25 11:26:55 +08:00
|
|
|
|
2009-10-29 04:37:47 +08:00
|
|
|
// Now scan the implementation declaration.
|
|
|
|
Scan(M, D);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-20 12:31:57 +08:00
|
|
|
// Any potentially unused ivars?
|
|
|
|
bool hasUnused = false;
|
|
|
|
for (IvarUsageMap::iterator I = M.begin(), E = M.end(); I!=E; ++I)
|
|
|
|
if (I->second == Unused) {
|
|
|
|
hasUnused = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-25 11:26:55 +08:00
|
|
|
|
2009-11-20 12:31:57 +08:00
|
|
|
if (!hasUnused)
|
|
|
|
return;
|
2010-02-25 11:26:55 +08:00
|
|
|
|
2009-11-20 12:31:57 +08:00
|
|
|
// We found some potentially unused ivars. Scan the entire translation unit
|
|
|
|
// for functions inside the @implementation that reference these ivars.
|
|
|
|
// FIXME: In the future hopefully we can just use the lexical DeclContext
|
|
|
|
// to go from the ObjCImplementationDecl to the lexically "nested"
|
|
|
|
// C functions.
|
|
|
|
SourceManager &SM = BR.getSourceManager();
|
|
|
|
Scan(M, D->getDeclContext(), SM.getFileID(D->getLocation()), SM);
|
|
|
|
|
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);
|
2010-04-17 17:33:03 +08:00
|
|
|
os << "Instance variable '" << I->first << "' in class '" << ID
|
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",
|
2009-11-30 02:27:55 +08:00
|
|
|
os.str(), I->first->getLocation());
|
2008-07-23 08:45:26 +08:00
|
|
|
}
|
|
|
|
}
|
2011-02-18 05:39:33 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ObjCUnusedIvarsChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ObjCUnusedIvarsChecker : public CheckerV2<
|
|
|
|
check::ASTDecl<ObjCImplementationDecl> > {
|
|
|
|
public:
|
|
|
|
void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
checkObjCUnusedIvar(D, BR);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerObjCUnusedIvarsChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<ObjCUnusedIvarsChecker>();
|
|
|
|
}
|