[wgsl-reader] Fix issue with missing open brace for functions.

This Cl fixes the function parsing so the opening brace is required.
This will return an error now. The statement::body_stmt is changed to
make the open paren optional.

Bug: tint:236
Change-Id: I3faee1fd5add19aebcdb86943d6f4190c03d0f7a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28520
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-09-21 15:55:32 +00:00 committed by Commit Bot service account
parent 43d17780ca
commit cb907b5bc2
2 changed files with 24 additions and 29 deletions

View File

@ -2089,19 +2089,21 @@ ast::PipelineStage ParserImpl::pipeline_stage() {
// : BRACKET_LEFT statements BRACKET_RIGHT // : BRACKET_LEFT statements BRACKET_RIGHT
std::unique_ptr<ast::BlockStatement> ParserImpl::body_stmt() { std::unique_ptr<ast::BlockStatement> ParserImpl::body_stmt() {
auto t = peek(); auto t = peek();
if (!t.IsBraceLeft()) if (!t.IsBraceLeft()) {
return {}; set_error(t, "missing {");
return nullptr;
}
next(); // Consume the peek next(); // Consume the peek
auto stmts = statements(); auto stmts = statements();
if (has_error()) if (has_error())
return {}; return nullptr;
t = next(); t = next();
if (!t.IsBraceRight()) { if (!t.IsBraceRight()) {
set_error(t, "missing }"); set_error(t, "missing }");
return {}; return nullptr;
} }
return stmts; return stmts;
@ -2165,7 +2167,7 @@ std::unique_ptr<ast::BlockStatement> ParserImpl::statements() {
// | continue_stmt SEMICOLON // | continue_stmt SEMICOLON
// | DISCARD SEMICOLON // | DISCARD SEMICOLON
// | assignment_stmt SEMICOLON // | assignment_stmt SEMICOLON
// | body_stmt // | body_stmt?
std::unique_ptr<ast::Statement> ParserImpl::statement() { std::unique_ptr<ast::Statement> ParserImpl::statement() {
auto t = peek(); auto t = peek();
if (t.IsSemicolon()) { if (t.IsSemicolon()) {
@ -2281,11 +2283,14 @@ std::unique_ptr<ast::Statement> ParserImpl::statement() {
return assign; return assign;
} }
auto body = body_stmt(); t = peek();
if (has_error()) if (t.IsBraceLeft()) {
return nullptr; auto body = body_stmt();
if (body != nullptr) if (has_error())
return body; return nullptr;
if (body != nullptr)
return body;
}
return nullptr; return nullptr;
} }
@ -2392,12 +2397,6 @@ std::unique_ptr<ast::IfStatement> ParserImpl::if_stmt() {
return nullptr; return nullptr;
} }
t = peek();
if (!t.IsBraceLeft()) {
set_error(t, "missing {");
return nullptr;
}
auto body = body_stmt(); auto body = body_stmt();
if (has_error()) if (has_error())
return nullptr; return nullptr;
@ -2440,12 +2439,6 @@ ast::ElseStatementList ParserImpl::elseif_stmt() {
return {}; return {};
} }
t = peek();
if (!t.IsBraceLeft()) {
set_error(t, "missing {");
return {};
}
auto body = body_stmt(); auto body = body_stmt();
if (has_error()) if (has_error())
return {}; return {};
@ -2471,12 +2464,6 @@ std::unique_ptr<ast::ElseStatement> ParserImpl::else_stmt() {
auto source = t.source(); auto source = t.source();
next(); // Consume the peek next(); // Consume the peek
t = peek();
if (!t.IsBraceLeft()) {
set_error(t, "missing {");
return nullptr;
}
auto body = body_stmt(); auto body = body_stmt();
if (has_error()) if (has_error())
return nullptr; return nullptr;
@ -2889,8 +2876,8 @@ std::unique_ptr<ast::BlockStatement> ParserImpl::continuing_stmt() {
if (!t.IsContinuing()) { if (!t.IsContinuing()) {
return std::make_unique<ast::BlockStatement>(); return std::make_unique<ast::BlockStatement>();
} }
next(); // Consume the peek next(); // Consume the peek
return body_stmt(); return body_stmt();
} }

View File

@ -169,6 +169,14 @@ TEST_F(ParserImplTest, FunctionDecl_InvalidBody) {
EXPECT_EQ(p->error(), "1:28: missing ;"); EXPECT_EQ(p->error(), "1:28: missing ;");
} }
TEST_F(ParserImplTest, FunctionDecl_MissingLeftBrace) {
auto* p = parser("fn main() -> void return; }");
auto f = p->function_decl();
ASSERT_TRUE(p->has_error());
ASSERT_EQ(f, nullptr);
EXPECT_EQ(p->error(), "1:19: missing {");
}
} // namespace } // namespace
} // namespace wgsl } // namespace wgsl
} // namespace reader } // namespace reader