addressing two problems in the composite location generation: 1) when a parameter...
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index e2783bf60ca44f66ec9506deff8756c9ab3f3e1c..feed5777c3278b1307dc93172ba33fcfb8665852 100644 (file)
@@ -13,7 +13,9 @@ import IR.ClassDescriptor;
 import IR.Descriptor;
 import IR.FieldDescriptor;
 import IR.MethodDescriptor;
+import IR.NameDescriptor;
 import IR.VarDescriptor;
+import IR.Tree.MethodInvokeNode;
 
 public class FlowGraph {
 
@@ -22,6 +24,7 @@ public class FlowGraph {
   Set<FlowNode> returnNodeSet;
   FlowNode thisVarNode;
 
+  Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
 
   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
@@ -36,6 +39,8 @@ public class FlowGraph {
   // DS for the lattice generation
   Map<Integer, FlowNode> mapIdxToFlowNode;
 
+  Map<MethodInvokeNode, FlowReturnNode> mapMethodInvokeNodeToFlowReturnNode;
+
   public static int interseed = 0;
 
   boolean debug = true;
@@ -50,6 +55,8 @@ public class FlowGraph {
     this.returnNodeSet = new HashSet<FlowNode>();
     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+    this.mapFlowNodeToInEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
+    this.mapMethodInvokeNodeToFlowReturnNode = new HashMap<MethodInvokeNode, FlowReturnNode>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
@@ -112,6 +119,25 @@ public class FlowGraph {
     return newNode;
   }
 
+  public FlowReturnNode getFlowReturnNode(MethodInvokeNode min) {
+    return mapMethodInvokeNodeToFlowReturnNode.get(min);
+  }
+
+  public FlowReturnNode createReturnNode(MethodInvokeNode min) {
+    NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+    NameDescriptor n = new NameDescriptor("RETURNLOC" + (LocationInference.locSeed++));
+    tuple.add(n);
+
+    FlowReturnNode newNode = new FlowReturnNode(tuple, min);
+    mapDescTupleToInferNode.put(tuple, newNode);
+    mapMethodInvokeNodeToFlowReturnNode.put(min, newNode);
+    // nodeSet.add(newNode);
+
+    System.out.println("create new set node= " + newNode);
+
+    return newNode;
+  }
+
   private void setupMapIdxToDesc() {
 
     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
@@ -217,11 +243,22 @@ public class FlowGraph {
     return mapFlowNodeToOutEdgeSet.get(node);
   }
 
+  public Set<FlowEdge> getInEdgeSet(FlowNode node) {
+    if (!mapFlowNodeToInEdgeSet.containsKey(node)) {
+      mapFlowNodeToInEdgeSet.put(node, new HashSet<FlowEdge>());
+    }
+    return mapFlowNodeToInEdgeSet.get(node);
+  }
+
   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
 
     FlowNode fromNode = getFlowNode(fromDescTuple);
     FlowNode toNode = getFlowNode(toDescTuple);
 
+    if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
+      return;
+    }
+
     // System.out.println("create an edge from " + fromNode + " to " + toNode);
 
     int fromTupleSize = fromDescTuple.size();
@@ -261,10 +298,15 @@ public class FlowGraph {
 
     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
     addOutEdge(fromNode, edge);
+    addInEdge(toNode, edge);
 
     // System.out.println("add a new edge=" + edge);
   }
 
+  private void addInEdge(FlowNode toNode, FlowEdge edge) {
+    getInEdgeSet(toNode).add(edge);
+  }
+
   private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
     if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
       mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
@@ -272,6 +314,10 @@ public class FlowGraph {
     mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
   }
 
+  public boolean contains(NTuple<Descriptor> descTuple) {
+    return mapDescTupleToInferNode.containsKey(descTuple);
+  }
+
   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
     if (!mapDescTupleToInferNode.containsKey(descTuple)) {
       FlowNode node = createNewFlowNode(descTuple);
@@ -313,8 +359,6 @@ public class FlowGraph {
     }
 
     FlowNode node = mapDescTupleToInferNode.get(tuple);
-    node.setReturn(true);
-
     returnNodeSet.add(node);
   }
 
@@ -451,7 +495,7 @@ public class FlowGraph {
         Location interLoc = new Location(md, localDesc.getSymbol());
         interLoc.setLocDescriptor(localDesc);
         locTuple.add(interLoc);
-      } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
+      } else if (localDesc.getSymbol().equals(SSJavaAnalysis.TOP)) {
         Location topLoc = new Location(md, Location.TOP);
         topLoc.setLocDescriptor(LocationInference.TOPDESC);
         locTuple.add(topLoc);
@@ -467,15 +511,23 @@ public class FlowGraph {
           if (i == 0) {
             loc = new Location(md, curDesc.getSymbol());
             loc.setLocDescriptor(curDesc);
-            cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            if (curDesc instanceof VarDescriptor) {
+              cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+            } else {
+              // otherwise it should be the last element
+              cd = null;
+            }
           } else {
             loc = new Location(cd, curDesc.getSymbol());
             loc.setLocDescriptor(curDesc);
 
             if (curDesc instanceof FieldDescriptor) {
               cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
-            } else {
+            } else if (curDesc instanceof LocationDescriptor) {
               cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
+            } else {
+              // otherwise it should be the last element of the tuple
+              cd = null;
             }
 
           }
@@ -506,50 +558,28 @@ public class FlowGraph {
         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
 
-          if (!visited.contains(incomingNode)) {
-            visited.add(incomingNode);
-            getIncomingFlowNodeSet(incomingNode, visited);
-          }
-        }
-      }
-    }
-
-  }
-
-  public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
-
-    NTuple<Descriptor> dstTuple = fn.getDescTuple();
-
-    Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
-
-    ClassDescriptor cd = null;
-
-    for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
-      FlowNode node = (FlowNode) iterator.next();
-
-      Set<FlowEdge> edgeSet = getOutEdgeSet(node);
-      for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
-        FlowEdge flowEdge = (FlowEdge) iterator2.next();
-        if (dstTuple.equals(flowEdge.getEndTuple())) {
-          NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
-          NTuple<Location> locTuple = new NTuple<Location>();
-          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();
+          if (incomingNode instanceof FlowReturnNode) {
+            FlowReturnNode rnode = (FlowReturnNode) incomingNode;
+            Set<NTuple<Descriptor>> nodeTupleSet = rnode.getTupleSet();
+            for (Iterator iterator3 = nodeTupleSet.iterator(); iterator3.hasNext();) {
+              NTuple<Descriptor> nodeTuple = (NTuple<Descriptor>) iterator3.next();
+              FlowNode fn = getFlowNode(nodeTuple);
+              if (!visited.contains(fn)) {
+                visited.add(fn);
+                getIncomingFlowNodeSet(fn, visited);
+              }
+            }
+          } else {
+            if (!visited.contains(incomingNode)) {
+              visited.add(incomingNode);
+              getIncomingFlowNodeSet(incomingNode, visited);
             }
-            locTuple.add(loc);
           }
-          set.add(locTuple);
+
         }
       }
     }
