From 0f56ed9759aa191568768d826a816cee66a3466c Mon Sep 17 00:00:00 2001 From: James Price Date: Thu, 17 Feb 2022 20:43:19 +0000 Subject: [PATCH] tests: Validate WGSL output WGSL was the only format that wasn't being validated for our E2E tests, and this meant that WGSL backend bugs were sometimes going unnoticed. This change uses Tint's WGSL reader to validate the WGSL output. Change-Id: I7d9e1c22feda530e7ba594725f21e576cf928647 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/80961 Kokoro: Kokoro Reviewed-by: Ben Clayton --- samples/main.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/samples/main.cc b/samples/main.cc index 50e1f7a131..d1d0ec285a 100644 --- a/samples/main.cc +++ b/samples/main.cc @@ -623,7 +623,24 @@ bool GenerateWgsl(const tint::Program* program, const Options& options) { return false; } - return WriteFile(options.output_file, "w", result.wgsl); + if (!WriteFile(options.output_file, "w", result.wgsl)) { + return false; + } + + if (options.validate) { + // Attempt to re-parse the output program with Tint's WGSL reader. + auto source = std::make_unique(options.input_filename, + result.wgsl); + auto reparsed_program = tint::reader::wgsl::Parse(source.get()); + if (!reparsed_program.IsValid()) { + auto diag_printer = tint::diag::Printer::create(stderr, true); + tint::diag::Formatter diag_formatter; + diag_formatter.format(reparsed_program.Diagnostics(), diag_printer.get()); + return false; + } + } + + return true; #else (void)program; (void)options;