private static void findFunctionUses(JassProg prog, final HashMap<String, Boolean> useMap) { prog.accept( new JassProg.DefaultVisitor() { @Override public void visit(JassFunction func) { func.accept( new JassFunction.DefaultVisitor() { @Override public void visit(JassExprFuncRef ref) { String name = ref.getFuncName(); useMap.put(name, true); } @Override public void visit(JassExprFunctionCall call) { String name = call.getFuncName(); useMap.put(name, true); } @Override public void visit(JassStmtCall call) { String name = call.getFuncName(); useMap.put(name, true); } }); } }); }
public static void removeGarbage(JassProg prog) { // Find uses HashMap<String, Boolean> useMap = new HashMap<String, Boolean>(); findFunctionUses(prog, useMap); System.out.println(useMap); // Remove Unneeded ones JassFunctions originalFuncs = prog.getFunctions(); Collection<JassFunction> toRemove = Sets.newHashSet(); for (JassFunction func : originalFuncs) { System.out.println("Process: " + func.getName()); if (useMap.get(func.getName()) == null && !RestrictedStandardNames.contains(func.getName())) { System.out.println("unneeded " + func.getName()); toRemove.add(func); System.out.println("Removed: " + func.getName()); } } originalFuncs.removeAll(toRemove); }