-    return set;
+
   }
 
   public boolean isParameter(NTuple<Descriptor> tuple) {
@@ -590,6 +620,46 @@ public class FlowGraph {
     return mapFlowNodeToOutEdgeSet;
   }
 
+  public Set<FlowNode> getIncomingNodeSetByPrefix(Descriptor prefix) {
+
+    Set<FlowNode> incomingNodeSet = new HashSet<FlowNode>();
+
+    for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
+      FlowNode curNode = (FlowNode) iterator.next();
+      Set<FlowEdge> outEdgeSet = getOutEdgeSet(curNode);
+
+      for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
+        FlowEdge outEdge = (FlowEdge) iterator2.next();
+
+        if (outEdge.getEndTuple().startsWith(prefix)) {
+          incomingNodeSet.add(curNode);
+          recurIncomingNodeSetByPrefix(prefix, curNode, incomingNodeSet);
+
+        }
+
+      }
+    }
+
+    return incomingNodeSet;
+
+  }
+
+  private void recurIncomingNodeSetByPrefix(Descriptor prefix, FlowNode node, Set<FlowNode> visited) {
+
+    Set<FlowEdge> inEdgeSet = getInEdgeSet(node);
+
+    for (Iterator iterator = inEdgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge inEdge = (FlowEdge) iterator.next();
+
+      FlowNode inNode = getFlowNode(inEdge.getInitTuple());
+      if (!inEdge.getInitTuple().startsWith(prefix) && !visited.contains(inNode)) {
+        visited.add(inNode);
+        recurIncomingNodeSetByPrefix(prefix, inNode, visited);
+      }
+    }
+
+  }
+
   public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
     Set<FlowNode> keySet = inMap.keySet();
     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
@@ -636,7 +706,13 @@ public class FlowGraph {
   }
 
   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
-    bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    if (node instanceof FlowReturnNode) {
+      FlowReturnNode rnode = (FlowReturnNode) node;
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    } else {
+      bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
+    }
+
   }
 
   public void writeGraph() throws java.io.IOException {
@@ -711,4 +787,5 @@ public class FlowGraph {
 
     bw.write("}\n");
   }
+
 }
\ No newline at end of file