Commit 4797821c authored by Javier Costa's avatar Javier Costa
Browse files

SwitchStmt to CFG

parent a42ee2e3
Loading
Loading
Loading
Loading
+7 −20
Original line number Diff line number Diff line
@@ -18,20 +18,17 @@ import tfm.visitors.PDGVisitor;
import java.io.*;
import java.util.Arrays;

import static guru.nidi.graphviz.model.Factory.graph;
import static guru.nidi.graphviz.model.Factory.node;

public class Main {

    private static long t0;

    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("/home/jacosro/IdeaProjects/TFM/src/main/java/tfm/programs/Example1.java");
        File file = new File("/home/jacosro/IdeaProjects/TFM/src/main/java/tfm/programs/cfg/Eval_1.java");
        CompilationUnit compilationUnit = JavaParser.parse(file);

        t0 = System.nanoTime();

        Graph<?> graph = pdg(file, compilationUnit);
        Graph<?> graph = cfg(file, compilationUnit);

        long tt = System.nanoTime();

@@ -82,21 +79,11 @@ public class Main {
        return pdgGraph;
    }

    private static void openGraphAsPng(Graph graph) throws IOException, InterruptedException {
        PrintWriter printWriter = new PrintWriter("./out/graph.txt");
        printWriter.println(graph.toGraphvizRepresentation());
        printWriter.close();

        int exit = new ProcessBuilder(
                Arrays.asList("dot", "-Tpng", "./out/graph.txt", "-o", "./out/graph.png")
        ).start()
        .waitFor();

        if (exit != 0) {
            Logger.log("Error procesando el archivo de grafo");
            return;
        }
    private static void openGraphAsPng(Graph graph) throws IOException {
        Graphviz.fromString(graph.toGraphvizRepresentation())
                .render(Format.PNG)
                .toFile(new File("./out/graph.png"));

        new ProcessBuilder(Arrays.asList("xdg-open", "./output/graph.png")).start();
        new ProcessBuilder(Arrays.asList("xdg-open", "./out/graph.png")).start();
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package tfm.graphs;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.Statement;
import edg.graphlib.Arrow;
import edg.graphlib.Vertex;
import tfm.arcs.Arc;
import tfm.arcs.cfg.ControlFlowArc;
import tfm.nodes.CFGNode;
@@ -37,6 +38,11 @@ public abstract class CFGGraph extends Graph<CFGNode> {
    public String toGraphvizRepresentation() {
        String lineSep = System.lineSeparator();

        String nodes = getNodes().stream()
                .sorted(Comparator.comparingInt(Node::getId))
                .map(node -> String.format("%s [label=\"%s: %s\"]", node.getId(), node.getId(), node.getData()))
                .collect(Collectors.joining(lineSep));

        String arrows =
                getArrows().stream()
                        .sorted(Comparator.comparingInt(arrow -> ((Node) arrow.getFrom()).getId()))
@@ -44,6 +50,7 @@ public abstract class CFGGraph extends Graph<CFGNode> {
                        .collect(Collectors.joining(lineSep));

        return "digraph g{" + lineSep +
                nodes + lineSep +
                arrows + lineSep +
                "}";
    }
+7 −0
Original line number Diff line number Diff line
@@ -85,6 +85,12 @@ public abstract class Graph<NodeType extends Node> extends edg.graphlib.Graph<St

    public abstract NodeType addNode(String instruction, Statement statement);

    public Optional<NodeType> findNodeByStatement(Statement statement) {
        return getNodes().stream()
                .filter(node -> Objects.equals(node.getStatement(), statement))
                .findFirst();
    }

    @SuppressWarnings("unchecked")
    public Set<NodeType> getNodes() {
        return getVerticies().stream()
@@ -100,6 +106,7 @@ public abstract class Graph<NodeType extends Node> extends edg.graphlib.Graph<St

    public String toString() {
        return getNodes().stream()
                .sorted(Comparator.comparingInt(Node::getId))
                .map(Node::toString)
                .collect(Collectors.joining(System.lineSeparator()));
    }
+65 −0
Original line number Diff line number Diff line
package tfm.programs.cfg;

import tfm.utils.Logger;

public class Eval_1 {

	public static void main(String[] args)
	{	
		int x=1;
		while (x<=10)
		{
			x++;
			while (x<=10)
			{
				if (x>0)
				{
				}
				else
					x++;
			}
		}
		do {
			x--;

			if (x == 1) {
				x = 2;
			} else {
				x = 4;
			}
		} while (x < 1);

		for (int z = 1; z < x; z++, x--) {
			int k = z + x;
			System.out.println(k);
		}

		int[] numbers = { 1, 2, 3, 4, 5 };

		for (int number : numbers) {
			System.out.println(number);
		}

		switch (x) {
			case 1:
				int z = 3;
				x = 3;
				break;
			case 2:
				while (x < 9) {
					x++;
					break;
				}
				x = 4;
				break;
			case 3:
				x = 5;
				break;
			default:
				x = 5;
				break;
		}

		Logger.log(x);
	}
}
+22 −0
Original line number Diff line number Diff line
package tfm.programs.cfg;

public class Eval_2 {

	public static void main(String[] args)
	{		
		int x=1;
		if (x<=1)
		{
			if (x<=2)
			{
				if (x<=3)
				{
					x++;
				}
				else x--;
			}
			else x--;
		}
		System.out.println(x);	
	}
}
Loading