Have tint executable use the diagnostic printer

Will now print with better formatting and colors on terminals that support it.

Bug: tint:282
Change-Id: Ibff341cb1dc2dcbda6fa0d72e24fdcb172990138
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31570
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-03 13:15:19 +00:00 committed by Commit Bot service account
parent ecea5c8aec
commit d59a5da9e5
7 changed files with 58 additions and 13 deletions

View File

@ -26,6 +26,8 @@
#include "tint/tint.h"
#include "src/diagnostic/printer.h"
namespace {
enum class Format {
@ -478,7 +480,8 @@ int main(int argc, const char** argv) {
return 1;
}
if (!reader->Parse()) {
std::cerr << "Parse: " << reader->error() << std::endl;
auto printer = tint::diag::Printer::create(stderr, true);
tint::diag::Formatter().format(reader->diagnostics(), printer.get());
return 1;
}

View File

@ -19,7 +19,13 @@ namespace diag {
List::List() = default;
List::List(std::initializer_list<Diagnostic> list) : entries_(list) {}
List::List(const List&) = default;
List::List(List&&) = default;
List::~List() = default;
List& List::operator=(const List&) = default;
List& List::operator=(List&&) = default;
} // namespace diag
} // namespace tint

View File

@ -54,12 +54,30 @@ class List {
/// Constructs the list with no elements.
List();
/// Constructs the list with a copy of the diagnostics in |list|.
/// Copy constructor. Copies the diagnostics from |list| into this list.
/// @param list the list of diagnostics to copy into this list.
List(std::initializer_list<Diagnostic> list);
/// Copy constructor. Copies the diagnostics from |list| into this list.
/// @param list the list of diagnostics to copy into this list.
List(const List& list);
/// Move constructor. Moves the diagnostics from |list| into this list.
/// @param list the list of diagnostics to move into this list.
List(List&& list);
~List();
/// Assignment operator. Copies the diagnostics from |list| into this list.
/// @param list the list to copy into this list.
/// @return this list.
List& operator=(const List& list);
/// Assignment move operator. Moves the diagnostics from |list| into this
/// list.
/// @param list the list to move into this list.
/// @return this list.
List& operator=(List&& list);
/// adds a diagnostic to the end of this list.
/// @param diag the diagnostic to append to this list.
void add(Diagnostic&& diag) {

View File

@ -19,6 +19,8 @@
#include "src/ast/module.h"
#include "src/context.h"
#include "src/diagnostic/diagnostic.h"
#include "src/diagnostic/formatter.h"
namespace tint {
namespace reader {
@ -32,10 +34,17 @@ class Reader {
/// @returns true if the parse was successful
virtual bool Parse() = 0;
/// @returns true if an error was encountered
bool has_error() const { return error_.size() > 0; }
/// @returns true if an error was encountered.
bool has_error() const { return diags_.contains_errors(); }
/// @returns the parser error string
const std::string& error() const { return error_; }
std::string error() const {
diag::Formatter formatter{{false, false, false}};
return formatter.format(diags_);
}
/// @returns the full list of diagnostic messages.
const diag::List& diagnostics() const { return diags_; }
/// @returns the module. The module in the parser will be reset after this.
virtual ast::Module module() = 0;
@ -45,15 +54,15 @@ class Reader {
/// @param ctx the context object, must be non-null
explicit Reader(Context* ctx);
/// Sets the error string
/// @param msg the error message
void set_error(const std::string& msg) { error_ = msg; }
/// Sets the diagnostic messages
/// @param diags the list of diagnostic messages
void set_diagnostics(const diag::List& diags) { diags_ = diags; }
/// The Tint context object
Context& ctx_;
/// An error message, if an error was encountered
std::string error_;
/// All diagnostic messages from the reader.
diag::List diags_;
};
} // namespace reader

View File

@ -27,7 +27,14 @@ Parser::~Parser() = default;
bool Parser::Parse() {
const auto result = impl_->Parse();
set_error(impl_->error());
auto err_msg = impl_->error();
if (!err_msg.empty()) {
// TODO(bclayton): Migrate spirv::ParserImpl to using diagnostics.
diag::Diagnostic error{};
error.severity = diag::Severity::Error;
error.message = err_msg;
set_diagnostics({error});
}
return result;
}

View File

@ -28,8 +28,7 @@ Parser::~Parser() = default;
bool Parser::Parse() {
bool ret = impl_->Parse();
if (impl_->has_error())
set_error(impl_->error());
set_diagnostics(std::move(impl_->diagnostics()));
return ret;
}

View File

@ -111,6 +111,9 @@ class ParserImpl {
/// @returns the diagnostic messages
const diag::List& diagnostics() const { return diags_; }
/// @returns the diagnostic messages
diag::List& diagnostics() { return diags_; }
/// @returns the module. The module in the parser will be reset after this.
ast::Module module() { return std::move(module_); }