From 46252ec3ba0053d08bbc76d15a40263910f7b90a Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Thu, 27 Aug 2020 18:17:10 +0000 Subject: [PATCH] [wgsl-parser] Consume empty struct closing brace. This CL updates the WGSL parser to consume the closing } of an empty struct. Bug: tint:218 Change-Id: I0b17d2178b1b4b7f44fcf007da867db07dc2a6ae Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27600 Reviewed-by: Sarah Mashayekhi Commit-Queue: dan sinclair --- src/reader/wgsl/parser_impl.cc | 15 ++++++--------- src/reader/wgsl/parser_impl_type_alias_test.cc | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 7e3064d62a..fdc9df7c6f 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -1021,12 +1021,6 @@ std::unique_ptr ParserImpl::struct_decl() { return nullptr; } - t = peek(); - if (!t.IsBraceLeft()) { - set_error(t, "missing { for struct declaration"); - return nullptr; - } - auto body = struct_body_decl(); if (has_error()) { return nullptr; @@ -1075,14 +1069,17 @@ ast::StructDecoration ParserImpl::struct_decoration(Token t) { // : BRACKET_LEFT struct_member* BRACKET_RIGHT ast::StructMemberList ParserImpl::struct_body_decl() { auto t = peek(); - if (!t.IsBraceLeft()) + if (!t.IsBraceLeft()) { + set_error(t, "missing { for struct declaration"); return {}; - + } next(); // Consume the peek t = peek(); - if (t.IsBraceRight()) + if (t.IsBraceRight()) { + next(); // Consume the peek return {}; + } ast::StructMemberList members; for (;;) { diff --git a/src/reader/wgsl/parser_impl_type_alias_test.cc b/src/reader/wgsl/parser_impl_type_alias_test.cc index 383a0359fc..2995881b60 100644 --- a/src/reader/wgsl/parser_impl_type_alias_test.cc +++ b/src/reader/wgsl/parser_impl_type_alias_test.cc @@ -109,6 +109,24 @@ TEST_F(ParserImplTest, TypeDecl_Struct_WithStride) { EXPECT_EQ(arr->array_stride(), 4u); } +// This was failing due to not finding the missing ;. https://crbug.com/tint/218 +TEST_F(ParserImplTest, TypeDecl_Struct_Empty) { + auto* p = parser("type str = struct {};"); + p->global_decl(); + ASSERT_FALSE(p->has_error()) << p->error(); + + auto module = p->module(); + ASSERT_EQ(module.alias_types().size(), 1u); + + auto* t = module.alias_types()[0]; + ASSERT_NE(t, nullptr); + EXPECT_EQ(t->name(), "str"); + + ASSERT_TRUE(t->type()->IsStruct()); + auto* s = t->type()->AsStruct(); + EXPECT_EQ(s->impl()->members().size(), 0u); +} + } // namespace } // namespace wgsl } // namespace reader