Newer
Older
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
setRootVertex(new GraphNode<>(getNextVertexId(), getRootNodeData(), new EmptyStmt()));
public <ASTNode extends Node> GraphNode<ASTNode> addNode(String instruction, ASTNode node) {
GraphNode<ASTNode> vertex = new GraphNode<>(getNextVertexId(), instruction, node);
protected String getRootNodeData() {
return "Start";
}
public void addControlFlowEdge(GraphNode from, GraphNode to) {
@Override
public String toGraphvizRepresentation() {
String lineSep = System.lineSeparator();
.map(node -> String.format("%s [label=\"%s: %s\"]", node.getId(), node.getId(), node.getData()))
.collect(Collectors.joining(lineSep));
.sorted(Comparator.comparingInt(arrow -> ((GraphNode) arrow.getFrom()).getId()))
.map(arrow -> ((Arc) arrow).toGraphvizRepresentation())
.collect(Collectors.joining(lineSep));
return "digraph g{" + lineSep +
public Graph slice(SlicingCriterion slicingCriterion) {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
public Set<GraphNode<?>> findLastDefinitionsFrom(GraphNode<?> startNode, String variable) {
// Logger.log("=======================================================");
// Logger.log("Starting from " + startNode);
// Logger.log("Looking for variable " + variable);
// Logger.log(cfgGraph.toString());
if (!this.contains(startNode)) {
throw new NodeNotFoundException(startNode, this);
}
return findLastDefinitionsFrom(new HashSet<>(), startNode, startNode, variable);
}
private Set<GraphNode<?>> findLastDefinitionsFrom(Set<Integer> visited, GraphNode<?> startNode, GraphNode<?> currentNode, String variable) {
visited.add(currentNode.getId());
// Logger.log("On " + currentNode);
Set<GraphNode<?>> res = new HashSet<>();
for (Arc<?> arc : currentNode.getIncomingArcs()) {
ControlFlowArc controlFlowArc = (ControlFlowArc) arc;
GraphNode<?> from = controlFlowArc.getFromNode();
// Logger.log("Arrow from node: " + from);
if (!Objects.equals(startNode, from) && visited.contains(from.getId())) {
// Logger.log("It's already visited. Continuing...");
continue;
}
if (from.getDefinedVariables().contains(variable)) {
// Logger.log("Contains defined variable: " + variable);
res.add(from);
} else {
// Logger.log("Doesn't contain the variable, searching inside it");
res.addAll(findLastDefinitionsFrom(visited, startNode, from, variable));
}
}
// Logger.format("Done with node %s", currentNode.getId());
return res;
}