authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-07-18 20:17:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-24 20:01:18-07:00
loge8e8d7e5c8eee5fe29add5aac0c07bf08a2debad
tree05bdd20c3c3215d50ad71230624fdd1786b6f363
parent01d993b2305961100d2e898270b7a6be832d0fa7

resinator: Update for latest aro


2 files changed, 51 insertions(+), 112 deletions(-)

lib/compiler/resinator/main.zig+38-93
......@@ -120,7 +120,20 @@ pub fn main() !void {
120120 defer aro_arena_state.deinit();
121121 const aro_arena = aro_arena_state.allocator();
122122
123 var comp = aro.Compilation.init(aro_arena, std.fs.cwd());
123 var stderr_buf: [512]u8 = undefined;
124 var stderr_writer = stderr.writer(&stderr_buf);
125 var diagnostics: aro.Diagnostics = switch (zig_integration) {
126 false => .{ .output = .{ .to_writer = .{
127 .writer = &stderr_writer.interface,
128 .color = stderr_config,
129 } } },
130 true => .{ .output = .{ .to_list = .{
131 .arena = .init(allocator),
132 } } },
133 };
134 defer diagnostics.deinit();
135
136 var comp = aro.Compilation.init(aro_arena, aro_arena, &diagnostics, std.fs.cwd());
124137 defer comp.deinit();
125138
126139 var argv: std.ArrayList([]const u8) = .empty;
......@@ -143,20 +156,24 @@ pub fn main() !void {
143156 try stdout.flush();
144157 }
145158
146 preprocess.preprocess(&comp, &preprocessed_buf.writer, argv.items, maybe_dependencies) catch |err| switch (err) {
159 preprocess.preprocess(&comp, &preprocessed_buf.writer, argv.items, maybe_dependencies_list) catch |err| switch (err) {
147160 error.GeneratedSourceError => {
148 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessor setup (this is always a bug):", &comp);
161 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessor setup (this is always a bug)", &comp);
149162 std.process.exit(1);
150163 },
151164 // ArgError can occur if e.g. the .rc file is not found
152165 error.ArgError, error.PreprocessError => {
153 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessing:", &comp);
166 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessing", &comp);
154167 std.process.exit(1);
155168 },
156 error.StreamTooLong => {
169 error.FileTooBig => {
157170 try error_handler.emitMessage(allocator, .err, "failed during preprocessing: maximum file size exceeded", .{});
158171 std.process.exit(1);
159172 },
173 error.WriteFailed => {
174 try error_handler.emitMessage(allocator, .err, "failed during preprocessing: error writing the preprocessed output", .{});
175 std.process.exit(1);
176 },
160177 error.OutOfMemory => |e| return e,
161178 };
162179
......@@ -660,11 +677,10 @@ const ErrorHandler = union(enum) {
660677 try server.serveErrorBundle(error_bundle);
661678 },
662679 .tty => {
663 // extra newline to separate this line from the aro errors
680 // aro errors have already been emitted
664681 const stderr = std.debug.lockStderrWriter(&.{});
665682 defer std.debug.unlockStderrWriter();
666 try renderErrorMessage(stderr, self.tty, .err, "{s}\n", .{fail_msg});
667 aro.Diagnostics.render(comp, self.tty);
683 try renderErrorMessage(stderr, self.tty, .err, "{s}", .{fail_msg});
668684 },
669685 }
670686 }
......@@ -883,12 +899,10 @@ fn aroDiagnosticsToErrorBundle(
883899 .msg = try bundle.addString(fail_msg),
884900 });
885901
886 var msg_writer = MsgWriter.init(gpa);
887 defer msg_writer.deinit();
888902 var cur_err: ?ErrorBundle.ErrorMessage = null;
889903 var cur_notes: std.ArrayList(ErrorBundle.ErrorMessage) = .empty;
890904 defer cur_notes.deinit(gpa);
891 for (comp.diagnostics.list.items) |msg| {
905 for (comp.diagnostics.output.to_list.messages.items) |msg| {
892906 switch (msg.kind) {
893907 // Clear the current error so that notes don't bleed into unassociated errors
894908 .off, .warning => {
......@@ -897,28 +911,19 @@ fn aroDiagnosticsToErrorBundle(
897911 },
898912 .note => if (cur_err == null) continue,
899913 .@"fatal error", .@"error" => {},
900 .default => unreachable,
901914 }
902 msg_writer.resetRetainingCapacity();
903 aro.Diagnostics.renderMessage(comp, &msg_writer, msg);
904915
905916 const src_loc = src_loc: {
906 if (msg_writer.path) |src_path| {
907 var src_loc: ErrorBundle.SourceLocation = .{
908 .src_path = try bundle.addString(src_path),
909 .line = msg_writer.line - 1, // 1-based -> 0-based
910 .column = msg_writer.col - 1, // 1-based -> 0-based
911 .span_start = 0,
912 .span_main = 0,
913 .span_end = 0,
914 };
915 if (msg_writer.source_line) |source_line| {
916 src_loc.span_start = msg_writer.span_main;
917 src_loc.span_main = msg_writer.span_main;
918 src_loc.span_end = msg_writer.span_main;
919 src_loc.source_line = try bundle.addString(source_line);
920 }
921 break :src_loc try bundle.addSourceLocation(src_loc);
917 if (msg.location) |location| {
918 break :src_loc try bundle.addSourceLocation(.{
919 .src_path = try bundle.addString(location.path),
920 .line = location.line_no - 1, // 1-based -> 0-based
921 .column = location.col - 1, // 1-based -> 0-based
922 .span_start = location.width,
923 .span_main = location.width,
924 .span_end = location.width,
925 .source_line = try bundle.addString(location.line),
926 });
922927 }
923928 break :src_loc ErrorBundle.SourceLocationIndex.none;
924929 };
......@@ -929,7 +934,7 @@ fn aroDiagnosticsToErrorBundle(
929934 try flushErrorMessageIntoBundle(&bundle, err, cur_notes.items);
930935 }
931936 cur_err = .{
932 .msg = try bundle.addString(msg_writer.buf.items),
937 .msg = try bundle.addString(msg.text),
933938 .src_loc = src_loc,
934939 };
935940 cur_notes.clearRetainingCapacity();
......@@ -937,11 +942,11 @@ fn aroDiagnosticsToErrorBundle(
937942 .note => {
938943 cur_err.?.notes_len += 1;
939944 try cur_notes.append(gpa, .{
940 .msg = try bundle.addString(msg_writer.buf.items),
945 .msg = try bundle.addString(msg.text),
941946 .src_loc = src_loc,
942947 });
943948 },
944 .off, .warning, .default => unreachable,
949 .off, .warning => unreachable,
945950 }
946951 }
947952 if (cur_err) |err| {
......@@ -950,63 +955,3 @@ fn aroDiagnosticsToErrorBundle(
950955
951956 return try bundle.toOwnedBundle("");
952957}
953
954// Similar to aro.Diagnostics.MsgWriter but:
955// - Writers to an ArrayList
956// - Only prints the message itself (no location, source line, error: prefix, etc)
957// - Keeps track of source path/line/col instead
958const MsgWriter = struct {
959 buf: std.array_list.Managed(u8),
960 path: ?[]const u8 = null,
961 // 1-indexed
962 line: u32 = undefined,
963 col: u32 = undefined,
964 source_line: ?[]const u8 = null,
965 span_main: u32 = undefined,
966
967 fn init(allocator: std.mem.Allocator) MsgWriter {
968 return .{
969 .buf = std.array_list.Managed(u8).init(allocator),
970 };
971 }
972
973 fn deinit(m: *MsgWriter) void {
974 m.buf.deinit();
975 }
976
977 fn resetRetainingCapacity(m: *MsgWriter) void {
978 m.buf.clearRetainingCapacity();
979 m.path = null;
980 m.source_line = null;
981 }
982
983 pub fn print(m: *MsgWriter, comptime fmt: []const u8, args: anytype) void {
984 m.buf.print(fmt, args) catch {};
985 }
986
987 pub fn write(m: *MsgWriter, msg: []const u8) void {
988 m.buf.appendSlice(msg) catch {};
989 }
990
991 pub fn setColor(m: *MsgWriter, color: std.Io.tty.Color) void {
992 _ = m;
993 _ = color;
994 }
995
996 pub fn location(m: *MsgWriter, path: []const u8, line: u32, col: u32) void {
997 m.path = path;
998 m.line = line;
999 m.col = col;
1000 }
1001
1002 pub fn start(m: *MsgWriter, kind: aro.Diagnostics.Kind) void {
1003 _ = m;
1004 _ = kind;
1005 }
1006
1007 pub fn end(m: *MsgWriter, maybe_line: ?[]const u8, col: u32, end_with_splice: bool) void {
1008 _ = end_with_splice;
1009 m.source_line = maybe_line;
1010 m.span_main = col;
1011 }
1012};
lib/compiler/resinator/preprocess.zig+13-19
......@@ -5,7 +5,7 @@ const cli = @import("cli.zig");
55const Dependencies = @import("compile.zig").Dependencies;
66const aro = @import("aro");
77
8const PreprocessError = error{ ArgError, GeneratedSourceError, PreprocessError, StreamTooLong, OutOfMemory };
8const PreprocessError = error{ ArgError, GeneratedSourceError, PreprocessError, FileTooBig, OutOfMemory, WriteFailed };
99
1010pub fn preprocess(
1111 comp: *aro.Compilation,
......@@ -16,18 +16,18 @@ pub fn preprocess(
1616) PreprocessError!void {
1717 try comp.addDefaultPragmaHandlers();
1818
19 var driver: aro.Driver = .{ .comp = comp, .aro_name = "arocc" };
19 var driver: aro.Driver = .{ .comp = comp, .diagnostics = comp.diagnostics, .aro_name = "arocc" };
2020 defer driver.deinit();
2121
22 var macro_buf: std.Io.Writer.Allocating = .init(comp.gpa);
23 defer macro_buf.deinit();
22 var macro_buf: std.ArrayListUnmanaged(u8) = .empty;
23 defer macro_buf.deinit(comp.gpa);
2424
25 var trash: [64]u8 = undefined;
26 var discarding: std.Io.Writer.Discarding = .init(&trash);
27 _ = driver.parseArgs(&discarding.writer, &macro_buf.writer, argv) catch |err| switch (err) {
25 var discard_buffer: [64]u8 = undefined;
26 var discarding: std.Io.Writer.Discarding = .init(&discard_buffer);
27 _ = driver.parseArgs(&discarding.writer, &macro_buf, argv) catch |err| switch (err) {
2828 error.FatalError => return error.ArgError,
2929 error.OutOfMemory => |e| return e,
30 error.WriteFailed => return error.OutOfMemory,
30 error.WriteFailed => unreachable,
3131 };
3232
3333 if (hasAnyErrors(comp)) return error.ArgError;
......@@ -46,7 +46,10 @@ pub fn preprocess(
4646 if (hasAnyErrors(comp)) return error.GeneratedSourceError;
4747
4848 comp.generated_buf.items.len = 0;
49 var pp = try aro.Preprocessor.initDefault(comp);
49 var pp = aro.Preprocessor.initDefault(comp) catch |err| switch (err) {
50 error.FatalError => return error.GeneratedSourceError,
51 error.OutOfMemory => |e| return e,
52 };
5053 defer pp.deinit();
5154
5255 if (comp.langopts.ms_extensions) {
......@@ -79,16 +82,7 @@ pub fn preprocess(
7982}
8083
8184fn hasAnyErrors(comp: *aro.Compilation) bool {
82 // In theory we could just check Diagnostics.errors != 0, but that only
83 // gets set during rendering of the error messages, see:
84 // https://github.com/Vexu/arocc/issues/603
85 for (comp.diagnostics.list.items) |msg| {
86 switch (msg.kind) {
87 .@"fatal error", .@"error" => return true,
88 else => {},
89 }
90 }
91 return false;
85 return comp.diagnostics.errors != 0;
9286}
9387
9488/// `arena` is used for temporary -D argument strings and the INCLUDE environment variable.