freebsd-skq/lib/Analysis/AnalysisContext.cpp

298 lines
9.1 KiB
C++
Raw Normal View History

2009-10-14 18:03:49 +00:00
//== AnalysisContext.cpp - Analysis context for Path Sens analysis -*- 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 AnalysisContext, a class that manages the analysis context
// data for path sensitive analysis.
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/PathSensitive/AnalysisContext.h"
2009-12-15 18:49:47 +00:00
#include "clang/Analysis/PathSensitive/MemRegion.h"
2009-10-14 18:03:49 +00:00
#include "clang/Analysis/Analyses/LiveVariables.h"
#include "clang/Analysis/CFG.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
2010-01-15 15:39:40 +00:00
#include "clang/AST/DeclTemplate.h"
2009-10-14 18:03:49 +00:00
#include "clang/AST/ParentMap.h"
2009-12-01 11:08:04 +00:00
#include "clang/AST/StmtVisitor.h"
#include "clang/Analysis/Support/BumpVector.h"
2009-10-14 18:03:49 +00:00
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
2009-10-23 14:22:18 +00:00
void AnalysisContextManager::clear() {
for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
delete I->second;
Contexts.clear();
}
2009-10-14 18:03:49 +00:00
Stmt *AnalysisContext::getBody() {
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
return FD->getBody();
else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
return MD->getBody();
2009-12-15 18:49:47 +00:00
else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
return BD->getBody();
2010-01-15 15:39:40 +00:00
else if (const FunctionTemplateDecl *FunTmpl
= dyn_cast_or_null<FunctionTemplateDecl>(D))
return FunTmpl->getTemplatedDecl()->getBody();
2009-10-14 18:03:49 +00:00
2009-12-15 18:49:47 +00:00
llvm_unreachable("unknown code decl");
2009-10-14 18:03:49 +00:00
}
const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
return MD->getSelfDecl();
return NULL;
}
CFG *AnalysisContext::getCFG() {
if (!cfg)
cfg = CFG::buildCFG(getBody(), &D->getASTContext());
return cfg;
}
ParentMap &AnalysisContext::getParentMap() {
if (!PM)
PM = new ParentMap(getBody());
return *PM;
}
LiveVariables *AnalysisContext::getLiveVariables() {
if (!liveness) {
CFG *c = getCFG();
if (!c)
return 0;
2009-12-01 11:08:04 +00:00
liveness = new LiveVariables(*this);
2009-10-14 18:03:49 +00:00
liveness->runOnCFG(*c);
liveness->runOnAllBlocks(*c, 0, true);
}
return liveness;
}
AnalysisContext *AnalysisContextManager::getContext(const Decl *D) {
AnalysisContext *&AC = Contexts[D];
if (!AC)
AC = new AnalysisContext(D);
return AC;
}
2009-12-15 18:49:47 +00:00
const BlockDecl *BlockInvocationContext::getBlockDecl() const {
return Data.is<const BlockDataRegion*>() ?
Data.get<const BlockDataRegion*>()->getDecl()
: Data.get<const BlockDecl*>();
}
//===----------------------------------------------------------------------===//
// FoldingSet profiling.
//===----------------------------------------------------------------------===//
void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
ContextKind ck,
AnalysisContext *ctx,
const LocationContext *parent,
const void* data) {
ID.AddInteger(ck);
2009-10-14 18:03:49 +00:00
ID.AddPointer(ctx);
ID.AddPointer(parent);
2009-12-15 18:49:47 +00:00
ID.AddPointer(data);
2009-10-14 18:03:49 +00:00
}
2009-12-15 18:49:47 +00:00
void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
2010-01-01 10:34:51 +00:00
Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
2009-10-14 18:03:49 +00:00
}
2009-12-15 18:49:47 +00:00
void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getAnalysisContext(), getParent(), Enter);
2009-10-14 18:03:49 +00:00
}
2009-12-15 18:49:47 +00:00
void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
if (const BlockDataRegion *BR = getBlockRegion())
Profile(ID, getAnalysisContext(), getParent(), BR);
else
Profile(ID, getAnalysisContext(), getParent(),
Data.get<const BlockDecl*>());
2009-10-23 14:22:18 +00:00
}
2009-12-15 18:49:47 +00:00
//===----------------------------------------------------------------------===//
// LocationContext creation.
//===----------------------------------------------------------------------===//
template <typename LOC, typename DATA>
const LOC*
LocationContextManager::getLocationContext(AnalysisContext *ctx,
const LocationContext *parent,
const DATA *d) {
llvm::FoldingSetNodeID ID;
LOC::Profile(ID, ctx, parent, d);
void *InsertPos;
2009-10-23 14:22:18 +00:00
2009-12-15 18:49:47 +00:00
LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
if (!L) {
L = new LOC(ctx, parent, d);
Contexts.InsertNode(L, InsertPos);
}
return L;
2009-10-23 14:22:18 +00:00
}
2009-12-15 18:49:47 +00:00
const StackFrameContext*
2009-10-14 18:03:49 +00:00
LocationContextManager::getStackFrame(AnalysisContext *ctx,
const LocationContext *parent,
2010-01-01 10:34:51 +00:00
const Stmt *s, const CFGBlock *blk,
unsigned idx) {
llvm::FoldingSetNodeID ID;
StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
void *InsertPos;
StackFrameContext *L =
cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
if (!L) {
L = new StackFrameContext(ctx, parent, s, blk, idx);
Contexts.InsertNode(L, InsertPos);
}
return L;
2009-12-15 18:49:47 +00:00
}
2009-10-14 18:03:49 +00:00
2009-12-15 18:49:47 +00:00
const ScopeContext *
LocationContextManager::getScope(AnalysisContext *ctx,
const LocationContext *parent,
const Stmt *s) {
return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
2009-10-14 18:03:49 +00:00
}
2009-12-15 18:49:47 +00:00
const BlockInvocationContext *
LocationContextManager::getBlockInvocation(AnalysisContext *ctx,
const LocationContext *parent,
const BlockDataRegion *BR) {
return getLocationContext<BlockInvocationContext, BlockDataRegion>(ctx,
parent,
BR);
}
2009-10-14 18:03:49 +00:00
2009-12-15 18:49:47 +00:00
//===----------------------------------------------------------------------===//
// LocationContext methods.
//===----------------------------------------------------------------------===//
2009-10-14 18:03:49 +00:00
2009-12-15 18:49:47 +00:00
const StackFrameContext *LocationContext::getCurrentStackFrame() const {
const LocationContext *LC = this;
while (LC) {
if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
return SFC;
LC = LC->getParent();
2009-10-14 18:03:49 +00:00
}
2009-12-15 18:49:47 +00:00
return NULL;
}
const StackFrameContext *
LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
const LocationContext *LC = this;
while (LC) {
if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
if (cast<DeclContext>(SFC->getDecl()) == DC)
return SFC;
}
LC = LC->getParent();
}
return NULL;
2009-10-14 18:03:49 +00:00
}
2009-12-01 11:08:04 +00:00
//===----------------------------------------------------------------------===//
// Lazily generated map to query the external variables referenced by a Block.
//===----------------------------------------------------------------------===//
namespace {
class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
BumpVector<const VarDecl*> &BEVals;
BumpVectorContext &BC;
public:
FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
BumpVectorContext &bc)
: BEVals(bevals), BC(bc) {}
void VisitStmt(Stmt *S) {
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
if (Stmt *child = *I)
Visit(child);
}
void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
BEVals.push_back(VD, BC);
}
};
} // end anonymous namespace
typedef BumpVector<const VarDecl*> DeclVec;
static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
void *&Vec,
llvm::BumpPtrAllocator &A) {
if (Vec)
return (DeclVec*) Vec;
BumpVectorContext BC(A);
DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
new (BV) DeclVec(BC, 10);
// Find the referenced variables.
FindBlockDeclRefExprsVals F(*BV, BC);
F.Visit(BD->getBody());
Vec = BV;
return BV;
}
std::pair<AnalysisContext::referenced_decls_iterator,
AnalysisContext::referenced_decls_iterator>
AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
if (!ReferencedBlockVars)
ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
return std::make_pair(V->begin(), V->end());
}
//===----------------------------------------------------------------------===//
// Cleanup.
//===----------------------------------------------------------------------===//
AnalysisContext::~AnalysisContext() {
delete cfg;
delete liveness;
delete PM;
delete ReferencedBlockVars;
}
AnalysisContextManager::~AnalysisContextManager() {
for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
delete I->second;
}
2009-12-15 18:49:47 +00:00
LocationContext::~LocationContext() {}
LocationContextManager::~LocationContextManager() {
clear();
}
void LocationContextManager::clear() {
for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
E = Contexts.end(); I != E; ) {
LocationContext *LC = &*I;
++I;
delete LC;
}
Contexts.clear();
}