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.VarDescriptor;
18 public class FlowGraph {
22 Set<FlowNode> returnNodeSet;
25 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
26 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
28 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
29 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
31 // maps the composite representation of field/var descriptors to infer nodes
32 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
34 // maps a paramter descriptor to its index
35 Map<Descriptor, Integer> mapParamDescToIdx;
37 // DS for the lattice generation
38 Map<Integer, FlowNode> mapIdxToFlowNode;
40 public static int interseed = 0;
44 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
46 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
47 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
48 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
49 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
50 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
51 this.returnNodeSet = new HashSet<FlowNode>();
52 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
53 this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
54 this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
57 // create a node for 'this' varialbe
58 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
59 // thisDescTuple.add(md.getThis());
61 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
62 thisVarTuple.add(md.getThis());
63 FlowNode thisNode = createNewFlowNode(thisVarTuple);
64 thisNode.setSkeleton(true);
65 thisVarNode = thisNode;
72 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
73 return mapDescTupleToInferNode;
76 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
77 this.mapDescTupleToInferNode.putAll(in);
80 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
81 return mapLocTupleToFlowNode;
84 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
85 this.mapLocTupleToFlowNode.putAll(in);
88 public void setReturnNodeSet(Set<FlowNode> in) {
89 this.returnNodeSet.addAll(in);
92 public void setThisVarNode(FlowNode thisVarNode) {
93 this.thisVarNode = thisVarNode;
96 public Map<Descriptor, Integer> getMapParamDescToIdx() {
97 return mapParamDescToIdx;
100 public FlowNode createIntermediateNode() {
101 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
102 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
103 tuple.add(interDesc);
106 FlowNode newNode = new FlowNode(tuple);
107 newNode.setIntermediate(true);
109 mapDescTupleToInferNode.put(tuple, newNode);
110 // nodeSet.add(newNode);
112 System.out.println("create new intermediate node= " + newNode);
117 private void setupMapIdxToDesc() {
119 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
120 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
121 Descriptor paramDesc = (Descriptor) iterator.next();
122 int idx = mapParamDescToIdx.get(paramDesc);
123 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
124 descTuple.add(paramDesc);
125 FlowNode paramNode = getFlowNode(descTuple);
126 mapIdxToFlowNode.put(idx, paramNode);
127 paramNode.setSkeleton(true);
132 public int getNumParameters() {
133 return mapIdxToFlowNode.keySet().size();
136 public FlowNode getParamFlowNode(int idx) {
137 return mapIdxToFlowNode.get(idx);
140 public Set<FlowEdge> getEdgeSet() {
141 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
143 Set<FlowNode> nodeSet = getNodeSet();
144 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
145 FlowNode flowNode = (FlowNode) iterator.next();
146 edgeSet.addAll(getOutEdgeSet(flowNode));
152 public Set<FlowNode> getNodeSet() {
153 Set<FlowNode> set = new HashSet<FlowNode>();
154 set.addAll(mapDescTupleToInferNode.values());
158 public MethodDescriptor getMethodDescriptor() {
162 public boolean isParamDesc(Descriptor desc) {
164 if (mapParamDescToIdx.containsKey(desc)) {
165 int idx = mapParamDescToIdx.get(desc);
166 if (!md.isStatic() && idx == 0) {
175 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
177 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
178 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
180 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
181 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
182 FlowEdge flowEdge = (FlowEdge) iterator.next();
183 if (flowEdge.getDst().equals(toNode)) {
186 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
195 public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
197 Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
199 // returns the set of edges that share the same prefix of startNode
200 Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
202 for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
203 Set<FlowEdge> nodeEdgeSet = iter.next();
204 for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
205 FlowEdge edge = iter2.next();
206 if (edge.getInitTuple().get(0).equals(prefixDesc)) {
215 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
216 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
217 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
219 return mapFlowNodeToOutEdgeSet.get(node);
222 public Set<FlowEdge> getInEdgeSet(FlowNode node) {
223 if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
224 mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
226 return mapFlowNodeToInEdgeSet.get(node);
229 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
231 FlowNode fromNode = getFlowNode(fromDescTuple);
232 FlowNode toNode = getFlowNode(toDescTuple);
234 // System.out.println("create an edge from " + fromNode + " to " + toNode);
236 int fromTupleSize = fromDescTuple.size();
237 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
238 for (int i = 0; i < fromTupleSize; i++) {
239 Descriptor desc = fromDescTuple.get(i);
240 curFromTuple.add(desc);
241 int toTupleSize = toDescTuple.size();
242 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
243 for (int k = 0; k < toTupleSize; k++) {
244 Descriptor toDesc = toDescTuple.get(k);
245 curToTuple.add(toDesc);
246 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
250 // int fromTupleSize = fromDescTuple.size();
251 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
252 // for (int i = 0; i < fromTupleSize; i++) {
253 // Descriptor desc = fromDescTuple.get(i);
254 // curTuple.add(desc);
255 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
258 // int toTupleSize = toDescTuple.size();
259 // curTuple = new NTuple<Descriptor>();
260 // for (int i = 0; i < toTupleSize; i++) {
261 // Descriptor desc = toDescTuple.get(i);
262 // curTuple.add(desc);
263 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
268 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
269 NTuple<Descriptor> endTuple) {
271 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
272 addOutEdge(fromNode, edge);
273 addInEdge(toNode, edge);
275 // System.out.println("add a new edge=" + edge);
278 private void addInEdge(FlowNode toNode, FlowEdge edge) {
279 getInEdgeSet(toNode).add(edge);
282 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
283 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
284 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
286 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
289 public boolean contains(NTuple<Descriptor> descTuple) {
290 return mapDescTupleToInferNode.containsKey(descTuple);
293 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
294 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
295 FlowNode node = createNewFlowNode(descTuple);
296 mapDescTupleToInferNode.put(descTuple, node);
298 return mapDescTupleToInferNode.get(descTuple);
301 public FlowNode getThisVarNode() {
305 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
307 if (!mapDescTupleToInferNode.containsKey(tuple)) {
308 FlowNode node = new FlowNode(tuple);
309 mapDescTupleToInferNode.put(tuple, node);
310 // nodeSet.add(node);
312 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
314 if (tuple.size() > 1) {
315 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
316 getFlowNode(baseTuple).addFieldNode(node);
319 // System.out.println("Creating new node=" + node);
322 return mapDescTupleToInferNode.get(tuple);
327 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
329 if (!mapDescTupleToInferNode.containsKey(tuple)) {
330 createNewFlowNode(tuple);
333 FlowNode node = mapDescTupleToInferNode.get(tuple);
334 node.setReturn(true);
336 returnNodeSet.add(node);
339 public Set<FlowNode> getReturnNodeSet() {
340 return returnNodeSet;
343 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
344 Set<FlowNode> set = new HashSet<FlowNode>();
345 recurLocalReachFlowNodeSet(fn, set);
349 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
351 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
352 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
353 FlowEdge edge = (FlowEdge) iterator.next();
354 FlowNode dstNode = edge.getDst();
356 if (!visited.contains(dstNode)) {
357 visited.add(dstNode);
358 recurLocalReachFlowNodeSet(dstNode, visited);
364 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
366 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
367 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
368 FlowEdge edge = (FlowEdge) iterator.next();
370 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
371 FlowNode dstNode = getFlowNode(edge.getEndTuple());
372 if (!visited.contains(dstNode)) {
373 visited.add(dstNode);
374 getReachFlowNodeSetFrom(dstNode, visited);
381 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
382 Set<FlowNode> set = new HashSet<FlowNode>();
383 getReachFlowNodeSetFrom(fn, set);
387 public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
388 Set<FlowNode> reachableSet = new HashSet<FlowNode>();
390 Set<FlowNode> nodeSet = getNodeSet();
391 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
392 FlowNode flowNode = (FlowNode) iterator.next();
393 if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
394 recurReachableSetFrom(flowNode, reachableSet);
401 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
403 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
404 // iterator.hasNext();) {
405 // FlowEdge edge = (FlowEdge) iterator.next();
407 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
409 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
411 // if (!visited.contains(dstNode)) {
412 // visited.add(dstNode);
413 // getReachFlowNodeSetFrom(dstNode, visited);
420 private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
422 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
423 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
424 FlowEdge edge = (FlowEdge) iterator.next();
425 FlowNode dstNode = getFlowNode(edge.getEndTuple());
426 if (!reachableSet.contains(dstNode)) {
427 reachableSet.add(dstNode);
428 recurReachableSetFrom(dstNode, reachableSet);
434 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
436 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
438 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
439 FlowEdge edge = (FlowEdge) iterator.next();
441 if (fn.getDescTuple().equals(edge.getInitTuple())) {
442 FlowNode dstNode = getFlowNode(edge.getEndTuple());
443 NTuple<Location> dstTuple = getLocationTuple(dstNode);
445 if (!visited.contains(dstTuple)) {
446 visited.add(dstTuple);
447 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
455 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
456 return getLocationTuple(getFlowNode(descTuple));
459 public NTuple<Location> getLocationTuple(FlowNode fn) {
461 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
462 NTuple<Descriptor> descTuple = fn.getDescTuple();
463 NTuple<Location> locTuple = new NTuple<Location>();
464 ClassDescriptor cd = null;
466 Descriptor localDesc = fn.getDescTuple().get(0);
468 if (fn.isIntermediate()) {
469 Location interLoc = new Location(md, localDesc.getSymbol());
470 interLoc.setLocDescriptor(localDesc);
471 locTuple.add(interLoc);
472 } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
473 Location topLoc = new Location(md, Location.TOP);
474 topLoc.setLocDescriptor(LocationInference.TOPDESC);
475 locTuple.add(topLoc);
476 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
477 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
478 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
479 locTuple.add(globalLoc);
482 for (int i = 0; i < descTuple.size(); i++) {
483 Descriptor curDesc = descTuple.get(i);
486 loc = new Location(md, curDesc.getSymbol());
487 loc.setLocDescriptor(curDesc);
488 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
490 loc = new Location(cd, curDesc.getSymbol());
491 loc.setLocDescriptor(curDesc);
493 if (curDesc instanceof FieldDescriptor) {
494 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
496 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
504 mapFlowNodeToLocTuple.put(fn, locTuple);
506 return mapFlowNodeToLocTuple.get(fn);
509 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
510 Set<FlowNode> set = new HashSet<FlowNode>();
511 getIncomingFlowNodeSet(node, set);
515 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
517 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
518 FlowNode curNode = (FlowNode) iterator.next();
519 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
521 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
522 FlowEdge flowEdge = (FlowEdge) iterator2.next();
524 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
525 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
527 if (!visited.contains(incomingNode)) {
528 visited.add(incomingNode);
529 getIncomingFlowNodeSet(incomingNode, visited);
537 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
539 NTuple<Descriptor> dstTuple = fn.getDescTuple();
541 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
543 ClassDescriptor cd = null;
545 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
546 FlowNode node = (FlowNode) iterator.next();
548 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
549 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
550 FlowEdge flowEdge = (FlowEdge) iterator2.next();
551 if (dstTuple.equals(flowEdge.getEndTuple())) {
552 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
553 NTuple<Location> locTuple = new NTuple<Location>();
554 for (int i = 0; i < initTuple.size(); i++) {
555 Descriptor d = initTuple.get(i);
558 loc = new Location(md, d.getSymbol());
559 cd = ((VarDescriptor) d).getType().getClassDesc();
561 loc = new Location(cd, d.getSymbol());
562 cd = ((FieldDescriptor) d).getType().getClassDesc();
573 public boolean isParameter(NTuple<Descriptor> tuple) {
574 // return true if a descriptor tuple is started with a parameter descriptor
575 Descriptor firstIdxDesc = tuple.get(0);
576 return mapParamDescToIdx.containsKey(firstIdxDesc);
579 public int getParamIdx(NTuple<Descriptor> tuple) {
580 Descriptor firstIdxDesc = tuple.get(0);
581 return mapParamDescToIdx.get(firstIdxDesc);
584 public FlowGraph clone() {
585 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
587 // clone.setNodeSet(getNodeSet());
588 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
589 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
590 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
592 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
593 clone.setReturnNodeSet(getReturnNodeSet());
594 clone.setThisVarNode(getThisVarNode());
599 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
600 return mapFlowNodeToLocTuple;
603 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
604 this.mapFlowNodeToLocTuple.putAll(in);
607 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
608 return mapFlowNodeToOutEdgeSet;
611 public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
613 Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
615 for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
616 FlowNode curNode = (FlowNode) iterator.next();
617 Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
619 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
620 FlowEdge outEdge = (FlowEdge) iterator2.next();
622 if (outEdge.getEndTuple().startsWith(prefix)) {
623 incomingNodeSet.add(curNode);
624 recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
631 return incomingNodeSet;
635 private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
637 Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
639 for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
640 FlowEdge inEdge = (FlowEdge) iterator.next();
642 FlowNode inNode = getFlowNode(inEdge.getInitTuple());
643 if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
645 recurIncomingNodeSetByPrefix(prefix, inNode, visited);
651 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
652 Set<FlowNode> keySet = inMap.keySet();
653 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
654 FlowNode key = (FlowNode) iterator.next();
655 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
656 newEdgeSet.addAll(inMap.get(key));
657 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
661 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
662 Set<FlowEdge> addedEdgeSet) throws IOException {
664 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
666 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
667 FlowEdge flowEdge = iterator.next();
669 FlowNode u = flowEdge.getSrc();
670 FlowNode v = flowEdge.getDst();
672 if (u.getDescTuple().equals(flowEdge.getInitTuple())
673 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
674 // only draw an edge of the actual value flow
676 if (!addedEdgeSet.contains(flowEdge)) {
678 if (!addedNodeSet.contains(u)) {
682 if (!addedNodeSet.contains(v)) {
687 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
688 addedEdgeSet.add(flowEdge);
696 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
697 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
700 public void writeGraph() throws java.io.IOException {
704 public void writeGraph(String suffix) throws java.io.IOException {
706 String graphName = "flowgraph_" + md.toString() + suffix;
707 graphName = graphName.replaceAll("[\\W]", "");
709 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
710 bw.write("digraph " + graphName + " {\n");
711 bw.write("compound=true;\n");
713 // then visit every flow node
715 // Iterator<FlowNode> iter = nodeSet.iterator();
716 Iterator<FlowNode> iter = getNodeSet().iterator();
718 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
719 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
721 while (iter.hasNext()) {
722 FlowNode node = iter.next();
724 if (node.getDescTuple().size() == 1) {
725 // here, we just care about the local variable
726 if (node.getFieldNodeSet().size() > 0) {
727 drawSubgraph(node, bw, addedEdgeSet);
730 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
739 public boolean constainsNode(FlowNode node) {
740 return getNodeSet().contains(node);
743 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
746 bw.write("subgraph cluster_" + node.getID() + "{\n");
747 bw.write("label=\"" + node.getPrettyID() + "\";\n");
749 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
750 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
751 FlowNode fieldNode = (FlowNode) iterator.next();
752 if (fieldNode.getFieldNodeSet().size() > 0) {
753 drawSubgraph(fieldNode, bw, addedSet);
755 Descriptor desc = fieldNode.getDescTuple().getLastElement();
756 if (desc instanceof VarDescriptor) {
757 VarDescriptor varDesc = (VarDescriptor) desc;
758 if (varDesc.getType().isPrimitive()) {
759 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
761 } else if (desc instanceof FieldDescriptor) {
762 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
763 if (fieldDesc.getType().isPrimitive()) {
764 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");