Commit 0b9d81b8 authored by Javier Costa's avatar Javier Costa
Browse files

Fix for statement

parent f88f878d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
package tfm.programs.pdg;

import tfm.utils.Logger;

import java.util.Arrays;

public class Example2 {
@@ -22,5 +24,15 @@ public class Example2 {
        for(int i : integers) {
            System.out.println(x + i);
        }

        int a = 8;

        int b = 2;

        for (int i = 0; a < 100; a++, i++) {
            b++;
        }

        Logger.log(b);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ public class Slice {
        Logger.log("= Starting slice =");
        Logger.log("==================");

        PDGGraph sliced = pdgGraph.slice(new LineNumberCriterion(18, "x"));
        PDGGraph sliced = pdgGraph.slice(new LineNumberCriterion(16, "x"));

        PDGLog pdgLog = new PDGLog(sliced);
        pdgLog.log();
+12 −11
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import jdk.nashorn.internal.ir.Block;
import tfm.graphs.CFGGraph;
import tfm.nodes.CFGNode;
import tfm.utils.Utils;
@@ -122,28 +123,28 @@ public class CFGVisitor extends VoidVisitorAdapter<Void> {

    @Override
    public void visit(ForStmt forStmt, Void arg) {
        String inizialization = forStmt.getInitialization().stream()
                .map(Node::toString)
                .collect(Collectors.joining(","));

        String update = forStmt.getUpdate().stream()
                .map(Node::toString)
                .collect(Collectors.joining(","));
//        String inizialization = forStmt.getInitialization().stream()
//                .map(Node::toString)
//                .collect(Collectors.joining(","));
//
//        String update = forStmt.getUpdate().stream()
//                .map(Node::toString)
//                .collect(Collectors.joining(","));

        Expression comparison = forStmt.getCompare().orElse(new BooleanLiteralExpr(true));
//
//        forStmt.getInitialization().forEach(expression -> new ExpressionStmt(expression).accept(this, null));
        forStmt.getInitialization().forEach(expression -> new ExpressionStmt(expression).accept(this, null));

        CFGNode forNode = addNodeAndArcs(
                String.format("for (%s;%s;%s)", inizialization, comparison, update),
                String.format("for (;%s;)", comparison),
                forStmt
        );

        lastParentNodes.add(forNode);

        BlockStmt body = Utils.blockWrapper(forStmt.getBody());
        BlockStmt body = Utils.blockWrapper(forStmt.getBody()).clone();

//        forStmt.getUpdate().forEach(body::addStatement);
        forStmt.getUpdate().forEach(body::addStatement);

        body.accept(this, arg);

+23 −2
Original line number Diff line number Diff line
@@ -5,8 +5,11 @@ import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import tfm.graphs.CFGGraph;
import tfm.graphs.PDGGraph;
import tfm.nodes.CFGNode;
import tfm.nodes.Node;
import tfm.nodes.PDGNode;

import java.util.stream.Collectors;

public class ControlDependencyVisitor extends VoidVisitorAdapter<PDGNode> {

    private CFGGraph cfgGraph;
@@ -40,9 +43,27 @@ public class ControlDependencyVisitor extends VoidVisitorAdapter<PDGNode> {

    @Override
    public void visit(ForStmt forStmt, PDGNode parent) {
        PDGNode node = addNodeAndControlDependency(forStmt, parent);
        String initialization = forStmt.getInitialization().stream()
                .map(com.github.javaparser.ast.Node::toString)
                .collect(Collectors.joining(","));

        String update = forStmt.getUpdate().stream()
                .map(com.github.javaparser.ast.Node::toString)
                .collect(Collectors.joining(","));

        String compare = forStmt.getCompare()
                .map(com.github.javaparser.ast.Node::toString)
                .orElse("true");


        PDGNode forNode = pdgGraph.addNode(
                String.format("for (%s;%s;%s)", initialization, compare, update),
                forStmt
        );

        pdgGraph.addControlDependencyArc(parent, forNode);

        forStmt.getBody().accept(this, node);
        forStmt.getBody().accept(this, forNode);
    }

    @Override
+30 −8
Original line number Diff line number Diff line
package tfm.visitors;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import edg.graphlib.Arrow;
import tfm.arcs.cfg.ControlFlowArc;
import tfm.graphs.CFGGraph;
import tfm.graphs.PDGGraph;
import tfm.nodes.CFGNode;
@@ -11,8 +10,6 @@ import tfm.nodes.PDGNode;
import tfm.utils.Utils;
import tfm.variables.VariableExtractor;

import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

@@ -49,13 +46,19 @@ public class DataDependencyVisitor extends VoidVisitorAdapter<Void> {

    @Override
    public void visit(ForStmt forStmt, Void ignored) {
        buildDataDependency(forStmt);
        PDGNode forNode = pdgGraph.findNodeByASTNode(forStmt).get();

        forStmt.getInitialization().accept(this, null);
        forStmt.getInitialization().stream()
                .map(ExpressionStmt::new)
                .forEach(expressionStmt -> buildDataDependency(forNode, expressionStmt));

        forStmt.getBody().accept(this, null);
        buildDataDependency(forStmt); // Only for comparison

        forStmt.getUpdate().stream()
                .map(ExpressionStmt::new)
                .forEach(expressionStmt -> buildDataDependency(forNode, expressionStmt));

        forStmt.getUpdate().accept(this, null);
        forStmt.getBody().accept(this, null);
    }

    @Override
@@ -93,4 +96,23 @@ public class DataDependencyVisitor extends VoidVisitorAdapter<Void> {
                .setOnVariableDeclarationListener(node::addDeclaredVariable)
                .visit(node.getAstNode());
    }

    // For statement special case
    private void buildDataDependency(PDGNode forNode, Statement statement) {
        new VariableExtractor()
                .setOnVariableUseListener(variable -> {
                    forNode.addUsedVariable(variable);

                    Optional<CFGNode> nodeOptional = cfgGraph.findNodeByASTNode(statement);

                    if (!nodeOptional.isPresent()) {
                        return;
                    }

                    pdgGraph.addDataDependencyArc(forNode, forNode, variable);
                })
                .setOnVariableDefinitionListener(forNode::addDefinedVariable)
                .setOnVariableDeclarationListener(forNode::addDeclaredVariable)
                .visit(statement);
    }
}