fuzzers: Switch AST fuzzers to new generator API

Change-Id: If9f843a318be6e9bbb44bc852814811a5e42baf0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57980
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price 2021-07-14 12:49:32 +00:00 committed by Tint LUCI CQ
parent 6e459fecb7
commit 5db7d38c13
2 changed files with 18 additions and 14 deletions

View File

@ -82,14 +82,15 @@ extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* data,
if (!cli_params.record_mutations) { if (!cli_params.record_mutations) {
// If mutations are not being recorded, then the mutated `program` must be // If mutations are not being recorded, then the mutated `program` must be
// stored into the `mutator_state`. // stored into the `mutator_state`.
writer::wgsl::Generator generator(&program); writer::wgsl::Options options;
if (!generator.Generate()) { auto result = writer::wgsl::Generate(&program, options);
if (!result.success) {
std::cout << "Can't generate WGSL for valid tint::Program:" << std::endl std::cout << "Can't generate WGSL for valid tint::Program:" << std::endl
<< " seed: " << seed << std::endl << " seed: " << seed << std::endl
<< generator.error() << std::endl; << result.error << std::endl;
return 0; return 0;
} }
*mutator_state.mutable_program() = generator.result(); *mutator_state.mutable_program() = result.wgsl;
} }
if (mutator_state.ByteSizeLong() > max_size) { if (mutator_state.ByteSizeLong() > max_size) {
@ -120,10 +121,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Replay(reader::wgsl::Parse(&file), mutator_state.mutation_sequence()); Replay(reader::wgsl::Parse(&file), mutator_state.mutation_sequence());
assert(program.IsValid() && "Replayed program is invalid"); assert(program.IsValid() && "Replayed program is invalid");
writer::wgsl::Generator generator(&program); writer::wgsl::Options options;
success = generator.Generate(); auto result = writer::wgsl::Generate(&program, options);
assert(success && "Can't generate a shader for the valid tint::Program"); assert(result.success &&
program_text = generator.result(); "Can't generate a shader for the valid tint::Program");
program_text = result.wgsl;
} else { } else {
program_text.assign(data, data + size); program_text.assign(data, data + size);
} }

View File

@ -540,8 +540,9 @@ fn f() {
&program, &node_id_map, nullptr)); &program, &node_id_map, nullptr));
ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str(); ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str();
writer::wgsl::Generator generator(&program); writer::wgsl::Options options;
ASSERT_TRUE(generator.Generate()) << generator.error(); auto result = writer::wgsl::Generate(&program, options);
ASSERT_TRUE(result.success) << result.error;
std::string expected_shader = R"(fn f() { std::string expected_shader = R"(fn f() {
var b : vec2<u32>; var b : vec2<u32>;
@ -549,7 +550,7 @@ fn f() {
(*(&(b)))[1] = 3u; (*(&(b)))[1] = 3u;
} }
)"; )";
ASSERT_EQ(expected_shader, generator.result()); ASSERT_EQ(expected_shader, result.wgsl);
} }
TEST(ReplaceIdentifierTest, Applicable2) { TEST(ReplaceIdentifierTest, Applicable2) {
@ -588,8 +589,9 @@ fn f(b: ptr<function, vec2<u32>>) {
&program, &node_id_map, nullptr)); &program, &node_id_map, nullptr));
ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str(); ASSERT_TRUE(program.IsValid()) << program.Diagnostics().str();
writer::wgsl::Generator generator(&program); writer::wgsl::Options options;
ASSERT_TRUE(generator.Generate()) << generator.error(); auto result = writer::wgsl::Generate(&program, options);
ASSERT_TRUE(result.success) << result.error;
std::string expected_shader = R"(fn f(b : ptr<function, vec2<u32>>) { std::string expected_shader = R"(fn f(b : ptr<function, vec2<u32>>) {
var a = vec2<u32>(34u, 45u); var a = vec2<u32>(34u, 45u);
@ -597,7 +599,7 @@ fn f(b: ptr<function, vec2<u32>>) {
(*(b))[1] = 3u; (*(b))[1] = 3u;
} }
)"; )";
ASSERT_EQ(expected_shader, generator.result()); ASSERT_EQ(expected_shader, result.wgsl);
} }
TEST(ReplaceIdentifierTest, NotApplicable12) { TEST(ReplaceIdentifierTest, NotApplicable12) {