Commit 6bb53029 authored by Javier Costa's avatar Javier Costa
Browse files

build root node on demand

parent 6ed533e7
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
package tfm.graphs;

import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.EmptyStmt;
import org.jgrapht.io.DOTExporter;
import tfm.arcs.Arc;
@@ -11,17 +12,12 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class CFGGraph extends GraphWithRootNode {
public class CFGGraph extends GraphWithRootNode<MethodDeclaration> {

    public CFGGraph() {
        super();
    }

    @Override
    protected GraphNode<?> buildRootNode() {
        return new GraphNode<>(getNextVertexId(), "Start", new EmptyStmt());
    }

    public void addControlFlowEdge(GraphNode<?> from, GraphNode<?> to) {
        super.addEdge(from, to, new ControlFlowArc());
    }
+8 −1
Original line number Diff line number Diff line
@@ -16,10 +16,17 @@ import java.util.stream.Collectors;
 * */
public abstract class Graph extends DefaultDirectedGraph<GraphNode<?>, Arc> {

    private int nextVertexId = 0;
    protected static final int DEFAULT_VERTEX_START_ID = 0;

    private int nextVertexId;

    public Graph() {
        this(DEFAULT_VERTEX_START_ID);
    }

    protected Graph(int vertexStartId) {
        super(null, null, false);
        this.nextVertexId = vertexStartId;
    }

    private <ASTNode extends Node> GraphNode<ASTNode> addNode(GraphNode<ASTNode> node) {
+29 −9
Original line number Diff line number Diff line
package tfm.graphs;

import com.github.javaparser.ast.Node;
import tfm.nodes.GraphNode;
import tfm.utils.NodeFactory;

import java.util.Objects;
import java.util.Optional;

public abstract class GraphWithRootNode extends Graph {
public abstract class GraphWithRootNode<ASTRootNode extends Node> extends Graph {

    protected GraphNode<?> rootNode;
    protected final int ROOT_NODE_ID = 0;

    protected GraphNode<ASTRootNode> rootNode;

    public GraphWithRootNode() {
        super();
        super(1);
    }

        this.rootNode = buildRootNode();
        this.addVertex(rootNode);
    /**
     * Builds the root node with the given instruction and AST node.
     * If the root node already exists, just returns false
     *
     * @param instruction the instruction string
     * @param rootNodeAst the AST node
     * @return true if the root node is created, false otherwise
     */
    public boolean buildRootNode(String instruction, ASTRootNode rootNodeAst) {
        if (rootNode != null) {
            return false;
        }

    protected abstract GraphNode<?> buildRootNode();
        GraphNode<ASTRootNode> root = NodeFactory.graphNode(ROOT_NODE_ID, instruction, rootNodeAst);
        this.rootNode = root;
        this.addVertex(root);

        return true;
    }

    public GraphNode<?> getRootNode() {
        return rootNode;
    public Optional<GraphNode<?>> getRootNode() {
        return Optional.ofNullable(rootNode);
    }

    @Override
+1 −6
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ import tfm.visitors.pdg.PDGBuilder;

import java.util.*;

public class PDGGraph extends GraphWithRootNode implements Sliceable<PDGGraph> {
public class PDGGraph extends GraphWithRootNode<MethodDeclaration> implements Sliceable<PDGGraph> {

    private CFGGraph cfgGraph;

@@ -23,11 +23,6 @@ public class PDGGraph extends GraphWithRootNode implements Sliceable<PDGGraph> {
        super();
    }

    @Override
    protected GraphNode<?> buildRootNode() {
        return new GraphNode<>(getNextVertexId(), "ENTER", new MethodDeclaration());
    }

    public PDGGraph(CFGGraph cfgGraph) {
        super();
        this.cfgGraph = cfgGraph;
+7 −5
Original line number Diff line number Diff line
@@ -20,11 +20,7 @@ public class CFGBuilder extends VoidVisitorAdapter<Void> {

    public CFGBuilder(CFGGraph graph) {
        this.graph = graph;
        this.lastParentNodes = Collections.asLifoQueue(
                new ArrayDeque<>(
                        Collections.singletonList(graph.getRootNode())
                )
        );
        this.lastParentNodes = Collections.asLifoQueue(new ArrayDeque<>());

        this.bodyBreaks = new ArrayList<>();
    }
@@ -225,6 +221,12 @@ public class CFGBuilder extends VoidVisitorAdapter<Void> {
            throw new IllegalStateException("CFG is only allowed for one method, not multiple!");
        }

        this.graph.buildRootNode("Start", methodDeclaration);

        assert this.graph.getRootNode().isPresent();

        lastParentNodes.add(this.graph.getRootNode().get());

        super.visit(methodDeclaration, arg);

        lastParentNodes.add(addNodeAndArcs("Stop", new EmptyStmt()));
Loading