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> getNodeSet() {
177 Set<FlowNode> set = new HashSet<FlowNode>();
178 set.addAll(mapDescTupleToInferNode.values());
182 public MethodDescriptor getMethodDescriptor() {
186 public boolean isParamDesc(Descriptor desc) {
188 if (mapParamDescToIdx.containsKey(desc)) {
189 int idx = mapParamDescToIdx.get(desc);
190 if (!md.isStatic() && idx == 0) {
199 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
201 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
202 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
204 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
205 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
206 FlowEdge flowEdge = (FlowEdge) iterator.next();
207 if (flowEdge.getDst().equals(toNode)) {
210 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
219 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
221 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
223 // returns the set of edges that share the same prefix of startNode
224 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
226 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
227 Set<FlowEdge> nodeEdgeSet = iter.next();
228 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
229 FlowEdge edge = iter2.next();
230 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
239 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
240 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
241 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
243 return mapFlowNodeToOutEdgeSet.get(node);
246 public Set<FlowEdge> getInEdgeSet(FlowNode node) {
247 if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
248 mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
250 return mapFlowNodeToInEdgeSet.get(node);
253 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
255 FlowNode fromNode = getFlowNode(fromDescTuple);
256 FlowNode toNode = getFlowNode(toDescTuple);
258 if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
262 // System.out.println("create an edge from " + fromNode + " to " + toNode);
264 int fromTupleSize = fromDescTuple.size();
265 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
266 for (int i = 0; i < fromTupleSize; i++) {
267 Descriptor desc = fromDescTuple.get(i);
268 curFromTuple.add(desc);
269 int toTupleSize = toDescTuple.size();
270 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
271 for (int k = 0; k < toTupleSize; k++) {
272 Descriptor toDesc = toDescTuple.get(k);
273 curToTuple.add(toDesc);
274 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
278 // int fromTupleSize = fromDescTuple.size();
279 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
280 // for (int i = 0; i < fromTupleSize; i++) {
281 // Descriptor desc = fromDescTuple.get(i);
282 // curTuple.add(desc);
283 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
286 // int toTupleSize = toDescTuple.size();
287 // curTuple = new NTuple<Descriptor>();
288 // for (int i = 0; i < toTupleSize; i++) {
289 // Descriptor desc = toDescTuple.get(i);
290 // curTuple.add(desc);
291 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
296 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
297 NTuple<Descriptor> endTuple) {
299 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
300 addOutEdge(fromNode, edge);
301 addInEdge(toNode, edge);
303 // System.out.println("add a new edge=" + edge);
306 private void addInEdge(FlowNode toNode, FlowEdge edge) {
307 getInEdgeSet(toNode).add(edge);
310 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
311 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
312 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
314 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
317 public boolean contains(NTuple<Descriptor> descTuple) {
318 return mapDescTupleToInferNode.containsKey(descTuple);
321 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
322 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
323 FlowNode node = createNewFlowNode(descTuple);
324 mapDescTupleToInferNode.put(descTuple, node);
326 return mapDescTupleToInferNode.get(descTuple);
329 public FlowNode getThisVarNode() {
333 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
335 if (!mapDescTupleToInferNode.containsKey(tuple)) {
336 FlowNode node = new FlowNode(tuple);
337 mapDescTupleToInferNode.put(tuple, node);
338 // nodeSet.add(node);
340 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
342 if (tuple.size() > 1) {
343 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
344 getFlowNode(baseTuple).addFieldNode(node);
347 // System.out.println("Creating new node=" + node);
350 return mapDescTupleToInferNode.get(tuple);
355 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
357 if (!mapDescTupleToInferNode.containsKey(tuple)) {
358 createNewFlowNode(tuple);
361 FlowNode node = mapDescTupleToInferNode.get(tuple);
362 returnNodeSet.add(node);
365 public Set<FlowNode> getReturnNodeSet() {
366 return returnNodeSet;
369 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
370 Set<FlowNode> set = new HashSet<FlowNode>();
371 recurLocalReachFlowNodeSet(fn, set);
375 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
377 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
378 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
379 FlowEdge edge = (FlowEdge) iterator.next();
380 FlowNode dstNode = edge.getDst();
382 if (!visited.contains(dstNode)) {
383 visited.add(dstNode);
384 recurLocalReachFlowNodeSet(dstNode, visited);
390 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
392 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
393 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
394 FlowEdge edge = (FlowEdge) iterator.next();
396 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
397 FlowNode dstNode = getFlowNode(edge.getEndTuple());
398 if (!visited.contains(dstNode)) {
399 visited.add(dstNode);
400 getReachFlowNodeSetFrom(dstNode, visited);
407 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
408 Set<FlowNode> set = new HashSet<FlowNode>();
409 getReachFlowNodeSetFrom(fn, set);
413 public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
414 Set<FlowNode> reachableSet = new HashSet<FlowNode>();
416 Set<FlowNode> nodeSet = getNodeSet();
417 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
418 FlowNode flowNode = (FlowNode) iterator.next();
419 if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
420 recurReachableSetFrom(flowNode, reachableSet);
427 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
429 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
430 // iterator.hasNext();) {
431 // FlowEdge edge = (FlowEdge) iterator.next();
433 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
435 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
437 // if (!visited.contains(dstNode)) {
438 // visited.add(dstNode);
439 // getReachFlowNodeSetFrom(dstNode, visited);
446 private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
448 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
449 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
450 FlowEdge edge = (FlowEdge) iterator.next();
451 FlowNode dstNode = getFlowNode(edge.getEndTuple());
452 if (!reachableSet.contains(dstNode)) {
453 reachableSet.add(dstNode);
454 recurReachableSetFrom(dstNode, reachableSet);
460 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
462 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
464 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
465 FlowEdge edge = (FlowEdge) iterator.next();
467 if (fn.getDescTuple().equals(edge.getInitTuple())) {
468 FlowNode dstNode = getFlowNode(edge.getEndTuple());
469 NTuple<Location> dstTuple = getLocationTuple(dstNode);
471 if (!visited.contains(dstTuple)) {
472 visited.add(dstTuple);
473 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
481 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
482 return getLocationTuple(getFlowNode(descTuple));
485 public NTuple<Location> getLocationTuple(FlowNode fn) {
487 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
488 NTuple<Descriptor> descTuple = fn.getDescTuple();
489 NTuple<Location> locTuple = new NTuple<Location>();
490 ClassDescriptor cd = null;
492 Descriptor localDesc = fn.getDescTuple().get(0);
494 if (fn.isIntermediate()) {
495 Location interLoc = new Location(md, localDesc.getSymbol());
496 interLoc.setLocDescriptor(localDesc);
497 locTuple.add(interLoc);
498 } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
499 Location topLoc = new Location(md, Location.TOP);
500 topLoc.setLocDescriptor(LocationInference.TOPDESC);
501 locTuple.add(topLoc);
502 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
503 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
504 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
505 locTuple.add(globalLoc);
508 for (int i = 0; i < descTuple.size(); i++) {
509 Descriptor curDesc = descTuple.get(i);
512 loc = new Location(md, curDesc.getSymbol());
513 loc.setLocDescriptor(curDesc);
514 if (curDesc instanceof VarDescriptor) {
515 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
517 // otherwise it should be the last element
521 loc = new Location(cd, curDesc.getSymbol());
522 loc.setLocDescriptor(curDesc);
524 if (curDesc instanceof FieldDescriptor) {
525 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
526 } else if (curDesc instanceof LocationDescriptor) {
527 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
529 // otherwise it should be the last element of the tuple
538 mapFlowNodeToLocTuple.put(fn, locTuple);
540 return mapFlowNodeToLocTuple.get(fn);
543 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
544 Set<FlowNode> set = new HashSet<FlowNode>();
545 getIncomingFlowNodeSet(node, set);
549 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
551 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
552 FlowNode curNode = (FlowNode) iterator.next();
553 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
555 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
556 FlowEdge flowEdge = (FlowEdge) iterator2.next();
558 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
559 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
561 if (incomingNode instanceof FlowReturnNode) {
562 FlowReturnNode rnode = (FlowReturnNode) incomingNode;
563 Set<NTuple<Descriptor>> nodeTupleSet = rnode.getTupleSet();
564 for (Iterator iterator3 = nodeTupleSet.iterator(); iterator3.hasNext();) {
565 NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
566 FlowNode fn = getFlowNode(nodeTuple);
567 if (!visited.contains(fn)) {
569 getIncomingFlowNodeSet(fn, visited);
573 if (!visited.contains(incomingNode)) {
574 visited.add(incomingNode);
575 getIncomingFlowNodeSet(incomingNode, visited);
585 public boolean isParameter(NTuple<Descriptor> tuple) {
586 // return true if a descriptor tuple is started with a parameter descriptor
587 Descriptor firstIdxDesc = tuple.get(0);
588 return mapParamDescToIdx.containsKey(firstIdxDesc);
591 public int getParamIdx(NTuple<Descriptor> tuple) {
592 Descriptor firstIdxDesc = tuple.get(0);
593 return mapParamDescToIdx.get(firstIdxDesc);
596 public FlowGraph clone() {
597 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
599 // clone.setNodeSet(getNodeSet());
600 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
601 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
602 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
604 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
605 clone.setReturnNodeSet(getReturnNodeSet());
606 clone.setThisVarNode(getThisVarNode());
611 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
612 return mapFlowNodeToLocTuple;
615 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
616 this.mapFlowNodeToLocTuple.putAll(in);
619 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
620 return mapFlowNodeToOutEdgeSet;
623 public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
625 Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
627 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
628 FlowNode curNode = (FlowNode) iterator.next();
629 Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
631 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
632 FlowEdge outEdge = (FlowEdge) iterator2.next();
634 if (outEdge.getEndTuple().startsWith(prefix)) {
635 incomingNodeSet.add(curNode);
636 recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
643 return incomingNodeSet;
647 private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
649 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
651 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
652 FlowEdge inEdge = (FlowEdge) iterator.next();
654 FlowNode inNode = getFlowNode(inEdge.getInitTuple());
655 if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
657 recurIncomingNodeSetByPrefix(prefix, inNode, visited);
663 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
664 Set<FlowNode> keySet = inMap.keySet();
665 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
666 FlowNode key = (FlowNode) iterator.next();
667 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
668 newEdgeSet.addAll(inMap.get(key));
669 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
673 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
674 Set<FlowEdge> addedEdgeSet) throws IOException {
676 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
678 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
679 FlowEdge flowEdge = iterator.next();
681 FlowNode u = flowEdge.getSrc();
682 FlowNode v = flowEdge.getDst();
684 if (u.getDescTuple().equals(flowEdge.getInitTuple())
685 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
686 // only draw an edge of the actual value flow
688 if (!addedEdgeSet.contains(flowEdge)) {
690 if (!addedNodeSet.contains(u)) {
694 if (!addedNodeSet.contains(v)) {
699 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
700 addedEdgeSet.add(flowEdge);
708 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
709 if (node instanceof FlowReturnNode) {
710 FlowReturnNode rnode = (FlowReturnNode) node;
711 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
713 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
718 public void writeGraph() throws java.io.IOException {
722 public void writeGraph(String suffix) throws java.io.IOException {
724 String graphName = "flowgraph_" + md.toString() + suffix;
725 graphName = graphName.replaceAll("[\\W]", "");
727 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
728 bw.write("digraph " + graphName + " {\n");
729 bw.write("compound=true;\n");
731 // then visit every flow node
733 // Iterator<FlowNode> iter = nodeSet.iterator();
734 Iterator<FlowNode> iter = getNodeSet().iterator();
736 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
737 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
739 while (iter.hasNext()) {
740 FlowNode node = iter.next();
742 if (node.getDescTuple().size() == 1) {
743 // here, we just care about the local variable
744 if (node.getFieldNodeSet().size() > 0) {
745 drawSubgraph(node, bw, addedEdgeSet);
748 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
757 public boolean constainsNode(FlowNode node) {
758 return getNodeSet().contains(node);
761 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
764 bw.write("subgraph cluster_" + node.getID() + "{\n");
765 bw.write("label=\"" + node.getPrettyID() + "\";\n");
767 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
768 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
769 FlowNode fieldNode = (FlowNode) iterator.next();
770 if (fieldNode.getFieldNodeSet().size() > 0) {
771 drawSubgraph(fieldNode, bw, addedSet);
773 Descriptor desc = fieldNode.getDescTuple().getLastElement();
774 if (desc instanceof VarDescriptor) {
775 VarDescriptor varDesc = (VarDescriptor) desc;
776 if (varDesc.getType().isPrimitive()) {
777 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
779 } else if (desc instanceof FieldDescriptor) {
780 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
781 if (fieldDesc.getType().isPrimitive()) {
782 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");