diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-12-30 13:22:48 -0800 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-01-15 15:11:36 -0800 |
| commit | 4fccb5ae7a3c3ad0f0ec79bf5eb628807c10eb62 (patch) | |
| tree | 1e0d758a607bfce81eb9e285d5ef255f32b42f5b /src/link/Wasm.zig | |
| parent | 7d224516c4414015186bc1497ca6641b7390bf92 (diff) | |
| download | zig-4fccb5ae7a3c3ad0f0ec79bf5eb628807c10eb62.tar.gz zig-4fccb5ae7a3c3ad0f0ec79bf5eb628807c10eb62.zip | |
wasm linker: improve error messages by making source locations more lazy
Diffstat (limited to 'src/link/Wasm.zig')
| -rw-r--r-- | src/link/Wasm.zig | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 49df77d6a8..9d0a9385fe 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -465,17 +465,33 @@ pub const SourceLocation = enum(u32) { pub fn addNote( sl: SourceLocation, - wasm: *Wasm, err: *link.Diags.ErrorWithNotes, comptime f: []const u8, args: anytype, ) void { - switch (sl.unpack(wasm)) { - .none => err.addNote(f, args), - .zig_object_nofile => err.addNote("zig compilation unit: " ++ f, args), - .object_index => |i| err.addNote("{}: " ++ f, .{i.ptr(wasm).path} ++ args), + err.addNote(f, args); + const err_msg = &err.diags.msgs.items[err.index]; + err_msg.notes[err.note_slot - 1].source_location = .{ .wasm = sl }; + } + + pub fn string( + sl: SourceLocation, + msg: []const u8, + bundle: *std.zig.ErrorBundle.Wip, + wasm: *const Wasm, + ) Allocator.Error!std.zig.ErrorBundle.String { + return switch (sl.unpack(wasm)) { + .none => try bundle.addString(msg), + .zig_object_nofile => try bundle.printString("zig compilation unit: {s}", .{msg}), + .object_index => |i| { + const obj = i.ptr(wasm); + return if (obj.archive_member_name.slice(wasm)) |obj_name| + try bundle.printString("{} ({s}): {s}", .{ obj.path, std.fs.path.basename(obj_name), msg }) + else + try bundle.printString("{}: {s}", .{ obj.path, msg }); + }, .source_location_index => @panic("TODO"), - } + }; } }; @@ -3679,6 +3695,12 @@ fn defaultEntrySymbolName( }; } +pub fn internOptionalString(wasm: *Wasm, optional_bytes: ?[]const u8) Allocator.Error!OptionalString { + const bytes = optional_bytes orelse return .none; + const string = try internString(wasm, bytes); + return string.toOptional(); +} + pub fn internString(wasm: *Wasm, bytes: []const u8) Allocator.Error!String { assert(mem.indexOfScalar(u8, bytes, 0) == null); wasm.string_bytes_lock.lock(); |
