@Test public void testEmitGraphicsFuzzDefines() throws Exception { final String expected = "\n" + "#ifndef REDUCER\n" + "#define _GLF_ZERO(X, Y) (Y)\n" + "#define _GLF_ONE(X, Y) (Y)\n" + "#define _GLF_FALSE(X, Y) (Y)\n" + "#define _GLF_TRUE(X, Y) (Y)\n" + "#define _GLF_IDENTITY(X, Y) (Y)\n" + "#define _GLF_DEAD(X) (X)\n" + "#define _GLF_FUZZED(X) (X)\n" + "#define _GLF_WRAPPED_LOOP(X) X\n" + "#define _GLF_WRAPPED_IF_TRUE(X) X\n" + "#define _GLF_WRAPPED_IF_FALSE(X) X\n" + "#define _GLF_SWITCH(X) X\n" + "#define _GLF_MAKE_IN_BOUNDS_INT(IDX, SZ) ((IDX) < 0 ? 0 " + ": ((IDX) >= SZ ? SZ - 1 : (IDX)))\n" + "#define _GLF_MAKE_IN_BOUNDS_UINT(IDX, SZ) ((IDX) >= SZ ? SZ - 1u : (IDX))\n" + "#endif\n" + "\n" + " ByteArrayOutputStream bytes = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(bytes); PrettyPrinterVisitor.emitGraphicsFuzzDefines(printStream, ShadingLanguageVersion.ESSL_100); assertEquals(expected, new String(bytes.toByteArray(), StandardCharsets.UTF_8)); }
public static void emitGraphicsFuzzDefines(PrintStream out, ShadingLanguageVersion shadingLanguageVersion) { out.append("\n"); out.append("#ifndef REDUCER\n"); out.append("#define ").append(Constants.GLF_ZERO).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_ONE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_FALSE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_TRUE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_IDENTITY).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_DEAD).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_FUZZED).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_WRAPPED_LOOP).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_TRUE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_FALSE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_SWITCH).append("(X) X\n"); if (shadingLanguageVersion.supportedClampInt()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) clamp" + "(IDX, 0, SZ - 1)").append("\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) ((IDX) <" + " 0 ? 0 : (").append("(IDX) >= SZ ? SZ - 1 : (IDX)))\n"); } if (shadingLanguageVersion.supportedClampUint()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) clamp" + "(IDX, 0u, SZ - ").append("1u)\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) ((IDX) " + ">= SZ ? SZ - ").append("1u : (IDX))\n"); } out.append("#endif\n"); out.append("\n"); out.append(ParseHelper.END_OF_GRAPHICSFUZZ_DEFINES).append("\n"); }
PrettyPrinterVisitor extends StandardVisitor { public static void emitGraphicsFuzzDefines(PrintStream out, ShadingLanguageVersion shadingLanguageVersion) { out.append("\n"); out.append("#ifndef REDUCER\n"); out.append("#define ").append(Constants.GLF_ZERO).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_ONE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_FALSE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_TRUE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_IDENTITY).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_DEAD).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_FUZZED).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_WRAPPED_LOOP).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_TRUE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_FALSE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_SWITCH).append("(X) X\n"); if (shadingLanguageVersion.supportedClampInt()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) clamp" + "(IDX, 0, SZ - 1)").append("\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) ((IDX) <" + " 0 ? 0 : (").append("(IDX) >= SZ ? SZ - 1 : (IDX)))\n"); } if (shadingLanguageVersion.supportedClampUint()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) clamp" + "(IDX, 0u, SZ - ").append("1u)\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) ((IDX) " + ">= SZ ? SZ - ").append("1u : (IDX))\n"); } out.append("#endif\n"); out.append("\n"); out.append(ParseHelper.END_OF_GRAPHICSFUZZ_DEFINES).append("\n"); } }
PrettyPrinterVisitor extends StandardVisitor { public static void emitGraphicsFuzzDefines(PrintStream out, ShadingLanguageVersion shadingLanguageVersion) { out.append("\n"); out.append("#ifndef REDUCER\n"); out.append("#define ").append(Constants.GLF_ZERO).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_ONE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_FALSE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_TRUE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_IDENTITY).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_DEAD).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_FUZZED).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_WRAPPED_LOOP).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_TRUE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_FALSE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_SWITCH).append("(X) X\n"); if (shadingLanguageVersion.supportedClampInt()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) clamp" + "(IDX, 0, SZ - 1)").append("\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) ((IDX) <" + " 0 ? 0 : (").append("(IDX) >= SZ ? SZ - 1 : (IDX)))\n"); } if (shadingLanguageVersion.supportedClampUint()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) clamp" + "(IDX, 0u, SZ - ").append("1u)\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) ((IDX) " + ">= SZ ? SZ - ").append("1u : (IDX))\n"); } out.append("#endif\n"); out.append("\n"); out.append(ParseHelper.END_OF_GRAPHICSFUZZ_DEFINES).append("\n"); } PrettyPrinterVisitor(PrintStream out);  PrettyPrinterVisitor(PrintStream out,
                              int indentationWidth,
                              Supplier<String> newLineSupplier,
                              boolean emitGraphicsFuzzDefines,
                              Optional<String> license,
                              Optional<UniformValueSupplier> uniformValues); }
