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 {
40164016};
40174017
40184018fn 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 {
40204024 const index = self.misc_errors.items.len;
4021 const err = try self.misc_errors.addOne(gpa);
4022 err.* = .{ .msg = undefined, .notes = try gpa.alloc(link.File.ErrorMsg, note_count) };
4025 const err = self.misc_errors.addOneAssumeCapacity();
4026 err.* = .{ .msg = undefined, .notes = try self.base.allocator.alloc(link.File.ErrorMsg, note_count) };
40234027 return .{ .index = index };
40244028}
40254029
......@@ -4033,33 +4037,22 @@ fn reportUndefined(self: *Elf, undefs: anytype) !void {
40334037 while (it.next()) |entry| {
40344038 const undef_index = entry.key_ptr.*;
40354039 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);
4039 defer notes.deinit();
4043 var err = try self.addErrorWithNotesAssumeCapacity(nnotes);
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| {
40424047 const atom_ptr = self.atom(atom_index).?;
40434048 const file_ptr = self.file(atom_ptr.file_index).?;
4044 const note = try std.fmt.allocPrint(gpa, "referenced by {s}:{s}", .{
4045 file_ptr.fmtPath(),
4046 atom_ptr.name(self),
4047 });
4048 notes.appendAssumeCapacity(.{ .msg = note });
4049 try err.addNote(self, "referenced by {s}:{s}", .{ file_ptr.fmtPath(), atom_ptr.name(self) });
40494050 }
40504051
40514052 if (atoms.len > max_notes) {
40524053 const remaining = atoms.len - max_notes;
4053 const note = try std.fmt.allocPrint(gpa, "referenced {d} more times", .{remaining});
4054 notes.appendAssumeCapacity(.{ .msg = note });
4054 try err.addNote(self, "referenced {d} more times", .{remaining});
40554055 }
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);
40634056 }
40644057}
40654058
......@@ -4095,15 +4088,9 @@ fn reportParseError(
40954088 comptime format: []const u8,
40964089 args: anytype,
40974090) error{OutOfMemory}!void {
4098 const gpa = self.base.allocator;
4099 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
4100 var notes = try gpa.alloc(link.File.ErrorMsg, 1);
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 });
4091 var err = try self.addErrorWithNotes(1);
4092 try err.addMsg(self, format, args);
4093 try err.addNote(self, "while parsing {s}", .{path});
41074094}
41084095
41094096fn fmtShdrs(self: *Elf) std.fmt.Formatter(formatShdrs) {