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 <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2023-01-24 21:01:36 +00:00
parent 81e55754e1
commit e8ea579bce
8 changed files with 68 additions and 6 deletions

View File

@ -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",

View File

@ -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

View File

@ -250,7 +250,7 @@ bool GeneratorImpl::Generate() {
auto* mod = builder_.Sem().Module();
for (auto* decl : mod->DependencyOrderedDeclarations()) {
if (decl->IsAnyOf<ast::Alias, ast::ConstAssert>()) {
if (decl->IsAnyOf<ast::Alias, ast::ConstAssert, ast::DiagnosticControl>()) {
continue; // These are not emitted.
}

View File

@ -310,7 +310,7 @@ bool GeneratorImpl::Generate() {
auto* mod = builder_.Sem().Module();
for (auto* decl : mod->DependencyOrderedDeclarations()) {
if (decl->IsAnyOf<ast::Alias, ast::Enable, ast::ConstAssert>()) {
if (decl->IsAnyOf<ast::Alias, ast::DiagnosticControl, ast::Enable, ast::ConstAssert>()) {
continue; // These are not emitted.
}

View File

@ -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;

View File

@ -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<ast::Enable>()) {
if (decl->IsAnyOf<ast::DiagnosticControl, ast::Enable>()) {
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 << ";";

View File

@ -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);

View File

@ -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