#include "llvm/ADT/DenseMap.h"
#include "clang/AST/ParentMap.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/CFGStmtMap.h"
using namespace clang;
typedef llvm::DenseMap<Stmt*,CFGBlock*> SMap;
static SMap *AsMap(void *m) { return (SMap*) m; }
CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }
CFGBlock *CFGStmtMap::getBlock(Stmt *S) {
SMap *SM = AsMap(M);
Stmt *X = S;
while (X) {
SMap::iterator I = SM->find(X);
if (I != SM->end()) {
CFGBlock *B = I->second;
if (X != S)
(*SM)[X] = B;
return B;
}
X = PM->getParentIgnoreParens(X);
}
return 0;
}
static void Accumulate(SMap &SM, CFGBlock *B) {
for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) {
const CFGElement &CE = *I;
const CFGStmt *CS = CE.getAs<CFGStmt>();
if (!CS)
continue;
CFGBlock *&Entry = SM[CS->getStmt()];
if (Entry)
continue;
Entry = B;
}
if (Stmt *Label = B->getLabel())
SM[Label] = B;
if (Stmt *Term = B->getTerminator())
SM[Term] = B;
}
CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {
if (!C || !PM)
return 0;
SMap *SM = new SMap();
for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I)
Accumulate(*SM, *I);
return new CFGStmtMap(PM, SM);
}