Commit d44b0f48 authored by Javier Costa's avatar Javier Costa
Browse files

PDG works Expression, If, While

parent 2ec3631a
Loading
Loading
Loading
Loading

src/main/java/tfm/Main.java

deleted100644 → 0
+0 −109
Original line number Diff line number Diff line
package tfm;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.visitor.VoidVisitor;
import edg.graphlib.Vertex;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import tfm.graphs.CFGGraph;
import tfm.graphs.Graph;
import tfm.graphs.PDGGraph;
import tfm.nodes.Node;
import tfm.nodes.PDGNode;
import tfm.scopes.ScopeHolder;
import tfm.utils.Logger;
import tfm.visitors.CFGVisitor;
import tfm.visitors.PDGCFGVisitor;
import tfm.visitors.PDGVisitor;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

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/pdg/Example1.java");
        CompilationUnit compilationUnit = JavaParser.parse(file);

        t0 = System.nanoTime();

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

        long tt = System.nanoTime();

        Logger.log(
                "****************************\n" +
                "*           GRAPH          *\n" +
                "****************************"
        );
        Logger.log(graph);
        Logger.log(
                "****************************\n" +
                "*         GRAPHVIZ         *\n" +
                "****************************"
        );
        Logger.log(graph.toGraphvizRepresentation());
        Logger.log();
        Logger.format("Done in %.2f ms", (tt - t0) / 10e6);


        Logger.log("Nodes with variable info");
        Logger.log(
                graph.getNodes().stream()
                    .sorted(Comparator.comparingInt(Node::getId))
                    .map(node ->
                            String.format("Node { id: %s, declared: %s, defined: %s, used: %s }",
                                node.getId(),
                                node.getDeclaredVariables(),
                                node.getDefinedVariables(),
                                node.getUsedVariables())
                    ).collect(Collectors.joining(System.lineSeparator()))
        );

        openGraphAsPng(graph);
    }

    public static CFGGraph cfg(File file, CompilationUnit cu) {
        CFGGraph cfgGraph = new CFGGraph() {
            @Override
            protected String getRootNodeData() {
                return "Start";
            }
        };

        cu.accept(new CFGVisitor(cfgGraph), null);

        return cfgGraph;
    }

    public static PDGGraph pdg(File file, CompilationUnit cu) {
        PDGGraph pdgGraph = new PDGGraph() {
            @Override
            protected String getRootNodeData() {
                return "Entry";
            }
        };

//        ScopeHolder<PDGNode> scopeHolder = new ScopeHolder<>(pdgGraph.getRootNode());
//        PDGVisitor visitor = new PDGVisitor(pdgGraph, scopeHolder);

        PDGCFGVisitor visitor = new PDGCFGVisitor(pdgGraph);

        cu.accept(visitor, pdgGraph.getRootNode());

        return pdgGraph;
    }

    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", "./out/graph.png")).start();
    }
}
+42 −0
Original line number Diff line number Diff line
package tfm.exec;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import tfm.graphs.CFGGraph;
import tfm.graphs.Graph;
import tfm.visitors.CFGVisitor;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class CFGLog extends GraphLog<CFGGraph, CFGVisitor> {

    @Override
    public void visit(Node node) {
        this.graph = new CFGGraph() {
            @Override
            protected String getRootNodeData() {
                return "Start";
            }
        };

        this.visitor = new CFGVisitor(graph);

        node.accept(visitor, null);
    }

    @Override
    void generatePNGs() throws IOException {
        Graphviz.fromString(graph.toGraphvizRepresentation())
                .render(Format.PNG)
                .toFile(new File("./out/cfg.png"));
    }

    @Override
    public void openVisualRepresentation() throws IOException {
        new ProcessBuilder(Arrays.asList("xdg-open", "./out/cfg.png")).start();
    }
}
+47 −0
Original line number Diff line number Diff line
package tfm.exec;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import tfm.graphs.Graph;
import tfm.utils.Logger;

import java.io.IOException;
import java.util.Comparator;
import java.util.stream.Collectors;

public abstract class GraphLog<G extends Graph<?>, V extends VoidVisitor<?>> {

    static final String CFG = "cfg";
    static final String PDG = "pdg";
    static final String SDG = "sdg";

    G graph;
    V visitor;

    abstract void visit(Node node);


