VariableDeclToExprReductionOpportunities extends ReductionOpportunitiesBase<VariableDeclToExprReductionOpportunity> { static List<VariableDeclToExprReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  VariableDeclToExprReductionOpportunities(TranslationUnit tu, ReducerContext context); @Override void visitDeclarationStmt(DeclarationStmt declarationStmt);  }
@Test public void testDoNotReplace() throws Exception { final String original = "void main() { int a = 1, b = 2; int c = 3; }"; final TranslationUnit tu = ParseHelper.parse(original); final List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testDoNotReplaceConst() throws Exception { final String original = "void main() { const int a = 1;}"; final TranslationUnit tu = ParseHelper.parse(original); final List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testMultipleDeclarations() throws Exception { final String program = "void main() {" + "int a = 1;" + "int b = foo();" + "int c;" + "}"; final String expected = "void main() {" + " int a;" + " a = 1;" + " int b;" + " b = foo();" + " int c;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(2, ops.size()); ops.forEach(VariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testMultipleLineDeclarationsOneLine() throws Exception { final String program = "void main() {" + "int a;" + "int b = 1, c, d = foo(), e, f = bar(); " + "int g;" + "}"; final String expected = "void main() {" + " int a;" + " int b, c, d, e, f;" + " b = 1;" + " d = foo();" + " f = bar();" + " int g;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(3, ops.size()); ops.forEach(VariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testAssignVariableIdentifier() throws Exception { final String program = "void main() {" + "int a = 1;" + "int b = a, c = b;" + "int d = c;" + "}"; final String expected = "void main() {" + " int a;" + " a = 1;" + " int b, c;" + " b = a;" + " c = b;" + " int d;" + " d = c;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); List<VariableDeclToExprReductionOpportunity> ops = VariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(4, ops.size()); ops.forEach(VariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }