X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=Robust%2Fsrc%2FAnalysis%2FSSJava%2FFlowGraph.java;h=6382ed008123c476091c9c9a5abf0f86ab9db480;hb=dfaefc442488f69bf9f33038ddafb7ff47a67d8d;hp=c1181a5b62a913c975d8be8181e128777940d266;hpb=0b423adbb0f959f90ca7845e53fe8e7e0e151b96;p=IRC.git diff --git a/Robust/src/Analysis/SSJava/FlowGraph.java b/Robust/src/Analysis/SSJava/FlowGraph.java index c1181a5b..6382ed00 100644 --- a/Robust/src/Analysis/SSJava/FlowGraph.java +++ b/Robust/src/Analysis/SSJava/FlowGraph.java @@ -1,7 +1,6 @@ package Analysis.SSJava; import java.io.BufferedWriter; -import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; @@ -9,10 +8,8 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; -import Analysis.OoOJava.ConflictEdge; -import Analysis.OoOJava.ConflictNode; +import IR.ClassDescriptor; import IR.Descriptor; import IR.FieldDescriptor; import IR.MethodDescriptor; @@ -23,6 +20,7 @@ public class FlowGraph { MethodDescriptor md; Set nodeSet; + Set returnNodeSet; FlowNode thisVarNode; // maps the composite representation of field/var descriptors to infer nodes @@ -37,11 +35,12 @@ public class FlowGraph { public FlowGraph(MethodDescriptor md, Map mapParamDescToIdx) { this.md = md; - nodeSet = new HashSet(); - mapDescTupleToInferNode = new HashMap, FlowNode>(); - mapNodeToNeighborSet = new HashMap, Set>(); + this.nodeSet = new HashSet(); + this.mapDescTupleToInferNode = new HashMap, FlowNode>(); + this.mapNodeToNeighborSet = new HashMap, Set>(); this.mapParamDescToIdx = new HashMap(); this.mapParamDescToIdx.putAll(mapParamDescToIdx); + this.returnNodeSet = new HashSet(); // create a node for 'this' varialbe NTuple thisDescTuple = new NTuple(); @@ -58,6 +57,21 @@ public class FlowGraph { return nodeSet; } + public MethodDescriptor getMethodDescriptor() { + return md; + } + + public Set getParameterNodeSet() { + Set paramNodeSet = new HashSet(); + for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) { + FlowNode fn = (FlowNode) iterator.next(); + if (fn.isParameter()) { + paramNodeSet.add(fn); + } + } + return paramNodeSet; + } + public void addNeighbor(FlowNode node, FlowNode neighbor) { Set set = mapNodeToNeighborSet.get(node); if (set == null) { @@ -68,6 +82,26 @@ public class FlowGraph { System.out.println("add a new neighbor " + neighbor + " to " + node); } + public boolean hasEdge(NTuple fromDescTuple, NTuple toDescTuple) { + + FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple); + FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple); + + Set fromNodeOutEdgeSet = fromNode.getOutEdgeSet(); + for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) { + FlowEdge flowEdge = (FlowEdge) iterator.next(); + if (flowEdge.getDst().equals(toNode)) { + return true; + } else { + if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) { + return true; + } + } + } + + return false; + } + public void addValueFlowEdge(NTuple fromDescTuple, NTuple toDescTuple) { FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple); @@ -137,6 +171,151 @@ public class FlowGraph { } + public void setReturnFlowNode(NTuple tuple) { + + if (!mapDescTupleToInferNode.containsKey(tuple)) { + createNewFlowNode(tuple); + } + + FlowNode node = mapDescTupleToInferNode.get(tuple); + node.setReturn(true); + + returnNodeSet.add(node); + } + + public Set getReturnNodeSet() { + return returnNodeSet; + } + + public Set getReachableFlowNodeSet(FlowNode fn) { + Set set = new HashSet(); + getReachableFlowNodeSet(fn, set); + return set; + } + + private void getReachableFlowNodeSet(FlowNode fn, Set visited) { + + for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) { + FlowEdge edge = (FlowEdge) iterator.next(); + + if (fn.equals(getFlowNode(edge.getInitTuple()))) { + + FlowNode dstNode = getFlowNode(edge.getEndTuple()); + + if (!visited.contains(dstNode)) { + visited.add(dstNode); + getReachableFlowNodeSet(dstNode, visited); + } + } + } + + } + + public Set> getReachableFlowTupleSet(Set> visited, FlowNode fn) { + for (Iterator iterator = fn.getOutEdgeSet().iterator(); iterator.hasNext();) { + FlowEdge edge = (FlowEdge) iterator.next(); + + if (fn.getDescTuple().equals(edge.getInitTuple())) { + FlowNode dstNode = getFlowNode(edge.getEndTuple()); + NTuple dstTuple = getLocationTuple(dstNode); + + if (!visited.contains(dstTuple)) { + visited.add(dstTuple); + visited.addAll(getReachableFlowTupleSet(visited, dstNode)); + } + + } + } + return visited; + } + + public NTuple getLocationTuple(FlowNode fn) { + + NTuple descTuple = fn.getDescTuple(); + + NTuple locTuple = new NTuple(); + + ClassDescriptor cd = null; + + for (int i = 0; i < descTuple.size(); i++) { + Descriptor d = descTuple.get(i); + Location loc; + if (i == 0) { + loc = new Location(md, d.getSymbol()); + cd = ((VarDescriptor) d).getType().getClassDesc(); + } else { + loc = new Location(cd, d.getSymbol()); + cd = ((FieldDescriptor) d).getType().getClassDesc(); + } + locTuple.add(loc); + } + + return locTuple; + } + + public Set getIncomingFlowNodeSet(FlowNode node) { + Set set = new HashSet(); + getIncomingFlowNodeSet(node, set); + return set; + } + + public void getIncomingFlowNodeSet(FlowNode node, Set visited) { + + for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) { + FlowNode curNode = (FlowNode) iterator.next(); + Set edgeSet = curNode.getOutEdgeSet(); + + for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) { + FlowEdge flowEdge = (FlowEdge) iterator2.next(); + + if (node.equals(getFlowNode(flowEdge.getEndTuple()))) { + FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple()); + + if (!visited.contains(incomingNode)) { + visited.add(incomingNode); + getIncomingFlowNodeSet(incomingNode, visited); + } + } + } + } + + } + + public Set> getIncomingFlowTupleSet(FlowNode fn) { + + NTuple dstTuple = fn.getDescTuple(); + + Set> set = new HashSet>(); + + ClassDescriptor cd = null; + + for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) { + FlowNode node = (FlowNode) iterator.next(); + Set edgeSet = node.getOutEdgeSet(); + for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) { + FlowEdge flowEdge = (FlowEdge) iterator2.next(); + if (dstTuple.equals(flowEdge.getEndTuple())) { + NTuple initTuple = flowEdge.getInitTuple(); + NTuple locTuple = new NTuple(); + for (int i = 0; i < initTuple.size(); i++) { + Descriptor d = initTuple.get(i); + Location loc; + if (i == 0) { + loc = new Location(md, d.getSymbol()); + cd = ((VarDescriptor) d).getType().getClassDesc(); + } else { + loc = new Location(cd, d.getSymbol()); + cd = ((FieldDescriptor) d).getType().getClassDesc(); + } + locTuple.add(loc); + } + set.add(locTuple); + } + } + } + return set; + } + public boolean isParamter(NTuple tuple) { // return true if a descriptor tuple is started with a parameter descriptor Descriptor firstIdxDesc = tuple.get(0); @@ -189,7 +368,7 @@ public class FlowGraph { public void writeGraph() throws java.io.IOException { - String graphName = md.toString(); + String graphName = "flowgraph_" + md.toString(); graphName = graphName.replaceAll("[\\W]", ""); BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));