    void log() throws IOException {
        Logger.log(
                "****************************\n" +
                "*           GRAPH          *\n" +
                "****************************"
        );
        Logger.log(graph);
        Logger.log(
                "****************************\n" +
                "*         GRAPHVIZ         *\n" +
                "****************************"
        );
        Logger.log(graph.toGraphvizRepresentation());
        Logger.log();

        generatePNGs();
    }

    abstract void generatePNGs() throws IOException;

    abstract void openVisualRepresentation() throws IOException;
}
+77 −0
Original line number Diff line number Diff line
package tfm.exec;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration;
import tfm.utils.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

public class Main {

    public static final String PROGRAM = "src/main/java/tfm/programs/pdg/Example1.java";
    public static final String METHOD = "";
    public static final String GRAPH = GraphLog.PDG;

    public static void main(String[] args) throws IOException {
        JavaParser.getStaticConfiguration().setAttributeComments(false);

        // File
        File file = new File(PROGRAM);
        Node root = JavaParser.parse(file);

        // GraphLog
        GraphLog<?, ?> graphLog = getGraphLog(args.length == 1 ? args[0] : GRAPH);

        if (!METHOD.isEmpty()) {
            Optional<MethodDeclaration> methodDeclarationOptional = root.findFirst(MethodDeclaration.class,
                    methodDeclaration -> Objects.equals(methodDeclaration.getNameAsString(), METHOD));

            if (!methodDeclarationOptional.isPresent()) {
                Logger.format("Method '%s' not found in '%s'. Exiting...", METHOD, PROGRAM);
                return;
            }

            root = methodDeclarationOptional.get();
        }

        // Generate Graph and measure time
        long t0 = System.nanoTime();
        graphLog.visit(root);
        long tf = System.nanoTime();

        long tt = tf - t0;

        graphLog.log();

        graphLog.generatePNGs();

        graphLog.openVisualRepresentation();

        Logger.log();
        Logger.format("Graph generated in %.2f ms", tt / 10e6);
    }

    private static GraphLog<?, ?> getGraphLog(String graph) throws IOException {
        GraphLog<?, ?> graphLog = null;

        switch (graph) {
            case GraphLog.CFG:
                graphLog = new CFGLog();
                break;
            case GraphLog.PDG:
//                main(new String[]{GraphLog.CFG});
                graphLog = new PDGLog();
                break;
            default:
                Logger.log("Unkown graph type");
                System.exit(1);
        }

        return graphLog;
    }
}
+67 −0
Original line number Diff line number Diff line
package tfm.exec;

import com.github.javaparser.ast.CompilationUnit;
import guru.nidi.graphviz.engine.Format;
import guru.nidi.graphviz.engine.Graphviz;
import tfm.graphs.PDGGraph;
import tfm.nodes.Node;
import tfm.utils.Logger;
import tfm.visitors.PDGCFGVisitor;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

public class PDGLog extends GraphLog<PDGGraph, PDGCFGVisitor> {

    @Override
    void visit(com.github.javaparser.ast.Node node) {
        this.graph = new PDGGraph() {
            @Override
            protected String getRootNodeData() {
                return "Entry";
            }
        };

        this.visitor = new PDGCFGVisitor(graph);

        node.accept(this.visitor, this.graph.getRootNode());
    }

    @Override
    void log() throws IOException {
        super.log();

        Logger.log("Nodes with variable info");
        Logger.log(
                graph.getNodes().stream()
                        .sorted(Comparator.comparingInt(Node::getId))
                        .map(node ->
                                String.format("Node { id: %s, declared: %s, defined: %s, used: %s }",
                                        node.getId(),
                                        node.getDeclaredVariables(),
                                        node.getDefinedVariables(),
                                        node.getUsedVariables())
                        ).collect(Collectors.joining(System.lineSeparator()))
        );
    }

    @Override
    void generatePNGs() throws IOException {
        Graphviz.fromString(visitor.getCfgGraph().toGraphvizRepresentation())
                .render(Format.PNG)
                .toFile(new File("./out/pdg-cfg.png"));

        Graphviz.fromString(graph.toGraphvizRepresentation())
                .render(Format.PNG)
                .toFile(new File("./out/pdg.png"));
    }

    @Override
    void openVisualRepresentation() throws IOException {
        new ProcessBuilder(Arrays.asList("xdg-open", "./out/pdg-cfg.png")).start();
        new ProcessBuilder(Arrays.asList("xdg-open", "./out/pdg.png")).start();
    }
}
Loading