Skip to content

Commit eae3204

Browse files
committed
- Move aroDiagnosticsToErrorBundle to compiler/util.zig
1 parent 7b9408b commit eae3204

File tree

4 files changed

+93
-81
lines changed

4 files changed

+93
-81
lines changed

lib/compiler/aro/aro/Diagnostics.zig

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -562,82 +562,3 @@ fn addMessage(d: *Diagnostics, msg: Message) Compilation.Error!void {
562562
},
563563
}
564564
}
565-
566-
const ErrorBundle = std.zig.ErrorBundle;
567-
568-
pub 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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const cvtres = @import("cvtres.zig");
1313
const hasDisjointCodePage = @import("disjoint_code_page.zig").hasDisjointCodePage;
1414
const fmtResourceType = @import("res.zig").NameOrOrdinal.fmtResourceType;
1515
const aro = @import("aro");
16+
const compiler_util = @import("../util.zig");
1617

1718
pub 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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = std.debug.assert;
33
const mem = std.mem;
44
const process = std.process;
55
const aro = @import("aro");
6+
const compiler_util = @import("../util.zig");
67
const Translator = @import("Translator.zig");
78

89
const fast_exit = @import("builtin").mode != .Debug;
@@ -88,7 +89,11 @@ pub fn main() u8 {
8889
}
8990

9091
fn 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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! Utilities shared between compiler sub-commands
2+
const std = @import("std");
3+
const aro = @import("aro");
4+
const ErrorBundle = std.zig.ErrorBundle;
5+
6+
pub 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+
}

0 commit comments

Comments
 (0)