1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
17 import IR.VarDescriptor;
18 import IR.Tree.MethodInvokeNode;
20 public class FlowGraph {
24 Set<FlowNode> returnNodeSet;
27 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
28 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
30 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
31 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
33 // maps the composite representation of field/var descriptors to infer nodes
34 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
36 // maps a paramter descriptor to its index
37 Map<Descriptor, Integer> mapParamDescToIdx;
39 // DS for the lattice generation
40 Map<Integer, FlowNode> mapIdxToFlowNode;
42 Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
44 public static int interseed = 0;
48 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
50 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
51 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
52 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
53 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
54 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
55 this.returnNodeSet = new HashSet<FlowNode>();
56 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
57 this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
58 this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
59 this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
62 // create a node for 'this' varialbe
63 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
64 // thisDescTuple.add(md.getThis());
66 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
67 thisVarTuple.add(md.getThis());
68 FlowNode thisNode = createNewFlowNode(thisVarTuple);
69 thisNode.setSkeleton(true);
70 thisVarNode = thisNode;
77 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
78 return mapDescTupleToInferNode;
81 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
82 this.mapDescTupleToInferNode.putAll(in);
85 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
86 return mapLocTupleToFlowNode;
89 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
90 this.mapLocTupleToFlowNode.putAll(in);
93 public void setReturnNodeSet(Set<FlowNode> in) {
94 this.returnNodeSet.addAll(in);
97 public void setThisVarNode(FlowNode thisVarNode) {
98 this.thisVarNode = thisVarNode;
101 public Map<Descriptor, Integer> getMapParamDescToIdx() {
102 return mapParamDescToIdx;
105 public FlowNode createIntermediateNode() {
106 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
107 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
108 tuple.add(interDesc);
111 FlowNode newNode = new FlowNode(tuple);
112 newNode.setIntermediate(true);
114 mapDescTupleToInferNode.put(tuple, newNode);
115 // nodeSet.add(newNode);
117 System.out.println("create new intermediate node= " + newNode);
122 public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
123 return mapMethodInvokeNodeToFlowReturnNode.get(min);
126 public FlowReturnNode createReturnNode(MethodInvokeNode min) {
127 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
128 NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
131 FlowReturnNode newNode = new FlowReturnNode(tuple, min);
132 mapDescTupleToInferNode.put(tuple, newNode);
133 mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
134 // nodeSet.add(newNode);
136 System.out.println("create new set node= " + newNode);
141 private void setupMapIdxToDesc() {
143 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
144 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
145 Descriptor paramDesc = (Descriptor) iterator.next();
146 int idx = mapParamDescToIdx.get(paramDesc);
147 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
148 descTuple.add(paramDesc);
149 FlowNode paramNode = getFlowNode(descTuple);
150 mapIdxToFlowNode.put(idx, paramNode);
151 paramNode.setSkeleton(true);
156 public int getNumParameters() {
157 return mapIdxToFlowNode.keySet().size();
160 public FlowNode getParamFlowNode(int idx) {
161 return mapIdxToFlowNode.get(idx);
164 public Set<FlowEdge> getEdgeSet() {
165 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
167 Set<FlowNode> nodeSet = getNodeSet();
168 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
169 FlowNode flowNode = (FlowNode) iterator.next();
170 edgeSet.addAll(getOutEdgeSet(flowNode));
176 public Set<FlowNode> getParamFlowNodeSet() {
177 Set<FlowNode> setParamFlowNode = new HashSet<FlowNode>();
178 setParamFlowNode.addAll(mapIdxToFlowNode.values());
179 return setParamFlowNode;
182 public Set<FlowNode> getNodeSet() {
183 Set<FlowNode> set = new HashSet<FlowNode>();
184 set.addAll(mapDescTupleToInferNode.values());
188 public MethodDescriptor getMethodDescriptor() {
192 public boolean isParamDesc(Descriptor desc) {
194 if (mapParamDescToIdx.containsKey(desc)) {
195 int idx = mapParamDescToIdx.get(desc);
196 if (!md.isStatic() && idx == 0) {
205 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
207 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
208 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
210 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
211 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
212 FlowEdge flowEdge = (FlowEdge) iterator.next();
213 if (flowEdge.getDst().equals(toNode)) {
216 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
225 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
227 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
229 // returns the set of edges that share the same prefix of startNode
230 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
232 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
233 Set<FlowEdge> nodeEdgeSet = iter.next();
234 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
235 FlowEdge edge = iter2.next();
236 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
245 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
246 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
247 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
249 return mapFlowNodeToOutEdgeSet.get(node);
252 public Set<FlowEdge> getInEdgeSet(FlowNode node) {
253 if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
254 mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
256 return mapFlowNodeToInEdgeSet.get(node);
259 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
261 FlowNode fromNode = getFlowNode(fromDescTuple);
262 FlowNode toNode = getFlowNode(toDescTuple);
264 if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
268 // System.out.println("create an edge from " + fromNode + " to " + toNode);
270 int fromTupleSize = fromDescTuple.size();
271 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
272 for (int i = 0; i < fromTupleSize; i++) {
273 Descriptor desc = fromDescTuple.get(i);
274 curFromTuple.add(desc);
275 int toTupleSize = toDescTuple.size();
276 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
277 for (int k = 0; k < toTupleSize; k++) {
278 Descriptor toDesc = toDescTuple.get(k);
279 curToTuple.add(toDesc);
280 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
284 // int fromTupleSize = fromDescTuple.size();
285 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
286 // for (int i = 0; i < fromTupleSize; i++) {
287 // Descriptor desc = fromDescTuple.get(i);
288 // curTuple.add(desc);
289 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
292 // int toTupleSize = toDescTuple.size();
293 // curTuple = new NTuple<Descriptor>();
294 // for (int i = 0; i < toTupleSize; i++) {
295 // Descriptor desc = toDescTuple.get(i);
296 // curTuple.add(desc);
297 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
302 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
303 NTuple<Descriptor> endTuple) {
305 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
306 addOutEdge(fromNode, edge);
307 addInEdge(toNode, edge);
309 // System.out.println("add a new edge=" + edge);
312 private void addInEdge(FlowNode toNode, FlowEdge edge) {
313 getInEdgeSet(toNode).add(edge);
316 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
317 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
318 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
320 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
323 public boolean contains(NTuple<Descriptor> descTuple) {
324 return mapDescTupleToInferNode.containsKey(descTuple);
327 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
328 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
329 FlowNode node = createNewFlowNode(descTuple);
330 mapDescTupleToInferNode.put(descTuple, node);
332 return mapDescTupleToInferNode.get(descTuple);
335 public FlowNode getThisVarNode() {
339 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
341 if (!mapDescTupleToInferNode.containsKey(tuple)) {
342 FlowNode node = new FlowNode(tuple);
343 mapDescTupleToInferNode.put(tuple, node);
344 // nodeSet.add(node);
346 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
348 if (tuple.size() > 1) {
349 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
350 getFlowNode(baseTuple).addFieldNode(node);
353 // System.out.println("Creating new node=" + node);
356 return mapDescTupleToInferNode.get(tuple);
361 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
363 if (!mapDescTupleToInferNode.containsKey(tuple)) {
364 createNewFlowNode(tuple);
367 FlowNode node = mapDescTupleToInferNode.get(tuple);
368 returnNodeSet.add(node);
371 public Set<FlowNode> getReturnNodeSet() {
372 return returnNodeSet;
375 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
376 Set<FlowNode> set = new HashSet<FlowNode>();
377 recurLocalReachFlowNodeSet(fn, set);
381 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
383 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
384 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
385 FlowEdge edge = (FlowEdge) iterator.next();
386 FlowNode originalDstNode = edge.getDst();
388 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
389 if (originalDstNode instanceof FlowReturnNode) {
390 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
391 Set<NTuple<Descriptor>> rtupleSet = rnode.getReturnTupleSet();
392 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
393 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
394 dstNodeSet.add(getFlowNode(rtuple));
397 dstNodeSet.add(originalDstNode);
400 for (Iterator iterator2 = dstNodeSet.iterator(); iterator2.hasNext();) {
401 FlowNode dstNode = (FlowNode) iterator2.next();
402 if (!visited.contains(dstNode)) {
403 visited.add(dstNode);
404 recurLocalReachFlowNodeSet(dstNode, visited);
412 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
414 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
415 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
416 FlowEdge edge = (FlowEdge) iterator.next();
418 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
419 FlowNode dstNode = getFlowNode(edge.getEndTuple());
420 if (!visited.contains(dstNode)) {
421 visited.add(dstNode);
422 getReachFlowNodeSetFrom(dstNode, visited);
429 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
430 Set<FlowNode> set = new HashSet<FlowNode>();
431 getReachFlowNodeSetFrom(fn, set);
435 public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
436 Set<FlowNode> reachableSet = new HashSet<FlowNode>();
438 Set<FlowNode> nodeSet = getNodeSet();
439 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
440 FlowNode originalSrcNode = (FlowNode) iterator.next();
442 Set<FlowNode> srcNodeSet = new HashSet<FlowNode>();
443 if (originalSrcNode instanceof FlowReturnNode) {
444 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
445 Set<NTuple<Descriptor>> rtupleSet = rnode.getReturnTupleSet();
446 for (Iterator iterator2 = rtupleSet.iterator(); iterator2.hasNext();) {
447 NTuple<Descriptor> rtuple = (NTuple<Descriptor>) iterator2.next();
448 if (rtuple.startsWith(prefix)) {
449 System.out.println("rtuple=" + rtuple + " give it to recur=" + originalSrcNode);
450 recurReachableSetFrom(originalSrcNode, reachableSet);
454 if (originalSrcNode.getCurrentDescTuple().startsWith(prefix)) {
455 recurReachableSetFrom(originalSrcNode, reachableSet);
464 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
466 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
467 // iterator.hasNext();) {
468 // FlowEdge edge = (FlowEdge) iterator.next();
470 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
472 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
474 // if (!visited.contains(dstNode)) {
475 // visited.add(dstNode);
476 // getReachFlowNodeSetFrom(dstNode, visited);
483 private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
485 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
486 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
487 FlowEdge edge = (FlowEdge) iterator.next();
488 FlowNode dstNode = getFlowNode(edge.getEndTuple());
489 if (!reachableSet.contains(dstNode)) {
490 reachableSet.add(dstNode);
491 recurReachableSetFrom(dstNode, reachableSet);
497 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
499 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
501 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
502 FlowEdge edge = (FlowEdge) iterator.next();
504 if (fn.getDescTuple().equals(edge.getInitTuple())) {
505 FlowNode dstNode = getFlowNode(edge.getEndTuple());
506 NTuple<Location> dstTuple = getLocationTuple(dstNode);
508 if (!visited.contains(dstTuple)) {
509 visited.add(dstTuple);
510 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
518 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
519 return getLocationTuple(getFlowNode(descTuple));
522 public NTuple<Location> getLocationTuple(FlowNode fn) {
524 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
525 NTuple<Descriptor> descTuple = fn.getDescTuple();
526 NTuple<Location> locTuple = new NTuple<Location>();
527 ClassDescriptor cd = null;
529 Descriptor localDesc = fn.getDescTuple().get(0);
531 if (fn.isIntermediate()) {
532 Location interLoc = new Location(md, localDesc.getSymbol());
533 interLoc.setLocDescriptor(localDesc);
534 locTuple.add(interLoc);
535 } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
536 Location topLoc = new Location(md, Location.TOP);
537 topLoc.setLocDescriptor(LocationInference.TOPDESC);
538 locTuple.add(topLoc);
539 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
540 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
541 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
542 locTuple.add(globalLoc);
545 for (int i = 0; i < descTuple.size(); i++) {
546 Descriptor curDesc = descTuple.get(i);
549 loc = new Location(md, curDesc.getSymbol());
550 loc.setLocDescriptor(curDesc);
551 if (curDesc instanceof VarDescriptor) {
552 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
554 // otherwise it should be the last element
558 loc = new Location(cd, curDesc.getSymbol());
559 loc.setLocDescriptor(curDesc);
561 if (curDesc instanceof FieldDescriptor) {
562 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
563 } else if (curDesc instanceof LocationDescriptor) {
564 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
566 // otherwise it should be the last element of the tuple
575 mapFlowNodeToLocTuple.put(fn, locTuple);
577 return mapFlowNodeToLocTuple.get(fn);
580 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
581 Set<FlowNode> set = new HashSet<FlowNode>();
582 getIncomingFlowNodeSet(node, set);
586 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
588 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
589 FlowNode curNode = (FlowNode) iterator.next();
590 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
592 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
593 FlowEdge flowEdge = (FlowEdge) iterator2.next();
595 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
596 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
598 if (incomingNode instanceof FlowReturnNode) {
599 FlowReturnNode rnode = (FlowReturnNode) incomingNode;
600 Set<NTuple<Descriptor>> nodeTupleSet = rnode.getReturnTupleSet();
601 for (Iterator iterator3 = nodeTupleSet.iterator(); iterator3.hasNext();) {
602 NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
603 FlowNode fn = getFlowNode(nodeTuple);
604 if (!visited.contains(fn)) {
606 getIncomingFlowNodeSet(fn, visited);
610 if (!visited.contains(incomingNode)) {
611 visited.add(incomingNode);
612 getIncomingFlowNodeSet(incomingNode, visited);
622 public boolean isParameter(NTuple<Descriptor> tuple) {
623 // return true if a descriptor tuple is started with a parameter descriptor
624 Descriptor firstIdxDesc = tuple.get(0);
625 return mapParamDescToIdx.containsKey(firstIdxDesc);
628 public int getParamIdx(NTuple<Descriptor> tuple) {
629 Descriptor firstIdxDesc = tuple.get(0);
630 return mapParamDescToIdx.get(firstIdxDesc);
633 public FlowGraph clone() {
634 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
636 // clone.setNodeSet(getNodeSet());
637 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
638 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
639 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
641 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
642 clone.setReturnNodeSet(getReturnNodeSet());
643 clone.setThisVarNode(getThisVarNode());
648 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
649 return mapFlowNodeToLocTuple;
652 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
653 this.mapFlowNodeToLocTuple.putAll(in);
656 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
657 return mapFlowNodeToOutEdgeSet;
660 public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
662 Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
664 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
665 FlowNode curNode = (FlowNode) iterator.next();
666 Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
668 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
669 FlowEdge outEdge = (FlowEdge) iterator2.next();
671 if (outEdge.getEndTuple().startsWith(prefix)) {
672 incomingNodeSet.add(curNode);
673 recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
680 return incomingNodeSet;
684 private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
686 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
688 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
689 FlowEdge inEdge = (FlowEdge) iterator.next();
691 FlowNode inNode = getFlowNode(inEdge.getInitTuple());
692 if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
694 recurIncomingNodeSetByPrefix(prefix, inNode, visited);
700 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
701 Set<FlowNode> keySet = inMap.keySet();
702 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
703 FlowNode key = (FlowNode) iterator.next();
704 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
705 newEdgeSet.addAll(inMap.get(key));
706 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
710 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
711 Set<FlowEdge> addedEdgeSet) throws IOException {
713 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
715 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
716 FlowEdge flowEdge = iterator.next();
718 FlowNode u = flowEdge.getSrc();
719 FlowNode v = flowEdge.getDst();
721 if (u.getDescTuple().equals(flowEdge.getInitTuple())
722 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
723 // only draw an edge of the actual value flow
725 if (!addedEdgeSet.contains(flowEdge)) {
727 if (!addedNodeSet.contains(u)) {
731 if (!addedNodeSet.contains(v)) {
736 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
737 addedEdgeSet.add(flowEdge);
745 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
746 if (node instanceof FlowReturnNode) {
747 FlowReturnNode rnode = (FlowReturnNode) node;
748 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
750 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
755 public void writeGraph() throws java.io.IOException {
759 public void writeGraph(String suffix) throws java.io.IOException {
761 String graphName = "flowgraph_" + md.toString() + suffix;
762 graphName = graphName.replaceAll("[\\W]", "");
764 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
765 bw.write("digraph " + graphName + " {\n");
766 bw.write("compound=true;\n");
768 // then visit every flow node
770 // Iterator<FlowNode> iter = nodeSet.iterator();
771 Iterator<FlowNode> iter = getNodeSet().iterator();
773 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
774 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
776 while (iter.hasNext()) {
777 FlowNode node = iter.next();
779 if (node.getDescTuple().size() == 1) {
780 // here, we just care about the local variable
781 if (node.getFieldNodeSet().size() > 0) {
782 drawSubgraph(node, bw, addedEdgeSet);
785 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
794 public boolean constainsNode(FlowNode node) {
795 return getNodeSet().contains(node);
798 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
801 bw.write("subgraph cluster_" + node.getID() + "{\n");
802 bw.write("label=\"" + node.getPrettyID() + "\";\n");
804 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
805 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
806 FlowNode fieldNode = (FlowNode) iterator.next();
807 if (fieldNode.getFieldNodeSet().size() > 0) {
808 drawSubgraph(fieldNode, bw, addedSet);
810 Descriptor desc = fieldNode.getDescTuple().getLastElement();
811 if (desc instanceof VarDescriptor) {
812 VarDescriptor varDesc = (VarDescriptor) desc;
813 if (varDesc.getType().isPrimitive()) {
814 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
816 } else if (desc instanceof FieldDescriptor) {
817 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
818 if (fieldDesc.getType().isPrimitive()) {
819 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");