tint/reader/wgsl: Add source info to compound statements

Change-Id: I251dd3d061a32042368b170ba2a38d16e8b2d9dc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118320
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2023-02-02 12:40:53 +00:00 committed by Dawn LUCI CQ
parent 0eea3c5114
commit f6a48ee351
2 changed files with 22 additions and 8 deletions

View File

@ -1679,14 +1679,16 @@ Expect<ast::BlockStatement*> ParserImpl::expect_compound_statement(std::string_v
// : attribute* BRACE_LEFT statement* BRACE_RIGHT // : attribute* BRACE_LEFT statement* BRACE_RIGHT
Expect<ast::BlockStatement*> ParserImpl::expect_compound_statement(AttributeList& attrs, Expect<ast::BlockStatement*> ParserImpl::expect_compound_statement(AttributeList& attrs,
std::string_view use) { std::string_view use) {
return expect_brace_block(use, [&]() -> Expect<ast::BlockStatement*> { auto source_start = peek().source();
auto stmts = expect_statements(); auto stmts =
if (stmts.errored) { expect_brace_block(use, [&]() -> Expect<StatementList> { return expect_statements(); });
return Failure::kErrored; auto source_end = last_source();
} if (stmts.errored) {
TINT_DEFER(attrs.Clear()); return Failure::kErrored;
return create<ast::BlockStatement>(Source{}, stmts.value, std::move(attrs)); }
}); TINT_DEFER(attrs.Clear());
return create<ast::BlockStatement>(Source::Combine(source_start, source_end), stmts.value,
std::move(attrs));
} }
// paren_expression // paren_expression

View File

@ -24,6 +24,12 @@ TEST_F(ParserImplTest, CompoundStmt) {
return 1 + b / 2; return 1 + b / 2;
})"); })");
auto e = p->expect_compound_statement(""); auto e = p->expect_compound_statement("");
EXPECT_EQ(e->source.range.begin.line, 1u);
EXPECT_EQ(e->source.range.begin.column, 1u);
EXPECT_EQ(e->source.range.end.line, 4u);
EXPECT_EQ(e->source.range.end.column, 2u);
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(e.errored); ASSERT_FALSE(e.errored);
ASSERT_EQ(e->statements.Length(), 2u); ASSERT_EQ(e->statements.Length(), 2u);
@ -34,6 +40,12 @@ TEST_F(ParserImplTest, CompoundStmt) {
TEST_F(ParserImplTest, CompoundStmt_Empty) { TEST_F(ParserImplTest, CompoundStmt_Empty) {
auto p = parser("{}"); auto p = parser("{}");
auto e = p->expect_compound_statement(""); auto e = p->expect_compound_statement("");
EXPECT_EQ(e->source.range.begin.line, 1u);
EXPECT_EQ(e->source.range.begin.column, 1u);
EXPECT_EQ(e->source.range.end.line, 1u);
EXPECT_EQ(e->source.range.end.column, 3u);
ASSERT_FALSE(p->has_error()) << p->error(); ASSERT_FALSE(p->has_error()) << p->error();
ASSERT_FALSE(e.errored); ASSERT_FALSE(e.errored);
EXPECT_EQ(e->statements.Length(), 0u); EXPECT_EQ(e->statements.Length(), 0u);