fuzzers: Switch fuzzers to new generator API

Remove sanitizer transform fuzzers, as these will no longer be
publicly visible. We should fuzz the generator options instead.

Change-Id: If8f2c70f505bdaecd62a2f53a6586c3b84bd1c33
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57760
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/fuzzers/BUILD.gn b/fuzzers/BUILD.gn
index 64eefc8..b19e55e 100644
--- a/fuzzers/BUILD.gn
+++ b/fuzzers/BUILD.gn
@@ -91,11 +91,6 @@
       deps = [ ":tint_fuzzer_common" ]
     }
 
-    fuzzer_test("tint_spirv_transform_fuzzer") {
-      sources = [ "tint_spirv_transform_fuzzer.cc" ]
-      deps = [ ":tint_fuzzer_common" ]
-    }
-
     fuzzer_test("tint_vertex_pulling_fuzzer") {
       sources = [ "tint_vertex_pulling_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -108,11 +103,6 @@
   }
 
   if (tint_build_wgsl_reader && tint_build_hlsl_writer) {
-    fuzzer_test("tint_hlsl_transform_fuzzer") {
-      sources = [ "tint_hlsl_transform_fuzzer.cc" ]
-      deps = [ ":tint_fuzzer_common" ]
-    }
-
     fuzzer_test("tint_wgsl_reader_hlsl_writer_fuzzer") {
       sources = [ "tint_wgsl_reader_hlsl_writer_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -120,11 +110,6 @@
   }
 
   if (tint_build_wgsl_reader && tint_build_msl_writer) {
-    fuzzer_test("tint_msl_transform_fuzzer") {
-      sources = [ "tint_msl_transform_fuzzer.cc" ]
-      deps = [ ":tint_fuzzer_common" ]
-    }
-
     fuzzer_test("tint_wgsl_reader_msl_writer_fuzzer") {
       sources = [ "tint_wgsl_reader_msl_writer_fuzzer.cc" ]
       deps = [ ":tint_fuzzer_common" ]
@@ -192,20 +177,17 @@
         ":tint_inspector_fuzzer",
         ":tint_renamer_fuzzer",
         ":tint_single_entry_point_fuzzer",
-        ":tint_spirv_transform_fuzzer",
         ":tint_vertex_pulling_fuzzer",
         ":tint_wgsl_reader_spv_writer_fuzzer",
       ]
     }
     if (tint_build_wgsl_reader && tint_build_hlsl_writer) {
       deps += [
-        ":tint_hlsl_transform_fuzzer",
         ":tint_wgsl_reader_hlsl_writer_fuzzer",
       ]
     }
     if (tint_build_wgsl_reader && tint_build_msl_writer) {
       deps += [
-        ":tint_msl_transform_fuzzer",
         ":tint_wgsl_reader_msl_writer_fuzzer",
       ]
     }
diff --git a/fuzzers/CMakeLists.txt b/fuzzers/CMakeLists.txt
index ccaae93..70e1ceb 100644
--- a/fuzzers/CMakeLists.txt
+++ b/fuzzers/CMakeLists.txt
@@ -37,19 +37,16 @@
   add_tint_fuzzer(tint_renamer_fuzzer)
   add_tint_fuzzer(tint_robustness_fuzzer)
   add_tint_fuzzer(tint_single_entry_point_fuzzer)
-  add_tint_fuzzer(tint_spirv_transform_fuzzer)
   add_tint_fuzzer(tint_vertex_pulling_fuzzer)
   add_tint_fuzzer(tint_wgsl_reader_spv_writer_fuzzer)
 endif()
 
 if (${TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_HLSL_WRITER})
   add_tint_fuzzer(tint_wgsl_reader_hlsl_writer_fuzzer)
-  add_tint_fuzzer(tint_hlsl_transform_fuzzer)
 endif()
 
 if (${TINT_BUILD_WGSL_READER} AND ${TINT_BUILD_MSL_WRITER})
   add_tint_fuzzer(tint_wgsl_reader_msl_writer_fuzzer)
-  add_tint_fuzzer(tint_msl_transform_fuzzer)
 endif()
 
 if (${TINT_BUILD_SPV_READER})
diff --git a/fuzzers/tint_all_transforms_fuzzer.cc b/fuzzers/tint_all_transforms_fuzzer.cc
index dfdf4de..1a951e0 100644
--- a/fuzzers/tint_all_transforms_fuzzer.cc
+++ b/fuzzers/tint_all_transforms_fuzzer.cc
@@ -62,8 +62,6 @@
       return 0;
     }
 
-    config.manager.Add<transform::Hlsl>();
-
     fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kHLSL);
     fuzzer.SetTransformManager(&config.manager, std::move(config.inputs));
 
@@ -79,8 +77,6 @@
       return 0;
     }
 
