1 19 20 23 24 28 package soot.dava.toolkits.base.AST.traversals; 29 30 import soot.*; 31 import java.util.*; 32 import soot.jimple.*; 33 import soot.dava.internal.javaRep.*; 34 import soot.dava.internal.AST.*; 35 import soot.dava.internal.SET.*; 36 import soot.dava.toolkits.base.AST.analysis.*; 37 38 39 56 public class ClosestAbruptTargetFinder extends DepthFirstAdapter{ 57 58 public ClosestAbruptTargetFinder( Singletons.Global g ) {} 59 public static ClosestAbruptTargetFinder v() { return G.v().soot_dava_toolkits_base_AST_traversals_ClosestAbruptTargetFinder(); } 60 61 62 63 HashMap closestNode = new HashMap(); ArrayList nodeStack = new ArrayList(); 66 70 public ASTNode getTarget(DAbruptStmt ab){ 71 Object node = closestNode.get(ab); 72 if(node!=null) 73 return (ASTNode)node; 74 else 75 throw new RuntimeException ("Unable to find target for AbruptStmt"); 76 } 77 78 79 83 84 public void inASTWhileNode(ASTWhileNode node){ 85 nodeStack.add(node); 86 } 87 public void inASTDoWhileNode(ASTDoWhileNode node){ 88 nodeStack.add(node); 89 } 90 public void inASTUnconditionalLoopNode(ASTUnconditionalLoopNode node){ 91 nodeStack.add(node); 92 } 93 public void inASTForLoopNode(ASTForLoopNode node){ 94 nodeStack.add(node); 95 } 96 public void inASTSwitchNode(ASTSwitchNode node){ 97 nodeStack.add(node); 98 } 99 100 101 105 106 public void outASTWhileNode(ASTWhileNode node){ 107 if(nodeStack.isEmpty()) 108 throw new RuntimeException ("trying to remove node from empty stack: ClosestBreakTargetFinder"); 109 nodeStack.remove(nodeStack.size()-1); 110 } 111 public void outASTDoWhileNode(ASTDoWhileNode node){ 112 if(nodeStack.isEmpty()) 113 throw new RuntimeException ("trying to remove node from empty stack: ClosestBreakTargetFinder"); 114 nodeStack.remove(nodeStack.size()-1); 115 } 116 public void outASTUnconditionalLoopNode(ASTUnconditionalLoopNode node){ 117 if(nodeStack.isEmpty()) 118 throw new RuntimeException ("trying to remove node from empty stack: ClosestBreakTargetFinder"); 119 nodeStack.remove(nodeStack.size()-1); 120 } 121 public void outASTForLoopNode(ASTForLoopNode node){ 122 if(nodeStack.isEmpty()) 123 throw new RuntimeException ("trying to remove node from empty stack: ClosestBreakTargetFinder"); 124 nodeStack.remove(nodeStack.size()-1); 125 } 126 public void outASTSwitchNode(ASTSwitchNode node){ 127 if(nodeStack.isEmpty()) 128 throw new RuntimeException ("trying to remove node from empty stack: ClosestBreakTargetFinder"); 129 nodeStack.remove(nodeStack.size()-1); 130 } 131 132 133 public void inStmt(Stmt s){ 134 if(s instanceof DAbruptStmt){ 135 DAbruptStmt ab = (DAbruptStmt)s; 137 138 SETNodeLabel label = ab.getLabel(); 139 if(label != null){ 140 if(label.toString() != null){ 141 return; 143 } 144 } 145 146 if(ab.is_Break()){ 148 int index = nodeStack.size()-1; 150 if(index<0){ 151 throw new RuntimeException ("nodeStack empty??"+nodeStack.toString()); 153 } 154 ASTNode currentNode = (ASTNode)nodeStack.get(nodeStack.size()-1); 155 closestNode.put(ab,currentNode); 156 } 157 else if(ab.is_Continue()){ 158 int index = nodeStack.size()-1; 160 if(index<0){ 161 throw new RuntimeException ("nodeStack empty??"+nodeStack.toString()); 163 } 164 165 ASTNode currentNode = (ASTNode)nodeStack.get(index); 166 while(currentNode instanceof ASTSwitchNode){ 167 if(index>0){ 168 index--; 170 currentNode = (ASTNode)nodeStack.get(index); 171 } 172 else{ 173 throw new RuntimeException ("Unable to find closest break Target"); 175 } 176 } 177 closestNode.put(ab,currentNode); 179 } 180 } 181 } 182 183 191 } 192 | Popular Tags |