add a bunch of routine methods to AnalysisContext.

llvm-svn: 77961
This commit is contained in:
Zhongxing Xu 2009-08-03 07:23:22 +00:00
parent 129bb4165f
commit 9ad0b46a80
3 changed files with 117 additions and 16 deletions

View File

@ -65,32 +65,77 @@ public:
private: private:
ContextKind Kind; ContextKind Kind;
LocationContext *Parent;
AnalysisContext *Ctx; AnalysisContext *Ctx;
LocationContext *Parent;
protected: protected:
LocationContext(ContextKind k, LocationContext *parent, AnalysisContext *ctx) LocationContext(ContextKind k, AnalysisContext *ctx, LocationContext *parent)
: Kind(k), Parent(parent), Ctx(ctx) {} : Kind(k), Ctx(ctx), Parent(parent) {}
public:
ContextKind getKind() const { return Kind; }
AnalysisContext *getAnalysisContext() const { return Ctx; }
LocationContext *getParent() const { return Parent; }
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, Kind, Ctx, Parent);
}
static void Profile(llvm::FoldingSetNodeID &ID, ContextKind k,
AnalysisContext *ctx, LocationContext *parent);
static bool classof(const LocationContext*) { return true; }
}; };
class StackFrameContext : public LocationContext { class StackFrameContext : public LocationContext {
Stmt *CallSite; Stmt *CallSite;
public: public:
StackFrameContext(Stmt *s, LocationContext *parent, AnalysisContext *ctx) StackFrameContext(AnalysisContext *ctx, LocationContext *parent, Stmt *s)
: LocationContext(StackFrame, parent, ctx), CallSite(s) {} : LocationContext(StackFrame, ctx, parent), CallSite(s) {}
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getAnalysisContext(), getParent(), CallSite);
}
static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
LocationContext *parent, Stmt *s);
static bool classof(const LocationContext* Ctx) {
return Ctx->getKind() == StackFrame;
}
}; };
class ScopeContext : public LocationContext { class ScopeContext : public LocationContext {
Stmt *Enter; Stmt *Enter;
public: public:
ScopeContext(Stmt *s, LocationContext *parent, AnalysisContext *ctx) ScopeContext(AnalysisContext *ctx, LocationContext *parent, Stmt *s)
: LocationContext(Scope, parent, ctx), Enter(s) {} : LocationContext(Scope, ctx, parent), Enter(s) {}
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getAnalysisContext(), getParent(), Enter);
}
static void Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
LocationContext *parent, Stmt *s);
static bool classof(const LocationContext* Ctx) {
return Ctx->getKind() == Scope;
}
}; };
class LocationContextManager { class LocationContextManager {
llvm::FoldingSet<LocationContext> Contexts; llvm::FoldingSet<LocationContext> Contexts;
public:
StackFrameContext *getStackFrame(AnalysisContext *ctx,LocationContext *parent,
Stmt *s);
ScopeContext *getScope(AnalysisContext *ctx,LocationContext *parent, Stmt *s);
}; };
} }

View File

