| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const link = @import("link.zig"); | 2 | const link = @import("link.zig"); |
| 3 | const Module = @import("Module.zig"); | 3 | const Module = @import("Module.zig"); |
| | 4 | const ErrorMsg = Module.ErrorMsg; |
| 4 | const Allocator = std.mem.Allocator; | 5 | const Allocator = std.mem.Allocator; |
| 5 | const zir = @import("zir.zig"); | 6 | const zir = @import("zir.zig"); |
| 6 | const Package = @import("Package.zig"); | 7 | const Package = @import("Package.zig"); |
| ... | @@ -18,6 +19,7 @@ test "self-hosted" { | ... | @@ -18,6 +19,7 @@ test "self-hosted" { |
| 18 | pub const TestContext = struct { | 19 | pub const TestContext = struct { |
| 19 | zir_cmp_output_cases: std.ArrayList(ZIRCompareOutputCase), | 20 | zir_cmp_output_cases: std.ArrayList(ZIRCompareOutputCase), |
| 20 | zir_transform_cases: std.ArrayList(ZIRTransformCase), | 21 | zir_transform_cases: std.ArrayList(ZIRTransformCase), |
| | 22 | zir_error_cases: std.ArrayList(ZIRErrorCase), |
| 21 | | 23 | |
| 22 | pub const ZIRCompareOutputCase = struct { | 24 | pub const ZIRCompareOutputCase = struct { |
| 23 | name: []const u8, | 25 | name: []const u8, |
| ... | @@ -55,6 +57,15 @@ pub const TestContext = struct { | ... | @@ -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 | pub fn addZIRCompareOutput( | 69 | pub fn addZIRCompareOutput( |
| 59 | ctx: *TestContext, | 70 | ctx: *TestContext, |
| 60 | name: []const u8, | 71 | name: []const u8, |
| ... | @@ -87,30 +98,38 @@ pub const TestContext = struct { | ... | @@ -87,30 +98,38 @@ pub const TestContext = struct { |
| 87 | }) catch unreachable; | 98 | }) catch unreachable; |
| 88 | } | 99 | } |
| 89 | | 100 | |
| 90 | pub fn addZIRMulti( | 101 | pub fn addZIRError( |
| 91 | ctx: *TestContext, | 102 | ctx: *TestContext, |
| 92 | name: []const u8, | 103 | name: []const u8, |
| 93 | cross_target: std.zig.CrossTarget, | 104 | cross_target: std.zig.CrossTarget, |
| 94 | ) *ZIRTransformCase { | 105 | src: [:0]const u8, |
| 95 | const case = ctx.zir_transform_cases.addOne() catch unreachable; | 106 | expected_file_errors: []const ErrorMsg, |
| 96 | case.* = .{ | 107 | expected_decl_errors: []const ErrorMsg, |
| | 108 | expected_export_errors: []const ErrorMsg, |
| | 109 | ) void { |
| | 110 | ctx.zir_error_cases.append(.{ |
| 97 | .name = name, | 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 | .cross_target = cross_target, | 116 | .cross_target = cross_target, |
| 99 | .updates = std.ArrayList(ZIRTransformCase.Update).init(std.heap.page_allocator), | 117 | }) catch unreachable; |
| 100 | }; | | |
| 101 | return case; | | |
| 102 | } | 118 | } |
| 103 | | 119 | |
| 104 | fn init(self: *TestContext) !void { | 120 | fn init(self: *TestContext) !void { |
| | 121 | const allocator = std.heap.page_allocator; |
| 105 | self.* = .{ | 122 | self.* = .{ |
| 106 | .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(std.heap.page_allocator), | 123 | .zir_cmp_output_cases = std.ArrayList(ZIRCompareOutputCase).init(allocator), |
| 107 | .zir_transform_cases = std.ArrayList(ZIRTransformCase).init(std.heap.page_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 | fn deinit(self: *TestContext) void { | 129 | fn deinit(self: *TestContext) void { |
| 112 | self.zir_cmp_output_cases.deinit(); | 130 | self.zir_cmp_output_cases.deinit(); |
| 113 | self.zir_transform_cases.deinit(); | 131 | self.zir_transform_cases.deinit(); |
| | 132 | self.zir_error_cases.deinit(); |
| 114 | self.* = undefined; | 133 | self.* = undefined; |
| 115 | } | 134 | } |
| 116 | | 135 | |
| ... | @@ -133,6 +152,12 @@ pub const TestContext = struct { | ... | @@ -133,6 +152,12 @@ pub const TestContext = struct { |
| 133 | try self.runOneZIRTransformCase(std.testing.allocator, root_node, case, info.target); | 152 | try self.runOneZIRTransformCase(std.testing.allocator, root_node, case, info.target); |
| 134 | try std.testing.allocator_instance.validate(); | 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 | fn runOneZIRCmpOutputCase( | 163 | fn runOneZIRCmpOutputCase( |
| ... | @@ -300,6 +325,105 @@ pub const TestContext = struct { | ... | @@ -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 | fn debugPrintErrors(src: []const u8, errors: var) void { | 429 | fn debugPrintErrors(src: []const u8, errors: var) void { |