reader/wgsl: Remove ParserImpl::diagnostics()

Put all errors straight into the ProgramBuilder::Diagnostics()

Fixes a TODO. Kills an assert().

Bug: chromium:1185569
Change-Id: I4e6f3b06106c3cfe75cf2bcdfc56b14ad73e81d9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44046
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-03-08 16:17:55 +00:00 committed by Commit Bot service account
parent acc7712e25
commit fe2ceb9b5d
5 changed files with 24 additions and 37 deletions

View File

@ -25,11 +25,7 @@ namespace wgsl {
Program Parse(Source::File const* file) { Program Parse(Source::File const* file) {
ParserImpl parser(file); ParserImpl parser(file);
parser.Parse(); parser.Parse();
ProgramBuilder builder = std::move(parser.builder()); return Program(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 wgsl

View File

@ -241,7 +241,7 @@ ParserImpl::Failure::Errored ParserImpl::add_error(const Token& t,
ParserImpl::Failure::Errored ParserImpl::add_error(const Source& source, ParserImpl::Failure::Errored ParserImpl::add_error(const Source& source,
const std::string& err) { const std::string& err) {
if (silence_errors_ == 0) { if (silence_errors_ == 0) {
diags_.add_error(err, source); builder_.Diagnostics().add_error(err, source);
} }
return Failure::kErrored; return Failure::kErrored;
} }
@ -293,14 +293,12 @@ void ParserImpl::translation_unit() {
break; break;
} }
expect_global_decl(); expect_global_decl();
if (diags_.error_count() >= max_errors_) { if (builder_.Diagnostics().error_count() >= max_errors_) {
add_error(Source{{}, p.source().file_path}, add_error(Source{{}, p.source().file_path},
"stopping after " + std::to_string(max_errors_) + " errors"); "stopping after " + std::to_string(max_errors_) + " errors");
break; break;
} }
} }
assert(builder_.IsValid());
} }
// global_decl // global_decl

View File

@ -293,20 +293,14 @@ class ParserImpl {
size_t get_max_errors() const { return max_errors_; } size_t get_max_errors() const { return max_errors_; }
/// @returns true if an error was encountered. /// @returns true if an error was encountered.
bool has_error() const { return diags_.contains_errors(); } bool has_error() const { return builder_.Diagnostics().contains_errors(); }
/// @returns the parser error string /// @returns the parser error string
std::string error() const { std::string error() const {
diag::Formatter formatter{{false, false, false, false}}; diag::Formatter formatter{{false, false, false, false}};
return formatter.format(diags_); return formatter.format(builder_.Diagnostics());
} }
/// @returns the diagnostic messages
const diag::List& diagnostics() const { return diags_; }
/// @returns the diagnostic messages
diag::List& diagnostics() { return diags_; }
/// @returns the Program. The program builder in the parser will be reset /// @returns the Program. The program builder in the parser will be reset
/// after this. /// after this.
Program program() { return Program(std::move(builder_)); } Program program() { return Program(std::move(builder_)); }
@ -827,7 +821,6 @@ class ParserImpl {
return builder_.create<T>(std::forward<ARGS>(args)...); return builder_.create<T>(std::forward<ARGS>(args)...);
} }
diag::List diags_;
std::unique_ptr<Lexer> lexer_; std::unique_ptr<Lexer> lexer_;
std::deque<Token> token_queue_; std::deque<Token> token_queue_;
bool synchronized_ = true; bool synchronized_ = true;

View File

@ -27,16 +27,16 @@ const diag::Formatter::Style formatter_style{
class ParserImplErrorTest : public ParserImplTest {}; class ParserImplErrorTest : public ParserImplTest {};
#define EXPECT(SOURCE, EXPECTED) \ #define EXPECT(SOURCE, EXPECTED) \
do { \ do { \
std::string source = SOURCE; \ std::string source = SOURCE; \
std::string expected = EXPECTED; \ std::string expected = EXPECTED; \
auto p = parser(source); \ auto p = parser(source); \
p->set_max_errors(5); \ p->set_max_errors(5); \
EXPECT_EQ(false, p->Parse()); \ EXPECT_EQ(false, p->Parse()); \
EXPECT_EQ(true, p->diagnostics().contains_errors()); \ auto diagnostics = p->builder().Diagnostics(); \
EXPECT_EQ(expected, \ EXPECT_EQ(true, diagnostics.contains_errors()); \
diag::Formatter(formatter_style).format(p->diagnostics())); \ EXPECT_EQ(expected, diag::Formatter(formatter_style).format(diagnostics)); \
} while (false) } while (false)
TEST_F(ParserImplErrorTest, AdditiveInvalidExpr) { TEST_F(ParserImplErrorTest, AdditiveInvalidExpr) {

View File

@ -27,15 +27,15 @@ const diag::Formatter::Style formatter_style{
class ParserImplErrorResyncTest : public ParserImplTest {}; class ParserImplErrorResyncTest : public ParserImplTest {};
#define EXPECT(SOURCE, EXPECTED) \ #define EXPECT(SOURCE, EXPECTED) \
do { \ do { \
std::string source = SOURCE; \ std::string source = SOURCE; \
std::string expected = EXPECTED; \ std::string expected = EXPECTED; \
auto p = parser(source); \ auto p = parser(source); \
EXPECT_EQ(false, p->Parse()); \ EXPECT_EQ(false, p->Parse()); \
EXPECT_EQ(true, p->diagnostics().contains_errors()); \ auto diagnostics = p->builder().Diagnostics(); \
EXPECT_EQ(expected, \ EXPECT_EQ(true, diagnostics.contains_errors()); \
diag::Formatter(formatter_style).format(p->diagnostics())); \ EXPECT_EQ(expected, diag::Formatter(formatter_style).format(diagnostics)); \
} while (false) } while (false)
TEST_F(ParserImplErrorResyncTest, BadFunctionDecls) { TEST_F(ParserImplErrorResyncTest, BadFunctionDecls) {