Commit 0e6acf90 authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Tests comparing the APDG and the PPDG.

Each method is graphed, and then all possible slices are compared.
parent a815ec9f
Loading
Loading
Loading
Loading

.gitmodules

0 → 100644
+3 −0
Original line number Diff line number Diff line
[submodule "src/test/res/java-slicing-benchmarks"]
	path = src/test/res/java-slicing-benchmarks
	url = kaz:repos/java-slicing-benchmarks.git
+6 −0
Original line number Diff line number Diff line
@@ -47,5 +47,11 @@
            <version>1.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
 No newline at end of file
+11 −1
Original line number Diff line number Diff line
@@ -73,11 +73,21 @@ public class PDGLog extends GraphLog<PDG> {

    @Override
    public void generateImages(String imageName, Format format) throws IOException {
        super.generateImages(imageName + "-pdg", format);
        super.generateImages(imageName + "-" + getExtra(), format);
        if (cfgLog != null)
            cfgLog.generateImages(imageName + "-cfg", format);
    }

    private String getExtra() {
        if (graph instanceof PPDG)
            return "ppdg";
        else if (graph instanceof APDG)
            return "apdg";
        else if (graph instanceof PDG)
            return "pdg";
        throw new RuntimeException("invalid or null graph type");
    }

    @Override
    public void openVisualRepresentation() throws IOException {
        super.openVisualRepresentation();
+39 −0
Original line number Diff line number Diff line
package tfm;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.body.MethodDeclaration;
import org.junit.jupiter.params.provider.Arguments;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class FileFinder {
    public static Collection<Arguments> findFiles(File directory, String suffix) throws FileNotFoundException {
        Collection<Arguments> res = new ArrayList<>();
        File[] files = directory.listFiles();
        if (files == null) return Collections.emptyList();
        for (File f : files) {
            if (f.getName().endsWith(suffix))
                for (MethodDeclaration m : methodsOf(f))
                    res.add(Arguments.of(f, m.getNameAsString(), m));
            if (f.isDirectory())
                res.addAll(findFiles(f, suffix));
        }
        return res;
    }

    public static Arguments[] findAllMethodDeclarations() throws FileNotFoundException {
        Collection<Arguments> args = findFiles(new File("./src/test/res/"), ".java");
        args.add(Arguments.of(new File("./src/test/res/invalid/problem1"), "problem1", HandCraftedGraphs.problem1WithGotos()));
        args.add(Arguments.of(new File("./src/test/res/invalid/problem1continue"), "problem1", HandCraftedGraphs.problem1ContinueWithGotos()));
        return args.toArray(new Arguments[0]);
    }

    private static List<MethodDeclaration> methodsOf(File file) throws FileNotFoundException {
        return JavaParser.parse(file).findAll(MethodDeclaration.class);
    }
}
+93 −0
Original line number Diff line number Diff line
package tfm;

import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.ContinueStmt;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.WhileStmt;
import tfm.graphs.CFG.ACFG;
import tfm.graphs.PDG.APDG;
import tfm.graphs.PDG.PPDG;
import tfm.nodes.GraphNode;
import tfm.visitors.pdg.ControlDependencyBuilder;

public class HandCraftedGraphs {
    public static APDG problem1WithGotos() {
        // Generate the control flow of a graph
        ACFG cfg = new ACFG();
        GraphNode<?> wx = cfg.addNode("while (X)", new WhileStmt());
        GraphNode<?> ify = cfg.addNode("L: if (Y)", new IfStmt());
        GraphNode<?> ifz = cfg.addNode("if (Z)", new IfStmt());
        GraphNode<?> a = cfg.addNode("A();", new MethodCallExpr("A"));
        GraphNode<?> b = cfg.addNode("B();", new MethodCallExpr("B"));
        GraphNode<?> c = cfg.addNode("C();", new MethodCallExpr("C"));
        GraphNode<?> d = cfg.addNode("D();", new MethodCallExpr("D"));
        GraphNode<?> g1 = cfg.addNode("goto L;", new ContinueStmt("L"));
        GraphNode<?> g2 = cfg.addNode("goto L;", new ContinueStmt("L"));

        GraphNode<?> end = cfg.addNode("Exit", new EmptyStmt());

        cfg.addControlFlowEdge(cfg.getRootNode(), wx);
        cfg.addControlFlowEdge(wx, ify);
        cfg.addControlFlowEdge(wx, d);
        cfg.addControlFlowEdge(ify, ifz);
        cfg.addControlFlowEdge(ify, c);
        cfg.addControlFlowEdge(ifz, a);
        cfg.addControlFlowEdge(ifz, b);
        cfg.addControlFlowEdge(a, g1);
        cfg.addControlFlowEdge(b, g2);
        cfg.addControlFlowEdge(c, wx);
        cfg.addControlFlowEdge(d, end);
        cfg.addNonExecutableControlFlowEdge(g1, b);
        cfg.addControlFlowEdge(g1, ify);
        cfg.addNonExecutableControlFlowEdge(g2, c);
        cfg.addControlFlowEdge(g2, ify);
        cfg.addNonExecutableControlFlowEdge(cfg.getRootNode(), end);

        PPDG pdg = new PPDG(cfg);
        ControlDependencyBuilder gen = new ControlDependencyBuilder(pdg, cfg);
        gen.analyze();
        return pdg;
    }

    public static APDG problem1ContinueWithGotos() {
        // Generate the control flow of a graph
        ACFG cfg = new ACFG();
        GraphNode<?> wx = cfg.addNode("while (X)", new WhileStmt());
        GraphNode<?> ify = cfg.addNode("L: if (Y)", new IfStmt());
        GraphNode<?> ifz = cfg.addNode("if (Z)", new IfStmt());
        GraphNode<?> a = cfg.addNode("A();", new MethodCallExpr("A"));
        GraphNode<?> b = cfg.addNode("B();", new MethodCallExpr("B"));
        GraphNode<?> c = cfg.addNode("C();", new MethodCallExpr("C"));
        GraphNode<?> d = cfg.addNode("D();", new MethodCallExpr("D"));
        GraphNode<?> g1 = cfg.addNode("goto L1;", new ContinueStmt("L"));
        GraphNode<?> g2 = cfg.addNode("goto L2;", new ContinueStmt("L"));
        GraphNode<?> g3 = cfg.addNode("goto L3;", new ContinueStmt("L"));

        GraphNode<?> end = cfg.addNode("Exit", new EmptyStmt());

        cfg.addControlFlowEdge(cfg.getRootNode(), wx);
        cfg.addControlFlowEdge(wx, ify);
        cfg.addControlFlowEdge(wx, d);
        cfg.addControlFlowEdge(ify, ifz);
        cfg.addControlFlowEdge(ify, c);
        cfg.addControlFlowEdge(ifz, a);
        cfg.addControlFlowEdge(ifz, b);
        cfg.addControlFlowEdge(a, g1);
        cfg.addControlFlowEdge(b, g3);
        cfg.addControlFlowEdge(c, wx);
        cfg.addControlFlowEdge(d, end);
        cfg.addNonExecutableControlFlowEdge(g1, b);
        cfg.addControlFlowEdge(g1, ify);
        cfg.addNonExecutableControlFlowEdge(g2, c);
        cfg.addControlFlowEdge(g2, ify);
        cfg.addNonExecutableControlFlowEdge(g3, g2);
        cfg.addControlFlowEdge(g3, ify);
        cfg.addNonExecutableControlFlowEdge(cfg.getRootNode(), end);

        PPDG pdg = new PPDG(cfg);
        ControlDependencyBuilder gen = new ControlDependencyBuilder(pdg, cfg);
        gen.analyze();
        return pdg;
    }
}
Loading