| ... | @@ -1,5 +1,9 @@ | ... | @@ -1,5 +1,9 @@ |
| 1 | const std = @import("std"); | | |
| 2 | const builtin = @import("builtin"); | 1 | const builtin = @import("builtin"); |
| | 2 | |
| | 3 | const std = @import("std"); |
| | 4 | const Io = std.Io; |
| | 5 | const Allocator = std.mem.Allocator; |
| | 6 | |
| 3 | const removeComments = @import("comments.zig").removeComments; | 7 | const removeComments = @import("comments.zig").removeComments; |
| 4 | const parseAndRemoveLineCommands = @import("source_mapping.zig").parseAndRemoveLineCommands; | 8 | const parseAndRemoveLineCommands = @import("source_mapping.zig").parseAndRemoveLineCommands; |
| 5 | const compile = @import("compile.zig").compile; | 9 | const compile = @import("compile.zig").compile; |
| ... | @@ -16,19 +20,18 @@ const aro = @import("aro"); | ... | @@ -16,19 +20,18 @@ const aro = @import("aro"); |
| 16 | const compiler_util = @import("../util.zig"); | 20 | const compiler_util = @import("../util.zig"); |
| 17 | | 21 | |
| 18 | pub fn main() !void { | 22 | pub 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(); |
| 22 | | 26 | |
| 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(); |
| 26 | | 30 | |
| 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); |
| 29 | | 33 | |
| 30 | const args = try std.process.argsAlloc(allocator); | 34 | const args = try std.process.argsAlloc(arena); |
| 31 | defer std.process.argsFree(allocator, args); | | |
| 32 | | 35 | |
| 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 | }; |
| 60 | | 63 | |
| 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(); |
| 86 | | 89 | |
| | 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 | } |
| 101 | | 108 | |
| 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; |
| 105 | | 112 | |
| 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 { |
| 112 | | 120 | |
| 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(); |
| 117 | | 125 | |
| 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(); |
| 123 | | 131 | |
| ... | @@ -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(); |
| 136 | | 144 | |
| 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(); |
| 139 | | 147 | |
| 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 { |
| 159 | | 167 | |
| 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 found | 173 | // 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); |
| 201 | | 209 | |
| 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 | else | 235 | 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); |
| 233 | | 241 | |
| 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 comments | 245 | // 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 it | 246 | // 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 command | 249 | // 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 message | 259 | // 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); |
| 258 | | 266 | |
| 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); |
| 261 | | 269 | |
| 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); |
| 263 | | 271 | |
| 264 | var diagnostics = Diagnostics.init(allocator); | 272 | var diagnostics = Diagnostics.init(gpa, io); |
| 265 | defer diagnostics.deinit(); | 273 | defer diagnostics.deinit(); |
| 266 | | 274 | |
| 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(); |
| 271 | | 279 | |
| 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 error | 299 | // 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 depfile | 313 | // 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 { |
| 332 | | 340 | |
| 333 | if (options.output_format != .coff) return; | 341 | if (options.output_format != .coff) return; |
| 334 | | 342 | |
| 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 it | 348 | // 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); |
| 342 | | 350 | |
| 343 | std.debug.assert(options.output_format == .coff); | 351 | std.debug.assert(options.output_format == .coff); |
| 344 | | 352 | |
| 345 | // TODO: Maybe use a buffered file reader instead of reading file into memory -> fbs | 353 | // 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 errors | 356 | // 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(); |
| 354 | | 362 | |
| 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); |
| 360 | | 368 | |
| 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); |
| 363 | | 371 | |
| 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 error | 405 | // Delete the output file on error |
| ... | @@ -423,7 +431,7 @@ const IoStream = struct { | ... | @@ -423,7 +431,7 @@ const IoStream = struct { |
| 423 | }; | 431 | }; |
| 424 | } | 432 | } |
| 425 | | 433 | |
| 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 | } |
| 429 | | 437 | |
| ... | @@ -458,7 +466,7 @@ const IoStream = struct { | ... | @@ -458,7 +466,7 @@ const IoStream = struct { |
| 458 | } | 466 | } |
| 459 | } | 467 | } |
| 460 | | 468 | |
| 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, |
| 473 | | 481 | |
| 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 | }; |
| 480 | | 488 | |
| 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, |
| 498 | | 506 | |
| 499 | pub const Error = std.mem.Allocator.Error || std.fs.File.WriteError; | 507 | pub const Error = Allocator.Error || std.fs.File.WriteError; |
| 500 | | 508 | |
| 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 | }; |
| 516 | | 524 | |
| 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 | }; |
| 526 | | 534 | |
| 527 | const LazyIncludePaths = struct { | 535 | const 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, |
| 533 | | 542 | |
| 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; |
| 537 | | 548 | |
| 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 | }; |
| 558 | | 569 | |
| 559 | fn 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 { | 570 | fn 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{}; |
| 561 | | 578 | |
| 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) { |
| 647 | | 664 | |
| 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) { |
| 666 | | 683 | |
| 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) { |
| 692 | | 709 | |
| 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) { |
| 713 | | 730 | |
| 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 | }; |
| 739 | | 756 | |
| 740 | fn cliDiagnosticsToErrorBundle( | 757 | fn 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 | } |
| 784 | | 801 | |
| 785 | fn diagnosticsToErrorBundle( | 802 | fn 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 | } |
| 872 | | 889 | |
| 873 | fn errorStringToErrorBundle(allocator: std.mem.Allocator, comptime format: []const u8, args: anytype) !ErrorBundle { | 890 | fn 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); |