From 03ddb42b8bb96815c1bb4b857ffdfb94191ab861 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 23 Jun 2022 12:56:28 +0200 Subject: [PATCH] link-tests: rename check() to checkStart() Do not hardcode the symtab label; instead allow each parser to define its own. Check for missing extractor value in the matcher when matching `{}`. --- lib/std/build/CheckObjectStep.zig | 15 +++++++++++---- test/link/macho/dylib/build.zig | 6 +++--- test/link/macho/entry/build.zig | 4 ++-- test/link/macho/frameworks/build.zig | 4 ++-- test/link/macho/pagezero/build.zig | 6 +++--- test/link/macho/stack_size/build.zig | 2 +- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/std/build/CheckObjectStep.zig b/lib/std/build/CheckObjectStep.zig index 2aaec8c4a7..65a57f8832 100644 --- a/lib/std/build/CheckObjectStep.zig +++ b/lib/std/build/CheckObjectStep.zig @@ -83,6 +83,7 @@ const MatchAction = struct { if (closing_brace != needle_tok.len - 1) return error.ClosingBraceNotLast; const name = needle_tok[1..closing_brace]; + if (name.len == 0) return error.MissingBraceValue; const value = try std.fmt.parseInt(u64, hay_tok, 16); try global_vars.putNoClobber(name, value); } else { @@ -135,13 +136,13 @@ const Check = struct { }; /// Creates a new sequence of actions with `phrase` as the first anchor searched phrase. -pub fn check(self: *CheckObjectStep, phrase: []const u8) void { +pub fn checkStart(self: *CheckObjectStep, phrase: []const u8) void { var new_check = Check.create(self.builder); new_check.match(phrase); self.checks.append(new_check) catch unreachable; } -/// Adds another searched phrase to the latest created Check with `CheckObjectStep.check(...)`. +/// Adds another searched phrase to the latest created Check with `CheckObjectStep.checkStart(...)`. /// Asserts at least one check already exists. pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { assert(self.checks.items.len > 0); @@ -154,7 +155,11 @@ pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void { /// Issuing this check will force parsing and dumping of the symbol table. pub fn checkInSymtab(self: *CheckObjectStep) void { self.dump_symtab = true; - self.check("symtab"); + const symtab_label = switch (self.obj_format) { + .macho => MachODumper.symtab_label, + else => @panic("TODO other parsers"), + }; + self.checkStart(symtab_label); } /// Creates a new standalone, singular check which allows running simple binary operations @@ -274,6 +279,8 @@ const Opts = struct { }; const MachODumper = struct { + const symtab_label = "symtab"; + fn parseAndDump(bytes: []const u8, opts: Opts) ![]const u8 { const gpa = opts.gpa orelse unreachable; // MachO dumper requires an allocator var stream = std.io.fixedBufferStream(bytes); @@ -301,7 +308,7 @@ const MachODumper = struct { } if (symtab_cmd) |cmd| { - try writer.writeAll("symtab\n"); + try writer.writeAll(symtab_label ++ "\n"); const strtab = bytes[cmd.stroff..][0..cmd.strsize]; const raw_symtab = bytes[cmd.symoff..][0 .. cmd.nsyms * @sizeOf(macho.nlist_64)]; const symtab = mem.bytesAsSlice(macho.nlist_64, raw_symtab); diff --git a/test/link/macho/dylib/build.zig b/test/link/macho/dylib/build.zig index ad116e23e9..1587def9b8 100644 --- a/test/link/macho/dylib/build.zig +++ b/test/link/macho/dylib/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *Builder) void { dylib.install(); const check_dylib = dylib.checkObject(.macho); - check_dylib.check("cmd ID_DYLIB"); + check_dylib.checkStart("cmd ID_DYLIB"); check_dylib.checkNext("name @rpath/liba.dylib"); check_dylib.checkNext("timestamp 2"); check_dylib.checkNext("current version 10000"); @@ -31,13 +31,13 @@ pub fn build(b: *Builder) void { exe.addRPath(b.pathFromRoot("zig-out/lib")); const check_exe = exe.checkObject(.macho); - check_exe.check("cmd LOAD_DYLIB"); + check_exe.checkStart("cmd LOAD_DYLIB"); check_exe.checkNext("name @rpath/liba.dylib"); check_exe.checkNext("timestamp 2"); check_exe.checkNext("current version 10000"); check_exe.checkNext("compatibility version 10000"); - check_exe.check("cmd RPATH"); + check_exe.checkStart("cmd RPATH"); check_exe.checkNext(std.fmt.allocPrint(b.allocator, "path {s}", .{b.pathFromRoot("zig-out/lib")}) catch unreachable); test_step.dependOn(&check_exe.step); diff --git a/test/link/macho/entry/build.zig b/test/link/macho/entry/build.zig index 82d3917521..f0ac084e6a 100644 --- a/test/link/macho/entry/build.zig +++ b/test/link/macho/entry/build.zig @@ -15,10 +15,10 @@ pub fn build(b: *Builder) void { const check_exe = exe.checkObject(.macho); - check_exe.check("segname __TEXT"); + check_exe.checkStart("segname __TEXT"); check_exe.checkNext("vmaddr {vmaddr}"); - check_exe.check("cmd MAIN"); + check_exe.checkStart("cmd MAIN"); check_exe.checkNext("entryoff {entryoff}"); check_exe.checkInSymtab(); diff --git a/test/link/macho/frameworks/build.zig b/test/link/macho/frameworks/build.zig index 2e1c8f3211..7086606f30 100644 --- a/test/link/macho/frameworks/build.zig +++ b/test/link/macho/frameworks/build.zig @@ -14,12 +14,12 @@ pub fn build(b: *Builder) void { exe.linkFramework("Cocoa"); const check = exe.checkObject(.macho); - check.check("cmd LOAD_DYLIB"); + check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name {*}Cocoa"); switch (mode) { .Debug, .ReleaseSafe => { - check.check("cmd LOAD_DYLIB"); + check.checkStart("cmd LOAD_DYLIB"); check.checkNext("name {*}libobjc{*}.dylib"); }, else => {}, diff --git a/test/link/macho/pagezero/build.zig b/test/link/macho/pagezero/build.zig index a35be919c5..e858d1f4d8 100644 --- a/test/link/macho/pagezero/build.zig +++ b/test/link/macho/pagezero/build.zig @@ -15,12 +15,12 @@ pub fn build(b: *Builder) void { exe.pagezero_size = 0x4000; const check = exe.checkObject(.macho); - check.check("LC 0"); + check.checkStart("LC 0"); check.checkNext("segname __PAGEZERO"); check.checkNext("vmaddr 0"); check.checkNext("vmsize 4000"); - check.check("segname __TEXT"); + check.checkStart("segname __TEXT"); check.checkNext("vmaddr 4000"); test_step.dependOn(&check.step); @@ -34,7 +34,7 @@ pub fn build(b: *Builder) void { exe.pagezero_size = 0; const check = exe.checkObject(.macho); - check.check("LC 0"); + check.checkStart("LC 0"); check.checkNext("segname __TEXT"); check.checkNext("vmaddr 0"); diff --git a/test/link/macho/stack_size/build.zig b/test/link/macho/stack_size/build.zig index b840f8928c..8da59dcb53 100644 --- a/test/link/macho/stack_size/build.zig +++ b/test/link/macho/stack_size/build.zig @@ -14,7 +14,7 @@ pub fn build(b: *Builder) void { exe.stack_size = 0x100000000; const check_exe = exe.checkObject(.macho); - check_exe.check("cmd MAIN"); + check_exe.checkStart("cmd MAIN"); check_exe.checkNext("stacksize 100000000"); test_step.dependOn(&check_exe.step);