-    config.manager.Add<transform::Msl>();
-
     fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kMSL);
     fuzzer.SetTransformManager(&config.manager, std::move(config.inputs));
 
@@ -95,8 +91,6 @@
       return 0;
     }
 
-    config.manager.Add<transform::Spirv>();
-
     fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kSpv);
     fuzzer.SetTransformManager(&config.manager, std::move(config.inputs));
 
diff --git a/fuzzers/tint_ast_clone_fuzzer.cc b/fuzzers/tint_ast_clone_fuzzer.cc
index fdb83c1..2e642c7 100644
--- a/fuzzers/tint_ast_clone_fuzzer.cc
+++ b/fuzzers/tint_ast_clone_fuzzer.cc
@@ -92,10 +92,11 @@
   // original source so that reformatting doesn't impact the final wgsl
   // comparison.
   std::string src_wgsl;
+  tint::writer::wgsl::Options wgsl_options;
   {
-    tint::writer::wgsl::Generator src_gen(&src);
-    ASSERT_TRUE(src_gen.Generate());
-    src_wgsl = src_gen.result();
+    auto result = tint::writer::wgsl::Generate(&src, wgsl_options);
+    ASSERT_TRUE(result.success);
+    src_wgsl = result.wgsl;
 
     // Move the src program to a temporary that'll be dropped, so that the src
     // program is released before we attempt to print the dst program. This
@@ -106,9 +107,9 @@
   }
 
   // Print the dst program, check it matches the original source
-  tint::writer::wgsl::Generator dst_gen(&dst);
-  ASSERT_TRUE(dst_gen.Generate());
-  auto dst_wgsl = dst_gen.result();
+  auto result = tint::writer::wgsl::Generate(&dst, wgsl_options);
+  ASSERT_TRUE(result.success);
+  auto dst_wgsl = result.wgsl;
   ASSERT_EQ(src_wgsl, dst_wgsl);
 
   return 0;
diff --git a/fuzzers/tint_common_fuzzer.cc b/fuzzers/tint_common_fuzzer.cc
index c663648..ccc8c0c 100644
--- a/fuzzers/tint_common_fuzzer.cc
+++ b/fuzzers/tint_common_fuzzer.cc
@@ -321,47 +321,59 @@
   }
 
   switch (output_) {
-    case OutputFormat::kWGSL:
+    case OutputFormat::kWGSL: {
 #if TINT_BUILD_WGSL_WRITER
-      writer_ = std::make_unique<writer::wgsl::Generator>(&program);
+      writer::wgsl::Options options;
+      auto result = writer::wgsl::Generate(&program, options);
+      if (!result.success) {
+        errors_ = writer_->error();
+        return 0;
+      }
 #endif  // TINT_BUILD_WGSL_WRITER
       break;
-    case OutputFormat::kSpv:
+    }
+    case OutputFormat::kSpv: {
 #if TINT_BUILD_SPV_WRITER
-      writer_ = std::make_unique<writer::spirv::Generator>(&program);
+      writer::spirv::Options options;
+      auto result = writer::spirv::Generate(&program, options);
+      if (!result.success) {
+        errors_ = writer_->error();
+        return 0;
+      }
+      if (!SPIRVToolsValidationCheck(program, result.spirv)) {
+        FatalError(program.Diagnostics(),
+                   "Fuzzing detected invalid spirv being emitted by Tint");
+      }
+
 #endif  // TINT_BUILD_SPV_WRITER
       break;
-    case OutputFormat::kHLSL:
+    }
+    case OutputFormat::kHLSL: {
 #if TINT_BUILD_HLSL_WRITER
-      writer_ = std::make_unique<writer::hlsl::Generator>(&program);
+      writer::hlsl::Options options;
+      auto result = writer::hlsl::Generate(&program, options);
+      if (!result.success) {
+        errors_ = writer_->error();
+        return 0;
+      }
 #endif  // TINT_BUILD_HLSL_WRITER
       break;
-    case OutputFormat::kMSL:
+    }
+    case OutputFormat::kMSL: {
 #if TINT_BUILD_MSL_WRITER
-      writer_ = std::make_unique<writer::msl::Generator>(&program);
+      writer::msl::Options options;
+      auto result = writer::msl::Generate(&program, options);
+      if (!result.success) {
+        errors_ = writer_->error();
+        return 0;
+      }
 #endif  // TINT_BUILD_MSL_WRITER
       break;
+    }
     case OutputFormat::kNone:
       break;
   }
 
