StripUnusedGlobals extends ScopeTrackingVisitor { private StripUnusedGlobals(TranslationUnit tu) { this.unusedGlobals = new HashSet<>(); this.unusedStructs = new HashSet<>(); visit(tu); sweep(tu); } private  StripUnusedGlobals(TranslationUnit tu); static void strip(TranslationUnit tu); @Override void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration); @Override void visitStructNameType(StructNameType structNameType); @Override void visitTypeConstructorExpr(TypeConstructorExpr typeConstructorExpr); @Override void visitVariableDeclInfo(VariableDeclInfo variableDeclInfo); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr);  }
@Test public void testStripUnusedGlobals() throws Exception { final String original = "" + "int a = 0, b = a, c;" + "float d = 2.0, e[4];" + "void baz() {" + " d = 2;" + "}" + "int x, y = c;" + "void foo() {" + " d = float(x);" + "}" + "float t;" + "float t2 = d;" + "float l, m;" + "void main() {" + "}"; final String expected = "" + "int a = 0, c;" + "float d = 2.0;" + "void baz() {" + " d = 2;" + "}" + "int x;" + "void foo() {" + " d = float(x);" + "}" + "void main() {" + "}"; final TranslationUnit tu = ParseHelper.parse(original); StripUnusedGlobals.strip(tu); CompareAsts.assertEqualAsts(expected, tu); }