PrettyPrinterVisitor extends StandardVisitor { public static void emitGraphicsFuzzDefines(PrintStream out, ShadingLanguageVersion shadingLanguageVersion) { out.append("\n"); out.append("#ifndef REDUCER\n"); out.append("#define ").append(Constants.GLF_ZERO).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_ONE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_FALSE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_TRUE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_IDENTITY).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_DEAD).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_FUZZED).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_WRAPPED_LOOP).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_TRUE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_FALSE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_SWITCH).append("(X) X\n"); if (shadingLanguageVersion.supportedClampInt()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) clamp" + "(IDX, 0, SZ - 1)").append("\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) ((IDX) <" + " 0 ? 0 : (").append("(IDX) >= SZ ? SZ - 1 : (IDX)))\n"); } if (shadingLanguageVersion.supportedClampUint()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) clamp" + "(IDX, 0u, SZ - ").append("1u)\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) ((IDX) " + ">= SZ ? SZ - ").append("1u : (IDX))\n"); } out.append("#endif\n"); out.append("\n"); out.append(ParseHelper.END_OF_GRAPHICSFUZZ_DEFINES).append("\n"); } PrettyPrinterVisitor(PrintStream out);  PrettyPrinterVisitor(PrintStream out,
                              int indentationWidth,
                              Supplier<String> newLineSupplier,
                              boolean emitGraphicsFuzzDefines,
                              Optional<String> license,
                              Optional<UniformValueSupplier> uniformValues); static String prettyPrintAsString(IAstNode node); static String prettyPrintAsString(IAstNode node, UniformValueSupplier uniformValues); static void emitShader(TranslationUnit shader,
                                Optional<String> license,
                                PrintStream stream,
                                int indentationWidth,
                                Supplier<String> newlineSupplier); static void emitShader(TranslationUnit shader,
                                Optional<String> license,
                                PrintStream stream,
                                int indentationWidth,
                                Supplier<String> newlineSupplier,
                                Optional<UniformValueSupplier> uniformValueSupplier); @Override void visitPrecisionDeclaration(PrecisionDeclaration precisionDeclaration); @Override void visitDeclarationStmt(DeclarationStmt declarationStmt); @Override void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration); @Override void visitFunctionDefinition(FunctionDefinition functionDefinition); @Override void visitFunctionPrototype(FunctionPrototype functionPrototype); @Override void visitParameterDecl(ParameterDecl parameterDecl); @Override void visitBlockStmt(BlockStmt stmt); @Override void visitIfStmt(IfStmt ifStmt); @Override void visitBinaryExpr(BinaryExpr binaryExpr); @Override void visitParenExpr(ParenExpr parenExpr); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr); @Override void visitUnaryExpr(UnaryExpr unaryExpr); @Override void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr); @Override void visitIntConstantExpr(IntConstantExpr intConstantExpr); @Override void visitUIntConstantExpr(UIntConstantExpr intConstantExpr); @Override void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr); @Override void visitBoolConstantExpr(BoolConstantExpr boolConstantExpr); @Override void visitBreakStmt(BreakStmt breakStmt); @Override void visitContinueStmt(ContinueStmt continueStmt); @Override void visitDiscardStmt(DiscardStmt discardStmt); @Override void visitReturnStmt(ReturnStmt returnStmt); @Override void visitExprStmt(ExprStmt exprStmt); @Override void visitFunctionCallExpr(FunctionCallExpr functionCallExpr); @Override void visitTypeConstructorExpr(TypeConstructorExpr typeConstructorExpr); @Override void visitQualifiedType(QualifiedType qualifiedType); @Override void visitBasicType(BasicType basicType); @Override void visitSamplerType(SamplerType samplerType); @Override void visitImageType(ImageType imageType); @Override void visitVoidType(VoidType voidType); @Override void visitAtomicIntType(AtomicIntType atomicIntType); @Override void visitNullStmt(NullStmt nullStmt); @Override void visitWhileStmt(WhileStmt whileStmt); @Override void visitForStmt(ForStmt forStmt); @Override void visitDoStmt(DoStmt doStmt); @Override void visitTernaryExpr(TernaryExpr ternaryExpr); @Override void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr); @Override void visitStructNameType(StructNameType structNameType); @Override void visitArrayType(ArrayType arrayType); @Override void visitArrayConstructorExpr(ArrayConstructorExpr arrayConstructorExpr); @Override void visitStructDefinitionType(StructDefinitionType structDefinitionType); @Override void visitSwitchStmt(SwitchStmt switchStmt); @Override void visitDefaultCaseLabel(DefaultCaseLabel defaultCaseLabel); @Override void visitExprCaseLabel(ExprCaseLabel exprCaseLabel); @Override void visitInterfaceBlock(InterfaceBlock interfaceBlock); @Override void visitDefaultLayout(DefaultLayout defaultLayout); @Override void visitTranslationUnit(TranslationUnit translationUnit); @Override void visitPragmaStatement(PragmaStatement pragmaStatement); @Override void visitExtensionStatement(ExtensionStatement extensionStatement); @Override String toString(); static String defaultIndent(int level); static void emitGraphicsFuzzDefines(PrintStream out,
                                             ShadingLanguageVersion shadingLanguageVersion); }
