From e644a2ab6a99951ac8d367f7a6bac985cf16f9cc Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sun, 10 Jul 2022 16:58:25 +0300 Subject: [PATCH] Compilation: do not repeat same source line for notes --- lib/std/zig.zig | 4 ++++ src/Compilation.zig | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/std/zig.zig b/lib/std/zig.zig index 0d3c94d37b..b8f75f649e 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -49,6 +49,10 @@ pub const Loc = struct { column: usize, /// Does not include the trailing newline. source_line: []const u8, + + pub fn eql(a: Loc, b: Loc) bool { + return a.line == b.line and a.column == b.column and std.mem.eql(u8, a.source_line, b.source_line); + } }; pub fn findLineColumn(source: []const u8, byte_offset: usize) Loc { diff --git a/src/Compilation.zig b/src/Compilation.zig index b00f135813..48a287d40b 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -505,6 +505,9 @@ pub const AllErrors = struct { Message.HashContext, std.hash_map.default_max_load_percentage, ).init(allocator); + const err_source = try module_err_msg.src_loc.file_scope.getSource(module.gpa); + const err_byte_offset = try module_err_msg.src_loc.byteOffset(module.gpa); + const err_loc = std.zig.findLineColumn(err_source.bytes, err_byte_offset); for (module_err_msg.notes) |module_note| { const source = try module_note.src_loc.file_scope.getSource(module.gpa); @@ -519,7 +522,7 @@ pub const AllErrors = struct { .byte_offset = byte_offset, .line = @intCast(u32, loc.line), .column = @intCast(u32, loc.column), - .source_line = try allocator.dupe(u8, loc.source_line), + .source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line), }, }; const gop = try seen_notes.getOrPut(note); @@ -537,19 +540,16 @@ pub const AllErrors = struct { }); return; } - const source = try module_err_msg.src_loc.file_scope.getSource(module.gpa); - const byte_offset = try module_err_msg.src_loc.byteOffset(module.gpa); - const loc = std.zig.findLineColumn(source.bytes, byte_offset); const file_path = try module_err_msg.src_loc.file_scope.fullPath(allocator); try errors.append(.{ .src = .{ .src_path = file_path, .msg = try allocator.dupe(u8, module_err_msg.msg), - .byte_offset = byte_offset, - .line = @intCast(u32, loc.line), - .column = @intCast(u32, loc.column), + .byte_offset = err_byte_offset, + .line = @intCast(u32, err_loc.line), + .column = @intCast(u32, err_loc.column), .notes = notes_buf[0..note_i], - .source_line = try allocator.dupe(u8, loc.source_line), + .source_line = try allocator.dupe(u8, err_loc.source_line), }, }); }