-  if (writer_) {
-    if (!writer_->Generate()) {
-      errors_ = writer_->error();
-      return 0;
-    }
-
-#if TINT_BUILD_SPV_WRITER
-    if (output_ == OutputFormat::kSpv &&
-        !SPIRVToolsValidationCheck(
-            program,
-            static_cast<writer::spirv::Generator*>(writer_.get())->result())) {
-      FatalError(program.Diagnostics(),
-                 "Fuzzing detected invalid spirv being emitted by Tint");
-    }
-#endif  // TINT_BUILD_SPV_WRITER
-  }
-
   return 0;
 }
 
diff --git a/fuzzers/tint_hlsl_transform_fuzzer.cc b/fuzzers/tint_hlsl_transform_fuzzer.cc
deleted file mode 100644
index 6950f87..0000000
--- a/fuzzers/tint_hlsl_transform_fuzzer.cc
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2021 The Tint Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "fuzzers/tint_common_fuzzer.h"
-
-namespace tint {
-namespace fuzzers {
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  transform::Manager transform_manager;
-  transform_manager.Add<transform::Hlsl>();
-
-  fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kHLSL);
-  fuzzer.SetTransformManager(&transform_manager, {});
-
-  return fuzzer.Run(data, size);
-}
-
-}  // namespace fuzzers
-}  // namespace tint
diff --git a/fuzzers/tint_msl_transform_fuzzer.cc b/fuzzers/tint_msl_transform_fuzzer.cc
deleted file mode 100644
index 250dfbe..0000000
--- a/fuzzers/tint_msl_transform_fuzzer.cc
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2021 The Tint Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "fuzzers/tint_common_fuzzer.h"
-
-namespace tint {
-namespace fuzzers {
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  transform::Manager transform_manager;
-  transform_manager.Add<transform::Msl>();
-
-  fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kMSL);
-  fuzzer.SetTransformManager(&transform_manager, {});
-
-  return fuzzer.Run(data, size);
-}
-
-}  // namespace fuzzers
-}  // namespace tint
diff --git a/fuzzers/tint_spirv_transform_fuzzer.cc b/fuzzers/tint_spirv_transform_fuzzer.cc
deleted file mode 100644
index 7e12e95..0000000
--- a/fuzzers/tint_spirv_transform_fuzzer.cc
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright 2021 The Tint Authors.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "fuzzers/tint_common_fuzzer.h"
-
-namespace tint {
-namespace fuzzers {
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  transform::Manager transform_manager;
-  transform_manager.Add<transform::Spirv>();
-
-  fuzzers::CommonFuzzer fuzzer(InputFormat::kWGSL, OutputFormat::kSpv);
-  fuzzer.SetTransformManager(&transform_manager, {});
-
-  return fuzzer.Run(data, size);
-}
-
-}  // namespace fuzzers
-}  // namespace tint