use of org.autorefactor.cfg.CFGEdgeBuilder in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the provided node.
*
* @param node the node for which to build a CFG.
* @param state the blocks liveness state before current node
* @param throwers the thrower blocks information
* @return the blocks liveness state after current node
*/
public LivenessState buildCFG(BreakStatement node, LivenessState state, ThrowerBlocks throwers) {
final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
final Statement targetStmt;
if (node.getLabel() != null) {
targetStmt = findLabeledParentStmt(node);
} else {
targetStmt = findBreakableParentStmt(node);
}
addEdgeToBuild(targetStmt, new CFGEdgeBuilder(basicBlock), true);
return state.copyLiveBasicBlock();
}
use of org.autorefactor.cfg.CFGEdgeBuilder in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the provided node.
*
* @param node the node for which to build a CFG.
* @param state the blocks liveness state before current node
* @param throwers the thrower blocks information
* @return the blocks liveness state after current node
*/
public LivenessState buildCFG(TryStatement node, LivenessState state, ThrowerBlocks throwers) {
final ThrowerBlocks localThrowers = new ThrowerBlocks();
final LivenessState liveAfterTry = buildCFG(node.getBody(), state, localThrowers);
final LivenessState liveAfterCatchClauses = new LivenessState();
final Set<ITypeBinding> caughtExceptions = new HashSet<ITypeBinding>();
for (CatchClause catchClause : catchClauses(node)) {
final LivenessState catchState = new LivenessState();
CFGBasicBlock catchBasicBlock = getCFGBasicBlock(catchClause, catchState);
final SingleVariableDeclaration exceptionDecl = catchClause.getException();
addDeclaration(catchBasicBlock, exceptionDecl, DECL_INIT);
final ITypeBinding caughtException = exceptionDecl.getType().resolveBinding();
caughtExceptions.add(caughtException);
final List<CFGBasicBlock> throwingBlocksInTry = localThrowers.selectBlocksThrowing(caughtException);
if (throwingBlocksInTry.isEmpty()) {
// TODO JNR dead code found!!
}
final List<CFGEdgeBuilder> liveBeforeCatchClause = new LinkedList<CFGEdgeBuilder>();
for (CFGBasicBlock throwingBlockInTry : throwingBlocksInTry) {
// TODO JNR if a Statement throws an exception, it must break the current basicBlock
// TODO JNR how to specify this edge is due to an exception?
liveBeforeCatchClause.add(new CFGEdgeBuilder(throwingBlockInTry));
}
final LivenessState liveAfterCatchClause = buildCFG(catchClause.getBody(), catchState, new ThrowerBlocks());
liveAfterCatchClauses.addAll(liveAfterCatchClause);
}
// TODO JNR move uncaught exceptions from localThrowers to throwers
final Map<CFGBasicBlock, Set<ITypeBinding>> throwUncaughtExceptions = localThrowers.selectBlocksThrowingOtherThan(caughtExceptions);
for (Entry<CFGBasicBlock, Set<ITypeBinding>> throwing : throwUncaughtExceptions.entrySet()) {
final CFGEdgeBuilder uncaughtExceptionEdge = new CFGEdgeBuilder(throwing.getKey(), true);
liveAfterCatchClauses.add(uncaughtExceptionEdge);
throwers.addThrow(uncaughtExceptionEdge, throwing.getValue());
}
if (node.getFinally() != null) {
final LivenessState liveBeforeFinally = new LivenessState();
liveBeforeFinally.addAll(liveAfterTry);
liveBeforeFinally.addAll(liveAfterCatchClauses);
final LivenessState liveAfterFinally = buildCFG(node.getFinally(), liveBeforeFinally, throwers);
return liveAfterFinally;
} else {
return liveAfterCatchClauses.copyLiveBasicBlock();
}
}
use of org.autorefactor.cfg.CFGEdgeBuilder in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the provided node.
*
* @param node the node for which to build a CFG.
* @param state the blocks liveness state before current node
* @param throwers the thrower blocks information
* @return the blocks liveness state after current node
*/
public LivenessState buildCFG(ExpressionStatement node, LivenessState state, ThrowerBlocks throwers) {
boolean isNewBlock = state.requireNewBlock();
final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
boolean mightThrow = addVariableAccess(basicBlock, node.getExpression(), READ, throwers);
if (!isNewBlock && mightThrow) {
final CFGBasicBlock currentBlock = getCFGBasicBlock(node, state.nextStmtWillCreateNewBlock());
return new LivenessState(currentBlock, new CFGEdgeBuilder(currentBlock));
// TODO JNR create a new CFGBasicBlock from here to catch / finally / exit
}
return getInBlockStmtResult(state, basicBlock);
}
use of org.autorefactor.cfg.CFGEdgeBuilder in project AutoRefactor by JnRouvignac.
the class CFGBuilder method buildCFG.
/**
* Builds a CFG for the provided node.
*
* @param node the node for which to build a CFG.
* @param state the blocks liveness state before current node
* @param throwers the thrower blocks information
* @return the blocks liveness state after current node
*/
public LivenessState buildCFG(SwitchStatement node, LivenessState state, ThrowerBlocks throwers) {
final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
final LivenessState liveBeforeBody = new LivenessState(basicBlock, new CFGEdgeBuilder(basicBlock));
final LivenessState liveAfterBody = buildCFG(statements(node), liveBeforeBody, throwers);
liveAfterBody.add(new CFGEdgeBuilder(basicBlock));
buildEdgesAfterBranchableStmt(node, liveAfterBody, basicBlock);
return liveAfterBody.nextStmtWillCreateNewBlock();
}
Aggregations