| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const link = @import("link.zig"); |
| 3 | 3 | const Module = @import("Module.zig"); |
| 4 | const ErrorMsg = Module.ErrorMsg; |
| 4 | 5 | const Allocator = std.mem.Allocator; |
| 5 | 6 | const zir = @import("zir.zig"); |
| 6 | 7 | const Package = @import("Package.zig"); |
| ... | ... | @@ -18,6 +19,7 @@ test "self-hosted" { |
| 18 | 19 | pub const TestContext = struct { |
| 19 | 20 | zir_cmp_output_cases: std.ArrayList(ZIRCompareOutputCase), |
| 20 | 21 | zir_transform_cases: std.ArrayList(ZIRTransformCase), |
| 22 | zir_error_cases: std.ArrayList(ZIRErrorCase), |
| 21 | 23 | |
| 22 | 24 | pub const ZIRCompareOutputCase = struct { |
| 23 | 25 | name: []const u8, |
| ... | ... | @@ -55,6 +57,15 @@ pub const TestContext = struct { |
| 55 | 57 | } |
| 56 | 58 | }; |
| 57 | 59 | |
| 60 | pub const ZIRErrorCase = struct { |
| 61 | name: []const u8, |
| 62 | src: [:0]const u8, |
| 63 | expected_file_errors: []const ErrorMsg, |
| 64 | expected_decl_errors: []const ErrorMsg, |
| 65 | expected_export_errors: []const ErrorMsg, |
| 66 | cross_target: std.zig.CrossTarget, |
| 67 | }; |
| 68 | |
| 58 | 69 | pub fn addZIRCompareOutput( |
| 59 | 70 | ctx: *TestContext, |
| 60 | 71 | name: []const u8, |
| ... | ... | @@ -87,30 +98,38 @@ pub const TestContext = struct { |
| 87 | 98 | }) catch unreachable; |
| 88 | 99 | } |
| 89 | 100 | |
| 90 | | pub fn addZIRMulti( |
| 101 | pub fn addZIRError( |
| 91 | 102 | ctx: *TestContext, |
| 92 | 103 | name: []const u8, |
| 93 | 104 | cross_target: std.zig.CrossTarget, |
| 94 | | ) *ZIRTransformCase { |
| 95 | | const case = ctx.zir_transform_cases.addOne() catch unreachable; |
| 96 | | case.* = .{ |
| 105 | src: [:0]const u8, |
| 106 | expected_file_errors: []const ErrorMsg, |
| 107 | expected_decl_errors: []const ErrorMsg, |
| 108 | expected_export_errors: []const ErrorMsg, |
| 109 | ) void { |
| 110 | ctx.zir_error_cases.append(.{ |
| 97 | 111 | .name = name, |
| 112 | .src = src, |
| 113 | .expected_file_errors = expected_file_errors, |
| 114 | .expected_decl_errors = expected_decl_errors, |
| 115 | .expected_export_errors = expected_export_errors, |
| 98 | 116 | .cross_target = cross_target, |
| 99 | | .updates = std.ArrayList(ZIRTransformCase.Update).init(std.heap.page_allocator), |
| 100 | | }; |
| 101 | | return case; |
| 117 | }) catch unreachable; |
| 102 | 118 | } |
| 103 | 119 | |
| 104 | 120 | fn init(self: *TestContext) !void { |
| 121 | const allocator = std.heap.page_allocator; |
| 105 | 122 | self.* = .{ |
| 106 | | .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(std.heap.page_allocator), |
| 107 | | .zir_transform_cases = std.ArrayList(ZIRTransformCase).init(std.heap.page_allocator), |
| 123 | .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(allocator), |
| 124 | .zir_transform_cases = std.ArrayList(ZIRTransformCase).init(allocator), |
| 125 | .zir_error_cases = std.ArrayList(ZIRErrorCase).init(allocator), |
| 108 | 126 | }; |
| 109 | 127 | } |
| 110 | 128 | |
| 111 | 129 | fn deinit(self: *TestContext) void { |
| 112 | 130 | self.zir_cmp_output_cases.deinit(); |
| 113 | 131 | self.zir_transform_cases.deinit(); |
| 132 | self.zir_error_cases.deinit(); |
| 114 | 133 | self.* = undefined; |
| 115 | 134 | } |
| 116 | 135 | |
| ... | ... | @@ -133,6 +152,12 @@ pub const TestContext = struct { |
| 133 | 152 | try self.runOneZIRTransformCase(std.testing.allocator, root_node, case, info.target); |
| 134 | 153 | try std.testing.allocator_instance.validate(); |
| 135 | 154 | } |
| 155 | for (self.zir_error_cases.items) |case| { |
| 156 | std.testing.base_allocator_instance.reset(); |
| 157 | const info = try std.zig.system.NativeTargetInfo.detect(std.testing.allocator, case.cross_target); |
| 158 | try self.runOneZIRErrorCase(std.testing.allocator, root_node, case, info.target); |
| 159 | try std.testing.allocator_instance.validate(); |
| 160 | } |
| 136 | 161 | } |
| 137 | 162 | |
| 138 | 163 | fn runOneZIRCmpOutputCase( |
| ... | ... | @@ -300,6 +325,105 @@ pub const TestContext = struct { |
| 300 | 325 | } |
| 301 | 326 | } |
| 302 | 327 | } |
| 328 | |
| 329 | fn runOneZIRErrorCase( |
| 330 | self: *TestContext, |
| 331 | allocator: *Allocator, |
| 332 | root_node: *std.Progress.Node, |
| 333 | case: ZIRErrorCase, |
| 334 | target: std.Target, |
| 335 | ) !void { |
| 336 | var tmp = std.testing.tmpDir(.{}); |
| 337 | defer tmp.cleanup(); |
| 338 | |
| 339 | var prg_node = root_node.start(case.name, 1); |
| 340 | prg_node.activate(); |
| 341 | defer prg_node.end(); |
| 342 | |
| 343 | const tmp_src_path = "test-case.zir"; |
| 344 | try tmp.dir.writeFile(tmp_src_path, case.src); |
| 345 | |
| 346 | const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path); |
| 347 | defer root_pkg.destroy(); |
| 348 | |
| 349 | var module = try Module.init(allocator, .{ |
| 350 | .target = target, |
| 351 | .output_mode = .Obj, |
| 352 | .optimize_mode = .Debug, |
| 353 | .bin_file_dir = tmp.dir, |
| 354 | .bin_file_path = "test-case.o", |
| 355 | .root_pkg = root_pkg, |
| 356 | }); |
| 357 | defer module.deinit(); |
| 358 | |
| 359 | var module_node = prg_node.start("parse/analysis/codegen", null); |
| 360 | module_node.activate(); |
| 361 | const failed = f: { |
| 362 | module.update() catch break :f true; |
| 363 | break :f false; |
| 364 | }; |
| 365 | if (!failed) { |
| 366 | return error.DidNotFail; |
| 367 | } |
| 368 | module_node.end(); |
| 369 | { |
| 370 | var i = module.failed_files.iterator(); |
| 371 | var index: usize = 0; |
| 372 | while (i.next()) |pair| : (index += 1) { |
| 373 | if (index == case.expected_file_errors.len) { |
| 374 | return error.UnexpectedError; |
| 375 | } |
| 376 | const v1 = pair.value.*; |
| 377 | const v2 = case.expected_file_errors[index]; |
| 378 | if (v1.byte_offset != v2.byte_offset) { |
| 379 | std.debug.warn("Expected error at {}, found it at {}\n", .{ v2.byte_offset, v1.byte_offset }); |
| 380 | return error.ExpectedErrorElsewhere; |
| 381 | } |
| 382 | if (!std.mem.eql(u8, v1.msg, v2.msg)) { |
| 383 | std.debug.warn("Expected '{}', found '{}'\n", .{ v2.msg, v1.msg }); |
| 384 | return error.ExpectedOtherError; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | { |
| 389 | var i = module.failed_decls.iterator(); |
| 390 | var index: usize = 0; |
| 391 | while (i.next()) |pair| : (index += 1) { |
| 392 | if (index == case.expected_decl_errors.len) { |
| 393 | return error.UnexpectedError; |
| 394 | } |
| 395 | const v1 = pair.value.*; |
| 396 | const v2 = case.expected_decl_errors[index]; |
| 397 | if (v1.byte_offset != v2.byte_offset) { |
| 398 | std.debug.warn("Expected error at {}, found it at {}\n", .{ v2.byte_offset, v1.byte_offset }); |
| 399 | return error.ExpectedErrorElsewhere; |
| 400 | } |
| 401 | if (!std.mem.eql(u8, v1.msg, v2.msg)) { |
| 402 | std.debug.warn("Expected '{}', found '{}'\n", .{ v2.msg, v1.msg }); |
| 403 | return error.ExpectedOtherError; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | { |
| 408 | var i = module.failed_exports.iterator(); |
| 409 | var index: usize = 0; |
| 410 | while (i.next()) |pair| : (index += 1) { |
| 411 | if (index == case.expected_export_errors.len) { |
| 412 | return error.UnexpectedError; |
| 413 | } |
| 414 | const v1 = pair.value.*; |
| 415 | const v2 = case.expected_export_errors[index]; |
| 416 | if (v1.byte_offset != v2.byte_offset) { |
| 417 | std.debug.warn("Expected error at {}, found it at {}\n", .{ v2.byte_offset, v1.byte_offset }); |
| 418 | return error.ExpectedErrorElsewhere; |
| 419 | } |
| 420 | if (!std.mem.eql(u8, v1.msg, v2.msg)) { |
| 421 | std.debug.warn("Expected '{}', found '{}'\n", .{ v2.msg, v1.msg }); |
| 422 | return error.ExpectedOtherError; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 303 | 427 | }; |
| 304 | 428 | |
| 305 | 429 | fn debugPrintErrors(src: []const u8, errors: var) void { |