From e8ea579bceafe593964e6110559925362eb80842 Mon Sep 17 00:00:00 2001 From: James Price Date: Tue, 24 Jan 2023 21:01:36 +0000 Subject: [PATCH] tint/writer: Handle diagnostic directives The WGSL writer emits them, the other writers ignore them. Bug: tint:1809 Change-Id: I163d868005c076b8fd4602f13d3e267b16c474c0 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/117567 Reviewed-by: Ben Clayton Kokoro: Kokoro --- src/tint/BUILD.gn | 1 + src/tint/CMakeLists.txt | 1 + src/tint/writer/glsl/generator_impl.cc | 2 +- src/tint/writer/hlsl/generator_impl.cc | 2 +- src/tint/writer/msl/generator_impl.cc | 4 +++ src/tint/writer/wgsl/generator_impl.cc | 23 +++++++++++-- src/tint/writer/wgsl/generator_impl.h | 7 +++- .../wgsl/generator_impl_diagnostic_test.cc | 34 +++++++++++++++++++ 8 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/tint/writer/wgsl/generator_impl_diagnostic_test.cc diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 969262a251..a0e1292e3c 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -1672,6 +1672,7 @@ if (tint_build_unittests) { "writer/wgsl/generator_impl_cast_test.cc", "writer/wgsl/generator_impl_const_assert_test.cc", "writer/wgsl/generator_impl_continue_test.cc", + "writer/wgsl/generator_impl_diagnostic_test.cc", "writer/wgsl/generator_impl_discard_test.cc", "writer/wgsl/generator_impl_enable_test.cc", "writer/wgsl/generator_impl_function_test.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index a6306de2f9..f25bb02396 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -1212,6 +1212,7 @@ if(TINT_BUILD_TESTS) writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_const_assert_test.cc writer/wgsl/generator_impl_continue_test.cc + writer/wgsl/generator_impl_diagnostic_test.cc writer/wgsl/generator_impl_discard_test.cc writer/wgsl/generator_impl_enable_test.cc writer/wgsl/generator_impl_function_test.cc diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc index 5b1020948b..df496944a4 100644 --- a/src/tint/writer/glsl/generator_impl.cc +++ b/src/tint/writer/glsl/generator_impl.cc @@ -250,7 +250,7 @@ bool GeneratorImpl::Generate() { auto* mod = builder_.Sem().Module(); for (auto* decl : mod->DependencyOrderedDeclarations()) { - if (decl->IsAnyOf()) { + if (decl->IsAnyOf()) { continue; // These are not emitted. } diff --git a/src/tint/writer/hlsl/generator_impl.cc b/src/tint/writer/hlsl/generator_impl.cc index 69c19f15d9..bde9d703f3 100644 --- a/src/tint/writer/hlsl/generator_impl.cc +++ b/src/tint/writer/hlsl/generator_impl.cc @@ -310,7 +310,7 @@ bool GeneratorImpl::Generate() { auto* mod = builder_.Sem().Module(); for (auto* decl : mod->DependencyOrderedDeclarations()) { - if (decl->IsAnyOf()) { + if (decl->IsAnyOf()) { continue; // These are not emitted. } diff --git a/src/tint/writer/msl/generator_impl.cc b/src/tint/writer/msl/generator_impl.cc index bff14a293c..29ce69fd5f 100644 --- a/src/tint/writer/msl/generator_impl.cc +++ b/src/tint/writer/msl/generator_impl.cc @@ -313,6 +313,10 @@ bool GeneratorImpl::Generate() { } return EmitFunction(func); }, + [&](const ast::DiagnosticControl*) { + // Do nothing for diagnostic directives in MSL + return true; + }, [&](const ast::Enable*) { // Do nothing for enabling extension in MSL return true; diff --git a/src/tint/writer/wgsl/generator_impl.cc b/src/tint/writer/wgsl/generator_impl.cc index d83ad4a735..0dfd90910d 100644 --- a/src/tint/writer/wgsl/generator_impl.cc +++ b/src/tint/writer/wgsl/generator_impl.cc @@ -63,18 +63,28 @@ GeneratorImpl::GeneratorImpl(const Program* program) : TextGenerator(program) {} GeneratorImpl::~GeneratorImpl() = default; bool GeneratorImpl::Generate() { - // Generate enable directives before any other global declarations. + // Generate directives before any other global declarations. + bool has_directives = false; for (auto enable : program_->AST().Enables()) { if (!EmitEnable(enable)) { return false; } + has_directives = true; } - if (!program_->AST().Enables().IsEmpty()) { + for (auto diagnostic : program_->AST().DiagnosticControls()) { + auto out = line(); + if (!EmitDiagnosticControl(out, diagnostic)) { + return false; + } + out << ";"; + has_directives = true; + } + if (has_directives) { line(); } // Generate global declarations in the order they appear in the module. for (auto* decl : program_->AST().GlobalDeclarations()) { - if (decl->Is()) { + if (decl->IsAnyOf()) { continue; } if (!Switch( @@ -97,6 +107,13 @@ bool GeneratorImpl::Generate() { return true; } +bool GeneratorImpl::EmitDiagnosticControl(std::ostream& out, + const ast::DiagnosticControl* diagnostic) { + out << "diagnostic(" << diagnostic->severity << ", " + << program_->Symbols().NameFor(diagnostic->rule_name->symbol) << ")"; + return true; +} + bool GeneratorImpl::EmitEnable(const ast::Enable* enable) { auto out = line(); out << "enable " << enable->extension << ";"; diff --git a/src/tint/writer/wgsl/generator_impl.h b/src/tint/writer/wgsl/generator_impl.h index 6dd01b3c6c..87a1b54484 100644 --- a/src/tint/writer/wgsl/generator_impl.h +++ b/src/tint/writer/wgsl/generator_impl.h @@ -52,7 +52,12 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise bool Generate(); - /// Handles generating a enable directive + /// Handles generating a diagnostic control + /// @param out the output of the expression stream + /// @param diagnostic the diagnostic control node + /// @returns true if the diagnostic control was emitted + bool EmitDiagnosticControl(std::ostream& out, const ast::DiagnosticControl* diagnostic); + /// Handles generating an enable directive /// @param enable the enable node /// @returns true if the enable directive was emitted bool EmitEnable(const ast::Enable* enable); diff --git a/src/tint/writer/wgsl/generator_impl_diagnostic_test.cc b/src/tint/writer/wgsl/generator_impl_diagnostic_test.cc new file mode 100644 index 0000000000..1bcadd3cd8 --- /dev/null +++ b/src/tint/writer/wgsl/generator_impl_diagnostic_test.cc @@ -0,0 +1,34 @@ +// Copyright 2022 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 "src/tint/writer/wgsl/test_helper.h" + +namespace tint::writer::wgsl { +namespace { + +using WgslGeneratorImplTest = TestHelper; + +TEST_F(WgslGeneratorImplTest, Emit_DiagnosticDirective) { + DiagnosticDirective(ast::DiagnosticSeverity::kError, Expr("chromium_unreachable_code")); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.Generate()); + EXPECT_EQ(gen.result(), R"(diagnostic(error, chromium_unreachable_code); + +)"); +} + +} // namespace +} // namespace tint::writer::wgsl