use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project bazel by bazelbuild.
the class CFGDOTVisualizer method visualize.
/**
* Output a graph description in the DOT language, representing the control
* flow graph starting at <code>entry</code>.
*
* @param entry
* The entry node of the control flow graph to be represented.
* @param analysis
* An analysis containing information about the program
* represented by the CFG. The information includes {@link Store}
* s that are valid at the beginning of basic blocks reachable
* from <code>entry</code> and per-node information for value
* producing {@link Node}s. Can also be <code>null</code> to
* indicate that this information should not be output.
* @param verbose
* Add more output to the CFG description.
* @return String representation of the graph in the DOT language.
*/
public static <A extends AbstractValue<A>, S extends Store<S>, T extends TransferFunction<A, S>> String visualize(ControlFlowGraph cfg, Block entry, /*@Nullable*/
Analysis<A, S, T> analysis, boolean verbose) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
Set<Block> visited = new HashSet<>();
Queue<Block> worklist = new LinkedList<>();
Block cur = entry;
visited.add(entry);
// header
sb1.append("digraph {\n");
sb1.append(" node [shape=rectangle];\n\n");
// 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();
sb2.append(" " + ccur.getId() + " -> " + thenSuccessor.getId());
sb2.append(" [label=\"then\\n" + ccur.getThenFlowRule() + "\"];\n");
if (!visited.contains(thenSuccessor)) {
visited.add(thenSuccessor);
worklist.add(thenSuccessor);
}
Block elseSuccessor = ccur.getElseSuccessor();
sb2.append(" " + ccur.getId() + " -> " + elseSuccessor.getId());
sb2.append(" [label=\"else\\n" + ccur.getElseFlowRule() + "\"];\n");
if (!visited.contains(elseSuccessor)) {
visited.add(elseSuccessor);
worklist.add(elseSuccessor);
}
} else {
assert cur instanceof SingleSuccessorBlock;
Block b = ((SingleSuccessorBlock) cur).getSuccessor();
if (b != null) {
sb2.append(" " + cur.getId() + " -> " + b.getId());
sb2.append(" [label=\"" + ((SingleSuccessorBlock) cur).getFlowRule() + "\"];\n");
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) {
sb2.append(" " + cur.getId() + " -> " + b.getId());
sb2.append(" [label=\"" + exception + "\"];\n");
if (!visited.contains(b)) {
visited.add(b);
worklist.add(b);
}
}
}
}
cur = worklist.poll();
}
IdentityHashMap<Block, List<Integer>> processOrder = getProcessOrder(cfg);
// definition of all nodes including their labels
for (Block v : visited) {
sb1.append(" " + v.getId() + " [");
if (v.getType() == BlockType.CONDITIONAL_BLOCK) {
sb1.append("shape=polygon sides=8 ");
} else if (v.getType() == BlockType.SPECIAL_BLOCK) {
sb1.append("shape=oval ");
}
sb1.append("label=\"");
if (verbose) {
sb1.append("Process order: " + processOrder.get(v).toString().replaceAll("[\\[\\]]", "") + "\\n");
}
sb1.append(visualizeContent(v, analysis, verbose).replace("\\n", "\\l") + " \",];\n");
}
sb1.append("\n");
sb1.append(sb2);
// footer
sb1.append("}\n");
return sb1.toString();
}
use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project checker-framework by typetools.
the class ControlFlowGraph method getSuccessors.
/**
* Get a list of all successor Blocks for cur
*
* @return a Deque of successor Blocks
*/
private Deque<Block> getSuccessors(Block cur) {
Deque<Block> succs = new ArrayDeque<>();
if (cur.getType() == BlockType.CONDITIONAL_BLOCK) {
ConditionalBlock ccur = ((ConditionalBlock) cur);
succs.add(ccur.getThenSuccessor());
succs.add(ccur.getElseSuccessor());
} else {
assert cur instanceof SingleSuccessorBlock;
Block b = ((SingleSuccessorBlock) cur).getSuccessor();
if (b != null) {
succs.add(b);
}
}
if (cur.getType() == BlockType.EXCEPTION_BLOCK) {
ExceptionBlock ecur = (ExceptionBlock) cur;
for (Set<Block> exceptionSuccSet : ecur.getExceptionalSuccessors().values()) {
succs.addAll(exceptionSuccSet);
}
}
return succs;
}
use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project checker-framework by typetools.
the class AbstractCFGVisualizer method handleSuccessorsHelper.
/**
* Outputs, to sbGraph, a visualization of a block's edges, but not the block itself. (The block
* itself is output elsewhere.) Also adds the successors of the block to the work list and the
* visited blocks list.
*
* @param cur the current block
* @param visited the set of blocks that have already been visited or are in the work list; side
* effected by this method
* @param workList the queue of blocks to be processed; side effected by this method
* @param sbGraph the {@link StringBuilder} to store the graph; side effected by this method
*/
protected void handleSuccessorsHelper(Block cur, Set<Block> visited, Queue<Block> workList, StringBuilder sbGraph) {
if (cur.getType() == Block.BlockType.CONDITIONAL_BLOCK) {
ConditionalBlock ccur = ((ConditionalBlock) cur);
Block thenSuccessor = ccur.getThenSuccessor();
sbGraph.append(visualizeEdge(ccur.getUid(), thenSuccessor.getUid(), ccur.getThenFlowRule().toString()));
sbGraph.append(lineSeparator);
addBlock(thenSuccessor, visited, workList);
Block elseSuccessor = ccur.getElseSuccessor();
sbGraph.append(visualizeEdge(ccur.getUid(), elseSuccessor.getUid(), ccur.getElseFlowRule().toString()));
sbGraph.append(lineSeparator);
addBlock(elseSuccessor, visited, workList);
} else {
SingleSuccessorBlock sscur = (SingleSuccessorBlock) cur;
Block succ = sscur.getSuccessor();
if (succ != null) {
sbGraph.append(visualizeEdge(cur.getUid(), succ.getUid(), sscur.getFlowRule().name()));
sbGraph.append(lineSeparator);
addBlock(succ, visited, workList);
}
}
if (cur.getType() == Block.BlockType.EXCEPTION_BLOCK) {
ExceptionBlock ecur = (ExceptionBlock) cur;
for (Map.Entry<TypeMirror, Set<Block>> e : ecur.getExceptionalSuccessors().entrySet()) {
TypeMirror cause = e.getKey();
String exception = cause.toString();
if (exception.startsWith("java.lang.")) {
exception = exception.replace("java.lang.", "");
}
for (Block b : e.getValue()) {
sbGraph.append(visualizeEdge(cur.getUid(), b.getUid(), exception));
sbGraph.append(lineSeparator);
addBlock(b, visited, workList);
}
}
}
}
use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project bazel by bazelbuild.
the class ControlFlowGraph method getSuccessors.
/**
* Get a list of all successor Blocks for cur
* @param cur
* @return A Deque of successor Blocks
*/
private Deque<Block> getSuccessors(Block cur) {
Deque<Block> succs = new LinkedList<>();
if (cur.getType() == BlockType.CONDITIONAL_BLOCK) {
ConditionalBlock ccur = ((ConditionalBlock) cur);
succs.add(ccur.getThenSuccessor());
succs.add(ccur.getElseSuccessor());
} else {
assert cur instanceof SingleSuccessorBlock;
Block b = ((SingleSuccessorBlock) cur).getSuccessor();
if (b != null) {
succs.add(b);
}
}
if (cur.getType() == BlockType.EXCEPTION_BLOCK) {
ExceptionBlock ecur = (ExceptionBlock) cur;
for (Set<Block> exceptionSuccSet : ecur.getExceptionalSuccessors().values()) {
succs.addAll(exceptionSuccSet);
}
}
return succs;
}
use of org.checkerframework.dataflow.cfg.block.SingleSuccessorBlock in project bazel by bazelbuild.
the class ControlFlowGraph method getAllBlocks.
/**
* @return The set of all basic block in this control flow graph.
*/
public Set<Block> getAllBlocks() {
Set<Block> visited = new HashSet<>();
Queue<Block> worklist = new LinkedList<>();
Block cur = entryBlock;
visited.add(entryBlock);
// traverse the whole control flow graph
while (true) {
if (cur == null)
break;
Queue<Block> succs = new LinkedList<>();
if (cur.getType() == BlockType.CONDITIONAL_BLOCK) {
ConditionalBlock ccur = ((ConditionalBlock) cur);
succs.add(ccur.getThenSuccessor());
succs.add(ccur.getElseSuccessor());
} else {
assert cur instanceof SingleSuccessorBlock;
Block b = ((SingleSuccessorBlock) cur).getSuccessor();
if (b != null) {
succs.add(b);
}
}
if (cur.getType() == BlockType.EXCEPTION_BLOCK) {
ExceptionBlock ecur = (ExceptionBlock) cur;
for (Set<Block> exceptionSuccSet : ecur.getExceptionalSuccessors().values()) {
succs.addAll(exceptionSuccSet);
}
}
for (Block b : succs) {
if (!visited.contains(b)) {
visited.add(b);
worklist.add(b);
}
}
cur = worklist.poll();
}
return visited;
}
Aggregations