PrettyPrinterVisitor extends StandardVisitor { public static void emitGraphicsFuzzDefines(PrintStream out, ShadingLanguageVersion shadingLanguageVersion) { out.append("\n"); out.append("#ifndef REDUCER\n"); out.append("#define ").append(Constants.GLF_ZERO).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_ONE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_FALSE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_TRUE).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_IDENTITY).append("(X, Y) (Y)\n"); out.append("#define ").append(Constants.GLF_DEAD).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_FUZZED).append("(X) (X)\n"); out.append("#define ").append(Constants.GLF_WRAPPED_LOOP).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_TRUE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_WRAPPED_IF_FALSE).append("(X) X\n"); out.append("#define ").append(Constants.GLF_SWITCH).append("(X) X\n"); if (shadingLanguageVersion.supportedClampInt()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) clamp" + "(IDX, 0, SZ - 1)").append("\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_INT).append("(IDX, SZ) ((IDX) <" + " 0 ? 0 : (").append("(IDX) >= SZ ? SZ - 1 : (IDX)))\n"); } if (shadingLanguageVersion.supportedClampUint()) { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) clamp" + "(IDX, 0u, SZ - ").append("1u)\n"); } else { out.append("#define ").append(Constants.GLF_MAKE_IN_BOUNDS_UINT).append("(IDX, SZ) ((IDX) " + ">= SZ ? SZ - ").append("1u : (IDX))\n"); } out.append("#endif\n"); out.append("\n"); out.append(ParseHelper.END_OF_GRAPHICSFUZZ_DEFINES).append("\n"); } PrettyPrinterVisitor(PrintStream out);  PrettyPrinterVisitor(PrintStream out,
                              int indentationWidth,
                              Supplier<String> newLineSupplier,
                              boolean emitGraphicsFuzzDefines,
                              Optional<String> license,
                              Optional<UniformValueSupplier> uniformValues); static String prettyPrintAsString(IAstNode node); static String prettyPrintAsString(IAstNode node, UniformValueSupplier uniformValues); static void emitShader(TranslationUnit shader,
                                Optional<String> license,
                                PrintStream stream,
                                int indentationWidth,
                                Supplier<String> newlineSupplier); static void emitShader(TranslationUnit shader,
                                Optional<String> license,
                                PrintStream stream,
                                int indentationWidth,
                                Supplier<String> newlineSupplier,
                                Optional<UniformValueSupplier> uniformValueSupplier); @Override void visitPrecisionDeclaration(PrecisionDeclaration precisionDeclaration); @Override void visitDeclarationStmt(DeclarationStmt declarationStmt); @Override void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration); @Override void visitFunctionDefinition(FunctionDefinition functionDefinition); @Override void visitFunctionPrototype(FunctionPrototype functionPrototype); @Override void visitParameterDecl(ParameterDecl parameterDecl); @Override void visitBlockStmt(BlockStmt stmt); @Override void visitIfStmt(IfStmt ifStmt); @Override void visitBinaryExpr(BinaryExpr binaryExpr); @Override void visitParenExpr(ParenExpr parenExpr); @Override void visitVariableIdentifierExpr(VariableIdentifierExpr variableIdentifierExpr); @Override void visitUnaryExpr(UnaryExpr unaryExpr); @Override void visitMemberLookupExpr(MemberLookupExpr memberLookupExpr); @Override void visitIntConstantExpr(IntConstantExpr intConstantExpr); @Override void visitUIntConstantExpr(UIntConstantExpr intConstantExpr); @Override void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr); @Override void visitBoolConstantExpr(BoolConstantExpr boolConstantExpr); @Override void visitBreakStmt(BreakStmt breakStmt); @Override void visitContinueStmt(ContinueStmt continueStmt); @Override void visitDiscardStmt(DiscardStmt discardStmt); @Override void visitReturnStmt(ReturnStmt returnStmt); @Override void visitExprStmt(ExprStmt exprStmt); @Override void visitFunctionCallExpr(FunctionCallExpr functionCallExpr); @Override void visitTypeConstructorExpr(TypeConstructorExpr typeConstructorExpr); @Override void visitQualifiedType(QualifiedType qualifiedType); @Override void visitBasicType(BasicType basicType); @Override void visitSamplerType(SamplerType samplerType); @Override void visitImageType(ImageType imageType); @Override void visitVoidType(VoidType voidType); @Override void visitAtomicIntType(AtomicIntType atomicIntType); @Override void visitNullStmt(NullStmt nullStmt); @Override void visitWhileStmt(WhileStmt whileStmt); @Override void visitForStmt(ForStmt forStmt); @Override void visitDoStmt(DoStmt doStmt); @Override void visitTernaryExpr(TernaryExpr ternaryExpr); @Override void visitArrayIndexExpr(ArrayIndexExpr arrayIndexExpr); @Override void visitStructNameType(StructNameType structNameType); @Override void visitArrayType(ArrayType arrayType); @Override void visitArrayConstructorExpr(ArrayConstructorExpr arrayConstructorExpr); @Override void visitStructDefinitionType(StructDefinitionType structDefinitionType); @Override void visitSwitchStmt(SwitchStmt switchStmt); @Override void visitDefaultCaseLabel(DefaultCaseLabel defaultCaseLabel); @Override void visitExprCaseLabel(ExprCaseLabel exprCaseLabel); @Override void visitInterfaceBlock(InterfaceBlock interfaceBlock); @Override void visitDefaultLayout(DefaultLayout defaultLayout); @Override void visitTranslationUnit(TranslationUnit translationUnit); @Override void visitPragmaStatement(PragmaStatement pragmaStatement); @Override void visitExtensionStatement(ExtensionStatement extensionStatement); @Override String toString(); static String defaultIndent(int level); static void emitGraphicsFuzzDefines(PrintStream out,
                                             ShadingLanguageVersion shadingLanguageVersion); static final int DEFAULT_INDENTATION_WIDTH; static final Supplier<String> DEFAULT_NEWLINE_SUPPLIER; }