tint/reader/wgsl: Add source info to loop bodies

Also for continuing blocks.

Change-Id: Ic4a5f30fc0b882f1051c4995bd2b228c5ccc6d17
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118321
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
This commit is contained in:
James Price 2023-02-02 13:01:42 +00:00 committed by Dawn LUCI CQ
parent f6a48ee351
commit 92226dbfc3
2 changed files with 40 additions and 7 deletions

View File

@ -2258,20 +2258,30 @@ Maybe<const ast::LoopStatement*> ParserImpl::loop_statement() {
return Failure::kNoMatch; return Failure::kNoMatch;
} }
return expect_brace_block("loop", [&]() -> Maybe<const ast::LoopStatement*> { Maybe<const ast::BlockStatement*> continuing(Failure::kErrored);
auto body_start = peek().source();
auto body = expect_brace_block("loop", [&]() -> Maybe<StatementList> {
auto stmts = expect_statements(); auto stmts = expect_statements();
if (stmts.errored) { if (stmts.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
auto continuing = continuing_statement(); continuing = continuing_statement();
if (continuing.errored) { if (continuing.errored) {
return Failure::kErrored; return Failure::kErrored;
} }
return stmts;
auto* body = create<ast::BlockStatement>(source, stmts.value, utils::Empty);
return create<ast::LoopStatement>(source, body, continuing.value);
}); });
if (body.errored) {
return Failure::kErrored;
}
auto body_end = last_source();
return create<ast::LoopStatement>(
source,
create<ast::BlockStatement>(Source::Combine(body_start, body_end), body.value,
utils::Empty),
continuing.value);
} }
ForHeader::ForHeader(const ast::Statement* init, ForHeader::ForHeader(const ast::Statement* init,
@ -2482,7 +2492,8 @@ Maybe<const ast::Statement*> ParserImpl::break_if_statement() {
// continuing_compound_statement: // continuing_compound_statement:
// brace_left statement* break_if_statement? brace_right // brace_left statement* break_if_statement? brace_right
Maybe<const ast::BlockStatement*> ParserImpl::continuing_compound_statement() { Maybe<const ast::BlockStatement*> ParserImpl::continuing_compound_statement() {
return expect_brace_block("", [&]() -> Expect<ast::BlockStatement*> { auto source_start = peek().source();
auto body = expect_brace_block("", [&]() -> Expect<StatementList> {
StatementList stmts; StatementList stmts;
while (continue_parsing()) { while (continue_parsing()) {
@ -2506,8 +2517,15 @@ Maybe<const ast::BlockStatement*> ParserImpl::continuing_compound_statement() {
stmts.Push(stmt.value); stmts.Push(stmt.value);
} }
return create<ast::BlockStatement>(Source{}, stmts, utils::Empty); return stmts;
}); });
if (body.errored) {
return Failure::kErrored;
}
auto source_end = last_source();
return create<ast::BlockStatement>(Source::Combine(source_start, source_end), body.value,
utils::Empty);
} }
// continuing_statement // continuing_statement

View File

@ -26,6 +26,11 @@ TEST_F(ParserImplTest, LoopStmt_BodyNoContinuing) {
EXPECT_FALSE(p->has_error()) << p->error(); EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_NE(e.value, nullptr); ASSERT_NE(e.value, nullptr);
EXPECT_EQ(e->body->source.range.begin.line, 1u);
EXPECT_EQ(e->body->source.range.begin.column, 6u);
EXPECT_EQ(e->body->source.range.end.line, 1u);
EXPECT_EQ(e->body->source.range.end.column, 18u);
ASSERT_EQ(e->body->statements.Length(), 1u); ASSERT_EQ(e->body->statements.Length(), 1u);
EXPECT_TRUE(e->body->statements[0]->Is<ast::DiscardStatement>()); EXPECT_TRUE(e->body->statements[0]->Is<ast::DiscardStatement>());
@ -40,11 +45,21 @@ TEST_F(ParserImplTest, LoopStmt_BodyWithContinuing) {
EXPECT_FALSE(p->has_error()) << p->error(); EXPECT_FALSE(p->has_error()) << p->error();
ASSERT_NE(e.value, nullptr); ASSERT_NE(e.value, nullptr);
EXPECT_EQ(e->body->source.range.begin.line, 1u);
EXPECT_EQ(e->body->source.range.begin.column, 6u);
EXPECT_EQ(e->body->source.range.end.line, 1u);
EXPECT_EQ(e->body->source.range.end.column, 41u);
ASSERT_EQ(e->body->statements.Length(), 1u); ASSERT_EQ(e->body->statements.Length(), 1u);
EXPECT_TRUE(e->body->statements[0]->Is<ast::DiscardStatement>()); EXPECT_TRUE(e->body->statements[0]->Is<ast::DiscardStatement>());
EXPECT_EQ(e->continuing->statements.Length(), 1u); EXPECT_EQ(e->continuing->statements.Length(), 1u);
EXPECT_TRUE(e->continuing->statements[0]->Is<ast::DiscardStatement>()); EXPECT_TRUE(e->continuing->statements[0]->Is<ast::DiscardStatement>());
EXPECT_EQ(e->continuing->source.range.begin.line, 1u);
EXPECT_EQ(e->continuing->source.range.begin.column, 28u);
EXPECT_EQ(e->continuing->source.range.end.line, 1u);
EXPECT_EQ(e->continuing->source.range.end.column, 40u);
} }
TEST_F(ParserImplTest, LoopStmt_NoBodyNoContinuing) { TEST_F(ParserImplTest, LoopStmt_NoBodyNoContinuing) {