Add diag::List::add_error() helper

Refactors a common pattern in the tint codebase.

Change-Id: Ia8a70d952fd8c204facd0120f24e43ccc9305622
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38840
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-01-26 18:52:11 +00:00 committed by Commit Bot service account
parent a6b9a8eb2f
commit 1461cd96d6
9 changed files with 39 additions and 44 deletions

View File

@ -98,6 +98,26 @@ class List {
} }
} }
/// adds the error message without a source to the end of this list.
/// @param err_msg the error message
void add_error(const std::string& err_msg) {
diag::Diagnostic error{};
error.severity = diag::Severity::Error;
error.message = err_msg;
add(std::move(error));
}
/// adds the error message with the given Source to the end of this list.
/// @param err_msg the error message
/// @param source the source of the error diagnostic
void add_error(const std::string& err_msg, const Source& source) {
diag::Diagnostic error{};
error.severity = diag::Severity::Error;
error.source = source;
error.message = err_msg;
add(std::move(error));
}
/// @returns true iff the diagnostic list contains errors diagnostics (or of /// @returns true iff the diagnostic list contains errors diagnostics (or of
/// higher severity). /// higher severity).
bool contains_errors() const { return error_count_ > 0; } bool contains_errors() const { return error_count_ > 0; }

View File

@ -14,6 +14,8 @@
#include "src/reader/spirv/parser.h" #include "src/reader/spirv/parser.h"
#include <utility>
#include "src/reader/spirv/parser_impl.h" #include "src/reader/spirv/parser_impl.h"
namespace tint { namespace tint {
@ -30,10 +32,9 @@ bool Parser::Parse() {
auto err_msg = impl_->error(); auto err_msg = impl_->error();
if (!err_msg.empty()) { if (!err_msg.empty()) {
// TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics. // TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics.
diag::Diagnostic error{}; diag::List diagnostics;
error.severity = diag::Severity::Error; diagnostics.add_error(err_msg);
error.message = err_msg; set_diagnostics(std::move(diagnostics));
set_diagnostics({error});
} }
return result; return result;
} }

View File

@ -46,6 +46,7 @@ class Parser : public Reader {
private: private:
std::unique_ptr<ParserImpl> impl_; std::unique_ptr<ParserImpl> impl_;
Program program_;
}; };
} // namespace spirv } // namespace spirv

View File

@ -230,11 +230,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) {
diag::Diagnostic diagnostic; diags_.add_error(err, source);
diagnostic.severity = diag::Severity::Error;
diagnostic.message = err;
diagnostic.source = source;
diags_.add(std::move(diagnostic));
} }
return Failure::kErrored; return Failure::kErrored;
} }

View File

