Search in sources :

Example 11 with CFGEdgeBuilder

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();
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 12 with CFGEdgeBuilder

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();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) CatchClause(org.eclipse.jdt.core.dom.CatchClause) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder) LinkedList(java.util.LinkedList) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) HashSet(java.util.HashSet)

Example 13 with CFGEdgeBuilder

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);
}
Also used : CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 14 with CFGEdgeBuilder

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();
}
Also used : CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Aggregations

CFGEdgeBuilder (org.autorefactor.cfg.CFGEdgeBuilder)14 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)3 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)3 DoStatement (org.eclipse.jdt.core.dom.DoStatement)3 EmptyStatement (org.eclipse.jdt.core.dom.EmptyStatement)3 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 ForStatement (org.eclipse.jdt.core.dom.ForStatement)3 IfStatement (org.eclipse.jdt.core.dom.IfStatement)3 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)3 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)3 Statement (org.eclipse.jdt.core.dom.Statement)3 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)3 SynchronizedStatement (org.eclipse.jdt.core.dom.SynchronizedStatement)3 ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)3 TryStatement (org.eclipse.jdt.core.dom.TryStatement)3 TypeDeclarationStatement (org.eclipse.jdt.core.dom.TypeDeclarationStatement)3 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)3 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)3