changes on the composite location generation (but still it doesn't work) & found...
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
index e81fe7d073b93b29fe094b4f6313b353ec9d724f..2ab6dad9ce625cac5d96cda64b5d4f1f989c8b6a 100644 (file)
@@ -22,6 +22,7 @@ public class FlowGraph {
   Set<FlowNode> returnNodeSet;
   FlowNode thisVarNode;
 
+  Map<FlowNode, Set<FlowEdge>> mapFlowNodeToInEdgeSet;
   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
 
   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
@@ -50,6 +51,7 @@ 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>>();
 
     if (!md.isStatic()) {
       // create a node for 'this' varialbe
@@ -95,24 +97,19 @@ public class FlowGraph {
     return mapParamDescToIdx;
   }
 
-  public FlowNode createIntermediateNode(MethodDescriptor md) {
-
+  public FlowNode createIntermediateNode() {
     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
     Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
     tuple.add(interDesc);
     interseed++;
 
-    NTuple<Location> locTuple = new NTuple<Location>();
-    Location interLoc = new Location(md, interDesc);
-    locTuple.add(interLoc);
-
-    FlowNode newNode = new FlowNode(locTuple);
+    FlowNode newNode = new FlowNode(tuple);
     newNode.setIntermediate(true);
 
     mapDescTupleToInferNode.put(tuple, newNode);
     // nodeSet.add(newNode);
 
-    System.out.println("create new intermediate node= " + newNode);
+     System.out.println("create new intermediate node= " + newNode);
 
     return newNode;
   }
@@ -140,6 +137,18 @@ public class FlowGraph {
     return mapIdxToFlowNode.get(idx);
   }
 
+  public Set<FlowEdge> getEdgeSet() {
+    Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
+
+    Set<FlowNode> nodeSet = getNodeSet();
+    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+      FlowNode flowNode = (FlowNode) iterator.next();
+      edgeSet.addAll(getOutEdgeSet(flowNode));
+    }
+
+    return edgeSet;
+  }
+
   public Set<FlowNode> getNodeSet() {
     Set<FlowNode> set = new HashSet<FlowNode>();
     set.addAll(mapDescTupleToInferNode.values());
@@ -210,12 +219,23 @@ 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);
 
-    // System.out.println("create an edge from " + fromNode + " to " + toNode);
+    if (toNode.getDescTuple().get(0).equals(LocationInference.LITERALDESC)) {
+      return;
+    }
+
+    System.out.println("create an edge from " + fromNode + " to " + toNode);
 
     int fromTupleSize = fromDescTuple.size();
     NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
@@ -254,10 +274,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>());
@@ -265,6 +290,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);
@@ -279,10 +308,8 @@ public class FlowGraph {
 
   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
 
-    NTuple<Location> locTuple = translateToLocationTuple(tuple);
     if (!mapDescTupleToInferNode.containsKey(tuple)) {
-
-      FlowNode node = new FlowNode(locTuple);
+      FlowNode node = new FlowNode(tuple);
       mapDescTupleToInferNode.put(tuple, node);
       // nodeSet.add(node);
 
@@ -308,8 +335,6 @@ public class FlowGraph {
     }
 
     FlowNode node = mapDescTupleToInferNode.get(tuple);
-    node.setReturn(true);
-
     returnNodeSet.add(node);
   }
 
@@ -361,6 +386,20 @@ public class FlowGraph {
     return set;
   }
 
+  public Set<FlowNode> getReachableSetFrom(NTuple<Descriptor> prefix) {
+    Set<FlowNode> reachableSet = new HashSet<FlowNode>();
+
+    Set<FlowNode> nodeSet = getNodeSet();
+    for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+      FlowNode flowNode = (FlowNode) iterator.next();
+      if (flowNode.getCurrentDescTuple().startsWith(prefix)) {
+        recurReachableSetFrom(flowNode, reachableSet);
+      }
+    }
+
+    return reachableSet;
+  }
+
   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
   //
   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
@@ -380,6 +419,20 @@ public class FlowGraph {
   //
   // }
 
+  private void recurReachableSetFrom(FlowNode curNode, Set<FlowNode> reachableSet) {
+
+    Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
+    for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
+      FlowEdge edge = (FlowEdge) iterator.next();
+      FlowNode dstNode = getFlowNode(edge.getEndTuple());
+      if (!reachableSet.contains(dstNode)) {
+        reachableSet.add(dstNode);
+        recurReachableSetFrom(dstNode, reachableSet);
+      }
+    }
+
+  }
+
   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
 
     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
@@ -387,7 +440,7 @@ public class FlowGraph {
     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
       FlowEdge edge = (FlowEdge) iterator.next();
 
-      if (fn.getLocTuple().equals(edge.getInitTuple())) {
+      if (fn.getDescTuple().equals(edge.getInitTuple())) {
         FlowNode dstNode = getFlowNode(edge.getEndTuple());
         NTuple<Location> dstTuple = getLocationTuple(dstNode);
 
@@ -405,51 +458,6 @@ public class FlowGraph {
     return getLocationTuple(getFlowNode(descTuple));
   }
 
-  public NTuple<Location> translateToLocationTuple(NTuple<Descriptor> descTuple) {
-
-    NTuple<Location> locTuple = new NTuple<Location>();
-    ClassDescriptor cd = null;
-
-    Descriptor localDesc = descTuple.get(0);
-
-    if (localDesc instanceof InterDescriptor) {
-      Location interLoc = new Location(md, localDesc);
-      locTuple.add(interLoc);
-    } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
-      Location topLoc = new Location(md, Location.TOP);
-      topLoc.setLocDescriptor(LocationInference.TOPDESC);
-      locTuple.add(topLoc);
-    } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
-      Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
-      globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
-      locTuple.add(globalLoc);
-    } else {
-      // normal case
-      for (int i = 0; i < descTuple.size(); i++) {
-        Descriptor curDesc = descTuple.get(i);
-        Location loc;
-        if (i == 0) {
-          loc = new Location(md, curDesc.getSymbol());
-          loc.setLocDescriptor(curDesc);
-          cd = ((VarDescriptor) curDesc).getType().getClassDesc();
-        } else {
-          loc = new Location(cd, curDesc.getSymbol());
-          loc.setLocDescriptor(curDesc);
-
-          if (curDesc instanceof FieldDescriptor) {
-            cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
-          } else {
-            cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
-          }
-
-        }
-        locTuple.add(loc);
-      }
-    }
-
-    return locTuple;
-  }
-
   public NTuple<Location> getLocationTuple(FlowNode fn) {
 
     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
@@ -463,7 +471,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);
@@ -479,15 +487,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;
             }
 
           }
@@ -602,6 +618,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();) {
@@ -623,8 +679,8 @@ public class FlowGraph {
       FlowNode u = flowEdge.getSrc();
       FlowNode v = flowEdge.getDst();
 
-      if (u.getLocTuple().equals(flowEdge.getInitTuple())
-          && v.getLocTuple().equals(flowEdge.getEndTuple())) {
+      if (u.getDescTuple().equals(flowEdge.getInitTuple())
+          && v.getDescTuple().equals(flowEdge.getEndTuple())) {
         // only draw an edge of the actual value flow
 
         if (!addedEdgeSet.contains(flowEdge)) {
@@ -675,7 +731,7 @@ public class FlowGraph {
     while (iter.hasNext()) {
       FlowNode node = iter.next();
 
-      if (node.getLocTuple().size() == 1) {
+      if (node.getDescTuple().size() == 1) {
         // here, we just care about the local variable
         if (node.getFieldNodeSet().size() > 0) {
           drawSubgraph(node, bw, addedEdgeSet);
@@ -723,4 +779,5 @@ public class FlowGraph {
 
     bw.write("}\n");
   }
+
 }
\ No newline at end of file