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

CFG: finished switch

parent d0e56d0e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -55,9 +55,9 @@ public abstract class Graph<NodeType extends Node> extends edg.graphlib.Graph<St
        ArcData data = arrow.getData();

        if (!verticies.contains(from))
            throw new IllegalArgumentException("from is not in graph");
            throw new IllegalArgumentException(String.format("from (%s) is not in graph", from));
        if (!verticies.contains(to))
            throw new IllegalArgumentException("to is not in graph");
            throw new IllegalArgumentException(String.format("to (%s) is not in graph", to));

        List<Arrow<String, ArcData>> es2 = from.findEdges(to);

+26 −26
Original line number Diff line number Diff line
@@ -35,31 +35,31 @@ public class Example1 {
            y *= x;
        }

//        int e = (Integer) x;
//
//        switch (x) {
//            case 1:
//                e = 2;
//
//                while (1 < 4) {
//                    y = 2;
//
//                    if (4 < 1) {
//                        break;
//                    }
//                }
//
//                e = 5;
//
//                break;
//            case 2:
//                e = 3;
//            case 3:
//                e = 4;
//                break;
//            default:
//        }
        int e = (Integer) x;

        switch (x) {
            case 1:
                e = 2;

                while (1 < 4) {
                    y = 2;

                    if (4 < 1) {
                        break;
                    }
                }

                e = 5;

                break;
            case 2:
                e = 3;
            case 3:
                e = 4;
                break;
            default:
        }

        Logger.log(y);
        Logger.log(e);
    }
}
+19 −2
Original line number Diff line number Diff line
package tfm.visitors;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.*;
@@ -209,16 +210,32 @@ public class CFGVisitor extends VoidVisitorAdapter<Void> {

        List<CFGNode> allEntryBreaks = new ArrayList<>();

        List<CFGNode> lastEntryStatementsWithNoBreak = new ArrayList<>();

        switchStmt.getEntries().forEach(switchEntryStmt -> {
            String label = switchEntryStmt.getLabel()
                    .map(expression -> "case " + expression)
                    .orElse("default");

            CFGNode switchEntryNode = addNodeAndArcs(label, switchEntryStmt);

            lastParentNodes.add(switchEntryNode);
            lastParentNodes.addAll(lastEntryStatementsWithNoBreak);
            lastEntryStatementsWithNoBreak.clear();

            switchEntryStmt.getStatements().accept(this, null);

            if (!bodyBreaks.isEmpty()) { // means it has no break
            if (!bodyBreaks.isEmpty()) { // means it has break
                allEntryBreaks.addAll(bodyBreaks); // save breaks of entry

                lastParentNodes.clear();
                lastParentNodes.add(switchNode); // Set switch as the only parent
                lastParentNodes.add(switchEntryNode); // Set switch as the only parent

                bodyBreaks.clear(); // Clear breaks
            } else {
                lastEntryStatementsWithNoBreak.addAll(lastParentNodes);
                lastParentNodes.clear();
                lastParentNodes.add(switchEntryNode);
            }
        });

+14 −0
Original line number Diff line number Diff line
@@ -52,6 +52,20 @@ public class ControlDependencyVisitor extends VoidVisitorAdapter<PDGNode> {
        forEachStmt.getBody().accept(this, node);
    }

    @Override
    public void visit(SwitchStmt switchStmt, PDGNode parent) {
        PDGNode node = addNodeAndControlDependency(switchStmt, parent);

        switchStmt.getEntries().accept(this, node);
    }

    @Override
    public void visit(SwitchEntryStmt switchEntryStmt, PDGNode parent) {
        PDGNode node = addNodeAndControlDependency(switchEntryStmt, parent);

        switchEntryStmt.getStatements().accept(this, node);
    }

    private PDGNode addNodeAndControlDependency(Statement statement, PDGNode parent) {
        PDGNode node = pdgGraph.addNode(cfgGraph.findNodeByStatement(statement).get());
        pdgGraph.addControlDependencyArc(parent, node);