diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index a74c48e54c..c288b9d145 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -363,7 +363,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) { return false; } - return EmitStatementBlock(func->body()); + return EmitStatementBlockAndNewline(func->body()); } bool GeneratorImpl::EmitType(ast::type::Type* type) { @@ -704,11 +704,20 @@ bool GeneratorImpl::EmitStatementBlock( decrement_indent(); make_indent(); - out_ << "}" << std::endl; + out_ << "}"; return true; } +bool GeneratorImpl::EmitStatementBlockAndNewline( + const std::vector>& statements) { + const bool result = EmitStatementBlock(statements); + if (result) { + out_ << std::endl; + } + return result; +} + bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsAssign()) { return EmitAssign(stmt->AsAssign()); @@ -811,7 +820,7 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { out_ << ":"; } - return EmitStatementBlock(stmt->body()); + return EmitStatementBlockAndNewline(stmt->body()); } bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) { @@ -840,15 +849,14 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) { } bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { - make_indent(); if (stmt->HasCondition()) { - out_ << "elseif ("; + out_ << " elseif ("; if (!EmitExpression(stmt->condition())) { return false; } out_ << ")"; } else { - out_ << "else"; + out_ << " else"; } return EmitStatementBlock(stmt->body()); @@ -878,6 +886,7 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return false; } } + out_ << std::endl; return true; } @@ -906,7 +915,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { make_indent(); out_ << "continuing"; - if (!EmitStatementBlock(stmt->continuing())) { + if (!EmitStatementBlockAndNewline(stmt->continuing())) { return false; } } @@ -933,7 +942,7 @@ bool GeneratorImpl::EmitRegardless(ast::RegardlessStatement* stmt) { } out_ << ")"; - return EmitStatementBlock(stmt->body()); + return EmitStatementBlockAndNewline(stmt->body()); } bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { @@ -983,7 +992,7 @@ bool GeneratorImpl::EmitUnless(ast::UnlessStatement* stmt) { } out_ << ")"; - return EmitStatementBlock(stmt->body()); + return EmitStatementBlockAndNewline(stmt->body()); } } // namespace wgsl diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index e77ee95aef..8ec4572e31 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -176,11 +176,16 @@ class GeneratorImpl { /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted bool EmitReturn(ast::ReturnStatement* stmt); - /// Handles a list of statements + /// Handles a brace-enclosed list of statements. /// @param statements the statements to output /// @returns true if the statements were emitted bool EmitStatementBlock( const std::vector>& statements); + /// Handles a brace-enclosed list of statements and trailing newline. + /// @param statements the statements to output + /// @returns true if the statements were emitted + bool EmitStatementBlockAndNewline( + const std::vector>& statements); /// Handles statement /// @param stmt the statement to emit /// @returns true if the statement was emitted diff --git a/src/writer/wgsl/generator_impl_else_test.cc b/src/writer/wgsl/generator_impl_else_test.cc index 0d1d98a4f0..561babf9ac 100644 --- a/src/writer/wgsl/generator_impl_else_test.cc +++ b/src/writer/wgsl/generator_impl_else_test.cc @@ -38,10 +38,9 @@ TEST_F(GeneratorImplTest, Emit_Else) { g.increment_indent(); ASSERT_TRUE(g.EmitElse(&e)) << g.error(); - EXPECT_EQ(g.result(), R"( else { + EXPECT_EQ(g.result(), R"( else { kill; - } -)"); + })"); } TEST_F(GeneratorImplTest, Emit_ElseWithCondition) { @@ -56,10 +55,9 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) { g.increment_indent(); ASSERT_TRUE(g.EmitElse(&e)) << g.error(); - EXPECT_EQ(g.result(), R"( elseif (cond) { + EXPECT_EQ(g.result(), R"( elseif (cond) { kill; - } -)"); + })"); } } // namespace diff --git a/src/writer/wgsl/generator_impl_if_test.cc b/src/writer/wgsl/generator_impl_if_test.cc index cde3640d8e..274c7c0d64 100644 --- a/src/writer/wgsl/generator_impl_if_test.cc +++ b/src/writer/wgsl/generator_impl_if_test.cc @@ -69,8 +69,7 @@ TEST_F(GeneratorImplTest, Emit_IfWithElseIf) { ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); EXPECT_EQ(g.result(), R"( if (cond) { kill; - } - elseif (else_cond) { + } elseif (else_cond) { kill; } )"); @@ -96,8 +95,7 @@ TEST_F(GeneratorImplTest, Emit_IfWithElse) { ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); EXPECT_EQ(g.result(), R"( if (cond) { kill; - } - else { + } else { kill; } )"); @@ -130,11 +128,9 @@ TEST_F(GeneratorImplTest, Emit_IfWithMultiple) { ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); EXPECT_EQ(g.result(), R"( if (cond) { kill; - } - elseif (else_cond) { + } elseif (else_cond) { kill; - } - else { + } else { kill; } )");