Search in sources :

Example 11 with DiGraphNode

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphNode in project closure-compiler by google.

the class DotFormatter method traverseNodes.

private void traverseNodes(Node parent) throws IOException {
    // key
    int keyParent = key(parent);
    // edges
    for (Node child = parent.getFirstChild(); child != null; child = child.getNext()) {
        int keyChild = key(child);
        builder.append(INDENT);
        builder.append(formatNodeName(keyParent));
        builder.append(ARROW);
        builder.append(formatNodeName(keyChild));
        builder.append(" [weight=1];\n");
        traverseNodes(child);
    }
    // Flow Edges
    if (cfg != null && cfg.hasNode(parent)) {
        List<? extends DiGraphEdge<Node, Branch>> outEdges = cfg.getOutEdges(parent);
        String[] edgeList = new String[outEdges.size()];
        for (int i = 0; i < edgeList.length; i++) {
            DiGraphEdge<Node, ControlFlowGraph.Branch> edge = outEdges.get(i);
            DiGraphNode<Node, Branch> succ = edge.getDestination();
            String toNode = null;
            if (succ == cfg.getImplicitReturn()) {
                toNode = "RETURN";
            } else {
                int keySucc = key(succ.getValue());
                toNode = formatNodeName(keySucc);
            }
            edgeList[i] = formatNodeName(keyParent) + ARROW + toNode + " [label=\"" + edge.getValue() + "\", " + "fontcolor=\"red\", " + "weight=0.01, color=\"red\"];\n";
        }
        Arrays.sort(edgeList);
        for (String element : edgeList) {
            builder.append(INDENT);
            builder.append(element);
        }
    }
}
Also used : Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) GraphvizNode(com.google.javascript.jscomp.graph.GraphvizGraph.GraphvizNode) Node(com.google.javascript.rhino.Node)

Example 12 with DiGraphNode

use of com.google.javascript.jscomp.graph.DiGraph.DiGraphNode in project closure-compiler by google.

the class ControlFlowAnalysisTest method assertNodeOrder.

/**
 * Asserts the priority order of CFG nodes.
 *
 * Checks that the node type of the highest-priority node matches the
 * first element of the list, the type of the second node matches the
 * second element of the list, and so on.
 *
 * @param cfg The control flow graph.
 * @param nodeTypes The expected node types, in order.
 */
private void assertNodeOrder(ControlFlowGraph<Node> cfg, List<Token> nodeTypes) {
    List<? extends DiGraphNode<Node, Branch>> cfgNodes = Ordering.from(cfg.getOptionalNodeComparator(true)).sortedCopy(cfg.getNodes());
    // IMPLICIT RETURN must always be last.
    Node implicitReturn = cfgNodes.remove(cfgNodes.size() - 1).getValue();
    assertWithMessage(implicitReturn == null ? "null" : implicitReturn.toStringTree()).that(implicitReturn).isNull();
    assertThat(cfgNodes.stream().map(DiGraphNode::getValue).map(Node::getToken).collect(Collectors.toList())).isEqualTo(nodeTypes);
}
Also used : DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Branch(com.google.javascript.jscomp.ControlFlowGraph.Branch) DiGraphNode(com.google.javascript.jscomp.graph.DiGraph.DiGraphNode) Node(com.google.javascript.rhino.Node)

Aggregations

DiGraphNode (com.google.javascript.jscomp.graph.DiGraph.DiGraphNode)12 Node (com.google.javascript.rhino.Node)12 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)7 DiGraphEdge (com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge)4 TypeEnv (com.google.javascript.jscomp.newtypes.TypeEnv)4 LiveVariableLattice (com.google.javascript.jscomp.LiveVariablesAnalysis.LiveVariableLattice)2 JSType (com.google.javascript.jscomp.newtypes.JSType)2 LinkedHashSet (java.util.LinkedHashSet)2 GraphNode (com.google.javascript.jscomp.graph.GraphNode)1 GraphvizNode (com.google.javascript.jscomp.graph.GraphvizGraph.GraphvizNode)1 BitSet (java.util.BitSet)1