authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-22 22:40:05+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-22 22:40:05+02:00
loge6c012c7432a6de409bf08eb7964b3d99fe4362e
tree84e92264c5cdcfae042b873fa4eed073b8f889bb
parent51f2442fc4160415796cf6271d1951969ab71c83

link-tests: add better docs


1 files changed, 28 insertions(+), 0 deletions(-)

lib/std/build/CheckObjectStep.zig+28
......@@ -41,9 +41,22 @@ const Action = union(enum) {
4141 compute_eq: ComputeEqAction,
4242};
4343
44/// MatchAction is the main building block of standard matchers with optional eat-all token `{*}`
45/// and extractors by name such as `{n_value}`. Please note this action is very simplistic in nature
46/// i.e., it won't really handle edge cases/nontrivial examples. But given that we do want to use
47/// it mainly to test the output of our object format parser-dumpers when testing the linkers, etc.
48/// it should be plenty useful in its current form.
4449const MatchAction = struct {
4550 needle: []const u8,
4651
52 /// Will return true if the `needle` was found in the `haystack`.
53 /// Some examples include:
54 ///
55 /// LC 0 => will match in its entirety
56 /// vmaddr {vmaddr} => will match `vmaddr` and then extract the following value as u64
57 /// and save under `vmaddr` global name (see `global_vars` param)
58 /// name {*}libobjc{*}.dylib => will match `name` followed by a token which contains `libobjc` and `.dylib`
59 /// in that order with other letters in between
4760 fn match(act: MatchAction, haystack: []const u8, global_vars: anytype) !bool {
4861 var hay_it = mem.tokenize(u8, mem.trim(u8, haystack, " "), " ");
4962 var needle_it = mem.tokenize(u8, mem.trim(u8, act.needle, " "), " ");
......@@ -81,6 +94,12 @@ const MatchAction = struct {
8194 }
8295};
8396
97/// ComputeEqAction can be used to perform an operation on the extracted global variables
98/// using the MatchAction. It currently only supports an addition. The operation is required
99/// to be specified in Reverse Polish Notation to ease in operator-precedence parsing (well,
100/// to avoid any parsing really).
101/// For example, if the two extracted values were saved as `vmaddr` and `entryoff` respectively
102/// they could then be added with this simple program `vmaddr entryoff +`.
84103const ComputeEqAction = struct {
85104 expected: []const u8,
86105 var_stack: std.ArrayList([]const u8),
......@@ -115,23 +134,32 @@ const Check = struct {
115134 }
116135};
117136
137/// Creates a new sequence of actions with `phrase` as the first anchor searched phrase.
118138pub fn check(self: *CheckObjectStep, phrase: []const u8) void {
119139 var new_check = Check.create(self.builder);
120140 new_check.match(phrase);
121141 self.checks.append(new_check) catch unreachable;
122142}
123143
144/// Adds another searched phrase to the latest created Check with `CheckObjectStep.check(...)`.
145/// Asserts at least one check already exists.
124146pub fn checkNext(self: *CheckObjectStep, phrase: []const u8) void {
125147 assert(self.checks.items.len > 0);
126148 const last = &self.checks.items[self.checks.items.len - 1];
127149 last.match(phrase);
128150}
129151
152/// Creates a new check checking specifically symbol table parsed and dumped from the object
153/// file.
154/// Issuing this check will force parsing and dumping of the symbol table.
130155pub fn checkInSymtab(self: *CheckObjectStep) void {
131156 self.dump_symtab = true;
132157 self.check("symtab");
133158}
134159
160/// Creates a new standalone, singular check which allows running simple binary operations
161/// on the extracted variables. It will then compare the reduced program with the value of
162/// the expected variable.
135163pub fn checkComputeEq(self: *CheckObjectStep, program: []const u8, expected: []const u8) void {
136164 const gpa = self.builder.allocator;
137165 var ca = ComputeEqAction{