@ -23,7 +23,9 @@ namespace clang {
class AnalysisManager : public BugReporterData { class AnalysisManager : public BugReporterData {
AnalysisContextManager ContextMgr; AnalysisContextManager ContextMgr;
AnalysisContext *CurrentContext; AnalysisContext *RootContext;
LocationContextManager LocCtxMgr;
ASTContext &Ctx; ASTContext &Ctx;
Diagnostic &Diags; Diagnostic &Diags;
@ -57,7 +59,7 @@ public:
VisualizeEGDot(vizdot), VisualizeEGUbi(vizubi), PurgeDead(purge), VisualizeEGDot(vizdot), VisualizeEGUbi(vizubi), PurgeDead(purge),
EagerlyAssume(eager), TrimGraph(trim) { EagerlyAssume(eager), TrimGraph(trim) {
CurrentContext = ContextMgr.getContext(d); RootContext = ContextMgr.getContext(d);
} }
AnalysisManager(ASTContext &ctx, Diagnostic &diags, AnalysisManager(ASTContext &ctx, Diagnostic &diags,
@ -73,21 +75,21 @@ public:
VisualizeEGDot(vizdot), VisualizeEGUbi(vizubi), PurgeDead(purge), VisualizeEGDot(vizdot), VisualizeEGUbi(vizubi), PurgeDead(purge),
EagerlyAssume(eager), TrimGraph(trim) { EagerlyAssume(eager), TrimGraph(trim) {
CurrentContext = 0; RootContext = 0;
} }
void setContext(Decl *D) { void setContext(Decl *D) {
CurrentContext = ContextMgr.getContext(D); RootContext = ContextMgr.getContext(D);
} }
Decl *getCodeDecl() const { Decl *getCodeDecl() const {
assert (AScope == ScopeDecl); assert (AScope == ScopeDecl);
return CurrentContext->getDecl(); return RootContext->getDecl();
} }
Stmt *getBody() const { Stmt *getBody() const {
assert (AScope == ScopeDecl); assert (AScope == ScopeDecl);
return CurrentContext->getBody(); return RootContext->getBody();
} }
StoreManagerCreator getStoreManagerCreator() { StoreManagerCreator getStoreManagerCreator() {
@ -99,15 +101,15 @@ public:
} }
virtual CFG *getCFG() { virtual CFG *getCFG() {
return CurrentContext->getCFG(); return RootContext->getCFG();
} }
virtual ParentMap &getParentMap() { virtual ParentMap &getParentMap() {
return CurrentContext->getParentMap(); return RootContext->getParentMap();
} }
virtual LiveVariables *getLiveVariables() { virtual LiveVariables *getLiveVariables() {
return CurrentContext->getLiveVariables(); return RootContext->getLiveVariables();
} }
virtual ASTContext &getContext() { virtual ASTContext &getContext() {
@ -130,6 +132,10 @@ public:
return PD.get(); return PD.get();
} }
StackFrameContext *getRootStackFrame() {
return LocCtxMgr.getStackFrame(RootContext, 0, 0);
}
bool shouldVisualizeGraphviz() const { return VisualizeEGDot; } bool shouldVisualizeGraphviz() const { return VisualizeEGDot; }
bool shouldVisualizeUbigraph() const { return VisualizeEGUbi; } bool shouldVisualizeUbigraph() const { return VisualizeEGUbi; }

View File

@ -72,3 +72,53 @@ AnalysisContext *AnalysisContextManager::getContext(Decl *D) {
Ctx.setDecl(D); Ctx.setDecl(D);
return &Ctx; return &Ctx;
} }
void LocationContext::Profile(llvm::FoldingSetNodeID &ID, ContextKind k,
AnalysisContext *ctx, LocationContext *parent) {
ID.AddInteger(k);
ID.AddPointer(ctx);
ID.AddPointer(parent);
}
void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID,AnalysisContext *ctx,
LocationContext *parent, Stmt *s) {
LocationContext::Profile(ID, StackFrame, ctx, parent);
ID.AddPointer(s);
}
void ScopeContext::Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
LocationContext *parent, Stmt *s) {
LocationContext::Profile(ID, Scope, ctx, parent);
ID.AddPointer(s);
}
StackFrameContext *LocationContextManager::getStackFrame(AnalysisContext *ctx,
LocationContext *parent, Stmt *s) {
llvm::FoldingSetNodeID ID;
StackFrameContext::Profile(ID, ctx, parent, s);
void *InsertPos;
StackFrameContext *f =
cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
if (!f) {
f = new StackFrameContext(ctx, parent, s);
Contexts.InsertNode(f, InsertPos);
}
return f;
}
ScopeContext *LocationContextManager::getScope(AnalysisContext *ctx,
LocationContext *parent, Stmt *s) {
llvm::FoldingSetNodeID ID;
ScopeContext::Profile(ID, ctx, parent, s);
void *InsertPos;
ScopeContext *scope =
cast_or_null<ScopeContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
if (!scope) {
scope = new ScopeContext(ctx, parent, s);
Contexts.InsertNode(scope, InsertPos);
}
return scope;
}