mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-15 08:06:19 +00:00
Add Diagnostics to Program, reader::[wgsl,spirv]::Parse()
By putting diagnostics into the program, we can hold all the diagnostic messages for parsing and type determination in one place. This also means that we can simplify the public WGSL and SPIR-V Parser interfaces to a single function. Change-Id: Ib6ab5fa180addd45c4aaf0c6b192d47182ffb50a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38920 Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Commit Bot service account
parent
21b52b60b6
commit
844217fa34
@@ -43,6 +43,17 @@ Program Parser::program() {
|
||||
return impl_->program();
|
||||
}
|
||||
|
||||
Program Parse(const std::vector<uint32_t>& input) {
|
||||
ParserImpl parser(input);
|
||||
bool parsed = parser.Parse();
|
||||
ProgramBuilder builder = std::move(parser.builder());
|
||||
if (!parsed) {
|
||||
// TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics.
|
||||
builder.Diagnostics().add_error(parser.error());
|
||||
}
|
||||
return Program(std::move(builder));
|
||||
}
|
||||
|
||||
} // namespace spirv
|
||||
} // namespace reader
|
||||
} // namespace tint
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace spirv {
|
||||
class ParserImpl;
|
||||
|
||||
/// Parser for SPIR-V source data
|
||||
/// [DEPRECATED] - Use Parse()
|
||||
class Parser : public Reader {
|
||||
public:
|
||||
/// Creates a new parser
|
||||
@@ -46,9 +47,16 @@ class Parser : public Reader {
|
||||
|
||||
private:
|
||||
std::unique_ptr<ParserImpl> impl_;
|
||||
Program program_;
|
||||
};
|
||||
|
||||
/// Parses the SPIR-V source data, returning the parsed program.
|
||||
/// If the source data fails to parse then the returned
|
||||
/// `program.Diagnostics.contains_errors()` will be true, and the
|
||||
/// `program.Diagnostics()` will describe the error.
|
||||
/// @param input the source data
|
||||
/// @returns the parsed program
|
||||
Program Parse(const std::vector<uint32_t>& input);
|
||||
|
||||
} // namespace spirv
|
||||
} // namespace reader
|
||||
} // namespace tint
|
||||
|
||||
@@ -26,13 +26,21 @@ namespace {
|
||||
|
||||
using ParserTest = testing::Test;
|
||||
|
||||
TEST_F(ParserTest, Uint32VecEmpty) {
|
||||
TEST_F(ParserTest, Uint32VecEmptyOld) {
|
||||
std::vector<uint32_t> data;
|
||||
Parser p(data);
|
||||
EXPECT_FALSE(p.Parse());
|
||||
// TODO(dneto): What message?
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, DataEmpty) {
|
||||
std::vector<uint32_t> data;
|
||||
auto program = Parse(data);
|
||||
auto errs = diag::Formatter().format(program.Diagnostics());
|
||||
ASSERT_FALSE(program.IsValid()) << errs;
|
||||
EXPECT_EQ(errs, "error: line:0: Invalid SPIR-V magic number.\n");
|
||||
}
|
||||
|
||||
// TODO(dneto): uint32 vec, valid SPIR-V
|
||||
// TODO(dneto): uint32 vec, invalid SPIR-V
|
||||
|
||||
|
||||
@@ -39,6 +39,16 @@ Program Parser::program() {
|
||||
return impl_->program();
|
||||
}
|
||||
|
||||
Program Parse(Source::File const* file) {
|
||||
ParserImpl parser(file);
|
||||
parser.Parse();
|
||||
ProgramBuilder builder = std::move(parser.builder());
|
||||
// TODO(bclayton): Remove ParserImpl::diagnostics() and put all diagnostic
|
||||
// into the builder.
|
||||
builder.Diagnostics().add(parser.diagnostics());
|
||||
return Program(std::move(builder));
|
||||
}
|
||||
|
||||
} // namespace wgsl
|
||||
} // namespace reader
|
||||
} // namespace tint
|
||||
|
||||
@@ -28,6 +28,7 @@ namespace wgsl {
|
||||
class ParserImpl;
|
||||
|
||||
/// Parser for WGSL source data
|
||||
/// [DEPRECATED] - Use Parse()
|
||||
class Parser : public Reader {
|
||||
public:
|
||||
/// Creates a new parser from the given file.
|
||||
@@ -47,6 +48,14 @@ class Parser : public Reader {
|
||||
std::unique_ptr<ParserImpl> impl_;
|
||||
};
|
||||
|
||||
/// Parses the WGSL source, returning the parsed program.
|
||||
/// If the source fails to parse then the returned
|
||||
/// `program.Diagnostics.contains_errors()` will be true, and the
|
||||
/// `program.Diagnostics()` will describe the error.
|
||||
/// @param file the source file
|
||||
/// @returns the parsed program
|
||||
Program Parse(Source::File const* file);
|
||||
|
||||
} // namespace wgsl
|
||||
} // namespace reader
|
||||
} // namespace tint
|
||||
|
||||
@@ -25,13 +25,13 @@ namespace {
|
||||
|
||||
using ParserTest = testing::Test;
|
||||
|
||||
TEST_F(ParserTest, Empty) {
|
||||
TEST_F(ParserTest, EmptyOld) {
|
||||
Source::File file("test.wgsl", "");
|
||||
Parser p(&file);
|
||||
ASSERT_TRUE(p.Parse()) << p.error();
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, Parses) {
|
||||
TEST_F(ParserTest, ParsesOld) {
|
||||
Source::File file("test.wgsl", R"(
|
||||
[[location(0)]] var<out> gl_FragColor : vec4<f32>;
|
||||
|
||||
@@ -48,7 +48,7 @@ fn main() -> void {
|
||||
ASSERT_EQ(1u, program.AST().GlobalVariables().size());
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, HandlesError) {
|
||||
TEST_F(ParserTest, HandlesErrorOld) {
|
||||
Source::File file("test.wgsl", R"(
|
||||
fn main() -> { // missing return type
|
||||
return;
|
||||
@@ -60,6 +60,48 @@ fn main() -> { // missing return type
|
||||
EXPECT_EQ(p.error(), "2:15: unable to determine function return type");
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, Empty) {
|
||||
Source::File file("test.wgsl", "");
|
||||
auto program = Parse(&file);
|
||||
auto errs = diag::Formatter().format(program.Diagnostics());
|
||||
ASSERT_TRUE(program.IsValid()) << errs;
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, Parses) {
|
||||
Source::File file("test.wgsl", R"(
|
||||
[[location(0)]] var<out> gl_FragColor : vec4<f32>;
|
||||
|
||||
[[stage(vertex)]]
|
||||
fn main() -> void {
|
||||
gl_FragColor = vec4<f32>(.4, .2, .3, 1);
|
||||
}
|
||||
)");
|
||||
Parser p(&file);
|
||||
auto program = Parse(&file);
|
||||
auto errs = diag::Formatter().format(program.Diagnostics());
|
||||
ASSERT_TRUE(program.IsValid()) << errs;
|
||||
|
||||
ASSERT_EQ(1u, program.AST().Functions().size());
|
||||
ASSERT_EQ(1u, program.AST().GlobalVariables().size());
|
||||
}
|
||||
|
||||
TEST_F(ParserTest, HandlesError) {
|
||||
Source::File file("test.wgsl", R"(
|
||||
fn main() -> { // missing return type
|
||||
return;
|
||||
})");
|
||||
|
||||
auto program = Parse(&file);
|
||||
auto errs = diag::Formatter().format(program.Diagnostics());
|
||||
ASSERT_FALSE(program.IsValid()) << errs;
|
||||
EXPECT_EQ(errs,
|
||||
R"(test.wgsl:2:15 error: unable to determine function return type
|
||||
fn main() -> { // missing return type
|
||||
^
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace wgsl
|
||||
} // namespace reader
|
||||
|
||||
Reference in New Issue
Block a user