@ -102,11 +102,7 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform(
auto* limit = b.Sub(arr_len, b.Expr(1u)); auto* limit = b.Sub(arr_len, b.Expr(1u));
new_idx = b.Call("min", b.Construct<u32>(ctx->Clone(old_idx)), limit); new_idx = b.Call("min", b.Construct<u32>(ctx->Clone(old_idx)), limit);
} else { } else {
diag::Diagnostic err; diags->add_error("invalid 0 size", expr->source());
err.severity = diag::Severity::Error;
err.message = "invalid 0 size";
err.source = expr->source();
diags->add(std::move(err));
return nullptr; return nullptr;
} }
} else if (auto* c = old_idx->As<ast::ScalarConstructorExpression>()) { } else if (auto* c = old_idx->As<ast::ScalarConstructorExpression>()) {
@ -118,11 +114,8 @@ ast::ArrayAccessorExpression* BoundArrayAccessors::Transform(
} else if (auto* uint = lit->As<ast::UintLiteral>()) { } else if (auto* uint = lit->As<ast::UintLiteral>()) {
new_idx = b.Expr(std::min(uint->value(), size - 1)); new_idx = b.Expr(std::min(uint->value(), size - 1));
} else { } else {
diag::Diagnostic err; diags->add_error("unknown scalar constructor type for accessor",
err.severity = diag::Severity::Error; expr->source());
err.message = "unknown scalar constructor type for accessor";
err.source = expr->source();
diags->add(std::move(err));
return nullptr; return nullptr;
} }
} else { } else {

View File

@ -88,11 +88,9 @@ Transform::Output FirstIndexOffset::Run(const Program* in) {
for (ast::Variable* var : in->AST().GlobalVariables()) { for (ast::Variable* var : in->AST().GlobalVariables()) {
if (auto* dec_var = var->As<ast::Variable>()) { if (auto* dec_var = var->As<ast::Variable>()) {
if (dec_var->symbol() == in->Symbols().Get(kBufferName)) { if (dec_var->symbol() == in->Symbols().Get(kBufferName)) {
diag::Diagnostic err;
err.message = "First index offset transform has already been applied.";
err.severity = diag::Severity::Error;
Output out; Output out;
out.diagnostics.add(std::move(err)); out.diagnostics.add_error(
"First index offset transform has already been applied.");
return out; return out;
} }
} }
@ -106,11 +104,8 @@ Transform::Output FirstIndexOffset::Run(const Program* in) {
ProgramBuilder builder = in->CloneAsBuilder(); ProgramBuilder builder = in->CloneAsBuilder();
TypeDeterminer td(&builder); TypeDeterminer td(&builder);
if (!td.Determine()) { if (!td.Determine()) {
diag::Diagnostic err;
err.severity = diag::Severity::Error;
err.message = td.error();
Output out; Output out;
out.diagnostics.add(std::move(err)); out.diagnostics.add_error(td.error());
return out; return out;
} }
program = Program(std::move(builder)); program = Program(std::move(builder));

View File

@ -78,11 +78,8 @@ void VertexPulling::SetPullingBufferBindingSet(uint32_t number) {
Transform::Output VertexPulling::Run(const Program* in) { Transform::Output VertexPulling::Run(const Program* in) {
// Check SetVertexState was called // Check SetVertexState was called
if (!cfg.vertex_state_set) { if (!cfg.vertex_state_set) {
diag::Diagnostic err;
err.severity = diag::Severity::Error;
err.message = "SetVertexState not called";
Output out; Output out;
out.diagnostics.add(std::move(err)); out.diagnostics.add_error("SetVertexState not called");
return out; return out;
} }
@ -90,11 +87,8 @@ Transform::Output VertexPulling::Run(const Program* in) {
auto* func = in->AST().Functions().Find( auto* func = in->AST().Functions().Find(
in->Symbols().Get(cfg.entry_point_name), ast::PipelineStage::kVertex); in->Symbols().Get(cfg.entry_point_name), ast::PipelineStage::kVertex);
if (func == nullptr) { if (func == nullptr) {
diag::Diagnostic err;
err.severity = diag::Severity::Error;
err.message = "Vertex stage entry point not found";
Output out; Output out;
out.diagnostics.add(std::move(err)); out.diagnostics.add_error("Vertex stage entry point not found");
return out; return out;
} }

View File

@ -69,10 +69,9 @@ diag::List TypeDeterminer::Run(Program* program) {
ProgramBuilder builder = program->CloneAsBuilder(); ProgramBuilder builder = program->CloneAsBuilder();
TypeDeterminer td(&builder); TypeDeterminer td(&builder);
if (!td.Determine()) { if (!td.Determine()) {
diag::Diagnostic err; diag::List diagnostics;
err.severity = diag::Severity::Error; diagnostics.add_error(td.error());
err.message = td.error(); return diagnostics;
return {err};
} }
*program = Program(std::move(builder)); *program = Program(std::move(builder));
return {}; return {};

View File

@ -58,11 +58,7 @@ void ValidatorImpl::add_error(const Source& src,
} }
void ValidatorImpl::add_error(const Source& src, const std::string& msg) { void ValidatorImpl::add_error(const Source& src, const std::string& msg) {
diag::Diagnostic diag; diags_.add_error(msg, src);
diag.severity = diag::Severity::Error;
diag.source = src;
diag.message = msg;
diags_.add(std::move(diag));
} }
bool ValidatorImpl::Validate() { bool ValidatorImpl::Validate() {