authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-20 23:25:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:50-07:00
log4174ac18e9563c79f723e51db6c3abbb4eb3f73a
tree567c010ecd5f31762ea759d078fe08baa7b5f45d
parent76107e9e655378b7d62c1d5d93ef9e17d241975f

resinator: update for new Io APIs


5 files changed, 147 insertions(+), 94 deletions(-)

lib/compiler/resinator/compile.zig+15-8
...@@ -1,6 +1,12 @@...@@ -1,6 +1,12 @@
1const std = @import("std");
2const builtin = @import("builtin");1const builtin = @import("builtin");
2const native_endian = builtin.cpu.arch.endian();
3
4const std = @import("std");
5const Io = std.Io;
3const Allocator = std.mem.Allocator;6const Allocator = std.mem.Allocator;
7const WORD = std.os.windows.WORD;
8const DWORD = std.os.windows.DWORD;
9
4const Node = @import("ast.zig").Node;10const Node = @import("ast.zig").Node;
5const lex = @import("lex.zig");11const lex = @import("lex.zig");
6const Parser = @import("parse.zig").Parser;12const Parser = @import("parse.zig").Parser;
...@@ -17,8 +23,6 @@ const res = @import("res.zig");...@@ -17,8 +23,6 @@ const res = @import("res.zig");
17const ico = @import("ico.zig");23const ico = @import("ico.zig");
18const ani = @import("ani.zig");24const ani = @import("ani.zig");
19const bmp = @import("bmp.zig");25const bmp = @import("bmp.zig");
20const WORD = std.os.windows.WORD;
21const DWORD = std.os.windows.DWORD;
22const utils = @import("utils.zig");26const utils = @import("utils.zig");
23const NameOrOrdinal = res.NameOrOrdinal;27const NameOrOrdinal = res.NameOrOrdinal;
24const SupportedCodePage = @import("code_pages.zig").SupportedCodePage;28const SupportedCodePage = @import("code_pages.zig").SupportedCodePage;
...@@ -28,7 +32,6 @@ const windows1252 = @import("windows1252.zig");...@@ -28,7 +32,6 @@ const windows1252 = @import("windows1252.zig");
28const lang = @import("lang.zig");32const lang = @import("lang.zig");
29const code_pages = @import("code_pages.zig");33const code_pages = @import("code_pages.zig");
30const errors = @import("errors.zig");34const errors = @import("errors.zig");
31const native_endian = builtin.cpu.arch.endian();
3235
33pub const CompileOptions = struct {36pub const CompileOptions = struct {
34 cwd: std.fs.Dir,37 cwd: std.fs.Dir,
...@@ -77,7 +80,7 @@ pub const Dependencies = struct {...@@ -77,7 +80,7 @@ pub const Dependencies = struct {
77 }80 }
78};81};
7982
80pub fn compile(allocator: Allocator, source: []const u8, writer: *std.Io.Writer, options: CompileOptions) !void {83pub fn compile(allocator: Allocator, io: Io, source: []const u8, writer: *std.Io.Writer, options: CompileOptions) !void {
81 var lexer = lex.Lexer.init(source, .{84 var lexer = lex.Lexer.init(source, .{
82 .default_code_page = options.default_code_page,85 .default_code_page = options.default_code_page,
83 .source_mappings = options.source_mappings,86 .source_mappings = options.source_mappings,
...@@ -166,10 +169,11 @@ pub fn compile(allocator: Allocator, source: []const u8, writer: *std.Io.Writer,...@@ -166,10 +169,11 @@ pub fn compile(allocator: Allocator, source: []const u8, writer: *std.Io.Writer,
166 defer arena_allocator.deinit();169 defer arena_allocator.deinit();
167 const arena = arena_allocator.allocator();170 const arena = arena_allocator.allocator();
168171
169 var compiler = Compiler{172 var compiler: Compiler = .{
170 .source = source,173 .source = source,
171 .arena = arena,174 .arena = arena,
172 .allocator = allocator,175 .allocator = allocator,
176 .io = io,
173 .cwd = options.cwd,177 .cwd = options.cwd,
174 .diagnostics = options.diagnostics,178 .diagnostics = options.diagnostics,
175 .dependencies = options.dependencies,179 .dependencies = options.dependencies,
...@@ -191,6 +195,7 @@ pub const Compiler = struct {...@@ -191,6 +195,7 @@ pub const Compiler = struct {
191 source: []const u8,195 source: []const u8,
192 arena: Allocator,196 arena: Allocator,
193 allocator: Allocator,197 allocator: Allocator,
198 io: Io,
194 cwd: std.fs.Dir,199 cwd: std.fs.Dir,
195 state: State = .{},200 state: State = .{},
196 diagnostics: *Diagnostics,201 diagnostics: *Diagnostics,
...@@ -409,7 +414,7 @@ pub const Compiler = struct {...@@ -409,7 +414,7 @@ pub const Compiler = struct {
409 }414 }
410 }415 }
411416
412 var first_error: ?std.fs.File.OpenError = null;417 var first_error: ?(std.fs.File.OpenError || std.fs.File.StatError) = null;
413 for (self.search_dirs) |search_dir| {418 for (self.search_dirs) |search_dir| {
414 if (utils.openFileNotDir(search_dir.dir, path, .{})) |file| {419 if (utils.openFileNotDir(search_dir.dir, path, .{})) |file| {
415 errdefer file.close();420 errdefer file.close();
...@@ -496,6 +501,8 @@ pub const Compiler = struct {...@@ -496,6 +501,8 @@ pub const Compiler = struct {
496 }501 }
497502
498 pub fn writeResourceExternal(self: *Compiler, node: *Node.ResourceExternal, writer: *std.Io.Writer) !void {503 pub fn writeResourceExternal(self: *Compiler, node: *Node.ResourceExternal, writer: *std.Io.Writer) !void {
504 const io = self.io;
505
499 // Init header with data size zero for now, will need to fill it in later506 // Init header with data size zero for now, will need to fill it in later
500 var header = try self.resourceHeader(node.id, node.type, .{});507 var header = try self.resourceHeader(node.id, node.type, .{});
501 defer header.deinit(self.allocator);508 defer header.deinit(self.allocator);
...@@ -582,7 +589,7 @@ pub const Compiler = struct {...@@ -582,7 +589,7 @@ pub const Compiler = struct {
582 };589 };
583 defer file_handle.close();590 defer file_handle.close();
584 var file_buffer: [2048]u8 = undefined;591 var file_buffer: [2048]u8 = undefined;
585 var file_reader = file_handle.reader(&file_buffer);592 var file_reader = file_handle.reader(io, &file_buffer);
586593
587 if (maybe_predefined_type) |predefined_type| {594 if (maybe_predefined_type) |predefined_type| {
588 switch (predefined_type) {595 switch (predefined_type) {
lib/compiler/resinator/cvtres.zig+11-4
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const Io = std.Io;
2const Allocator = std.mem.Allocator;3const Allocator = std.mem.Allocator;
4
3const res = @import("res.zig");5const res = @import("res.zig");
4const NameOrOrdinal = res.NameOrOrdinal;6const NameOrOrdinal = res.NameOrOrdinal;
5const MemoryFlags = res.MemoryFlags;7const MemoryFlags = res.MemoryFlags;
...@@ -169,8 +171,7 @@ pub fn parseNameOrOrdinal(allocator: Allocator, reader: *std.Io.Reader) !NameOrO...@@ -169,8 +171,7 @@ pub fn parseNameOrOrdinal(allocator: Allocator, reader: *std.Io.Reader) !NameOrO
169171
170pub const CoffOptions = struct {172pub const CoffOptions = struct {
171 target: std.coff.IMAGE.FILE.MACHINE = .AMD64,173 target: std.coff.IMAGE.FILE.MACHINE = .AMD64,
172 /// If true, zeroes will be written to all timestamp fields174 timestamp: i64 = 0,
173 reproducible: bool = true,
174 /// If true, the MEM_WRITE flag will not be set in the .rsrc section header175 /// If true, the MEM_WRITE flag will not be set in the .rsrc section header
175 read_only: bool = false,176 read_only: bool = false,
176 /// If non-null, a symbol with this name and storage class EXTERNAL will be added to the symbol table.177 /// If non-null, a symbol with this name and storage class EXTERNAL will be added to the symbol table.
...@@ -188,7 +189,13 @@ pub const Diagnostics = union {...@@ -188,7 +189,13 @@ pub const Diagnostics = union {
188 overflow_resource: usize,189 overflow_resource: usize,
189};190};
190191
191pub fn writeCoff(allocator: Allocator, writer: *std.Io.Writer, resources: []const Resource, options: CoffOptions, diagnostics: ?*Diagnostics) !void {192pub fn writeCoff(
193 allocator: Allocator,
194 writer: *std.Io.Writer,
195 resources: []const Resource,
196 options: CoffOptions,
197 diagnostics: ?*Diagnostics,
198) !void {
192 var resource_tree = ResourceTree.init(allocator, options);199 var resource_tree = ResourceTree.init(allocator, options);
193 defer resource_tree.deinit();200 defer resource_tree.deinit();
194201
...@@ -215,7 +222,7 @@ pub fn writeCoff(allocator: Allocator, writer: *std.Io.Writer, resources: []cons...@@ -215,7 +222,7 @@ pub fn writeCoff(allocator: Allocator, writer: *std.Io.Writer, resources: []cons
215 const pointer_to_rsrc02_data = pointer_to_relocations + relocations_len;222 const pointer_to_rsrc02_data = pointer_to_relocations + relocations_len;
216 const pointer_to_symbol_table = pointer_to_rsrc02_data + lengths.rsrc02;223 const pointer_to_symbol_table = pointer_to_rsrc02_data + lengths.rsrc02;
217224
218 const timestamp: i64 = if (options.reproducible) 0 else std.time.timestamp();225 const timestamp: i64 = options.timestamp;
219 const size_of_optional_header = 0;226 const size_of_optional_header = 0;
220 const machine_type: std.coff.IMAGE.FILE.MACHINE = options.target;227 const machine_type: std.coff.IMAGE.FILE.MACHINE = options.target;
221 const flags = std.coff.Header.Flags{228 const flags = std.coff.Header.Flags{
lib/compiler/resinator/errors.zig+27-9
...@@ -1,5 +1,11 @@...@@ -1,5 +1,11 @@
1const builtin = @import("builtin");
2const native_endian = builtin.cpu.arch.endian();
3
1const std = @import("std");4const std = @import("std");
5const Io = std.Io;
2const assert = std.debug.assert;6const assert = std.debug.assert;
7const Allocator = std.mem.Allocator;
8
3const Token = @import("lex.zig").Token;9const Token = @import("lex.zig").Token;
4const SourceMappings = @import("source_mapping.zig").SourceMappings;10const SourceMappings = @import("source_mapping.zig").SourceMappings;
5const utils = @import("utils.zig");11const utils = @import("utils.zig");
...@@ -11,19 +17,19 @@ const parse = @import("parse.zig");...@@ -11,19 +17,19 @@ const parse = @import("parse.zig");
11const lang = @import("lang.zig");17const lang = @import("lang.zig");
12const code_pages = @import("code_pages.zig");18const code_pages = @import("code_pages.zig");
13const SupportedCodePage = code_pages.SupportedCodePage;19const SupportedCodePage = code_pages.SupportedCodePage;
14const builtin = @import("builtin");
15const native_endian = builtin.cpu.arch.endian();
1620
17pub const Diagnostics = struct {21pub const Diagnostics = struct {
18 errors: std.ArrayList(ErrorDetails) = .empty,22 errors: std.ArrayList(ErrorDetails) = .empty,
19 /// Append-only, cannot handle removing strings.23 /// Append-only, cannot handle removing strings.
20 /// Expects to own all strings within the list.24 /// Expects to own all strings within the list.
21 strings: std.ArrayList([]const u8) = .empty,25 strings: std.ArrayList([]const u8) = .empty,
22 allocator: std.mem.Allocator,26 allocator: Allocator,
27 io: Io,
2328
24 pub fn init(allocator: std.mem.Allocator) Diagnostics {29 pub fn init(allocator: Allocator, io: Io) Diagnostics {
25 return .{30 return .{
26 .allocator = allocator,31 .allocator = allocator,
32 .io = io,
27 };33 };
28 }34 }
2935
...@@ -62,10 +68,11 @@ pub const Diagnostics = struct {...@@ -62,10 +68,11 @@ pub const Diagnostics = struct {
62 }68 }
6369
64 pub fn renderToStdErr(self: *Diagnostics, cwd: std.fs.Dir, source: []const u8, tty_config: std.Io.tty.Config, source_mappings: ?SourceMappings) void {70 pub fn renderToStdErr(self: *Diagnostics, cwd: std.fs.Dir, source: []const u8, tty_config: std.Io.tty.Config, source_mappings: ?SourceMappings) void {
71 const io = self.io;
65 const stderr = std.debug.lockStderrWriter(&.{});72 const stderr = std.debug.lockStderrWriter(&.{});
66 defer std.debug.unlockStderrWriter();73 defer std.debug.unlockStderrWriter();
67 for (self.errors.items) |err_details| {74 for (self.errors.items) |err_details| {
68 renderErrorMessage(stderr, tty_config, cwd, err_details, source, self.strings.items, source_mappings) catch return;75 renderErrorMessage(io, stderr, tty_config, cwd, err_details, source, self.strings.items, source_mappings) catch return;
69 }76 }
70 }77 }
7178
...@@ -167,9 +174,9 @@ pub const ErrorDetails = struct {...@@ -167,9 +174,9 @@ pub const ErrorDetails = struct {
167 filename_string_index: FilenameStringIndex,174 filename_string_index: FilenameStringIndex,
168175
169 pub const FilenameStringIndex = std.meta.Int(.unsigned, 32 - @bitSizeOf(FileOpenErrorEnum));176 pub const FilenameStringIndex = std.meta.Int(.unsigned, 32 - @bitSizeOf(FileOpenErrorEnum));
170 pub const FileOpenErrorEnum = std.meta.FieldEnum(std.fs.File.OpenError);177 pub const FileOpenErrorEnum = std.meta.FieldEnum(std.fs.File.OpenError || std.fs.File.StatError);
171178
172 pub fn enumFromError(err: std.fs.File.OpenError) FileOpenErrorEnum {179 pub fn enumFromError(err: (std.fs.File.OpenError || std.fs.File.StatError)) FileOpenErrorEnum {
173 return switch (err) {180 return switch (err) {
174 inline else => |e| @field(ErrorDetails.FileOpenError.FileOpenErrorEnum, @errorName(e)),181 inline else => |e| @field(ErrorDetails.FileOpenError.FileOpenErrorEnum, @errorName(e)),
175 };182 };
...@@ -894,7 +901,16 @@ fn cellCount(code_page: SupportedCodePage, source: []const u8, start_index: usiz...@@ -894,7 +901,16 @@ fn cellCount(code_page: SupportedCodePage, source: []const u8, start_index: usiz
894901
895const truncated_str = "<...truncated...>";902const truncated_str = "<...truncated...>";
896903
897pub fn renderErrorMessage(writer: *std.Io.Writer, tty_config: std.Io.tty.Config, cwd: std.fs.Dir, err_details: ErrorDetails, source: []const u8, strings: []const []const u8, source_mappings: ?SourceMappings) !void {904pub fn renderErrorMessage(
905 io: Io,
906 writer: *std.Io.Writer,
907 tty_config: std.Io.tty.Config,
908 cwd: std.fs.Dir,
909 err_details: ErrorDetails,
910 source: []const u8,
911 strings: []const []const u8,
912 source_mappings: ?SourceMappings,
913) !void {
898 if (err_details.type == .hint) return;914 if (err_details.type == .hint) return;
899915
900 const source_line_start = err_details.token.getLineStartForErrorDisplay(source);916 const source_line_start = err_details.token.getLineStartForErrorDisplay(source);
...@@ -989,6 +1005,7 @@ pub fn renderErrorMessage(writer: *std.Io.Writer, tty_config: std.Io.tty.Config,...@@ -989,6 +1005,7 @@ pub fn renderErrorMessage(writer: *std.Io.Writer, tty_config: std.Io.tty.Config,
989 var initial_lines_err: ?anyerror = null;1005 var initial_lines_err: ?anyerror = null;
990 var file_reader_buf: [max_source_line_bytes * 2]u8 = undefined;1006 var file_reader_buf: [max_source_line_bytes * 2]u8 = undefined;
991 var corresponding_lines: ?CorrespondingLines = CorrespondingLines.init(1007 var corresponding_lines: ?CorrespondingLines = CorrespondingLines.init(
1008 io,
992 cwd,1009 cwd,
993 err_details,1010 err_details,
994 source_line_for_display.line,1011 source_line_for_display.line,
...@@ -1084,6 +1101,7 @@ const CorrespondingLines = struct {...@@ -1084,6 +1101,7 @@ const CorrespondingLines = struct {
1084 code_page: SupportedCodePage,1101 code_page: SupportedCodePage,
10851102
1086 pub fn init(1103 pub fn init(
1104 io: Io,
1087 cwd: std.fs.Dir,1105 cwd: std.fs.Dir,
1088 err_details: ErrorDetails,1106 err_details: ErrorDetails,
1089 line_for_comparison: []const u8,1107 line_for_comparison: []const u8,
...@@ -1108,7 +1126,7 @@ const CorrespondingLines = struct {...@@ -1108,7 +1126,7 @@ const CorrespondingLines = struct {
1108 .code_page = err_details.code_page,1126 .code_page = err_details.code_page,
1109 .file_reader = undefined,1127 .file_reader = undefined,
1110 };1128 };
1111 corresponding_lines.file_reader = corresponding_lines.file.reader(file_reader_buf);1129 corresponding_lines.file_reader = corresponding_lines.file.reader(io, file_reader_buf);
1112 errdefer corresponding_lines.deinit();1130 errdefer corresponding_lines.deinit();
11131131
1114 try corresponding_lines.writeLineFromStreamVerbatim(1132 try corresponding_lines.writeLineFromStreamVerbatim(
lib/compiler/resinator/main.zig+89-72
...@@ -1,5 +1,9 @@...@@ -1,5 +1,9 @@
1const std = @import("std");
2const builtin = @import("builtin");1const builtin = @import("builtin");
2
3const std = @import("std");
4const Io = std.Io;
5const Allocator = std.mem.Allocator;
6
3const removeComments = @import("comments.zig").removeComments;7const removeComments = @import("comments.zig").removeComments;
4const parseAndRemoveLineCommands = @import("source_mapping.zig").parseAndRemoveLineCommands;8const parseAndRemoveLineCommands = @import("source_mapping.zig").parseAndRemoveLineCommands;
5const compile = @import("compile.zig").compile;9const compile = @import("compile.zig").compile;
...@@ -16,19 +20,18 @@ const aro = @import("aro");...@@ -16,19 +20,18 @@ const aro = @import("aro");
16const compiler_util = @import("../util.zig");20const compiler_util = @import("../util.zig");
1721
18pub fn main() !void {22pub fn main() !void {
19 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;23 var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
20 defer std.debug.assert(gpa.deinit() == .ok);24 defer std.debug.assert(debug_allocator.deinit() == .ok);
21 const allocator = gpa.allocator();25 const gpa = debug_allocator.allocator();
2226
23 var arena_state = std.heap.ArenaAllocator.init(allocator);27 var arena_state = std.heap.ArenaAllocator.init(gpa);
24 defer arena_state.deinit();28 defer arena_state.deinit();
25 const arena = arena_state.allocator();29 const arena = arena_state.allocator();
2630
27 const stderr = std.fs.File.stderr();31 const stderr = std.fs.File.stderr();
28 const stderr_config = std.Io.tty.detectConfig(stderr);32 const stderr_config = std.Io.tty.detectConfig(stderr);
2933
30 const args = try std.process.argsAlloc(allocator);34 const args = try std.process.argsAlloc(arena);
31 defer std.process.argsFree(allocator, args);
3235
33 if (args.len < 2) {36 if (args.len < 2) {
34 try renderErrorMessage(std.debug.lockStderrWriter(&.{}), stderr_config, .err, "expected zig lib dir as first argument", .{});37 try renderErrorMessage(std.debug.lockStderrWriter(&.{}), stderr_config, .err, "expected zig lib dir as first argument", .{});
...@@ -59,11 +62,11 @@ pub fn main() !void {...@@ -59,11 +62,11 @@ pub fn main() !void {
59 };62 };
6063
61 var options = options: {64 var options = options: {
62 var cli_diagnostics = cli.Diagnostics.init(allocator);65 var cli_diagnostics = cli.Diagnostics.init(gpa);
63 defer cli_diagnostics.deinit();66 defer cli_diagnostics.deinit();
64 var options = cli.parse(allocator, cli_args, &cli_diagnostics) catch |err| switch (err) {67 var options = cli.parse(gpa, cli_args, &cli_diagnostics) catch |err| switch (err) {
65 error.ParseError => {68 error.ParseError => {
66 try error_handler.emitCliDiagnostics(allocator, cli_args, &cli_diagnostics);69 try error_handler.emitCliDiagnostics(gpa, cli_args, &cli_diagnostics);
67 std.process.exit(1);70 std.process.exit(1);
68 },71 },
69 else => |e| return e,72 else => |e| return e,
...@@ -84,6 +87,10 @@ pub fn main() !void {...@@ -84,6 +87,10 @@ pub fn main() !void {
84 };87 };
85 defer options.deinit();88 defer options.deinit();
8689
90 var threaded: std.Io.Threaded = .init(gpa);
91 defer threaded.deinit();
92 const io = threaded.io();
93
87 if (options.print_help_and_exit) {94 if (options.print_help_and_exit) {
88 try cli.writeUsage(stdout, "zig rc");95 try cli.writeUsage(stdout, "zig rc");
89 try stdout.flush();96 try stdout.flush();
...@@ -99,12 +106,13 @@ pub fn main() !void {...@@ -99,12 +106,13 @@ pub fn main() !void {
99 try stdout.flush();106 try stdout.flush();
100 }107 }
101108
102 var dependencies = Dependencies.init(allocator);109 var dependencies = Dependencies.init(gpa);
103 defer dependencies.deinit();110 defer dependencies.deinit();
104 const maybe_dependencies: ?*Dependencies = if (options.depfile_path != null) &dependencies else null;111 const maybe_dependencies: ?*Dependencies = if (options.depfile_path != null) &dependencies else null;
105112
106 var include_paths = LazyIncludePaths{113 var include_paths = LazyIncludePaths{
107 .arena = arena,114 .arena = arena,
115 .io = io,
108 .auto_includes_option = options.auto_includes,116 .auto_includes_option = options.auto_includes,
109 .zig_lib_dir = zig_lib_dir,117 .zig_lib_dir = zig_lib_dir,
110 .target_machine_type = options.coff_options.target,118 .target_machine_type = options.coff_options.target,
...@@ -112,12 +120,12 @@ pub fn main() !void {...@@ -112,12 +120,12 @@ pub fn main() !void {
112120
113 const full_input = full_input: {121 const full_input = full_input: {
114 if (options.input_format == .rc and options.preprocess != .no) {122 if (options.input_format == .rc and options.preprocess != .no) {
115 var preprocessed_buf: std.Io.Writer.Allocating = .init(allocator);123 var preprocessed_buf: std.Io.Writer.Allocating = .init(gpa);
116 errdefer preprocessed_buf.deinit();124 errdefer preprocessed_buf.deinit();
117125
118 // We're going to throw away everything except the final preprocessed output anyway,126 // We're going to throw away everything except the final preprocessed output anyway,
119 // so we can use a scoped arena for everything else.127 // so we can use a scoped arena for everything else.
120 var aro_arena_state = std.heap.ArenaAllocator.init(allocator);128 var aro_arena_state = std.heap.ArenaAllocator.init(gpa);
121 defer aro_arena_state.deinit();129 defer aro_arena_state.deinit();
122 const aro_arena = aro_arena_state.allocator();130 const aro_arena = aro_arena_state.allocator();
123131
...@@ -129,12 +137,12 @@ pub fn main() !void {...@@ -129,12 +137,12 @@ pub fn main() !void {
129 .color = stderr_config,137 .color = stderr_config,
130 } } },138 } } },
131 true => .{ .output = .{ .to_list = .{139 true => .{ .output = .{ .to_list = .{
132 .arena = .init(allocator),140 .arena = .init(gpa),
133 } } },141 } } },
134 };142 };
135 defer diagnostics.deinit();143 defer diagnostics.deinit();
136144
137 var comp = aro.Compilation.init(aro_arena, aro_arena, &diagnostics, std.fs.cwd());145 var comp = aro.Compilation.init(aro_arena, aro_arena, io, &diagnostics, std.fs.cwd());
138 defer comp.deinit();146 defer comp.deinit();
139147
140 var argv: std.ArrayList([]const u8) = .empty;148 var argv: std.ArrayList([]const u8) = .empty;
...@@ -159,20 +167,20 @@ pub fn main() !void {...@@ -159,20 +167,20 @@ pub fn main() !void {
159167
160 preprocess.preprocess(&comp, &preprocessed_buf.writer, argv.items, maybe_dependencies) catch |err| switch (err) {168 preprocess.preprocess(&comp, &preprocessed_buf.writer, argv.items, maybe_dependencies) catch |err| switch (err) {
161 error.GeneratedSourceError => {169 error.GeneratedSourceError => {
162 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessor setup (this is always a bug)", &comp);170 try error_handler.emitAroDiagnostics(gpa, "failed during preprocessor setup (this is always a bug)", &comp);
163 std.process.exit(1);171 std.process.exit(1);
164 },172 },
165 // ArgError can occur if e.g. the .rc file is not found173 // ArgError can occur if e.g. the .rc file is not found
166 error.ArgError, error.PreprocessError => {174 error.ArgError, error.PreprocessError => {
167 try error_handler.emitAroDiagnostics(allocator, "failed during preprocessing", &comp);175 try error_handler.emitAroDiagnostics(gpa, "failed during preprocessing", &comp);
168 std.process.exit(1);176 std.process.exit(1);
169 },177 },
170 error.FileTooBig => {178 error.FileTooBig => {
171 try error_handler.emitMessage(allocator, .err, "failed during preprocessing: maximum file size exceeded", .{});179 try error_handler.emitMessage(gpa, .err, "failed during preprocessing: maximum file size exceeded", .{});
172 std.process.exit(1);180 std.process.exit(1);
173 },181 },
174 error.WriteFailed => {182 error.WriteFailed => {
175 try error_handler.emitMessage(allocator, .err, "failed during preprocessing: error writing the preprocessed output", .{});183 try error_handler.emitMessage(gpa, .err, "failed during preprocessing: error writing the preprocessed output", .{});
176 std.process.exit(1);184 std.process.exit(1);
177 },185 },
178 error.OutOfMemory => |e| return e,186 error.OutOfMemory => |e| return e,
...@@ -182,22 +190,22 @@ pub fn main() !void {...@@ -182,22 +190,22 @@ pub fn main() !void {
182 } else {190 } else {
183 switch (options.input_source) {191 switch (options.input_source) {
184 .stdio => |file| {192 .stdio => |file| {
185 var file_reader = file.reader(&.{});193 var file_reader = file.reader(io, &.{});
186 break :full_input file_reader.interface.allocRemaining(allocator, .unlimited) catch |err| {194 break :full_input file_reader.interface.allocRemaining(gpa, .unlimited) catch |err| {
187 try error_handler.emitMessage(allocator, .err, "unable to read input from stdin: {s}", .{@errorName(err)});195 try error_handler.emitMessage(gpa, .err, "unable to read input from stdin: {s}", .{@errorName(err)});
188 std.process.exit(1);196 std.process.exit(1);
189 };197 };
190 },198 },
191 .filename => |input_filename| {199 .filename => |input_filename| {
192 break :full_input std.fs.cwd().readFileAlloc(input_filename, allocator, .unlimited) catch |err| {200 break :full_input std.fs.cwd().readFileAlloc(input_filename, gpa, .unlimited) catch |err| {
193 try error_handler.emitMessage(allocator, .err, "unable to read input file path '{s}': {s}", .{ input_filename, @errorName(err) });201 try error_handler.emitMessage(gpa, .err, "unable to read input file path '{s}': {s}", .{ input_filename, @errorName(err) });
194 std.process.exit(1);202 std.process.exit(1);
195 };203 };
196 },204 },
197 }205 }
198 }206 }
199 };207 };
200 defer allocator.free(full_input);208 defer gpa.free(full_input);
201209
202 if (options.preprocess == .only) {210 if (options.preprocess == .only) {
203 switch (options.output_source) {211 switch (options.output_source) {
...@@ -221,55 +229,55 @@ pub fn main() !void {...@@ -221,55 +229,55 @@ pub fn main() !void {
221 }229 }
222 else if (options.input_format == .res)230 else if (options.input_format == .res)
223 IoStream.fromIoSource(options.input_source, .input) catch |err| {231 IoStream.fromIoSource(options.input_source, .input) catch |err| {
224 try error_handler.emitMessage(allocator, .err, "unable to read res file path '{s}': {s}", .{ options.input_source.filename, @errorName(err) });232 try error_handler.emitMessage(gpa, .err, "unable to read res file path '{s}': {s}", .{ options.input_source.filename, @errorName(err) });
225 std.process.exit(1);233 std.process.exit(1);
226 }234 }
227 else235 else
228 IoStream.fromIoSource(options.output_source, .output) catch |err| {236 IoStream.fromIoSource(options.output_source, .output) catch |err| {
229 try error_handler.emitMessage(allocator, .err, "unable to create output file '{s}': {s}", .{ options.output_source.filename, @errorName(err) });237 try error_handler.emitMessage(gpa, .err, "unable to create output file '{s}': {s}", .{ options.output_source.filename, @errorName(err) });
230 std.process.exit(1);238 std.process.exit(1);
231 };239 };
232 defer res_stream.deinit(allocator);240 defer res_stream.deinit(gpa);
233241
234 const res_data = res_data: {242 const res_data = res_data: {
235 if (options.input_format != .res) {243 if (options.input_format != .res) {
236 // Note: We still want to run this when no-preprocess is set because:244 // Note: We still want to run this when no-preprocess is set because:
237 // 1. We want to print accurate line numbers after removing multiline comments245 // 1. We want to print accurate line numbers after removing multiline comments
238 // 2. We want to be able to handle an already-preprocessed input with #line commands in it246 // 2. We want to be able to handle an already-preprocessed input with #line commands in it
239 var mapping_results = parseAndRemoveLineCommands(allocator, full_input, full_input, .{ .initial_filename = options.input_source.filename }) catch |err| switch (err) {247 var mapping_results = parseAndRemoveLineCommands(gpa, full_input, full_input, .{ .initial_filename = options.input_source.filename }) catch |err| switch (err) {
240 error.InvalidLineCommand => {248 error.InvalidLineCommand => {
241 // TODO: Maybe output the invalid line command249 // TODO: Maybe output the invalid line command
242 try error_handler.emitMessage(allocator, .err, "invalid line command in the preprocessed source", .{});250 try error_handler.emitMessage(gpa, .err, "invalid line command in the preprocessed source", .{});
243 if (options.preprocess == .no) {251 if (options.preprocess == .no) {
244 try error_handler.emitMessage(allocator, .note, "line commands must be of the format: #line <num> \"<path>\"", .{});252 try error_handler.emitMessage(gpa, .note, "line commands must be of the format: #line <num> \"<path>\"", .{});
245 } else {253 } else {
246 try error_handler.emitMessage(allocator, .note, "this is likely to be a bug, please report it", .{});254 try error_handler.emitMessage(gpa, .note, "this is likely to be a bug, please report it", .{});
247 }255 }
248 std.process.exit(1);256 std.process.exit(1);
249 },257 },
250 error.LineNumberOverflow => {258 error.LineNumberOverflow => {
251 // TODO: Better error message259 // TODO: Better error message
252 try error_handler.emitMessage(allocator, .err, "line number count exceeded maximum of {}", .{std.math.maxInt(usize)});260 try error_handler.emitMessage(gpa, .err, "line number count exceeded maximum of {}", .{std.math.maxInt(usize)});
253 std.process.exit(1);261 std.process.exit(1);
254 },262 },
255 error.OutOfMemory => |e| return e,263 error.OutOfMemory => |e| return e,
256 };264 };
257 defer mapping_results.mappings.deinit(allocator);265 defer mapping_results.mappings.deinit(gpa);
258266
259 const default_code_page = options.default_code_page orelse .windows1252;267 const default_code_page = options.default_code_page orelse .windows1252;
260 const has_disjoint_code_page = hasDisjointCodePage(mapping_results.result, &mapping_results.mappings, default_code_page);268 const has_disjoint_code_page = hasDisjointCodePage(mapping_results.result, &mapping_results.mappings, default_code_page);
261269
262 const final_input = try removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings);270 const final_input = try removeComments(mapping_results.result, mapping_results.result, &mapping_results.mappings);
263271
264 var diagnostics = Diagnostics.init(allocator);272 var diagnostics = Diagnostics.init(gpa, io);
265 defer diagnostics.deinit();273 defer diagnostics.deinit();
266274
267 var output_buffer: [4096]u8 = undefined;275 var output_buffer: [4096]u8 = undefined;
268 var res_stream_writer = res_stream.source.writer(allocator, &output_buffer);276 var res_stream_writer = res_stream.source.writer(gpa, &output_buffer);
269 defer res_stream_writer.deinit(&res_stream.source);277 defer res_stream_writer.deinit(&res_stream.source);
270 const output_buffered_stream = res_stream_writer.interface();278 const output_buffered_stream = res_stream_writer.interface();
271279
272 compile(allocator, final_input, output_buffered_stream, .{280 compile(gpa, io, final_input, output_buffered_stream, .{
273 .cwd = std.fs.cwd(),281 .cwd = std.fs.cwd(),
274 .diagnostics = &diagnostics,282 .diagnostics = &diagnostics,
275 .source_mappings = &mapping_results.mappings,283 .source_mappings = &mapping_results.mappings,
...@@ -287,7 +295,7 @@ pub fn main() !void {...@@ -287,7 +295,7 @@ pub fn main() !void {
287 .warn_instead_of_error_on_invalid_code_page = options.warn_instead_of_error_on_invalid_code_page,295 .warn_instead_of_error_on_invalid_code_page = options.warn_instead_of_error_on_invalid_code_page,
288 }) catch |err| switch (err) {296 }) catch |err| switch (err) {
289 error.ParseError, error.CompileError => {297 error.ParseError, error.CompileError => {
290 try error_handler.emitDiagnostics(allocator, std.fs.cwd(), final_input, &diagnostics, mapping_results.mappings);298 try error_handler.emitDiagnostics(gpa, std.fs.cwd(), final_input, &diagnostics, mapping_results.mappings);
291 // Delete the output file on error299 // Delete the output file on error
292 res_stream.cleanupAfterError();300 res_stream.cleanupAfterError();
293 std.process.exit(1);301 std.process.exit(1);
...@@ -305,7 +313,7 @@ pub fn main() !void {...@@ -305,7 +313,7 @@ pub fn main() !void {
305 // write the depfile313 // write the depfile
306 if (options.depfile_path) |depfile_path| {314 if (options.depfile_path) |depfile_path| {
307 var depfile = std.fs.cwd().createFile(depfile_path, .{}) catch |err| {315 var depfile = std.fs.cwd().createFile(depfile_path, .{}) catch |err| {
308 try error_handler.emitMessage(allocator, .err, "unable to create depfile '{s}': {s}", .{ depfile_path, @errorName(err) });316 try error_handler.emitMessage(gpa, .err, "unable to create depfile '{s}': {s}", .{ depfile_path, @errorName(err) });
309 std.process.exit(1);317 std.process.exit(1);
310 };318 };
311 defer depfile.close();319 defer depfile.close();
...@@ -332,41 +340,41 @@ pub fn main() !void {...@@ -332,41 +340,41 @@ pub fn main() !void {
332340
333 if (options.output_format != .coff) return;341 if (options.output_format != .coff) return;
334342
335 break :res_data res_stream.source.readAll(allocator) catch |err| {343 break :res_data res_stream.source.readAll(gpa, io) catch |err| {
336 try error_handler.emitMessage(allocator, .err, "unable to read res from '{s}': {s}", .{ res_stream.name, @errorName(err) });344 try error_handler.emitMessage(gpa, .err, "unable to read res from '{s}': {s}", .{ res_stream.name, @errorName(err) });
337 std.process.exit(1);345 std.process.exit(1);
338 };346 };
339 };347 };
340 // No need to keep the res_data around after parsing the resources from it348 // No need to keep the res_data around after parsing the resources from it
341 defer res_data.deinit(allocator);349 defer res_data.deinit(gpa);
342350
343 std.debug.assert(options.output_format == .coff);351 std.debug.assert(options.output_format == .coff);
344352
345 // TODO: Maybe use a buffered file reader instead of reading file into memory -> fbs353 // TODO: Maybe use a buffered file reader instead of reading file into memory -> fbs
346 var res_reader: std.Io.Reader = .fixed(res_data.bytes);354 var res_reader: std.Io.Reader = .fixed(res_data.bytes);
347 break :resources cvtres.parseRes(allocator, &res_reader, .{ .max_size = res_data.bytes.len }) catch |err| {355 break :resources cvtres.parseRes(gpa, &res_reader, .{ .max_size = res_data.bytes.len }) catch |err| {
348 // TODO: Better errors356 // TODO: Better errors
349 try error_handler.emitMessage(allocator, .err, "unable to parse res from '{s}': {s}", .{ res_stream.name, @errorName(err) });357 try error_handler.emitMessage(gpa, .err, "unable to parse res from '{s}': {s}", .{ res_stream.name, @errorName(err) });
350 std.process.exit(1);358 std.process.exit(1);
351 };359 };
352 };360 };
353 defer resources.deinit();361 defer resources.deinit();
354362
355 var coff_stream = IoStream.fromIoSource(options.output_source, .output) catch |err| {363 var coff_stream = IoStream.fromIoSource(options.output_source, .output) catch |err| {
356 try error_handler.emitMessage(allocator, .err, "unable to create output file '{s}': {s}", .{ options.output_source.filename, @errorName(err) });364 try error_handler.emitMessage(gpa, .err, "unable to create output file '{s}': {s}", .{ options.output_source.filename, @errorName(err) });
357 std.process.exit(1);365 std.process.exit(1);
358 };366 };
359 defer coff_stream.deinit(allocator);367 defer coff_stream.deinit(gpa);
360368
361 var coff_output_buffer: [4096]u8 = undefined;369 var coff_output_buffer: [4096]u8 = undefined;
362 var coff_output_buffered_stream = coff_stream.source.writer(allocator, &coff_output_buffer);370 var coff_output_buffered_stream = coff_stream.source.writer(gpa, &coff_output_buffer);
363371
364 var cvtres_diagnostics: cvtres.Diagnostics = .{ .none = {} };372 var cvtres_diagnostics: cvtres.Diagnostics = .{ .none = {} };
365 cvtres.writeCoff(allocator, coff_output_buffered_stream.interface(), resources.list.items, options.coff_options, &cvtres_diagnostics) catch |err| {373 cvtres.writeCoff(gpa, coff_output_buffered_stream.interface(), resources.list.items, options.coff_options, &cvtres_diagnostics) catch |err| {
366 switch (err) {374 switch (err) {
367 error.DuplicateResource => {375 error.DuplicateResource => {
368 const duplicate_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];376 const duplicate_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];
369 try error_handler.emitMessage(allocator, .err, "duplicate resource [id: {f}, type: {f}, language: {f}]", .{377 try error_handler.emitMessage(gpa, .err, "duplicate resource [id: {f}, type: {f}, language: {f}]", .{
370 duplicate_resource.name_value,378 duplicate_resource.name_value,
371 fmtResourceType(duplicate_resource.type_value),379 fmtResourceType(duplicate_resource.type_value),
372 duplicate_resource.language,380 duplicate_resource.language,
...@@ -374,8 +382,8 @@ pub fn main() !void {...@@ -374,8 +382,8 @@ pub fn main() !void {
374 },382 },
375 error.ResourceDataTooLong => {383 error.ResourceDataTooLong => {
376 const overflow_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];384 const overflow_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];
377 try error_handler.emitMessage(allocator, .err, "resource has a data length that is too large to be written into a coff section", .{});385 try error_handler.emitMessage(gpa, .err, "resource has a data length that is too large to be written into a coff section", .{});
378 try error_handler.emitMessage(allocator, .note, "the resource with the invalid size is [id: {f}, type: {f}, language: {f}]", .{386 try error_handler.emitMessage(gpa, .note, "the resource with the invalid size is [id: {f}, type: {f}, language: {f}]", .{
379 overflow_resource.name_value,387 overflow_resource.name_value,
380 fmtResourceType(overflow_resource.type_value),388 fmtResourceType(overflow_resource.type_value),
381 overflow_resource.language,389 overflow_resource.language,
...@@ -383,15 +391,15 @@ pub fn main() !void {...@@ -383,15 +391,15 @@ pub fn main() !void {
383 },391 },
384 error.TotalResourceDataTooLong => {392 error.TotalResourceDataTooLong => {
385 const overflow_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];393 const overflow_resource = resources.list.items[cvtres_diagnostics.duplicate_resource];
386 try error_handler.emitMessage(allocator, .err, "total resource data exceeds the maximum of the coff 'size of raw data' field", .{});394 try error_handler.emitMessage(gpa, .err, "total resource data exceeds the maximum of the coff 'size of raw data' field", .{});
387 try error_handler.emitMessage(allocator, .note, "size overflow occurred when attempting to write this resource: [id: {f}, type: {f}, language: {f}]", .{395 try error_handler.emitMessage(gpa, .note, "size overflow occurred when attempting to write this resource: [id: {f}, type: {f}, language: {f}]", .{
388 overflow_resource.name_value,396 overflow_resource.name_value,
389 fmtResourceType(overflow_resource.type_value),397 fmtResourceType(overflow_resource.type_value),
390 overflow_resource.language,398 overflow_resource.language,
391 });399 });
392 },400 },
393 else => {401 else => {
394 try error_handler.emitMessage(allocator, .err, "unable to write coff output file '{s}': {s}", .{ coff_stream.name, @errorName(err) });402 try error_handler.emitMessage(gpa, .err, "unable to write coff output file '{s}': {s}", .{ coff_stream.name, @errorName(err) });
395 },403 },
396 }404 }
397 // Delete the output file on error405 // Delete the output file on error
...@@ -423,7 +431,7 @@ const IoStream = struct {...@@ -423,7 +431,7 @@ const IoStream = struct {
423 };431 };
424 }432 }
425433
426 pub fn deinit(self: *IoStream, allocator: std.mem.Allocator) void {434 pub fn deinit(self: *IoStream, allocator: Allocator) void {
427 self.source.deinit(allocator);435 self.source.deinit(allocator);
428 }436 }
429437
...@@ -458,7 +466,7 @@ const IoStream = struct {...@@ -458,7 +466,7 @@ const IoStream = struct {
458 }466 }
459 }467 }
460468
461 pub fn deinit(self: *Source, allocator: std.mem.Allocator) void {469 pub fn deinit(self: *Source, allocator: Allocator) void {
462 switch (self.*) {470 switch (self.*) {
463 .file => |file| file.close(),471 .file => |file| file.close(),
464 .stdio => {},472 .stdio => {},
...@@ -471,18 +479,18 @@ const IoStream = struct {...@@ -471,18 +479,18 @@ const IoStream = struct {
471 bytes: []const u8,479 bytes: []const u8,
472 needs_free: bool,480 needs_free: bool,
473481
474 pub fn deinit(self: Data, allocator: std.mem.Allocator) void {482 pub fn deinit(self: Data, allocator: Allocator) void {
475 if (self.needs_free) {483 if (self.needs_free) {
476 allocator.free(self.bytes);484 allocator.free(self.bytes);
477 }485 }
478 }486 }
479 };487 };
480488
481 pub fn readAll(self: Source, allocator: std.mem.Allocator) !Data {489 pub fn readAll(self: Source, allocator: Allocator, io: Io) !Data {
482 return switch (self) {490 return switch (self) {
483 inline .file, .stdio => |file| .{491 inline .file, .stdio => |file| .{
484 .bytes = b: {492 .bytes = b: {
485 var file_reader = file.reader(&.{});493 var file_reader = file.reader(io, &.{});
486 break :b try file_reader.interface.allocRemaining(allocator, .unlimited);494 break :b try file_reader.interface.allocRemaining(allocator, .unlimited);
487 },495 },
488 .needs_free = true,496 .needs_free = true,
...@@ -496,7 +504,7 @@ const IoStream = struct {...@@ -496,7 +504,7 @@ const IoStream = struct {
496 file: std.fs.File.Writer,504 file: std.fs.File.Writer,
497 allocating: std.Io.Writer.Allocating,505 allocating: std.Io.Writer.Allocating,
498506
499 pub const Error = std.mem.Allocator.Error || std.fs.File.WriteError;507 pub const Error = Allocator.Error || std.fs.File.WriteError;
500508
501 pub fn interface(this: *@This()) *std.Io.Writer {509 pub fn interface(this: *@This()) *std.Io.Writer {
502 return switch (this.*) {510 return switch (this.*) {
...@@ -514,7 +522,7 @@ const IoStream = struct {...@@ -514,7 +522,7 @@ const IoStream = struct {
514 }522 }
515 };523 };
516524
517 pub fn writer(source: *Source, allocator: std.mem.Allocator, buffer: []u8) Writer {525 pub fn writer(source: *Source, allocator: Allocator, buffer: []u8) Writer {
518 return switch (source.*) {526 return switch (source.*) {
519 .file, .stdio => |file| .{ .file = file.writer(buffer) },527 .file, .stdio => |file| .{ .file = file.writer(buffer) },
520 .memory => |*list| .{ .allocating = .fromArrayList(allocator, list) },528 .memory => |*list| .{ .allocating = .fromArrayList(allocator, list) },
...@@ -525,17 +533,20 @@ const IoStream = struct {...@@ -525,17 +533,20 @@ const IoStream = struct {
525};533};
526534
527const LazyIncludePaths = struct {535const LazyIncludePaths = struct {
528 arena: std.mem.Allocator,536 arena: Allocator,
537 io: Io,
529 auto_includes_option: cli.Options.AutoIncludes,538 auto_includes_option: cli.Options.AutoIncludes,
530 zig_lib_dir: []const u8,539 zig_lib_dir: []const u8,
531 target_machine_type: std.coff.IMAGE.FILE.MACHINE,540 target_machine_type: std.coff.IMAGE.FILE.MACHINE,
532 resolved_include_paths: ?[]const []const u8 = null,541 resolved_include_paths: ?[]const []const u8 = null,
533542
534 pub fn get(self: *LazyIncludePaths, error_handler: *ErrorHandler) ![]const []const u8 {543 pub fn get(self: *LazyIncludePaths, error_handler: *ErrorHandler) ![]const []const u8 {
544 const io = self.io;
545
535 if (self.resolved_include_paths) |include_paths|546 if (self.resolved_include_paths) |include_paths|
536 return include_paths;547 return include_paths;
537548
538 return getIncludePaths(self.arena, self.auto_includes_option, self.zig_lib_dir, self.target_machine_type) catch |err| switch (err) {549 return getIncludePaths(self.arena, io, self.auto_includes_option, self.zig_lib_dir, self.target_machine_type) catch |err| switch (err) {
539 error.OutOfMemory => |e| return e,550 error.OutOfMemory => |e| return e,
540 else => |e| {551 else => |e| {
541 switch (e) {552 switch (e) {
...@@ -556,7 +567,13 @@ const LazyIncludePaths = struct {...@@ -556,7 +567,13 @@ const LazyIncludePaths = struct {
556 }567 }
557};568};
558569
559fn getIncludePaths(arena: std.mem.Allocator, auto_includes_option: cli.Options.AutoIncludes, zig_lib_dir: []const u8, target_machine_type: std.coff.IMAGE.FILE.MACHINE) ![]const []const u8 {570fn getIncludePaths(
571 arena: Allocator,
572 io: Io,
573 auto_includes_option: cli.Options.AutoIncludes,
574 zig_lib_dir: []const u8,
575 target_machine_type: std.coff.IMAGE.FILE.MACHINE,
576) ![]const []const u8 {
560 if (auto_includes_option == .none) return &[_][]const u8{};577 if (auto_includes_option == .none) return &[_][]const u8{};
561578
562 const includes_arch: std.Target.Cpu.Arch = switch (target_machine_type) {579 const includes_arch: std.Target.Cpu.Arch = switch (target_machine_type) {
...@@ -626,7 +643,7 @@ fn getIncludePaths(arena: std.mem.Allocator, auto_includes_option: cli.Options.A...@@ -626,7 +643,7 @@ fn getIncludePaths(arena: std.mem.Allocator, auto_includes_option: cli.Options.A
626 .cpu_arch = includes_arch,643 .cpu_arch = includes_arch,
627 .abi = .gnu,644 .abi = .gnu,
628 };645 };
629 const target = std.zig.resolveTargetQueryOrFatal(target_query);646 const target = std.zig.resolveTargetQueryOrFatal(io, target_query);
630 const is_native_abi = target_query.isNativeAbi();647 const is_native_abi = target_query.isNativeAbi();
631 const detected_libc = std.zig.LibCDirs.detect(arena, zig_lib_dir, &target, is_native_abi, true, null) catch |err| switch (err) {648 const detected_libc = std.zig.LibCDirs.detect(arena, zig_lib_dir, &target, is_native_abi, true, null) catch |err| switch (err) {
632 error.OutOfMemory => |e| return e,649 error.OutOfMemory => |e| return e,
...@@ -647,7 +664,7 @@ const ErrorHandler = union(enum) {...@@ -647,7 +664,7 @@ const ErrorHandler = union(enum) {
647664
648 pub fn emitCliDiagnostics(665 pub fn emitCliDiagnostics(
649 self: *ErrorHandler,666 self: *ErrorHandler,
650 allocator: std.mem.Allocator,667 allocator: Allocator,
651 args: []const []const u8,668 args: []const []const u8,
652 diagnostics: *cli.Diagnostics,669 diagnostics: *cli.Diagnostics,
653 ) !void {670 ) !void {
...@@ -666,7 +683,7 @@ const ErrorHandler = union(enum) {...@@ -666,7 +683,7 @@ const ErrorHandler = union(enum) {
666683
667 pub fn emitAroDiagnostics(684 pub fn emitAroDiagnostics(
668 self: *ErrorHandler,685 self: *ErrorHandler,
669 allocator: std.mem.Allocator,686 allocator: Allocator,
670 fail_msg: []const u8,687 fail_msg: []const u8,
671 comp: *aro.Compilation,688 comp: *aro.Compilation,
672 ) !void {689 ) !void {
...@@ -692,7 +709,7 @@ const ErrorHandler = union(enum) {...@@ -692,7 +709,7 @@ const ErrorHandler = union(enum) {
692709
693 pub fn emitDiagnostics(710 pub fn emitDiagnostics(
694 self: *ErrorHandler,711 self: *ErrorHandler,
695 allocator: std.mem.Allocator,712 allocator: Allocator,
696 cwd: std.fs.Dir,713 cwd: std.fs.Dir,
697 source: []const u8,714 source: []const u8,
698 diagnostics: *Diagnostics,715 diagnostics: *Diagnostics,
...@@ -713,7 +730,7 @@ const ErrorHandler = union(enum) {...@@ -713,7 +730,7 @@ const ErrorHandler = union(enum) {
713730
714 pub fn emitMessage(731 pub fn emitMessage(
715 self: *ErrorHandler,732 self: *ErrorHandler,
716 allocator: std.mem.Allocator,733 allocator: Allocator,
717 msg_type: @import("utils.zig").ErrorMessageType,734 msg_type: @import("utils.zig").ErrorMessageType,
718 comptime format: []const u8,735 comptime format: []const u8,
719 args: anytype,736 args: anytype,
...@@ -738,7 +755,7 @@ const ErrorHandler = union(enum) {...@@ -738,7 +755,7 @@ const ErrorHandler = union(enum) {
738};755};
739756
740fn cliDiagnosticsToErrorBundle(757fn cliDiagnosticsToErrorBundle(
741 gpa: std.mem.Allocator,758 gpa: Allocator,
742 diagnostics: *cli.Diagnostics,759 diagnostics: *cli.Diagnostics,
743) !ErrorBundle {760) !ErrorBundle {
744 @branchHint(.cold);761 @branchHint(.cold);
...@@ -783,7 +800,7 @@ fn cliDiagnosticsToErrorBundle(...@@ -783,7 +800,7 @@ fn cliDiagnosticsToErrorBundle(
783}800}
784801
785fn diagnosticsToErrorBundle(802fn diagnosticsToErrorBundle(
786 gpa: std.mem.Allocator,803 gpa: Allocator,
787 source: []const u8,804 source: []const u8,
788 diagnostics: *Diagnostics,805 diagnostics: *Diagnostics,
789 mappings: SourceMappings,806 mappings: SourceMappings,
...@@ -870,7 +887,7 @@ fn diagnosticsToErrorBundle(...@@ -870,7 +887,7 @@ fn diagnosticsToErrorBundle(
870 return try bundle.toOwnedBundle("");887 return try bundle.toOwnedBundle("");
871}888}
872889
873fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []const u8, args: anytype) !ErrorBundle {890fn errorStringToErrorBundle(allocator: Allocator, comptime format: []const u8, args: anytype) !ErrorBundle {
874 @branchHint(.cold);891 @branchHint(.cold);
875 var bundle: ErrorBundle.Wip = undefined;892 var bundle: ErrorBundle.Wip = undefined;
876 try bundle.init(allocator);893 try bundle.init(allocator);
lib/compiler/resinator/utils.zig+5-1
...@@ -26,7 +26,11 @@ pub const UncheckedSliceWriter = struct {...@@ -26,7 +26,11 @@ pub const UncheckedSliceWriter = struct {
26/// Cross-platform 'std.fs.Dir.openFile' wrapper that will always return IsDir if26/// Cross-platform 'std.fs.Dir.openFile' wrapper that will always return IsDir if
27/// a directory is attempted to be opened.27/// a directory is attempted to be opened.
28/// TODO: Remove once https://github.com/ziglang/zig/issues/5732 is addressed.28/// TODO: Remove once https://github.com/ziglang/zig/issues/5732 is addressed.
29pub fn openFileNotDir(cwd: std.fs.Dir, path: []const u8, flags: std.fs.File.OpenFlags) std.fs.File.OpenError!std.fs.File {29pub fn openFileNotDir(
30 cwd: std.fs.Dir,
31 path: []const u8,
32 flags: std.fs.File.OpenFlags,
33) (std.fs.File.OpenError || std.fs.File.StatError)!std.fs.File {
30 const file = try cwd.openFile(path, flags);34 const file = try cwd.openFile(path, flags);
31 errdefer file.close();35 errdefer file.close();
32 // https://github.com/ziglang/zig/issues/573236 // https://github.com/ziglang/zig/issues/5732