Unverified Commit 4276420b authored by Javier Costa's avatar Javier Costa Committed by GitHub
Browse files

Merge pull request #24 from jacosro/15-ctrldep-complexity

Reduce complexity of control dependency computation
parents b2c98ba9 14b8ae0e
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -87,8 +87,7 @@ public abstract class Graph extends DefaultDirectedGraph<GraphNode<?>, Arc> {

    @Override
    public String toString() {
        return vertexSet().stream()
                .sorted(Comparator.comparingInt(GraphNode::getId))
        return vertexSet().stream().sorted()
                .map(GraphNode::toString)
                .collect(Collectors.joining(System.lineSeparator()));
    }
+5 −0
Original line number Diff line number Diff line
@@ -10,6 +10,11 @@ public class ACFG extends CFG {
        addControlFlowEdge(from, to, new ControlFlowArc.NonExecutable());
    }

    @Override
    protected void setExitNode(GraphNode<?> exitNode) {
        super.setExitNode(exitNode);
    }

    @Override
    protected CFGBuilder newCFGBuilder() {
        return new ACFGBuilder(this);
+2 −1
Original line number Diff line number Diff line
@@ -243,6 +243,7 @@ public class ACFGBuilder extends CFGBuilder {
        methodDeclaration.getBody().get().accept(this, arg);
        returnList.stream().filter(node -> !hangingNodes.contains(node)).forEach(hangingNodes::add);
        nonExecHangingNodes.add(graph.getRootNode().get());
        connectTo(new EmptyStmt(), "Exit");
        GraphNode<EmptyStmt> exitNode = connectTo(new EmptyStmt(), "Exit");
        ((ACFG) graph).setExitNode(exitNode);
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import java.util.Set;
 */
public class CFG extends GraphWithRootNode<MethodDeclaration> {
    private boolean built = false;
    protected GraphNode<?> exitNode;

    public CFG() {
        super();
@@ -62,9 +63,28 @@ public class CFG extends GraphWithRootNode<MethodDeclaration> {
        return res;
    }

    @Override
    public boolean removeVertex(GraphNode<?> graphNode) {
        if (Objects.equals(graphNode, exitNode))
            return false;
        return super.removeVertex(graphNode);
    }

    public GraphNode<?> getExitNode() {
        return exitNode;
    }

    protected void setExitNode(GraphNode<?> exitNode) {
        if (this.exitNode != null)
            throw new IllegalStateException("Exit node already set!");
        this.exitNode = exitNode;
    }

    @Override
    public void build(MethodDeclaration method) {
        method.accept(newCFGBuilder(), null);
        if (exitNode == null)
            throw new IllegalStateException("Exit node missing!");
        built = true;
    }

+2 −1
Original line number Diff line number Diff line
@@ -275,6 +275,7 @@ public class CFGBuilder extends VoidVisitorAdapter<Void> {
        hangingNodes.add(graph.getRootNode().get());
        methodDeclaration.getBody().get().accept(this, arg);
        returnList.stream().filter(node -> !hangingNodes.contains(node)).forEach(hangingNodes::add);
        connectTo(new EmptyStmt(), "Exit");
        GraphNode<EmptyStmt> exitNode = connectTo(new EmptyStmt(), "Exit");
        graph.setExitNode(exitNode);
    }
}
Loading