diff options
author | Ben Smith <binji@chromium.org> | 2018-09-03 13:08:23 -0700 |
---|---|---|
committer | Ben Smith <binjimin@gmail.com> | 2018-09-04 15:13:23 -0700 |
commit | e0719fe0fe504c497d9fb7510fe68499c08179f4 (patch) | |
tree | 1e2f3d3b4ac76b239bb75a6f819d8fd1f1cc8050 /src/validator.cc | |
parent | b4125e90c70a6b1bef4480e33f3c838ab4c7cfcb (diff) | |
download | wabt-e0719fe0fe504c497d9fb7510fe68499c08179f4.tar.gz wabt-e0719fe0fe504c497d9fb7510fe68499c08179f4.tar.bz2 wabt-e0719fe0fe504c497d9fb7510fe68499c08179f4.zip |
Replace ErrorHandler with Errors and FormatErrors*
`ErrorHandler` complicated all error handling in wabt, since it was
callback-based. Callbacks would be useful if we ever wanted to change
behavior when an error occurred, but instead all that the handler ever
did was write the errors to stdout/stderr or to a buffer.
This change adds a new class `Error`, which contains an `ErrorLevel`, a
`Location` and an error message. It also replaces ErrorHandler with
`Errors` (a typedef for `std::vector<Error>`), and adds a couple of
functions that can format a list of `Errors` for output:
`FormatErrorsTo{String,File}`.
Diffstat (limited to 'src/validator.cc')
-rw-r--r-- | src/validator.cc | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/src/validator.cc b/src/validator.cc index 9ec8e9ee..a418e309 100644 --- a/src/validator.cc +++ b/src/validator.cc @@ -25,7 +25,6 @@ #include "src/binary-reader.h" #include "src/cast.h" -#include "src/error-handler.h" #include "src/expr-visitor.h" #include "src/ir.h" #include "src/type-checker.h" @@ -37,7 +36,7 @@ namespace { class Validator : public ExprVisitor::Delegate { public: WABT_DISALLOW_COPY_AND_ASSIGN(Validator); - Validator(ErrorHandler*, const Script*, const ValidateOptions& options); + Validator(Errors*, const Script*, const ValidateOptions& options); Result CheckModule(const Module* module); Result CheckScript(const Script* script); @@ -196,7 +195,7 @@ class Validator : public ExprVisitor::Delegate { Result CheckExceptVar(const Var* var, const Exception** out_except); const ValidateOptions& options_; - ErrorHandler* error_handler_ = nullptr; + Errors* errors_ = nullptr; const Script* script_ = nullptr; const Module* current_module_ = nullptr; const Func* current_func_ = nullptr; @@ -211,20 +210,18 @@ class Validator : public ExprVisitor::Delegate { Result result_ = Result::Ok; }; -Validator::Validator(ErrorHandler* error_handler, +Validator::Validator(Errors* errors, const Script* script, const ValidateOptions& options) - : options_(options), error_handler_(error_handler), script_(script) { + : options_(options), errors_(errors), script_(script) { typechecker_.set_error_callback( [this](const char* msg) { OnTypecheckerError(msg); }); } -void Validator::PrintError(const Location* loc, const char* fmt, ...) { +void Validator::PrintError(const Location* loc, const char* format, ...) { result_ = Result::Error; - va_list args; - va_start(args, fmt); - error_handler_->OnError(ErrorLevel::Error, *loc, fmt, args); - va_end(args); + WABT_SNPRINTF_ALLOCA(buffer, length, format); + errors_->emplace_back(ErrorLevel::Error, *loc, buffer); } void Validator::OnTypecheckerError(const char* msg) { @@ -1435,25 +1432,25 @@ Result Validator::CheckAllFuncSignatures(const Module* module) { } // end anonymous namespace Result ValidateScript(const Script* script, - ErrorHandler* error_handler, + Errors* errors, const ValidateOptions& options) { - Validator validator(error_handler, script, options); + Validator validator(errors, script, options); return validator.CheckScript(script); } Result ValidateModule(const Module* module, - ErrorHandler* error_handler, + Errors* errors, const ValidateOptions& options) { - Validator validator(error_handler, nullptr, options); + Validator validator(errors, nullptr, options); return validator.CheckModule(module); } Result ValidateFuncSignatures(const Module* module, - ErrorHandler* error_handler, + Errors* errors, const ValidateOptions& options) { - Validator validator(error_handler, nullptr, options); + Validator validator(errors, nullptr, options); return validator.CheckAllFuncSignatures(module); } |