From 16edcf9b034f7b8d5629e807185e3c295aae6c28 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 31 Aug 2021 18:58:26 +0000 Subject: [PATCH] reader/wgsl: Error for unconsumed decorations When applied to valid module-scope declarations. Fixed: chromium:1244349 Change-Id: Icb19200cae751ac70974481693ecbcf48fd627f0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/63160 Auto-Submit: Ben Clayton Commit-Queue: James Price Kokoro: Kokoro Reviewed-by: James Price --- src/reader/wgsl/parser_impl.cc | 17 +++++++++------ src/reader/wgsl/parser_impl_error_msg_test.cc | 21 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 18ae65656d..637375d8f1 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -427,27 +427,32 @@ Expect ParserImpl::expect_global_decl() { return Failure::kNoMatch; }); - if (decl.errored) + if (decl.errored) { errored = true; - if (decl.matched) - return true; + } + if (decl.matched) { + return expect_decorations_consumed(decos.value); + } auto func = function_decl(decos.value); - if (func.errored) + if (func.errored) { errored = true; + } if (func.matched) { builder_.AST().AddFunction(func.value); return true; } - if (errored) + if (errored) { return Failure::kErrored; + } // Invalid syntax found - try and determine the best error message // We have decorations parsed, but nothing to consume them? - if (decos.value.size() > 0) + if (decos.value.size() > 0) { return add_error(next(), "expected declaration after decorations"); + } // We have a statement outside of a function? auto t = peek(); diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc index 65e8eafdf3..07007b4857 100644 --- a/src/reader/wgsl/parser_impl_error_msg_test.cc +++ b/src/reader/wgsl/parser_impl_error_msg_test.cc @@ -51,6 +51,13 @@ TEST_F(ParserImplErrorTest, AndInvalidExpr) { " ^\n"); } +TEST_F(ParserImplErrorTest, AliasDeclInvalidDeco) { + EXPECT("[[block]]type e=u32;", + "test.wgsl:1:3 error: unexpected decorations\n" + "[[block]]type e=u32;\n" + " ^^^^^\n"); +} + TEST_F(ParserImplErrorTest, ArrayIndexExprInvalidExpr) { EXPECT("fn f() { x = y[^]; }", "test.wgsl:1:16 error: unable to parse expression inside []\n" @@ -275,13 +282,6 @@ TEST_F(ParserImplErrorTest, ForLoopMissingRBrace) { " ^\n"); } -TEST_F(ParserImplErrorTest, FunctionDeclInvalid) { - EXPECT("[[stage(vertex)]] x;", - "test.wgsl:1:19 error: expected declaration after decorations\n" - "[[stage(vertex)]] x;\n" - " ^\n"); -} - TEST_F(ParserImplErrorTest, FunctionDeclDecoMissingEnd) { EXPECT("[[stage(vertex) fn f() {}", "test.wgsl:1:17 error: expected ']]' for decoration list\n" @@ -551,6 +551,13 @@ TEST_F(ParserImplErrorTest, GlobalDeclConstExprMissingRParen) { " ^\n"); } +TEST_F(ParserImplErrorTest, GlobalDeclInvalidDeco) { + EXPECT("[[stage(vertex)]] x;", + "test.wgsl:1:19 error: expected declaration after decorations\n" + "[[stage(vertex)]] x;\n" + " ^\n"); +} + TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingLessThan) { EXPECT("var x : texture_1d;", "test.wgsl:1:19 error: expected '<' for sampled texture type\n"