Search in sources :

Example 6 with SingleSuccessorBlock

use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project checker-framework by typetools.

the class DOTCFGVisualizer method generateDotGraph.

/**
 * Generate the dot representation as String.
 */
protected String generateDotGraph(ControlFlowGraph cfg, Block entry, @Nullable Analysis<A, S, T> analysis) {
    this.sbDigraph.setLength(0);
    Set<Block> visited = new HashSet<>();
    // header
    this.sbDigraph.append("digraph {\n");
    Block cur = entry;
    Queue<Block> worklist = new ArrayDeque<>();
    visited.add(entry);
    // traverse control flow graph and define all arrows
    while (true) {
        if (cur == null) {
            break;
        }
        if (cur.getType() == BlockType.CONDITIONAL_BLOCK) {
            ConditionalBlock ccur = ((ConditionalBlock) cur);
            Block thenSuccessor = ccur.getThenSuccessor();
            addDotEdge(ccur.getId(), thenSuccessor.getId(), "then\\n" + ccur.getThenFlowRule());
            if (!visited.contains(thenSuccessor)) {
                visited.add(thenSuccessor);
                worklist.add(thenSuccessor);
            }
            Block elseSuccessor = ccur.getElseSuccessor();
            addDotEdge(ccur.getId(), elseSuccessor.getId(), "else\\n" + ccur.getElseFlowRule());
            if (!visited.contains(elseSuccessor)) {
                visited.add(elseSuccessor);
                worklist.add(elseSuccessor);
            }
        } else {
            assert cur instanceof SingleSuccessorBlock;
            Block b = ((SingleSuccessorBlock) cur).getSuccessor();
            if (b != null) {
                addDotEdge(cur.getId(), b.getId(), ((SingleSuccessorBlock) cur).getFlowRule().name());
                if (!visited.contains(b)) {
                    visited.add(b);
                    worklist.add(b);
                }
            }
        }
        // exceptional edges
        if (cur.getType() == BlockType.EXCEPTION_BLOCK) {
            ExceptionBlock ecur = (ExceptionBlock) cur;
            for (Entry<TypeMirror, Set<Block>> e : ecur.getExceptionalSuccessors().entrySet()) {
                Set<Block> blocks = e.getValue();
                TypeMirror cause = e.getKey();
                String exception = cause.toString();
                if (exception.startsWith("java.lang.")) {
                    exception = exception.replace("java.lang.", "");
                }
                for (Block b : blocks) {
                    addDotEdge(cur.getId(), b.getId(), exception);
                    if (!visited.contains(b)) {
                        visited.add(b);
                        worklist.add(b);
                    }
                }
            }
        }
        cur = worklist.poll();
    }
    generateDotNodes(visited, cfg, analysis);
    // footer
    this.sbDigraph.append("}\n");
    return this.sbDigraph.toString();
}
Also used : ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) HashSet(java.util.HashSet) Set(java.util.Set) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) TypeMirror(javax.lang.model.type.TypeMirror) ExceptionBlock(org.checkerframework.dataflow.cfg.block.ExceptionBlock) SpecialBlock(org.checkerframework.dataflow.cfg.block.SpecialBlock) Block(org.checkerframework.dataflow.cfg.block.Block) RegularBlock(org.checkerframework.dataflow.cfg.block.RegularBlock) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) ArrayDeque(java.util.ArrayDeque) HashSet(java.util.HashSet)

Example 7 with SingleSuccessorBlock

use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project checker-framework by typetools.

the class MustCallInferenceLogic method getNormalSuccessors.

/**
 * Returns the non-exceptional successors of the current block.
 *
 * @param cur the current block
 * @return the successors of this current block
 */
private List<Block> getNormalSuccessors(Block cur) {
    List<Block> successorBlock = new ArrayList<>();
    if (cur.getType() == Block.BlockType.CONDITIONAL_BLOCK) {
        ConditionalBlock ccur = (ConditionalBlock) cur;
        successorBlock.add(ccur.getThenSuccessor());
        successorBlock.add(ccur.getElseSuccessor());
    } else {
        if (!(cur instanceof SingleSuccessorBlock)) {
            throw new BugInCF("BlockImpl is neither a conditional block nor a SingleSuccessorBlock");
        }
        Block b = ((SingleSuccessorBlock) cur).getSuccessor();
        if (b != null) {
            successorBlock.add(b);
        }
    }
    return successorBlock;
}
Also used : ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) ArrayList(java.util.ArrayList) SingleSuccessorBlock(org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock) Block(org.checkerframework.dataflow.cfg.block.Block) ConditionalBlock(org.checkerframework.dataflow.cfg.block.ConditionalBlock) BugInCF(org.checkerframework.javacutil.BugInCF)

Aggregations

Block (org.checkerframework.dataflow.cfg.block.Block)7 ConditionalBlock (org.checkerframework.dataflow.cfg.block.ConditionalBlock)7 SingleSuccessorBlock (org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock)7 ExceptionBlock (org.checkerframework.dataflow.cfg.block.ExceptionBlock)6 SpecialBlock (org.checkerframework.dataflow.cfg.block.SpecialBlock)6 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Set (java.util.Set)3 TypeMirror (javax.lang.model.type.TypeMirror)3 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 RegularBlock (org.checkerframework.dataflow.cfg.block.RegularBlock)2 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 BugInCF (org.checkerframework.javacutil.BugInCF)1