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 <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/samples/main.cc b/samples/main.cc
index 50e1f7a..d1d0ec2 100644
--- a/samples/main.cc
+++ b/samples/main.cc
@@ -623,7 +623,24 @@
     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<tint::Source::File>(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;