tint/reader/wgsl: Parse @diagnostic attributes

Future patches will expand the places where they can be used.

Bug: tint:1809
Change-Id: I74d87ce5164119ae1351380041f9ef4b1091d854
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/117571
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 75326f88e6
commit c744a23d77
4 changed files with 48 additions and 1 deletions

View File

@ -1594,6 +1594,7 @@ if (tint_build_unittests) {
"reader/wgsl/parser_impl_continuing_stmt_test.cc",
"reader/wgsl/parser_impl_core_lhs_expression_test.cc",
"reader/wgsl/parser_impl_depth_texture_test.cc",
"reader/wgsl/parser_impl_diagnostic_attribute_test.cc",
"reader/wgsl/parser_impl_diagnostic_control_test.cc",
"reader/wgsl/parser_impl_diagnostic_directive_test.cc",
"reader/wgsl/parser_impl_element_count_expression_test.cc",

View File

@ -1097,6 +1097,7 @@ if(TINT_BUILD_TESTS)
reader/wgsl/parser_impl_continuing_stmt_test.cc
reader/wgsl/parser_impl_core_lhs_expression_test.cc
reader/wgsl/parser_impl_depth_texture_test.cc
reader/wgsl/parser_impl_diagnostic_attribute_test.cc
reader/wgsl/parser_impl_diagnostic_control_test.cc
reader/wgsl/parser_impl_diagnostic_directive_test.cc
reader/wgsl/parser_impl_element_count_expression_test.cc

View File

@ -3503,6 +3503,7 @@ Expect<const ast::Attribute*> ParserImpl::expect_attribute() {
// | ATTR 'binding' PAREN_LEFT expression COMMA? PAREN_RIGHT
// | ATTR 'builtin' PAREN_LEFT builtin_value_name COMMA? PAREN_RIGHT
// | ATTR 'const'
// | ATTR 'diagnostic' diagnostic_control
// | ATTR 'group' PAREN_LEFT expression COMMA? PAREN_RIGHT
// | ATTR 'id' PAREN_LEFT expression COMMA? PAREN_RIGHT
// | ATTR 'interpolate' PAREN_LEFT interpolation_type_name COMMA? PAREN_RIGHT
@ -3522,7 +3523,7 @@ Maybe<const ast::Attribute*> ParserImpl::attribute() {
using Result = Maybe<const ast::Attribute*>;
auto& t = next();
if (!t.IsIdentifier()) {
if (!t.IsIdentifier() && !(t.Is(Token::Type::kDiagnostic))) {
return Failure::kNoMatch;
}
@ -3576,6 +3577,14 @@ Maybe<const ast::Attribute*> ParserImpl::attribute() {
// Note, `const` is not valid in a WGSL source file, it's internal only
if (t.Is(Token::Type::kDiagnostic)) {
auto control = expect_diagnostic_control();
if (control.errored) {
return Failure::kErrored;
}
return create<ast::DiagnosticAttribute>(t.source(), control.value);
}
if (t == "fragment") {
return create<ast::StageAttribute>(t.source(), ast::PipelineStage::kFragment);
}

View File

@ -0,0 +1,36 @@
// Copyright 2023 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/reader/wgsl/parser_impl_test_helper.h"
#include "src/tint/ast/diagnostic_control.h"
namespace tint::reader::wgsl {
namespace {
TEST_F(ParserImplTest, DiagnosticAttribute_Valid) {
auto p = parser("diagnostic(off, foo)");
auto a = p->attribute();
EXPECT_FALSE(p->has_error()) << p->error();
EXPECT_TRUE(a.matched);
auto* d = a.value->As<ast::DiagnosticAttribute>();
ASSERT_NE(d, nullptr);
EXPECT_EQ(d->control->severity, ast::DiagnosticSeverity::kOff);
auto* r = As<ast::IdentifierExpression>(d->control->rule_name);
ASSERT_NE(r, nullptr);
EXPECT_EQ(p->builder().Symbols().NameFor(r->symbol), "foo");
}
} // namespace
} // namespace tint::reader::wgsl