Commit 128fde28 authored by jacosro's avatar jacosro
Browse files

Implemented method call nodes for variables as arguments

parent ab67fdac
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -91,9 +91,9 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter<Context> {
        NodeList<Expression> arguments = methodCallExpr.getArguments();

        // Parse first method call expressions as arguments
        arguments.stream()
                .filter(Expression::isMethodCallExpr)
                .forEach(expression -> expression.accept(this, context));
//        arguments.stream()
//                .filter(Expression::isMethodCallExpr)
//                .forEach(expression -> expression.accept(this, context));

        Logger.log("MethodCallReplacerVisitor", context);

@@ -187,6 +187,7 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter<Context> {

            // Here, variablesForOutNode may have 1 variable or more depending on the expression

            Logger.log("MethodCallReplacerVisitor", String.format("Variables for out node: %s", variablesForOutNode));
            if (variablesForOutNode.isEmpty()) {
                /*
                    If the argument is not a variable or it is not declared in the scope,
@@ -197,7 +198,11 @@ class MethodCallReplacerVisitor extends VoidVisitorAdapter<Context> {
            } else if (variablesForOutNode.size() == 1) {
                String variable = variablesForOutNode.iterator().next();

                if (sdg.findDeclarationsOfVariable(variable, originalMethodCallNode).isEmpty()) {
                List<GraphNode<?>> declarations = sdg.findDeclarationsOfVariable(variable, originalMethodCallNode);

                Logger.log("MethodCallReplacerVisitor", String.format("Declarations of variable: '%s': %s", variable, declarations));

                if (declarations.isEmpty()) {
                    Logger.log("MethodCallReplacerVisitor", String.format("Expression '%s' should not have out node", argument.toString()));
                    continue;
                }
+18 −11
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import tfm.arcs.Arc;
import tfm.arcs.cfg.ControlFlowArc;
import tfm.arcs.pdg.ControlDependencyArc;
import tfm.arcs.pdg.DataDependencyArc;
import tfm.arcs.sdg.CallArc;
@@ -19,6 +20,7 @@ import tfm.slicing.Slice;
import tfm.slicing.Sliceable;
import tfm.slicing.SlicingCriterion;
import tfm.utils.Context;
import tfm.utils.Utils;

import java.util.*;
import java.util.function.Function;
@@ -82,19 +84,24 @@ public class SDG extends Graph implements Sliceable, Buildable<NodeList<Compilat
    }

    public List<GraphNode<?>> findDeclarationsOfVariable(String variable, GraphNode<?> root) {
        List<GraphNode<?>> res = new ArrayList<>();

        // First, expand the node
        for (Arc arc : incomingEdgesOf(root)) {
            if (arc.isDataDependencyArc() || arc.isControlDependencyArc()) {
                res.addAll(findDeclarationsOfVariable(variable, getEdgeSource(arc)));
            }
        return this.methodCFGMap.values().stream()
                .filter(cfg -> cfg.containsVertex(root))
                .findFirst()
                .map(cfg -> doFindDeclarationsOfVariable(variable, root, cfg, Utils.emptyList()))
                .orElse(Utils.emptyList());
    }

        // Finally, the current node
        // This way, the last element of the list is the most recent declaration
        if (root.getDeclaredVariables().contains(variable)) {
    private List<GraphNode<?>> doFindDeclarationsOfVariable(String variable, GraphNode<?> root, CFG cfg, List<GraphNode<?>> res) {
        Set<Arc> controlDependencies = cfg.incomingEdgesOf(root);

        for (Arc arc : controlDependencies) {
            GraphNode<?> source = cfg.getEdgeSource(arc);

            if (source.getDeclaredVariables().contains(variable)) {
                res.add(root);
            } else {
                res.addAll(doFindDeclarationsOfVariable(variable, source, cfg, res));
            }
        }

        return res;
+6 −6
Original line number Diff line number Diff line
@@ -14,16 +14,16 @@ public class Example1 {
    int num;

    public static void main(String[] args) {
        int x = 1;
        int y = 2;
        int n1 = 1;
        int n2 = 2;

        Example1 example1 = new Example1();
        Example1 example2 = new Example1();
//        Example1 example1 = new Example1();
//        Example1 example2 = new Example1();

        int f = sum(example1.getNum(), example2.num);
        int f = sum(sum(n1, n2), n2);

        Logger.log(example1.num);
        Logger.log(f);
        Logger.log(z);
    }

    public int getNum() {