authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-27 16:40:51+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-27 16:40:51+02:00
log85132965f4163f211c3b04a6316f8dac46d1c260
tree78561c496b47265c45cd673beeae8fc26dfae0fb
parent09863fc97043f3aadcad476c3eda8f3e3dde18dd

elf: use new error reporting API


1 files changed, 17 insertions(+), 30 deletions(-)

src/link/Elf.zig+17-30
...@@ -4016,10 +4016,14 @@ const ErrorWithNotes = struct {...@@ -4016,10 +4016,14 @@ const ErrorWithNotes = struct {
4016};4016};
40174017
4018fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {4018fn addErrorWithNotes(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {
4019 const gpa = self.base.allocator;4019 try self.misc_errors.ensureUnusedCapacity(self.base.allocator, 1);
4020 return self.addErrorWithNotesAssumeCapacity(note_count);
4021}
4022
4023fn addErrorWithNotesAssumeCapacity(self: *Elf, note_count: usize) error{OutOfMemory}!ErrorWithNotes {
4020 const index = self.misc_errors.items.len;4024 const index = self.misc_errors.items.len;
4021 const err = try self.misc_errors.addOne(gpa);4025 const err = self.misc_errors.addOneAssumeCapacity();
4022 err.* = .{ .msg = undefined, .notes = try gpa.alloc(link.File.ErrorMsg, note_count) };4026 err.* = .{ .msg = undefined, .notes = try self.base.allocator.alloc(link.File.ErrorMsg, note_count) };
4023 return .{ .index = index };4027 return .{ .index = index };
4024}4028}
40254029
...@@ -4033,33 +4037,22 @@ fn reportUndefined(self: *Elf, undefs: anytype) !void {...@@ -4033,33 +4037,22 @@ fn reportUndefined(self: *Elf, undefs: anytype) !void {
4033 while (it.next()) |entry| {4037 while (it.next()) |entry| {
4034 const undef_index = entry.key_ptr.*;4038 const undef_index = entry.key_ptr.*;
4035 const atoms = entry.value_ptr.*.items;4039 const atoms = entry.value_ptr.*.items;
4036 const nnotes = @min(atoms.len, max_notes);4040 const natoms = @min(atoms.len, max_notes);
4041 const nnotes = natoms + @intFromBool(atoms.len > max_notes);
40374042
4038 var notes = try std.ArrayList(link.File.ErrorMsg).initCapacity(gpa, max_notes + 1);4043 var err = try self.addErrorWithNotesAssumeCapacity(nnotes);
4039 defer notes.deinit();4044 try err.addMsg(self, "undefined symbol: {s}", .{self.symbol(undef_index).name(self)});
40404045
4041 for (atoms[0..nnotes]) |atom_index| {4046 for (atoms[0..natoms]) |atom_index| {
4042 const atom_ptr = self.atom(atom_index).?;4047 const atom_ptr = self.atom(atom_index).?;
4043 const file_ptr = self.file(atom_ptr.file_index).?;4048 const file_ptr = self.file(atom_ptr.file_index).?;
4044 const note = try std.fmt.allocPrint(gpa, "referenced by {s}:{s}", .{4049 try err.addNote(self, "referenced by {s}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) });
4045 file_ptr.fmtPath(),
4046 atom_ptr.name(self),
4047 });
4048 notes.appendAssumeCapacity(.{ .msg = note });
4049 }4050 }
40504051
4051 if (atoms.len > max_notes) {4052 if (atoms.len > max_notes) {
4052 const remaining = atoms.len - max_notes;4053 const remaining = atoms.len - max_notes;
4053 const note = try std.fmt.allocPrint(gpa, "referenced {d} more times", .{remaining});4054 try err.addNote(self, "referenced {d} more times", .{remaining});
4054 notes.appendAssumeCapacity(.{ .msg = note });
4055 }4055 }
4056
4057 var err_msg = link.File.ErrorMsg{
4058 .msg = try std.fmt.allocPrint(gpa, "undefined symbol: {s}", .{self.symbol(undef_index).name(self)}),
4059 };
4060 err_msg.notes = try notes.toOwnedSlice();
4061
4062 self.misc_errors.appendAssumeCapacity(err_msg);
4063 }4056 }
4064}4057}
40654058
...@@ -4095,15 +4088,9 @@ fn reportParseError(...@@ -4095,15 +4088,9 @@ fn reportParseError(
4095 comptime format: []const u8,4088 comptime format: []const u8,
4096 args: anytype,4089 args: anytype,
4097) error{OutOfMemory}!void {4090) error{OutOfMemory}!void {
4098 const gpa = self.base.allocator;4091 var err = try self.addErrorWithNotes(1);
4099 try self.misc_errors.ensureUnusedCapacity(gpa, 1);4092 try err.addMsg(self, format, args);
4100 var notes = try gpa.alloc(link.File.ErrorMsg, 1);4093 try err.addNote(self, "while parsing {s}", .{path});
4101 errdefer gpa.free(notes);
4102 notes[0] = .{ .msg = try std.fmt.allocPrint(gpa, "while parsing {s}", .{path}) };
4103 self.misc_errors.appendAssumeCapacity(.{
4104 .msg = try std.fmt.allocPrint(gpa, format, args),
4105 .notes = notes,
4106 });
4107}4094}
41084095
4109fn fmtShdrs(self: *Elf) std.fmt.Formatter(formatShdrs) {4096fn fmtShdrs(self: *Elf) std.fmt.Formatter(formatShdrs) {