[wgsl-writer] Restore huggie-style braces

Change-Id: Ieec33cccbfeb2589d1292103001f0de5267e02bb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18040
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-03-30 19:56:59 +00:00
parent 15cbc98a8c
commit b020cd900e
4 changed files with 32 additions and 24 deletions

View File

@ -363,7 +363,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) {
return false; return false;
} }
return EmitStatementBlock(func->body()); return EmitStatementBlockAndNewline(func->body());
} }
bool GeneratorImpl::EmitType(ast::type::Type* type) { bool GeneratorImpl::EmitType(ast::type::Type* type) {
@ -704,11 +704,20 @@ bool GeneratorImpl::EmitStatementBlock(
decrement_indent(); decrement_indent();
make_indent(); make_indent();
out_ << "}" << std::endl; out_ << "}";
return true; return true;
} }
bool GeneratorImpl::EmitStatementBlockAndNewline(
const std::vector<std::unique_ptr<ast::Statement>>& statements) {
const bool result = EmitStatementBlock(statements);
if (result) {
out_ << std::endl;
}
return result;
}
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) { if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign()); return EmitAssign(stmt->AsAssign());
@ -811,7 +820,7 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
out_ << ":"; out_ << ":";
} }
return EmitStatementBlock(stmt->body()); return EmitStatementBlockAndNewline(stmt->body());
} }
bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) { bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
@ -840,15 +849,14 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
} }
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
make_indent();
if (stmt->HasCondition()) { if (stmt->HasCondition()) {
out_ << "elseif ("; out_ << " elseif (";
if (!EmitExpression(stmt->condition())) { if (!EmitExpression(stmt->condition())) {
return false; return false;
} }
out_ << ")"; out_ << ")";
} else { } else {
out_ << "else"; out_ << " else";
} }
return EmitStatementBlock(stmt->body()); return EmitStatementBlock(stmt->body());
@ -878,6 +886,7 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
return false; return false;
} }
} }
out_ << std::endl;
return true; return true;
} }
@ -906,7 +915,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
make_indent(); make_indent();
out_ << "continuing"; out_ << "continuing";
if (!EmitStatementBlock(stmt->continuing())) { if (!EmitStatementBlockAndNewline(stmt->continuing())) {
return false; return false;
} }
} }
@ -933,7 +942,7 @@ bool GeneratorImpl::EmitRegardless(ast::RegardlessStatement* stmt) {
} }
out_ << ")"; out_ << ")";
return EmitStatementBlock(stmt->body()); return EmitStatementBlockAndNewline(stmt->body());
} }
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
@ -983,7 +992,7 @@ bool GeneratorImpl::EmitUnless(ast::UnlessStatement* stmt) {
} }
out_ << ")"; out_ << ")";
return EmitStatementBlock(stmt->body()); return EmitStatementBlockAndNewline(stmt->body());
} }
} // namespace wgsl } // namespace wgsl

View File

@ -176,11 +176,16 @@ class GeneratorImpl {
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt); bool EmitReturn(ast::ReturnStatement* stmt);
/// Handles a list of statements /// Handles a brace-enclosed list of statements.
/// @param statements the statements to output /// @param statements the statements to output
/// @returns true if the statements were emitted /// @returns true if the statements were emitted
bool EmitStatementBlock( bool EmitStatementBlock(
const std::vector<std::unique_ptr<ast::Statement>>& statements); const std::vector<std::unique_ptr<ast::Statement>>& 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<std::unique_ptr<ast::Statement>>& statements);
/// Handles statement /// Handles statement
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted

View File

@ -38,10 +38,9 @@ TEST_F(GeneratorImplTest, Emit_Else) {
g.increment_indent(); g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error(); ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( else { EXPECT_EQ(g.result(), R"( else {
kill; kill;
} })");
)");
} }
TEST_F(GeneratorImplTest, Emit_ElseWithCondition) { TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
@ -56,10 +55,9 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
g.increment_indent(); g.increment_indent();
ASSERT_TRUE(g.EmitElse(&e)) << g.error(); ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( elseif (cond) { EXPECT_EQ(g.result(), R"( elseif (cond) {
kill; kill;
} })");
)");
} }
} // namespace } // namespace

View File

@ -69,8 +69,7 @@ TEST_F(GeneratorImplTest, Emit_IfWithElseIf) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} } elseif (else_cond) {
elseif (else_cond) {
kill; kill;
} }
)"); )");
@ -96,8 +95,7 @@ TEST_F(GeneratorImplTest, Emit_IfWithElse) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} } else {
else {
kill; kill;
} }
)"); )");
@ -130,11 +128,9 @@ TEST_F(GeneratorImplTest, Emit_IfWithMultiple) {
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(g.result(), R"( if (cond) {
kill; kill;
} } elseif (else_cond) {
elseif (else_cond) {
kill; kill;
} } else {
else {
kill; kill;
} }
)"); )");