transform: LoopToForLoop - fix bad emission

For loops only support assignments or function calls for the continuing statement.

Fixed: tint:1064
Change-Id: I07065b2119e7b9f97ca7e46b1464fd72333ca429
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/60212
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2021-07-29 18:05:19 +00:00
parent a52324fde1
commit ed60a9905c
9 changed files with 213 additions and 128 deletions

View File

@@ -89,7 +89,8 @@ void LoopToForLoop::Run(CloneContext& ctx, const DataMap&, DataMap&) {
return nullptr;
}
// The continuing block must be empty or contain a single statement
// The continuing block must be empty or contain a single, assignment or
// function call statement.
ast::Statement* continuing = nullptr;
if (auto* loop_cont = loop->continuing()) {
if (loop_cont->statements().size() != 1) {
@@ -97,6 +98,10 @@ void LoopToForLoop::Run(CloneContext& ctx, const DataMap&, DataMap&) {
}
continuing = loop_cont->statements()[0];
if (!continuing
->IsAnyOf<ast::AssignmentStatement, ast::CallStatement>()) {
return nullptr;
}
// And the continuing statement must not use any of the variables declared
// in the loop body.

View File

@@ -208,6 +208,32 @@ fn f() {
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_ContinuingIsCompound) {
auto* src = R"(
fn f() {
var i : i32;
i = 0;
loop {
if ((i < 15)) {
break;
}
ignore(123);
continuing {
if (false) {
}
}
}
}
)";
auto* expect = src;
auto got = Run<LoopToForLoop>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(LoopToForLoopTest, NoTransform_ContinuingMultipleStmts) {
auto* src = R"(
fn f() {