[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 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<std::unique_ptr<ast::Statement>>& 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,7 +849,6 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
}
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
make_indent();
if (stmt->HasCondition()) {
out_ << " elseif (";
if (!EmitExpression(stmt->condition())) {
@ -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

View File

@ -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<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
/// @param stmt the statement to emit
/// @returns true if the statement was emitted

View File

@ -40,8 +40,7 @@ TEST_F(GeneratorImplTest, Emit_Else) {
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( else {
kill;
}
)");
})");
}
TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
@ -58,8 +57,7 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
EXPECT_EQ(g.result(), R"( elseif (cond) {
kill;
}
)");
})");
}
} // namespace

View File

@ -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;
}
)");