have a debug flag
[IRC.git] / Robust / src / Analysis / SSJava / BuildLattice.java
index fd2d0e8f6114fb1c787ba18a2ad1f11aa54582b2..754db0887ee07c6c214dbf7cc2c84c3c845b2b49 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
+import IR.ClassDescriptor;
 import IR.Descriptor;
 import IR.MethodDescriptor;
 import IR.NameDescriptor;
@@ -17,6 +18,8 @@ public class BuildLattice {
   private Map<HNode, TripleItem> mapSharedNodeToTripleItem;
   private Map<HNode, Integer> mapHNodeToHighestIndex;
 
+  private Map<Descriptor, Map<TripleItem, String>> mapDescToIntermediateLocMap;
+
   private Map<Pair<HNode, HNode>, Integer> mapItemToHighestIndex;
 
   public BuildLattice(LocationInference infer) {
@@ -24,7 +27,7 @@ public class BuildLattice {
     this.mapSharedNodeToTripleItem = new HashMap<HNode, TripleItem>();
     this.mapHNodeToHighestIndex = new HashMap<HNode, Integer>();
     this.mapItemToHighestIndex = new HashMap<Pair<HNode, HNode>, Integer>();
-
+    this.mapDescToIntermediateLocMap = new HashMap<Descriptor, Map<TripleItem, String>>();
   }
 
   public SSJavaLattice<String> buildLattice(Descriptor desc) {
@@ -32,6 +35,10 @@ public class BuildLattice {
     HierarchyGraph inputGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
     LocationSummary locSummary = infer.getLocationSummary(desc);
 
+    HierarchyGraph naiveGraph = infer.getSimpleHierarchyGraph(desc);
+
+    // I don't think we need to keep the below if statement anymore
+    // because hierarchy graph does not have any composite location
     Set<HNode> nodeSetWithCompositeLocation = new HashSet<HNode>();
     if (desc instanceof MethodDescriptor) {
       FlowGraph flowGraph = infer.getFlowGraph((MethodDescriptor) desc);
@@ -55,8 +62,27 @@ public class BuildLattice {
 
     }
 
+    // /////////////////////////////////////////////////////////////////////////////////////
+    // lattice generation for the native approach
+
+    if (infer.state.SSJAVA_INFER_NAIVE_WRITEDOTS) {
+      BasisSet naiveBasisSet = naiveGraph.computeBasisSet(nodeSetWithCompositeLocation);
+
+      Family naiveFamily = generateFamily(naiveBasisSet);
+      Map<Set<Integer>, Set<Set<Integer>>> naive_mapImSucc =
+          coveringGraph(naiveBasisSet, naiveFamily);
+
+      SSJavaLattice<String> naive_lattice =
+          buildLattice(desc, naiveBasisSet, naiveGraph, null, naive_mapImSucc);
+      LocationInference.numLocationsNaive += naive_lattice.getKeySet().size();
+      infer.addNaiveLattice(desc, naive_lattice);
+    }
+
+    // /////////////////////////////////////////////////////////////////////////////////////
+
+    // lattice generation for the proposed approach
     BasisSet basisSet = inputGraph.computeBasisSet(nodeSetWithCompositeLocation);
-    debug_print(inputGraph);
+    // debug_print(inputGraph);
 
     Family family = generateFamily(basisSet);
     Map<Set<Integer>, Set<Set<Integer>>> mapImSucc = coveringGraph(basisSet, family);
@@ -66,10 +92,33 @@ public class BuildLattice {
 
   }
 
+  public void setIntermediateLocMap(Descriptor desc, Map<TripleItem, String> map) {
+    mapDescToIntermediateLocMap.put(desc, map);
+  }
+
+  public Map<TripleItem, String> getIntermediateLocMap(Descriptor desc) {
+    if (!mapDescToIntermediateLocMap.containsKey(desc)) {
+      mapDescToIntermediateLocMap.put(desc, new HashMap<TripleItem, String>());
+    }
+    return mapDescToIntermediateLocMap.get(desc);
+  }
+
+  private Descriptor getParent(Descriptor desc) {
+    if (desc instanceof MethodDescriptor) {
+      MethodDescriptor md = (MethodDescriptor) desc;
+      ClassDescriptor cd = md.getClassDesc();
+      return infer.getParentMethodDesc(cd, md);
+    } else {
+      return ((ClassDescriptor) desc).getSuperDesc();
+    }
+  }
+
   private SSJavaLattice<String> buildLattice(Descriptor desc, BasisSet basisSet,
       HierarchyGraph inputGraph, LocationSummary locSummary,
       Map<Set<Integer>, Set<Set<Integer>>> mapImSucc) {
 
+    System.out.println("\nBuild Lattice:" + inputGraph.getName());
+
     SSJavaLattice<String> lattice =
         new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
 
@@ -95,10 +144,14 @@ public class BuildLattice {
       Set<Descriptor> descSet = inputGraph.getDescSetOfNode(higherNode);
       // System.out.println("higherName=" + higherName + "  higherNode=" + higherNode + "  descSet="
       // + descSet);
-      for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
-        Descriptor d = (Descriptor) iterator2.next();
-        locSummary.addMapHNodeNameToLocationName(d.getSymbol(), higherName);
+
+      if (locSummary != null) {
+        for (Iterator iterator2 = descSet.iterator(); iterator2.hasNext();) {
+          Descriptor d = (Descriptor) iterator2.next();
+          locSummary.addMapHNodeNameToLocationName(d.getSymbol(), higherName);
+        }
       }
+
       // locSummary.addMapHNodeNameToLocationName(higherName, higherName);
 
       Set<Set<Integer>> lowerSet = mapImSucc.get(higher);
@@ -125,9 +178,11 @@ public class BuildLattice {
         Set<Descriptor> lowerDescSet = inputGraph.getDescSetOfNode(lowerNode);
         // System.out.println("lowerName=" + lowerName + "  lowerNode=" + lowerNode + "  descSet="
         // + lowerDescSet);
-        for (Iterator iterator3 = lowerDescSet.iterator(); iterator3.hasNext();) {
-          Descriptor d = (Descriptor) iterator3.next();
-          locSummary.addMapHNodeNameToLocationName(d.getSymbol(), lowerName);
+        if (locSummary != null) {
+          for (Iterator iterator3 = lowerDescSet.iterator(); iterator3.hasNext();) {
+            Descriptor d = (Descriptor) iterator3.next();
+            locSummary.addMapHNodeNameToLocationName(d.getSymbol(), lowerName);
+          }
         }
         // locSummary.addMapHNodeNameToLocationName(lowerName, lowerName);
 
@@ -169,6 +224,32 @@ public class BuildLattice {
 
   public SSJavaLattice<String> insertIntermediateNodesToStraightLine(Descriptor desc,
       SSJavaLattice<String> skeletonLattice) {
+    // copy nodes/edges from the parent method/class if possible
+    SSJavaLattice<String> lattice = skeletonLattice.clone();
+
+    Descriptor parentDesc = getParent(desc);
+    if (parentDesc != null) {
+      SSJavaLattice<String> parentLattice = infer.getLattice(parentDesc);
+
+      Map<String, Set<String>> parentMap = parentLattice.getTable();
+      Set<String> parentKeySet = parentMap.keySet();
+      for (Iterator iterator = parentKeySet.iterator(); iterator.hasNext();) {
+        String parentKey = (String) iterator.next();
+        Set<String> parentValueSet = parentMap.get(parentKey);
+        for (Iterator iterator2 = parentValueSet.iterator(); iterator2.hasNext();) {
+          String value = (String) iterator2.next();
+          lattice.put(parentKey, value);
+        }
+      }
+
+      Set<String> parentSharedLocSet = parentLattice.getSharedLocSet();
+      for (Iterator iterator = parentSharedLocSet.iterator(); iterator.hasNext();) {
+        String parentSharedLoc = (String) iterator.next();
+        lattice.addSharedLoc(parentSharedLoc);
+      }
+    }
+
+    // ////
 
     // perform DFS that starts from each skeleton/combination node and ends by another
     // skeleton/combination node
@@ -179,13 +260,12 @@ public class BuildLattice {
     HierarchyGraph scGraph = infer.getSkeletonCombinationHierarchyGraph(desc);
     LocationSummary locSummary = infer.getLocationSummary(desc);
 
-    SSJavaLattice<String> lattice = skeletonLattice.clone();
-
     Set<HNode> visited = new HashSet<HNode>();
 
     Set<HNode> nodeSet = simpleGraph.getNodeSet();
 
-    Map<TripleItem, String> mapIntermediateLoc = new HashMap<TripleItem, String>();
+    Map<TripleItem, String> mapIntermediateLoc = getIntermediateLocMap(desc);
+    // Map<TripleItem, String> mapIntermediateLoc = new HashMap<TripleItem, String>();
 
     // System.out.println("*insert=" + desc);
     // System.out.println("***nodeSet=" + nodeSet);
@@ -293,7 +373,7 @@ public class BuildLattice {
       HNode sharedNode = (HNode) iterator.next();
       TripleItem item = mapSharedNodeToTripleItem.get(sharedNode);
       String nonSharedLocName = mapIntermediateLoc.get(item);
-      
+
       System.out.println("sharedNode=" + sharedNode + "    locName=" + nonSharedLocName);
 
       String newLocName;
@@ -572,6 +652,7 @@ public class BuildLattice {
 
     if (curNode.isSharedNode()) {
       // if the current node is shared location, add a shared location to the lattice later
+      System.out.println("###SHARED ITEM=" + item);
       mapSharedNodeToTripleItem.put(curNode, item);
     } else {
       Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
@@ -664,6 +745,7 @@ public class BuildLattice {
     String locName = mapIntermediateLoc.get(item);
     if (curNode.isSharedNode()) {
       // if the current node is shared location, add a shared location to the lattice later
+      System.out.println("###SHARED ITEM=" + item);
       mapSharedNodeToTripleItem.put(curNode, item);
     } else {
       Set<Descriptor> descSet = simpleHierarchyGraph.getDescSetOfNode(curNode);
@@ -915,8 +997,8 @@ public class BuildLattice {
 
   private void debug_print(HierarchyGraph inputGraph) {
     System.out.println("\nBuild Lattice:" + inputGraph.getName());
-    // System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
-    // System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
+    System.out.println("Node2Index:\n" + inputGraph.getMapHNodeToUniqueIndex());
+    System.out.println("Node2Basis:\n" + inputGraph.getMapHNodeToBasis());
   }
 
 }