summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binary-reader-interp.cc16
-rw-r--r--src/binary-reader-ir.cc16
-rw-r--r--src/binary-reader-logging.cc4
-rw-r--r--src/binary-reader-logging.h2
-rw-r--r--src/binary-reader-nop.h2
-rw-r--r--src/binary-reader-objdump.cc5
-rw-r--r--src/binary-reader.cc27
-rw-r--r--src/binary-reader.h2
-rw-r--r--src/common.h17
-rw-r--r--src/error-handler.cc19
-rw-r--r--src/error-handler.h21
-rw-r--r--src/wast-parser-lexer-shared.cc4
-rw-r--r--src/wast-parser.cc7
13 files changed, 96 insertions, 46 deletions
diff --git a/src/binary-reader-interp.cc b/src/binary-reader-interp.cc
index 38817b32..4849bbe4 100644
--- a/src/binary-reader-interp.cc
+++ b/src/binary-reader-interp.cc
@@ -78,7 +78,7 @@ class BinaryReaderInterp : public BinaryReaderNop {
std::unique_ptr<OutputBuffer> ReleaseOutputBuffer();
// Implement BinaryReader.
- bool OnError(const char* message) override;
+ bool OnError(ErrorLevel, const char* message) override;
wabt::Result EndModule() override;
@@ -222,7 +222,7 @@ class BinaryReaderInterp : public BinaryReaderNop {
void PushLabel(IstreamOffset offset, IstreamOffset fixup_offset);
void PopLabel();
- bool HandleError(Offset offset, const char* message);
+ bool HandleError(ErrorLevel, Offset offset, const char* message);
void PrintError(const char* format, ...);
Index TranslateSigIndexToEnv(Index sig_index);
@@ -340,14 +340,16 @@ Label* BinaryReaderInterp::TopLabel() {
return GetLabel(0);
}
-bool BinaryReaderInterp::HandleError(Offset offset, const char* message) {
- return error_handler_->OnError(offset, message);
+bool BinaryReaderInterp::HandleError(ErrorLevel error_level,
+ Offset offset,
+ const char* message) {
+ return error_handler_->OnError(error_level, offset, message);
}
void WABT_PRINTF_FORMAT(2, 3) BinaryReaderInterp::PrintError(const char* format,
...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
- HandleError(kInvalidOffset, buffer);
+ HandleError(ErrorLevel::Error, kInvalidOffset, buffer);
}
Index BinaryReaderInterp::TranslateSigIndexToEnv(Index sig_index) {
@@ -545,8 +547,8 @@ wabt::Result BinaryReaderInterp::EmitFuncOffset(DefinedFunc* func,
return wabt::Result::Ok;
}
-bool BinaryReaderInterp::OnError(const char* message) {
- return HandleError(state->offset, message);
+bool BinaryReaderInterp::OnError(ErrorLevel error_level, const char* message) {
+ return HandleError(error_level, state->offset, message);
}
wabt::Result BinaryReaderInterp::OnTypeCount(Index count) {
diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc
index 33c2ff5d..3fa430c2 100644
--- a/src/binary-reader-ir.cc
+++ b/src/binary-reader-ir.cc
@@ -50,7 +50,7 @@ class BinaryReaderIR : public BinaryReaderNop {
const char* filename,
ErrorHandler* error_handler);
- bool OnError(const char* message) override;
+ bool OnError(ErrorLevel, const char* message) override;
Result OnTypeCount(Index count) override;
Result OnType(Index index,
@@ -222,7 +222,7 @@ class BinaryReaderIR : public BinaryReaderNop {
Result OnInitExprI64ConstExpr(Index index, uint64_t value) override;
private:
- bool HandleError(Offset offset, const char* message);
+ bool HandleError(ErrorLevel, Offset offset, const char* message);
Location GetLocation() const;
void PrintError(const char* format, ...);
void PushLabel(LabelType label_type,
@@ -257,7 +257,7 @@ Location BinaryReaderIR::GetLocation() const {
void WABT_PRINTF_FORMAT(2, 3) BinaryReaderIR::PrintError(const char* format,
...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
- HandleError(kInvalidOffset, buffer);
+ HandleError(ErrorLevel::Error, kInvalidOffset, buffer);
}
void BinaryReaderIR::PushLabel(LabelType label_type,
@@ -299,12 +299,14 @@ Result BinaryReaderIR::AppendExpr(std::unique_ptr<Expr> expr) {
return Result::Ok;
}
-bool BinaryReaderIR::HandleError(Offset offset, const char* message) {
- return error_handler_->OnError(offset, message);
+bool BinaryReaderIR::HandleError(ErrorLevel error_level,
+ Offset offset,
+ const char* message) {
+ return error_handler_->OnError(error_level, offset, message);
}
-bool BinaryReaderIR::OnError(const char* message) {
- return HandleError(state->offset, message);
+bool BinaryReaderIR::OnError(ErrorLevel error_level, const char* message) {
+ return HandleError(error_level, state->offset, message);
}
Result BinaryReaderIR::OnTypeCount(Index count) {
diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc
index b86fb550..bd5faaa3 100644
--- a/src/binary-reader-logging.cc
+++ b/src/binary-reader-logging.cc
@@ -91,8 +91,8 @@ void BinaryReaderLogging::LogTypes(TypeVector& types) {
LogTypes(types.size(), types.data());
}
-bool BinaryReaderLogging::OnError(const char* message) {
- return reader_->OnError(message);
+bool BinaryReaderLogging::OnError(ErrorLevel error_level, const char* message) {
+ return reader_->OnError(error_level, message);
}
void BinaryReaderLogging::OnSetState(const State* s) {
diff --git a/src/binary-reader-logging.h b/src/binary-reader-logging.h
index 2e9c6495..ad12c557 100644
--- a/src/binary-reader-logging.h
+++ b/src/binary-reader-logging.h
@@ -27,7 +27,7 @@ class BinaryReaderLogging : public BinaryReaderDelegate {
public:
BinaryReaderLogging(Stream*, BinaryReaderDelegate* forward);
- bool OnError(const char* message) override;
+ bool OnError(ErrorLevel, const char* message) override;
void OnSetState(const State* s) override;
Result BeginModule(uint32_t version) override;
diff --git a/src/binary-reader-nop.h b/src/binary-reader-nop.h
index d2c893a5..c5e68e0c 100644
--- a/src/binary-reader-nop.h
+++ b/src/binary-reader-nop.h
@@ -23,7 +23,7 @@ namespace wabt {
class BinaryReaderNop : public BinaryReaderDelegate {
public:
- bool OnError(const char* message) override { return false; }
+ bool OnError(ErrorLevel, const char* message) override { return false; }
/* Module */
Result BeginModule(uint32_t version) override { return Result::Ok; }
diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc
index 8522a17a..4c89466f 100644
--- a/src/binary-reader-objdump.cc
+++ b/src/binary-reader-objdump.cc
@@ -38,7 +38,7 @@ class BinaryReaderObjdumpBase : public BinaryReaderNop {
ObjdumpOptions* options,
ObjdumpState* state);
- bool OnError(const char* message) override;
+ bool OnError(ErrorLevel, const char* message) override;
Result BeginModule(uint32_t version) override;
Result BeginSection(BinarySection section_type, Offset size) override;
@@ -85,7 +85,8 @@ Result BinaryReaderObjdumpBase::BeginSection(BinarySection section_code,
return Result::Ok;
}
-bool BinaryReaderObjdumpBase::OnError(const char* message) {
+bool BinaryReaderObjdumpBase::OnError(ErrorLevel error_level,
+ const char* message) {
// Tell the BinaryReader that this error is "handled" for all passes other
// than the prepass. When the error is handled the default message will be
// suppressed.
diff --git a/src/binary-reader.cc b/src/binary-reader.cc
index 05832f46..d9fb6ace 100644
--- a/src/binary-reader.cc
+++ b/src/binary-reader.cc
@@ -72,13 +72,14 @@ class BinaryReader {
Result ReadModule();
private:
- struct ReadEndRestoreGuard {
- explicit ReadEndRestoreGuard(BinaryReader* this_)
- : this_(this_), previous_read_end_(this_->read_end_) {}
- ~ReadEndRestoreGuard() { this_->read_end_ = previous_read_end_; }
+ template <typename T, T BinaryReader::*member>
+ struct ValueRestoreGuard {
+ explicit ValueRestoreGuard(BinaryReader* this_)
+ : this_(this_), previous_value_(this_->*member) {}
+ ~ValueRestoreGuard() { this_->*member = previous_value_; }
BinaryReader* this_;
- size_t previous_read_end_;
+ T previous_value_;
};
void WABT_PRINTF_FORMAT(2, 3) PrintError(const char* format, ...);
@@ -148,6 +149,7 @@ class BinaryReader {
const ReadBinaryOptions* options_ = nullptr;
BinarySection last_known_section_ = BinarySection::Invalid;
bool did_read_names_section_ = false;
+ bool reading_custom_section_ = false;
Index num_signatures_ = 0;
Index num_imports_ = 0;
Index num_func_imports_ = 0;
@@ -162,6 +164,9 @@ class BinaryReader {
Index num_exports_ = 0;
Index num_function_bodies_ = 0;
Index num_exceptions_ = 0;
+
+ using ReadEndRestoreGuard =
+ ValueRestoreGuard<size_t, &BinaryReader::read_end_>;
};
BinaryReader::BinaryReader(const void* data,
@@ -179,12 +184,18 @@ BinaryReader::BinaryReader(const void* data,
void WABT_PRINTF_FORMAT(2, 3) BinaryReader::PrintError(const char* format,
...) {
+ ErrorLevel error_level =
+ reading_custom_section_ && !options_->fail_on_custom_section_error
+ ? ErrorLevel::Warning
+ : ErrorLevel::Error;
+
WABT_SNPRINTF_ALLOCA(buffer, length, format);
- bool handled = delegate_->OnError(buffer);
+ bool handled = delegate_->OnError(error_level, buffer);
if (!handled) {
// Not great to just print, but we don't want to eat the error either.
- fprintf(stderr, "*ERROR*: @0x%08zx: %s\n", state_.offset, buffer);
+ fprintf(stderr, "%07" PRIzx ": %s: %s\n", state_.offset,
+ GetErrorLevelName(error_level), buffer);
}
}
@@ -1597,6 +1608,8 @@ Result BinaryReader::ReadCustomSection(Offset section_size) {
string_view section_name;
CHECK_RESULT(ReadStr(&section_name, "section name"));
CALLBACK(BeginCustomSection, section_size, section_name);
+ ValueRestoreGuard<bool, &BinaryReader::reading_custom_section_> guard(this);
+ reading_custom_section_ = true;
if (options_->read_debug_names && section_name == WABT_BINARY_SECTION_NAME) {
CHECK_RESULT(ReadNameSection(section_size));
diff --git a/src/binary-reader.h b/src/binary-reader.h
index 3910ae97..b0b9b848 100644
--- a/src/binary-reader.h
+++ b/src/binary-reader.h
@@ -63,7 +63,7 @@ class BinaryReaderDelegate {
virtual ~BinaryReaderDelegate() {}
- virtual bool OnError(const char* message) = 0;
+ virtual bool OnError(ErrorLevel, const char* message) = 0;
virtual void OnSetState(const State* s) { state = s; }
/* Module */
diff --git a/src/common.h b/src/common.h
index 0d7fa7b5..67cc7320 100644
--- a/src/common.h
+++ b/src/common.h
@@ -102,6 +102,11 @@ struct v128 {
namespace wabt {
+enum class ErrorLevel {
+ Warning,
+ Error,
+};
+
typedef uint32_t Index; // An index into one of the many index spaces.
typedef uint32_t Address; // An address or size in linear memory.
typedef size_t Offset; // An offset into a host's file or memory buffer.
@@ -351,6 +356,18 @@ static WABT_INLINE const char* GetTypeName(Type type) {
WABT_UNREACHABLE;
}
+/* error level */
+
+static WABT_INLINE const char* GetErrorLevelName(ErrorLevel error_level) {
+ switch (error_level) {
+ case ErrorLevel::Warning:
+ return "warning";
+ case ErrorLevel::Error:
+ return "error";
+ }
+ WABT_UNREACHABLE;
+}
+
template <typename T>
void ConvertBackslashToSlash(T begin, T end) {
std::replace(begin, end, '\\', '/');
diff --git a/src/error-handler.cc b/src/error-handler.cc
index 7c70281f..49e8d818 100644
--- a/src/error-handler.cc
+++ b/src/error-handler.cc
@@ -23,7 +23,8 @@ namespace wabt {
ErrorHandler::ErrorHandler(Location::Type location_type)
: location_type_(location_type) {}
-std::string ErrorHandler::DefaultErrorMessage(const Color& color,
+std::string ErrorHandler::DefaultErrorMessage(ErrorLevel error_level,
+ const Color& color,
const Location& loc,
const std::string& error,
const std::string& source_line,
@@ -46,7 +47,8 @@ std::string ErrorHandler::DefaultErrorMessage(const Color& color,
}
result += color.MaybeRedCode();
- result += "error: ";
+ result += GetErrorLevelName(error_level);
+ result += ": ";
result += color.MaybeDefaultCode();
result += error;
@@ -89,15 +91,17 @@ ErrorHandlerFile::ErrorHandlerFile(Location::Type location_type,
source_line_max_length_(source_line_max_length),
color_(file) {}
-bool ErrorHandlerFile::OnError(const Location& loc,
+bool ErrorHandlerFile::OnError(ErrorLevel level,
+ const Location& loc,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) {
PrintErrorHeader();
int indent = header_.empty() ? 0 : 2;
- std::string message = DefaultErrorMessage(color_, loc, error, source_line,
- source_line_column_offset, indent);
+ std::string message =
+ DefaultErrorMessage(level, color_, loc, error, source_line,
+ source_line_column_offset, indent);
fwrite(message.data(), 1, message.size(), file_);
return true;
}
@@ -127,11 +131,12 @@ ErrorHandlerBuffer::ErrorHandlerBuffer(Location::Type location_type,
source_line_max_length_(source_line_max_length),
color_(nullptr, false) {}
-bool ErrorHandlerBuffer::OnError(const Location& loc,
+bool ErrorHandlerBuffer::OnError(ErrorLevel level,
+ const Location& loc,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) {
- buffer_ += DefaultErrorMessage(color_, loc, error, source_line,
+ buffer_ += DefaultErrorMessage(level, color_, loc, error, source_line,
source_line_column_offset, 0);
return true;
}
diff --git a/src/error-handler.h b/src/error-handler.h
index 5bee0639..cc407993 100644
--- a/src/error-handler.h
+++ b/src/error-handler.h
@@ -31,20 +31,24 @@ class ErrorHandler {
virtual ~ErrorHandler() {}
// Returns true if the error was handled.
- virtual bool OnError(const Location&,
+ virtual bool OnError(ErrorLevel,
+ const Location&,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) = 0;
// Helper function for binary locations.
- bool OnError(size_t offset, const std::string& error) {
- return OnError(Location(offset), error, std::string(), 0);
+ bool OnError(ErrorLevel error_level,
+ size_t offset,
+ const std::string& error) {
+ return OnError(error_level, Location(offset), error, std::string(), 0);
}
// OnError will be called with with source_line trimmed to this length.
virtual size_t source_line_max_length() const = 0;
- std::string DefaultErrorMessage(const Color&,
+ std::string DefaultErrorMessage(ErrorLevel,
+ const Color&,
const Location&,
const std::string& error,
const std::string& source_line,
@@ -59,7 +63,8 @@ class ErrorHandlerNop : public ErrorHandler {
public:
ErrorHandlerNop();
- bool OnError(const Location&,
+ bool OnError(ErrorLevel,
+ const Location&,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) override {
@@ -83,7 +88,8 @@ class ErrorHandlerFile : public ErrorHandler {
PrintHeader print_header = PrintHeader::Never,
size_t source_line_max_length = 80);
- bool OnError(const Location&,
+ bool OnError(ErrorLevel,
+ const Location&,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) override;
@@ -107,7 +113,8 @@ class ErrorHandlerBuffer : public ErrorHandler {
explicit ErrorHandlerBuffer(Location::Type,
size_t source_line_max_length = 80);
- bool OnError(const Location&,
+ bool OnError(ErrorLevel,
+ const Location&,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) override;
diff --git a/src/wast-parser-lexer-shared.cc b/src/wast-parser-lexer-shared.cc
index e7931530..58b9b7a3 100644
--- a/src/wast-parser-lexer-shared.cc
+++ b/src/wast-parser-lexer-shared.cc
@@ -49,8 +49,8 @@ void WastFormatError(ErrorHandler* error_handler,
}
}
- error_handler->OnError(*loc, std::string(buffer), source_line.line,
- source_line.column_offset);
+ error_handler->OnError(ErrorLevel::Error, *loc, std::string(buffer),
+ source_line.line, source_line.column_offset);
va_end(args_copy);
}
diff --git a/src/wast-parser.cc b/src/wast-parser.cc
index 3381b65e..ae793bf8 100644
--- a/src/wast-parser.cc
+++ b/src/wast-parser.cc
@@ -108,7 +108,8 @@ void RemoveEscapes(const TextVector& texts, OutputIter out) {
class BinaryErrorHandlerModule : public ErrorHandler {
public:
BinaryErrorHandlerModule(Location* loc, WastParser* parser);
- bool OnError(const Location&,
+ bool OnError(ErrorLevel,
+ const Location&,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) override;
@@ -125,10 +126,12 @@ BinaryErrorHandlerModule::BinaryErrorHandlerModule(Location* loc,
WastParser* parser)
: ErrorHandler(Location::Type::Binary), loc_(loc), parser_(parser) {}
-bool BinaryErrorHandlerModule::OnError(const Location& binary_loc,
+bool BinaryErrorHandlerModule::OnError(ErrorLevel error_level,
+ const Location& binary_loc,
const std::string& error,
const std::string& source_line,
size_t source_line_column_offset) {
+ assert(error_level == ErrorLevel::Error);
if (binary_loc.offset == kInvalidOffset) {
parser_->Error(*loc_, "error in binary module: %s", error.c_str());
} else {