Commit 18b0928f authored by Jonathan Andrade's avatar Jonathan Andrade
Browse files

fix for inline functions

parent 8ed7e50a
Loading
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -220,7 +220,7 @@ public class ICFG extends Graph implements Buildable<NodeList<CompilationUnit>>
                if (va instanceof VariableAction.Movable)
                    throw new IllegalStateException("Movable outside Enter/Exit or call");
                else if (va instanceof VariableAction.CallMarker marker && marker.isEnter()) {
                    extracted.addAll(extractMovables(iterator, marker));
                    extracted.addAll(extractMovables(iterator, marker, graphNode));
                }
            }
            // Place extracted sequence before the node
@@ -237,17 +237,23 @@ public class ICFG extends Graph implements Buildable<NodeList<CompilationUnit>>
         *     Includes all nested calls. The first node in the list has no incoming edges.
         *     The last node in the list has no outgoing edges.
         */
        protected LinkedList<GraphNode<?>> extractMovables(Iterator<VariableAction> iterator, VariableAction.CallMarker callMarker) {
        LinkedList<GraphNode<?>> callList = new LinkedList<>();

        protected LinkedList<GraphNode<?>> extractMovables(Iterator<VariableAction> iterator, VariableAction.CallMarker callMarker, GraphNode<?> graphNode) {
            LinkedList<GraphNode<?>> res = new LinkedList<>();
            int count = 0;
            boolean input = true;
            while (iterator.hasNext()) {
                VariableAction va = iterator.next();
                if (va instanceof VariableAction.CallMarker newMarker) {
                    if (newMarker.isEnter()) {
                        // Recursively enter the next function and connect it. Consumes movables.
                        List<GraphNode<?>> moved = extractMovables(iterator, newMarker);
                        List<GraphNode<?>> moved = extractMovables(iterator, newMarker, graphNode);
                        connectAppend(res, moved);
                    } else if (newMarker.getCall().equals(callMarker.getCall())) {
                        if(count==graphNode.getMethodCalls().size()) {
                            callList.clear();
                        }
                        return res; // Process ended.
                    } else {
                        throw new IllegalStateException("Invalid pairing of call markers");
@@ -261,15 +267,19 @@ public class ICFG extends Graph implements Buildable<NodeList<CompilationUnit>>
                        input = false;
                        insertCallerNode(res, CallNode.create(callMarker.getCall()), callMarker);
                        // If it is a call to void method, add a blank "return"
                        if (!(movable.getRealNode() instanceof CallNode.Return))
                        if (!(movable.getRealNode() instanceof CallNode.Return)){
                            insertCallerNode(res, CallNode.Return.create(callMarker.getCall()), callMarker);
                        else {
                        } else {
                            addNonExecControlFlowArc(res.getLast(), movable.getRealNode());
                            res.add(movable.getRealNode());
                            callList.add(movable.getRealNode());
                            continue;
                        }
                    }
                    // Connect node to previous nodes and add to resulting list
                    if(count<graphNode.getMethodCalls().size()){
                        connectAppend(callList, movable.getRealNode());
                    }
                    connectAppend(res, movable.getRealNode());
                }
            }
+5 −2
Original line number Diff line number Diff line
@@ -14,14 +14,17 @@ public class ICFGTest {
        StaticJavaParser.getConfiguration().setAttributeComments(false);
        StaticTypeSolver.addTypeSolverJRE();

        createGraph("TestInicial.java", "grafoInicial");
        /*createGraph("TestInicial.java", "grafoInicial");
        System.out.println("Grafo 1 generado...");

        createGraph("TestGlobalVariables.java", "grafoGlobalVariables");
        System.out.println("Grafo 2 generado...");

        createGraph("TestEmbebedFunctions.java", "grafoEmbebedFunctions");
        System.out.println("Grafo 3 generado...");
        System.out.println("Grafo 3 generado...");*/

        createGraph("TestInlineFunctions.java", "grafoInlineVariables");
        System.out.println("Grafo 4 generado...");
    }

    private static void createGraph(String fileName, String graphName) throws IOException {
+42 −0
Original line number Diff line number Diff line
public class TestInlineFunctions {

    public static int x = 0;
    public static int y = 5;
    public static int z = 7;

    public static void main(String[] args) {

        System.out.println("Valores iniciales:");
        System.out.println("x: " + x);
        System.out.println("y: " + y);
        System.out.println("z: " + z);

        x = 10;
        y = y + x;

        System.out.println("\nValores despues de modificar:");
        System.out.println("x: " + x);
        System.out.println("y: " + y);
        System.out.println("z: " + z);

        int valor1 = incrementar(x, y) + incrementar(y, z) + incrementar(z, x);
        System.out.println("valor1: " + valor1);
    }

    private static int incrementar(int a, int b) {
        b++;
        return a + 1;
    }

    private static int incrementarBucle(int a) {
        if (a > 0) {
            int x = 0;
            for (int i = 0; i < a; i++) {
                x = x + i;
            }
            return x;
        } else {
            return a;
        }
    }
}
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -29,6 +29,11 @@ public class GraphNode<N extends Node> implements Comparable<GraphNode<?>> {
    protected final N astNode;
    /** A sorted list of actions (usages, definitions and declarations) performed in this node. */
    protected final List<VariableAction> variableActions;

    public List<Resolvable<? extends ResolvedMethodLikeDeclaration>> getMethodCalls() {
        return methodCalls;
    }

    /** The method calls contained  */
    protected final List<Resolvable<? extends ResolvedMethodLikeDeclaration>> methodCalls = new LinkedList<>();
    /** Nodes that are generated as a result of the instruction represented by this GraphNode and that may