authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2025-10-09 00:58:18-04:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2025-10-09 01:06:09-04:00
log447280d0d98b540aed4df83db7cf95a417d81611
tree6f490bb2bbae07b6108408dbc8261f0c850df912
parent478cb9ce6af2e1875840de6618bbf23c624937c8

- Move aroDiagnosticsToErrorBundle to compiler/util.zig


4 files changed, 93 insertions(+), 81 deletions(-)

lib/compiler/aro/aro/Diagnostics.zig-79
......@@ -562,82 +562,3 @@ fn addMessage(d: *Diagnostics, msg: Message) Compilation.Error!void {
562562 },
563563 }
564564}
565
566const ErrorBundle = std.zig.ErrorBundle;
567
568pub fn toErrorBundle(
569 d: *const Diagnostics,
570 gpa: std.mem.Allocator,
571 fail_msg: ?[]const u8,
572) !ErrorBundle {
573 @branchHint(.cold);
574
575 var bundle: ErrorBundle.Wip = undefined;
576 try bundle.init(gpa);
577 errdefer bundle.deinit();
578
579 if (fail_msg) |msg| {
580 try bundle.addRootErrorMessage(.{
581 .msg = try bundle.addString(msg),
582 });
583 }
584
585 var cur_err: ?ErrorBundle.ErrorMessage = null;
586 var cur_notes: std.ArrayList(ErrorBundle.ErrorMessage) = .empty;
587 defer cur_notes.deinit(gpa);
588 for (d.output.to_list.messages.items) |msg| {
589 switch (msg.kind) {
590 .off, .warning => {
591 if (cur_err) |err| {
592 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
593 // Clear the current error so that notes don't bleed into unassociated errors
594 cur_err = null;
595 }
596 continue;
597 },
598 .note => if (cur_err == null) continue,
599 .@"fatal error", .@"error" => {},
600 }
601
602 const src_loc = src_loc: {
603 if (msg.location) |location| {
604 break :src_loc try bundle.addSourceLocation(.{
605 .src_path = try bundle.addString(location.path),
606 .line = location.line_no - 1, // 1-based -> 0-based
607 .column = location.col - 1, // 1-based -> 0-based
608 .span_start = location.width,
609 .span_main = location.width,
610 .span_end = location.width,
611 .source_line = try bundle.addString(location.line),
612 });
613 }
614 break :src_loc ErrorBundle.SourceLocationIndex.none;
615 };
616
617 switch (msg.kind) {
618 .@"fatal error", .@"error" => {
619 if (cur_err) |err| {
620 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
621 }
622 cur_err = .{
623 .msg = try bundle.addString(msg.text),
624 .src_loc = src_loc,
625 };
626 cur_notes.clearRetainingCapacity();
627 },
628 .note => {
629 cur_err.?.notes_len += 1;
630 try cur_notes.append(gpa, .{
631 .msg = try bundle.addString(msg.text),
632 .src_loc = src_loc,
633 });
634 },
635 .off, .warning => unreachable,
636 }
637 }
638 if (cur_err) |err| {
639 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
640 }
641
642 return try bundle.toOwnedBundle("");
643}
lib/compiler/resinator/main.zig+6-1
......@@ -13,6 +13,7 @@ const cvtres = @import("cvtres.zig");
1313const hasDisjointCodePage = @import("disjoint_code_page.zig").hasDisjointCodePage;
1414const fmtResourceType = @import("res.zig").NameOrOrdinal.fmtResourceType;
1515const aro = @import("aro");
16const compiler_util = @import("../util.zig");
1617
1718pub fn main() !void {
1819 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
......@@ -671,7 +672,11 @@ const ErrorHandler = union(enum) {
671672 ) !void {
672673 switch (self.*) {
673674 .server => |*server| {
674 var error_bundle = try comp.diagnostics.toErrorBundle(allocator, fail_msg);
675 var error_bundle = try compiler_util.aroDiagnosticsToErrorBundle(
676 comp.diagnostics,
677 allocator,
678 fail_msg,
679 );
675680 defer error_bundle.deinit(allocator);
676681
677682 try server.serveErrorBundle(error_bundle);
lib/compiler/translate-c/main.zig+6-1
......@@ -3,6 +3,7 @@ const assert = std.debug.assert;
33const mem = std.mem;
44const process = std.process;
55const aro = @import("aro");
6const compiler_util = @import("../util.zig");
67const Translator = @import("Translator.zig");
78
89const fast_exit = @import("builtin").mode != .Debug;
......@@ -88,7 +89,11 @@ pub fn main() u8 {
8889}
8990
9091fn serveErrorBundle(arena: std.mem.Allocator, diagnostics: *const aro.Diagnostics) !void {
91 const error_bundle = try diagnostics.toErrorBundle(arena, "translation failure");
92 const error_bundle = try compiler_util.aroDiagnosticsToErrorBundle(
93 diagnostics,
94 arena,
95 "translation failure",
96 );
9297 var stdout_buffer: [1024]u8 = undefined;
9398 var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
9499 var server: std.zig.Server = .{
lib/compiler/util.zig created+81
......@@ -0,0 +1,81 @@
1//! Utilities shared between compiler sub-commands
2const std = @import("std");
3const aro = @import("aro");
4const ErrorBundle = std.zig.ErrorBundle;
5
6pub fn aroDiagnosticsToErrorBundle(
7 d: *const aro.Diagnostics,
8 gpa: std.mem.Allocator,
9 fail_msg: ?[]const u8,
10) !ErrorBundle {
11 @branchHint(.cold);
12
13 var bundle: ErrorBundle.Wip = undefined;
14 try bundle.init(gpa);
15 errdefer bundle.deinit();
16
17 if (fail_msg) |msg| {
18 try bundle.addRootErrorMessage(.{
19 .msg = try bundle.addString(msg),
20 });
21 }
22
23 var cur_err: ?ErrorBundle.ErrorMessage = null;
24 var cur_notes: std.ArrayList(ErrorBundle.ErrorMessage) = .empty;
25 defer cur_notes.deinit(gpa);
26 for (d.output.to_list.messages.items) |msg| {
27 switch (msg.kind) {
28 .off, .warning => {
29 if (cur_err) |err| {
30 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
31 // Clear the current error so that notes don't bleed into unassociated errors
32 cur_err = null;
33 }
34 continue;
35 },
36 .note => if (cur_err == null) continue,
37 .@"fatal error", .@"error" => {},
38 }
39
40 const src_loc = src_loc: {
41 if (msg.location) |location| {
42 break :src_loc try bundle.addSourceLocation(.{
43 .src_path = try bundle.addString(location.path),
44 .line = location.line_no - 1, // 1-based -> 0-based
45 .column = location.col - 1, // 1-based -> 0-based
46 .span_start = location.width,
47 .span_main = location.width,
48 .span_end = location.width,
49 .source_line = try bundle.addString(location.line),
50 });
51 }
52 break :src_loc ErrorBundle.SourceLocationIndex.none;
53 };
54
55 switch (msg.kind) {
56 .@"fatal error", .@"error" => {
57 if (cur_err) |err| {
58 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
59 }
60 cur_err = .{
61 .msg = try bundle.addString(msg.text),
62 .src_loc = src_loc,
63 };
64 cur_notes.clearRetainingCapacity();
65 },
66 .note => {
67 cur_err.?.notes_len += 1;
68 try cur_notes.append(gpa, .{
69 .msg = try bundle.addString(msg.text),
70 .src_loc = src_loc,
71 });
72 },
73 .off, .warning => unreachable,
74 }
75 }
76 if (cur_err) |err| {
77 try bundle.addRootErrorMessageWithNotes(err, cur_notes.items);
78 }
79
80 return try bundle.toOwnedBundle("");
81}