Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move filename out of Location
  • Loading branch information
zherczeg committed Mar 26, 2026
commit 3538745985e2f4bb9fb0001f66c52cc4d5fdfcc1
7 changes: 2 additions & 5 deletions include/wabt/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,14 @@ struct Location {
};

Location() : line(0), first_column(0), last_column(0) {}
Location(std::string_view filename,
int line,
Location(int line,
int first_column,
int last_column)
: filename(filename),
line(line),
: line(line),
first_column(first_column),
last_column(last_column) {}
explicit Location(size_t offset) : offset(offset) {}

std::string_view filename;
union {
// For text files.
struct {
Expand Down
9 changes: 7 additions & 2 deletions include/wabt/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ static inline const char* GetErrorLevelName(ErrorLevel error_level) {
class Error {
public:
Error() : error_level(ErrorLevel::Error) {}
Error(ErrorLevel error_level, Location loc, std::string_view message)
: error_level(error_level), loc(loc), message(message) {}
Error(ErrorLevel error_level,
Location loc,
std::string_view filename,
std::string_view message)
: error_level(error_level), loc(loc), filename(filename),
message(message) {}

ErrorLevel error_level;
Location loc;
std::string_view filename;
std::string message;
};

Expand Down
2 changes: 2 additions & 0 deletions include/wabt/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,7 @@ struct Module {

Location loc;
std::string name;
std::string_view filename;
ModuleFieldList fields;

Index num_tag_imports = 0;
Expand Down Expand Up @@ -1578,6 +1579,7 @@ struct Script {

CommandPtrVector commands;
BindingHash module_bindings;
std::string_view filename;
};

void MakeTypeBindingReverseMapping(
Expand Down
5 changes: 4 additions & 1 deletion include/wabt/shared-validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class SharedValidator {
public:
WABT_DISALLOW_COPY_AND_ASSIGN(SharedValidator);
using FuncType = TypeChecker::FuncType;
SharedValidator(Errors*, const ValidateOptions& options);
SharedValidator(Errors*,
std::string_view filename,
const ValidateOptions& options);

// TODO: Move into SharedValidator?
using Label = TypeChecker::Label;
Expand Down Expand Up @@ -347,6 +349,7 @@ class SharedValidator {

ValidateOptions options_;
Errors* errors_;
std::string_view filename_;
TypeChecker typechecker_; // TODO: Move into SharedValidator.
// Cached for access by OnTypecheckerError.
Location expr_loc_ = Location(kInvalidOffset);
Expand Down
2 changes: 2 additions & 0 deletions include/wabt/wast-lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class WastLexer {

Token GetToken();

std::string_view Filename() const { return filename_; }

// TODO(binji): Move this out of the lexer.
std::unique_ptr<LexerSourceLineFinder> MakeLineFinder() {
return std::make_unique<LexerSourceLineFinder>(source_->Clone());
Expand Down
9 changes: 5 additions & 4 deletions src/binary-reader-ir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ class BinaryReaderIR : public BinaryReaderNop {

Func* current_func_ = nullptr;
std::vector<LabelNode> label_stack_;
const char* filename_;

CodeMetadataExprQueue code_metadata_queue_;
std::string_view current_metadata_name_;
Expand All @@ -411,19 +410,21 @@ class BinaryReaderIR : public BinaryReaderNop {
BinaryReaderIR::BinaryReaderIR(Module* out_module,
const char* filename,
Errors* errors)
: errors_(errors), module_(out_module), filename_(filename) {}
: errors_(errors), module_(out_module) {
out_module->filename = filename;
}

Location BinaryReaderIR::GetLocation() const {
Location loc;
loc.filename = filename_;
loc.offset = state->offset;
return loc;
}

void WABT_PRINTF_FORMAT(2, 3) BinaryReaderIR::PrintError(const char* format,
...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, Location(kInvalidOffset), buffer);
errors_->emplace_back(ErrorLevel::Error, Location(kInvalidOffset),
std::string_view(), buffer);
}

Result BinaryReaderIR::PushLabel(LabelType label_type,
Expand Down
2 changes: 1 addition & 1 deletion src/binary-reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void WABT_PRINTF_FORMAT(2, 3) BinaryReader::PrintError(const char* format,
: ErrorLevel::Error;

WABT_SNPRINTF_ALLOCA(buffer, length, format);
Error error(error_level, Location(state_.offset), buffer);
Error error(error_level, Location(state_.offset), std::string_view(), buffer);
bool handled = delegate_->OnError(error);

if (!handled) {
Expand Down
4 changes: 2 additions & 2 deletions src/error-formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ std::string FormatError(const Error& error,
result += color.MaybeBoldCode();

const Location& loc = error.loc;
if (!loc.filename.empty()) {
result += loc.filename;
if (!error.filename.empty()) {
result += error.filename;
result += ":";
}

Expand Down
6 changes: 3 additions & 3 deletions src/interp/binary-reader-interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ class BinaryReaderInterp : public BinaryReaderNop {

Location BinaryReaderInterp::GetLocation() const {
Location loc;
loc.filename = filename_;
loc.offset = state->offset;
return loc;
}
Expand Down Expand Up @@ -397,7 +396,7 @@ BinaryReaderInterp::BinaryReaderInterp(ModuleDesc* module,
: errors_(errors),
module_(*module),
istream_(module->istream),
validator_(errors, ValidateOptions(features)),
validator_(errors, filename, ValidateOptions(features)),
filename_(filename) {}

Label* BinaryReaderInterp::GetLabel(Index depth) {
Expand All @@ -422,7 +421,8 @@ Label* BinaryReaderInterp::TopLabel() {
void WABT_PRINTF_FORMAT(2, 3) BinaryReaderInterp::PrintError(const char* format,
...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, Location(kInvalidOffset), buffer);
errors_->emplace_back(ErrorLevel::Error, Location(kInvalidOffset), filename_,
buffer);
}

Result BinaryReaderInterp::GetDropCount(Index keep_count,
Expand Down
3 changes: 2 additions & 1 deletion src/resolve-names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ void WABT_PRINTF_FORMAT(3, 4) NameResolver::PrintError(const Location* loc,
...) {
result_ = Result::Error;
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, *loc, buffer);
errors_->emplace_back(ErrorLevel::Error, *loc, current_module_->filename,
buffer);
}

void NameResolver::PushLabel(const std::string& label) {
Expand Down
7 changes: 5 additions & 2 deletions src/shared-validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ TypeVector SharedValidator::ToTypeVector(Index count, const Type* types) {
return TypeVector(&types[0], &types[count]);
}

SharedValidator::SharedValidator(Errors* errors, const ValidateOptions& options)
SharedValidator::SharedValidator(Errors* errors,
std::string_view filename,
const ValidateOptions& options)
: options_(options),
errors_(errors),
filename_(filename),
typechecker_(options.features, func_types_) {
typechecker_.set_error_callback(
[this](const char* msg) { OnTypecheckerError(msg); });
Expand All @@ -38,7 +41,7 @@ Result WABT_PRINTF_FORMAT(3, 4) SharedValidator::PrintError(const Location& loc,
const char* format,
...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, loc, buffer);
errors_->emplace_back(ErrorLevel::Error, loc, filename_, buffer);
return Result::Error;
}

Expand Down
8 changes: 4 additions & 4 deletions src/tools/spectest-interp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class JSONParser {
// Parsing info.
std::vector<uint8_t> json_data_;
size_t json_offset_ = 0;
std::string_view spec_json_filename_;
Location loc_;
Location prev_loc_;
bool has_prev_loc_ = false;
Expand All @@ -428,7 +429,7 @@ class JSONParser {
CHECK_RESULT(ParseKeyStringValue(key, value))

wabt::Result JSONParser::ReadFile(std::string_view spec_json_filename) {
loc_.filename = spec_json_filename;
spec_json_filename_ = spec_json_filename;
loc_.line = 1;
loc_.first_column = 1;

Expand All @@ -437,7 +438,7 @@ wabt::Result JSONParser::ReadFile(std::string_view spec_json_filename) {

void JSONParser::PrintError(const char* format, ...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
fprintf(stderr, "%s:%d:%d: %s\n", std::string(loc_.filename).c_str(),
fprintf(stderr, "%s:%d:%d: %s\n", std::string(spec_json_filename_).c_str(),
loc_.line, loc_.first_column, buffer);
}

Expand Down Expand Up @@ -1028,8 +1029,7 @@ static std::string_view GetDirname(std::string_view path) {
}

std::string JSONParser::CreateModulePath(std::string_view filename) {
std::string_view spec_json_filename = loc_.filename;
std::string_view dirname = GetDirname(spec_json_filename);
std::string_view dirname = GetDirname(spec_json_filename_);
std::string path;

if (dirname.size() == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ ScriptValidator::ScriptValidator(Errors* errors,
void ScriptValidator::PrintError(const Location* loc, const char* format, ...) {
result_ = Result::Error;
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, *loc, buffer);
errors_->emplace_back(ErrorLevel::Error, *loc, script_->filename, buffer);
}

static Result CheckType(Type actual, Type expected) {
Expand Down Expand Up @@ -757,7 +757,7 @@ Validator::Validator(Errors* errors,
const ValidateOptions& options)
: options_(options),
errors_(errors),
validator_(errors_, options_),
validator_(errors_, module->filename, options_),
current_module_(module) {}

Result Validator::CheckModule() {
Expand Down
4 changes: 2 additions & 2 deletions src/wast-lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Location WastLexer::GetLocation() {
auto column = [this](const char* p) {
return std::max(1, static_cast<int>(p - line_start_ + 1));
};
return Location(filename_, line_, column(token_start_), column(cursor_));
return Location(line_, column(token_start_), column(cursor_));
}

std::string_view WastLexer::GetText(size_t offset) {
Expand Down Expand Up @@ -638,7 +638,7 @@ Token WastLexer::GetReservedToken() {

void WastLexer::Error(Location loc, const char* format, ...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
errors_->emplace_back(ErrorLevel::Error, loc, buffer);
errors_->emplace_back(ErrorLevel::Error, loc, filename_, buffer);
}

} // namespace wabt
Loading
Loading