| ... | ... | @@ -1,5 +1,9 @@ |
| 1 | | const std = @import("std"); |
| 2 | 1 | const builtin = @import("builtin"); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Io = std.Io; |
| 5 | const Allocator = std.mem.Allocator; |
| 6 | |
| 3 | 7 | const removeComments = @import("comments.zig").removeComments; |
| 4 | 8 | const parseAndRemoveLineCommands = @import("source_mapping.zig").parseAndRemoveLineCommands; |
| 5 | 9 | const compile = @import("compile.zig").compile; |
| ... | ... | @@ -16,19 +20,18 @@ const aro = @import("aro"); |
| 16 | 20 | const compiler_util = @import("../util.zig"); |
| 17 | 21 | |
| 18 | 22 | pub fn main() !void { |
| 19 | | var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init; |
| 20 | | defer std.debug.assert(gpa.deinit() == .ok); |
| 21 | | const allocator = gpa.allocator(); |
| 23 | var debug_allocator: std.heap.DebugAllocator(.{}) = .init; |
| 24 | defer std.debug.assert(debug_allocator.deinit() == .ok); |
| 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 | 28 | defer arena_state.deinit(); |
| 25 | 29 | const arena = arena_state.allocator(); |
| 26 | 30 | |
| 27 | 31 | const stderr = std.fs.File.stderr(); |
| 28 | 32 | const stderr_config = std.Io.tty.detectConfig(stderr); |
| 29 | 33 | |
| 30 | | const args = try std.process.argsAlloc(allocator); |
| 31 | | defer std.process.argsFree(allocator, args); |
| 34 | const args = try std.process.argsAlloc(arena); |
| 32 | 35 | |
| 33 | 36 | if (args.len < 2) { |
| 34 | 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 | 62 | }; |
| 60 | 63 | |
| 61 | 64 | var options = options: { |
| 62 | | var cli_diagnostics = cli.Diagnostics.init(allocator); |
| 65 | var cli_diagnostics = cli.Diagnostics.init(gpa); |
| 63 | 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 | 68 | error.ParseError => { |
| 66 | | try error_handler.emitCliDiagnostics(allocator, cli_args, &cli_diagnostics); |
| 69 | try error_handler.emitCliDiagnostics(gpa, cli_args, &cli_diagnostics); |
| 67 | 70 | std.process.exit(1); |
| 68 | 71 | }, |
| 69 | 72 | else => |e| return e, |
| ... | ... | @@ -84,6 +87,10 @@ pub fn main() !void { |
| 84 | 87 | }; |
| 85 | 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 | 94 | if (options.print_help_and_exit) { |
| 88 | 95 | try cli.writeUsage(stdout, "zig rc"); |
| 89 | 96 | try stdout.flush(); |
| ... | ... | @@ -99,12 +106,13 @@ pub fn main() !void { |
| 99 | 106 | try stdout.flush(); |
| 100 | 107 | } |
| 101 | 108 | |
| 102 | | var dependencies = Dependencies.init(allocator); |
| 109 | var dependencies = Dependencies.init(gpa); |
| 103 | 110 | defer dependencies.deinit(); |
| 104 | 111 | const maybe_dependencies: ?*Dependencies = if (options.depfile_path != null) &dependencies else null; |
| 105 | 112 | |
| 106 | 113 | var include_paths = LazyIncludePaths{ |
| 107 | 114 | .arena = arena, |
| 115 | .io = io, |
| 108 | 116 | .auto_includes_option = options.auto_includes, |
| 109 | 117 | .zig_lib_dir = zig_lib_dir, |
| 110 | 118 | .target_machine_type = options.coff_options.target, |
| ... | ... | @@ -112,12 +120,12 @@ pub fn main() !void { |
| 112 | 120 | |
| 113 | 121 | const full_input = full_input: { |
| 114 | 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 | 124 | errdefer preprocessed_buf.deinit(); |
| 117 | 125 | |
| 118 | 126 | // We're going to throw away everything except the final preprocessed output anyway, |
| 119 | 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 | 129 | defer aro_arena_state.deinit(); |
| 122 | 130 | const aro_arena = aro_arena_state.allocator(); |
| 123 | 131 | |
| ... | ... | @@ -129,12 +137,12 @@ pub fn main() !void { |
| 129 | 137 | .color = stderr_config, |
| 130 | 138 | } } }, |
| 131 | 139 | true => .{ .output = .{ .to_list = .{ |
| 132 | | .arena = .init(allocator), |
| 140 | .arena = .init(gpa), |
| 133 | 141 | } } }, |
| 134 | 142 | }; |
| 135 | 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 | 146 | defer comp.deinit(); |
| 139 | 147 | |
| 140 | 148 | var argv: std.ArrayList([]const u8) = .empty; |
| ... | ... | @@ -159,20 +167,20 @@ pub fn main() !void { |
| 159 | 167 | |
| 160 | 168 | preprocess.preprocess(&comp, &preprocessed_buf.writer, argv.items, maybe_dependencies) catch |err| switch (err) { |
| 161 | 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 | 171 | std.process.exit(1); |
| 164 | 172 | }, |
| 165 | 173 | // ArgError can occur if e.g. the .rc file is not found |
| 166 | 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 | 176 | std.process.exit(1); |
| 169 | 177 | }, |
| 170 | 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 | 180 | std.process.exit(1); |
| 173 | 181 | }, |
| 174 | 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 | 184 | std.process.exit(1); |
| 177 | 185 | }, |
| 178 | 186 | error.OutOfMemory => |e| return e, |
| ... | ... | @@ -182,22 +190,22 @@ pub fn main() !void { |
| 182 | 190 | } else { |
| 183 | 191 | switch (options.input_source) { |
| 184 | 192 | .stdio => |file| { |
| 185 | | var file_reader = file.reader(&.{}); |
| 186 | | break :full_input file_reader.interface.allocRemaining(allocator, .unlimited) catch |err| { |
| 187 | | try error_handler.emitMessage(allocator, .err, "unable to read input from stdin: {s}", .{@errorName(err)}); |
| 193 | var file_reader = file.reader(io, &.{}); |
| 194 | break :full_input file_reader.interface.allocRemaining(gpa, .unlimited) catch |err| { |
| 195 | try error_handler.emitMessage(gpa, .err, "unable to read input from stdin: {s}", .{@errorName(err)}); |
| 188 | 196 | std.process.exit(1); |
| 189 | 197 | }; |
| 190 | 198 | }, |
| 191 | 199 | .filename => |input_filename| { |
| 192 | | break :full_input std.fs.cwd().readFileAlloc(input_filename, allocator, .unlimited) catch |err| { |
| 193 | | try error_handler.emitMessage(allocator, .err, "unable to read input file path '{s}': {s}", .{ input_filename, @errorName(err) }); |
| 200 | break :full_input std.fs.cwd().readFileAlloc(input_filename, gpa, .unlimited) catch |err| { |
| 201 | try error_handler.emitMessage(gpa, .err, "unable to read input file path '{s}': {s}", .{ input_filename, @errorName(err) }); |
| 194 | 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 | 210 | if (options.preprocess == .only) { |
| 203 | 211 | switch (options.output_source) { |
| ... | ... | @@ -221,55 +229,55 @@ pub fn main() !void { |
| 221 | 229 | } |
| 222 | 230 | else if (options.input_format == .res) |
| 223 | 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 | 233 | std.process.exit(1); |
| 226 | 234 | } |
| 227 | 235 | else |
| 228 | 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 | 238 | std.process.exit(1); |
| 231 | 239 | }; |
| 232 | | defer res_stream.deinit(allocator); |
| 240 | defer res_stream.deinit(gpa); |
| 233 | 241 | |
| 234 | 242 | const res_data = res_data: { |
| 235 | 243 | if (options.input_format != .res) { |
| 236 | 244 | // Note: We still want to run this when no-preprocess is set because: |
| 237 | 245 | // 1. We want to print accurate line numbers after removing multiline comments |
| 238 | 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 | 248 | error.InvalidLineCommand => { |
| 241 | 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 | 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 | 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 | 256 | std.process.exit(1); |
| 249 | 257 | }, |
| 250 | 258 | error.LineNumberOverflow => { |
| 251 | 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 | 261 | std.process.exit(1); |
| 254 | 262 | }, |
| 255 | 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 | 267 | const default_code_page = options.default_code_page orelse .windows1252; |
| 260 | 268 | const has_disjoint_code_page = hasDisjointCodePage(mapping_results.result, &mapping_results.mappings, default_code_page); |
| 261 | 269 | |
| 262 | 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 | 273 | defer diagnostics.deinit(); |
| 266 | 274 | |
| 267 | 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 | 277 | defer res_stream_writer.deinit(&res_stream.source); |
| 270 | 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 | 281 | .cwd = std.fs.cwd(), |
| 274 | 282 | .diagnostics = &diagnostics, |
| 275 | 283 | .source_mappings = &mapping_results.mappings, |
| ... | ... | @@ -287,7 +295,7 @@ pub fn main() !void { |
| 287 | 295 | .warn_instead_of_error_on_invalid_code_page = options.warn_instead_of_error_on_invalid_code_page, |
| 288 | 296 | }) catch |err| switch (err) { |
| 289 | 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 | 299 | // Delete the output file on error |
| 292 | 300 | res_stream.cleanupAfterError(); |
| 293 | 301 | std.process.exit(1); |
| ... | ... | @@ -305,7 +313,7 @@ pub fn main() !void { |
| 305 | 313 | // write the depfile |
| 306 | 314 | if (options.depfile_path) |depfile_path| { |
| 307 | 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 | 317 | std.process.exit(1); |
| 310 | 318 | }; |
| 311 | 319 | defer depfile.close(); |
| ... | ... | @@ -332,41 +340,41 @@ pub fn main() !void { |
| 332 | 340 | |
| 333 | 341 | if (options.output_format != .coff) return; |
| 334 | 342 | |
| 335 | | break :res_data res_stream.source.readAll(allocator) catch |err| { |
| 336 | | try error_handler.emitMessage(allocator, .err, "unable to read res from '{s}': {s}", .{ res_stream.name, @errorName(err) }); |
| 343 | break :res_data res_stream.source.readAll(gpa, io) catch |err| { |
| 344 | try error_handler.emitMessage(gpa, .err, "unable to read res from '{s}': {s}", .{ res_stream.name, @errorName(err) }); |
| 337 | 345 | std.process.exit(1); |
| 338 | 346 | }; |
| 339 | 347 | }; |
| 340 | 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 | 351 | std.debug.assert(options.output_format == .coff); |
| 344 | 352 | |
| 345 | 353 | // TODO: Maybe use a buffered file reader instead of reading file into memory -> fbs |
| 346 | 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 | 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 | 358 | std.process.exit(1); |
| 351 | 359 | }; |
| 352 | 360 | }; |
| 353 | 361 | defer resources.deinit(); |
| 354 | 362 | |
| 355 | 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 | 365 | std.process.exit(1); |
| 358 | 366 | }; |
| 359 | | defer coff_stream.deinit(allocator); |
| 367 | defer coff_stream.deinit(gpa); |
| 360 | 368 | |
| 361 | 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 | 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 | 374 | switch (err) { |
| 367 | 375 | error.DuplicateResource => { |
| 368 | 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 | 378 | duplicate_resource.name_value, |
| 371 | 379 | fmtResourceType(duplicate_resource.type_value), |
| 372 | 380 | duplicate_resource.language, |
| ... | ... | @@ -374,8 +382,8 @@ pub fn main() !void { |
| 374 | 382 | }, |
| 375 | 383 | error.ResourceDataTooLong => { |
| 376 | 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", .{}); |
| 378 | | try error_handler.emitMessage(allocator, .note, "the resource with the invalid size is [id: {f}, type: {f}, language: {f}]", .{ |
| 385 | try error_handler.emitMessage(gpa, .err, "resource has a data length that is too large to be written into a coff section", .{}); |
| 386 | try error_handler.emitMessage(gpa, .note, "the resource with the invalid size is [id: {f}, type: {f}, language: {f}]", .{ |
| 379 | 387 | overflow_resource.name_value, |
| 380 | 388 | fmtResourceType(overflow_resource.type_value), |
| 381 | 389 | overflow_resource.language, |
| ... | ... | @@ -383,15 +391,15 @@ pub fn main() !void { |
| 383 | 391 | }, |
| 384 | 392 | error.TotalResourceDataTooLong => { |
| 385 | 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", .{}); |
| 387 | | try error_handler.emitMessage(allocator, .note, "size overflow occurred when attempting to write this resource: [id: {f}, type: {f}, language: {f}]", .{ |
| 394 | try error_handler.emitMessage(gpa, .err, "total resource data exceeds the maximum of the coff 'size of raw data' field", .{}); |
| 395 | try error_handler.emitMessage(gpa, .note, "size overflow occurred when attempting to write this resource: [id: {f}, type: {f}, language: {f}]", .{ |
| 388 | 396 | overflow_resource.name_value, |
| 389 | 397 | fmtResourceType(overflow_resource.type_value), |
| 390 | 398 | overflow_resource.language, |
| 391 | 399 | }); |
| 392 | 400 | }, |
| 393 | 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 | 405 | // Delete the output file on error |
| ... | ... | @@ -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 | 435 | self.source.deinit(allocator); |
| 428 | 436 | } |
| 429 | 437 | |
| ... | ... | @@ -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 | 470 | switch (self.*) { |
| 463 | 471 | .file => |file| file.close(), |
| 464 | 472 | .stdio => {}, |
| ... | ... | @@ -471,18 +479,18 @@ const IoStream = struct { |
| 471 | 479 | bytes: []const u8, |
| 472 | 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 | 483 | if (self.needs_free) { |
| 476 | 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 | 490 | return switch (self) { |
| 483 | 491 | inline .file, .stdio => |file| .{ |
| 484 | 492 | .bytes = b: { |
| 485 | | var file_reader = file.reader(&.{}); |
| 493 | var file_reader = file.reader(io, &.{}); |
| 486 | 494 | break :b try file_reader.interface.allocRemaining(allocator, .unlimited); |
| 487 | 495 | }, |
| 488 | 496 | .needs_free = true, |
| ... | ... | @@ -496,7 +504,7 @@ const IoStream = struct { |
| 496 | 504 | file: std.fs.File.Writer, |
| 497 | 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 | 509 | pub fn interface(this: *@This()) *std.Io.Writer { |
| 502 | 510 | return switch (this.*) { |
| ... | ... | @@ -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 | 526 | return switch (source.*) { |
| 519 | 527 | .file, .stdio => |file| .{ .file = file.writer(buffer) }, |
| 520 | 528 | .memory => |*list| .{ .allocating = .fromArrayList(allocator, list) }, |
| ... | ... | @@ -525,17 +533,20 @@ const IoStream = struct { |
| 525 | 533 | }; |
| 526 | 534 | |
| 527 | 535 | const LazyIncludePaths = struct { |
| 528 | | arena: std.mem.Allocator, |
| 536 | arena: Allocator, |
| 537 | io: Io, |
| 529 | 538 | auto_includes_option: cli.Options.AutoIncludes, |
| 530 | 539 | zig_lib_dir: []const u8, |
| 531 | 540 | target_machine_type: std.coff.IMAGE.FILE.MACHINE, |
| 532 | 541 | resolved_include_paths: ?[]const []const u8 = null, |
| 533 | 542 | |
| 534 | 543 | pub fn get(self: *LazyIncludePaths, error_handler: *ErrorHandler) ![]const []const u8 { |
| 544 | const io = self.io; |
| 545 | |
| 535 | 546 | if (self.resolved_include_paths) |include_paths| |
| 536 | 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 | 550 | error.OutOfMemory => |e| return e, |
| 540 | 551 | else => |e| { |
| 541 | 552 | switch (e) { |
| ... | ... | @@ -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 | 577 | if (auto_includes_option == .none) return &[_][]const u8{}; |
| 561 | 578 | |
| 562 | 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 | 643 | .cpu_arch = includes_arch, |
| 627 | 644 | .abi = .gnu, |
| 628 | 645 | }; |
| 629 | | const target = std.zig.resolveTargetQueryOrFatal(target_query); |
| 646 | const target = std.zig.resolveTargetQueryOrFatal(io, target_query); |
| 630 | 647 | const is_native_abi = target_query.isNativeAbi(); |
| 631 | 648 | const detected_libc = std.zig.LibCDirs.detect(arena, zig_lib_dir, &target, is_native_abi, true, null) catch |err| switch (err) { |
| 632 | 649 | error.OutOfMemory => |e| return e, |
| ... | ... | @@ -647,7 +664,7 @@ const ErrorHandler = union(enum) { |
| 647 | 664 | |
| 648 | 665 | pub fn emitCliDiagnostics( |
| 649 | 666 | self: *ErrorHandler, |
| 650 | | allocator: std.mem.Allocator, |
| 667 | allocator: Allocator, |
| 651 | 668 | args: []const []const u8, |
| 652 | 669 | diagnostics: *cli.Diagnostics, |
| 653 | 670 | ) !void { |
| ... | ... | @@ -666,7 +683,7 @@ const ErrorHandler = union(enum) { |
| 666 | 683 | |
| 667 | 684 | pub fn emitAroDiagnostics( |
| 668 | 685 | self: *ErrorHandler, |
| 669 | | allocator: std.mem.Allocator, |
| 686 | allocator: Allocator, |
| 670 | 687 | fail_msg: []const u8, |
| 671 | 688 | comp: *aro.Compilation, |
| 672 | 689 | ) !void { |
| ... | ... | @@ -692,7 +709,7 @@ const ErrorHandler = union(enum) { |
| 692 | 709 | |
| 693 | 710 | pub fn emitDiagnostics( |
| 694 | 711 | self: *ErrorHandler, |
| 695 | | allocator: std.mem.Allocator, |
| 712 | allocator: Allocator, |
| 696 | 713 | cwd: std.fs.Dir, |
| 697 | 714 | source: []const u8, |
| 698 | 715 | diagnostics: *Diagnostics, |
| ... | ... | @@ -713,7 +730,7 @@ const ErrorHandler = union(enum) { |
| 713 | 730 | |
| 714 | 731 | pub fn emitMessage( |
| 715 | 732 | self: *ErrorHandler, |
| 716 | | allocator: std.mem.Allocator, |
| 733 | allocator: Allocator, |
| 717 | 734 | msg_type: @import("utils.zig").ErrorMessageType, |
| 718 | 735 | comptime format: []const u8, |
| 719 | 736 | args: anytype, |
| ... | ... | @@ -738,7 +755,7 @@ const ErrorHandler = union(enum) { |
| 738 | 755 | }; |
| 739 | 756 | |
| 740 | 757 | fn cliDiagnosticsToErrorBundle( |
| 741 | | gpa: std.mem.Allocator, |
| 758 | gpa: Allocator, |
| 742 | 759 | diagnostics: *cli.Diagnostics, |
| 743 | 760 | ) !ErrorBundle { |
| 744 | 761 | @branchHint(.cold); |
| ... | ... | @@ -783,7 +800,7 @@ fn cliDiagnosticsToErrorBundle( |
| 783 | 800 | } |
| 784 | 801 | |
| 785 | 802 | fn diagnosticsToErrorBundle( |
| 786 | | gpa: std.mem.Allocator, |
| 803 | gpa: Allocator, |
| 787 | 804 | source: []const u8, |
| 788 | 805 | diagnostics: *Diagnostics, |
| 789 | 806 | mappings: SourceMappings, |
| ... | ... | @@ -870,7 +887,7 @@ fn diagnosticsToErrorBundle( |
| 870 | 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 | 891 | @branchHint(.cold); |
| 875 | 892 | var bundle: ErrorBundle.Wip = undefined; |
| 876 | 893 | try bundle.init(allocator); |