authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-09-18 20:00:44+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-09-18 20:00:44+02:00
log2698cb346abe01a978edf98ba16ab6aad506b596
tree0fd918ce9734a5ad24f1a1f59e7beb0cfa40d308
parent54854e2ab87e667751f2eae86f41b9d41bcfda9d

autodoc: don't collect tests, usingnamespace and comptime blocks

Previously we were collecting as autodoc decls everything that was a ZIR decl in a rather naive way. Now we only collect decltests as part of the data relative to the decl they refer to, and ignore everything else.

2 files changed, 90 insertions(+), 69 deletions(-)

lib/docs/main.js+7-8
......@@ -2271,10 +2271,10 @@ var zigAnalysis;
22712271 let decl = getDecl(decls[i]);
22722272 let declValue = resolveValue(decl.value);
22732273
2274 if (decl.isTest) {
2275 testsList.push(decl);
2276 continue;
2277 }
2274 // if (decl.isTest) {
2275 // testsList.push(decl);
2276 // continue;
2277 // }
22782278
22792279 if (decl.kind === "var") {
22802280 varsList.push(decl);
......@@ -3522,10 +3522,9 @@ var zigAnalysis;
35223522 return {
35233523 name: decl[0],
35243524 kind: decl[1],
3525 isTest: decl[2],
3526 src: decl[3],
3527 value: decl[4],
3528 decltest: decl[5],
3525 src: decl[2],
3526 value: decl[3],
3527 decltest: decl[4],
35293528 };
35303529 }
35313530
src/Autodoc.zig+83-61
......@@ -471,7 +471,6 @@ const DocData = struct {
471471 const Decl = struct {
472472 name: []const u8,
473473 kind: []const u8,
474 isTest: bool,
475474 src: usize, // index into astNodes
476475 value: WalkResult,
477476 // The index in astNodes of the `test declname { }` node
......@@ -2522,14 +2521,22 @@ fn walkInstruction(
25222521 // even if we haven't fully analyzed the decl yet.
25232522 {
25242523 var it = file.zir.declIterator(@intCast(u32, inst_index));
2525 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
2526 for (self.decls.items[decls_first_index..]) |*slot| {
2527 slot._analyzed = false;
2528 }
2529 var decls_slot_index = decls_first_index;
2530 while (it.next()) |d| : (decls_slot_index += 1) {
2524 while (it.next()) |d| {
25312525 const decl_name_index = file.zir.extra[d.sub_index + 5];
2532 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
2526 switch (decl_name_index) {
2527 0, 1, 2 => continue,
2528 else => if (file.zir.string_bytes[decl_name_index] == 0) {
2529 continue;
2530 },
2531 }
2532
2533 const decl_slot_index = self.decls.items.len;
2534 try self.decls.append(self.arena, undefined);
2535 self.decls.items[decl_slot_index]._analyzed = false;
2536
2537 // TODO: inspect usingnamespace decls and unpack their contents!
2538
2539 try scope.insertDeclRef(self.arena, decl_name_index, decl_slot_index);
25332540 }
25342541 }
25352542
......@@ -2642,14 +2649,22 @@ fn walkInstruction(
26422649 // even if we haven't fully analyzed the decl yet.
26432650 {
26442651 var it = file.zir.declIterator(@intCast(u32, inst_index));
2645 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
2646 for (self.decls.items[decls_first_index..]) |*slot| {
2647 slot._analyzed = false;
2648 }
2649 var decls_slot_index = decls_first_index;
2650 while (it.next()) |d| : (decls_slot_index += 1) {
2652 while (it.next()) |d| {
26512653 const decl_name_index = file.zir.extra[d.sub_index + 5];
2652 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
2654 switch (decl_name_index) {
2655 0, 1, 2 => continue,
2656 else => if (file.zir.string_bytes[decl_name_index] == 0) {
2657 continue;
2658 },
2659 }
2660
2661 const decl_slot_index = self.decls.items.len;
2662 try self.decls.append(self.arena, undefined);
2663 self.decls.items[decl_slot_index]._analyzed = false;
2664
2665 // TODO: inspect usingnamespace decls and unpack their contents!
2666
2667 try scope.insertDeclRef(self.arena, decl_name_index, decl_slot_index);
26532668 }
26542669 }
26552670
......@@ -2773,14 +2788,22 @@ fn walkInstruction(
27732788 // even if we haven't fully analyzed the decl yet.
27742789 {
27752790 var it = file.zir.declIterator(@intCast(u32, inst_index));
2776 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
2777 for (self.decls.items[decls_first_index..]) |*slot| {
2778 slot._analyzed = false;
2779 }
2780 var decls_slot_index = decls_first_index;
2781 while (it.next()) |d| : (decls_slot_index += 1) {
2791 while (it.next()) |d| {
27822792 const decl_name_index = file.zir.extra[d.sub_index + 5];
2783 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
2793 switch (decl_name_index) {
2794 0, 1, 2 => continue,
2795 else => if (file.zir.string_bytes[decl_name_index] == 0) {
2796 continue;
2797 },
2798 }
2799
2800 const decl_slot_index = self.decls.items.len;
2801 try self.decls.append(self.arena, undefined);
2802 self.decls.items[decl_slot_index]._analyzed = false;
2803
2804 // TODO: inspect usingnamespace decls and unpack their contents!
2805
2806 try scope.insertDeclRef(self.arena, decl_name_index, decl_slot_index);
27842807 }
27852808 }
27862809
......@@ -2925,14 +2948,22 @@ fn walkInstruction(
29252948 // even if we haven't fully analyzed the decl yet.
29262949 {
29272950 var it = file.zir.declIterator(@intCast(u32, inst_index));
2928 try self.decls.resize(self.arena, decls_first_index + it.decls_len);
2929 for (self.decls.items[decls_first_index..]) |*slot| {
2930 slot._analyzed = false;
2931 }
2932 var decls_slot_index = decls_first_index;
2933 while (it.next()) |d| : (decls_slot_index += 1) {
2951 while (it.next()) |d| {
29342952 const decl_name_index = file.zir.extra[d.sub_index + 5];
2935 try scope.insertDeclRef(self.arena, decl_name_index, decls_slot_index);
2953 switch (decl_name_index) {
2954 0, 1, 2 => continue,
2955 else => if (file.zir.string_bytes[decl_name_index] == 0) {
2956 continue;
2957 },
2958 }
2959
2960 const decl_slot_index = self.decls.items.len;
2961 try self.decls.append(self.arena, undefined);
2962 self.decls.items[decl_slot_index]._analyzed = false;
2963
2964 // TODO: inspect usingnamespace decls and unpack their contents!
2965
2966 try scope.insertDeclRef(self.arena, decl_name_index, decl_slot_index);
29362967 }
29372968 }
29382969
......@@ -3034,7 +3065,7 @@ fn walkDecls(
30343065 scope: *Scope,
30353066 parent_src: SrcLocInfo,
30363067 decls_first_index: usize,
3037 decls_len: u32,
3068 decls_len: usize,
30383069 decl_indexes: *std.ArrayListUnmanaged(usize),
30393070 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
30403071 extra_start: usize,
......@@ -3046,8 +3077,12 @@ fn walkDecls(
30463077 var cur_bit_bag: u32 = undefined;
30473078 var decl_i: u32 = 0;
30483079
3080 // NOTE: we're not outputting every ZIR decl as a Autodoc decl.
3081 // tests, comptime blocks and usingnamespace are skipped.
3082 // this is why we `need good_decls_i`.
3083 var good_decls_i: usize = 0;
30493084 while (decl_i < decls_len) : (decl_i += 1) {
3050 const decls_slot_index = decls_first_index + decl_i;
3085 const decls_slot_index = decls_first_index + good_decls_i;
30513086
30523087 if (decl_i % 8 == 0) {
30533088 cur_bit_bag = file.zir.extra[bit_bag_index];
......@@ -3056,6 +3091,7 @@ fn walkDecls(
30563091 const is_pub = @truncate(u1, cur_bit_bag) != 0;
30573092 cur_bit_bag >>= 1;
30583093 const is_exported = @truncate(u1, cur_bit_bag) != 0;
3094 _ = is_exported;
30593095 cur_bit_bag >>= 1;
30603096 const has_align = @truncate(u1, cur_bit_bag) != 0;
30613097 cur_bit_bag >>= 1;
......@@ -3101,15 +3137,10 @@ fn walkDecls(
31013137 const value_pl_node = data[value_index].pl_node;
31023138 const decl_src = try self.srcLocInfo(file, value_pl_node.src_node, parent_src);
31033139
3104 var is_test = false; // we discover if it's a test by looking at its name
3105 const name: []const u8 = blk: {
3106 if (decl_name_index == 0) {
3107 break :blk if (is_exported) "usingnamespace" else "comptime";
3108 } else if (decl_name_index == 1) {
3109 is_test = true;
3110 break :blk "test";
3111 } else if (decl_name_index == 2) {
3112 // it is a decltest
3140 const name: []const u8 = switch (decl_name_index) {
3141 0, 1 => continue, // comptime or usingnamespace decl
3142 2 => {
3143 // decl test
31133144 const decl_being_tested = scope.resolveDeclName(doc_comment_index);
31143145 const func_index = getBlockInlineBreak(file.zir, value_index);
31153146
......@@ -3126,26 +3157,21 @@ fn walkDecls(
31263157 .code = test_source_code,
31273158 });
31283159 self.decls.items[decl_being_tested].decltest = ast_node_index;
3129 self.decls.items[decls_slot_index] = .{
3130 ._analyzed = true,
3131 .name = "test",
3132 .isTest = true,
3133 .src = ast_node_index,
3134 .value = .{ .expr = .{ .type = 0 } },
3135 .kind = "const",
3136 };
31373160 continue;
3138 } else {
3139 const raw_decl_name = file.zir.nullTerminatedString(decl_name_index);
3140 if (raw_decl_name.len == 0) {
3141 is_test = true;
3142 break :blk file.zir.nullTerminatedString(decl_name_index + 1);
3143 } else {
3144 break :blk raw_decl_name;
3161 },
3162 else => blk: {
3163 if (file.zir.string_bytes[decl_name_index] == 0) {
3164 // test decl
3165 continue;
31453166 }
3146 }
3167 break :blk file.zir.nullTerminatedString(decl_name_index);
3168 },
31473169 };
31483170
3171 // If we got here, it means that this decl is not a test, usingnamespace
3172 // or a comptime block decl.
3173 good_decls_i += 1;
3174
31493175 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)
31503176 file.zir.nullTerminatedString(doc_comment_index)
31513177 else
......@@ -3164,10 +3190,7 @@ fn walkDecls(
31643190 break :idx idx;
31653191 };
31663192
3167 const walk_result = if (is_test) // TODO: decide if tests should show up at all
3168 DocData.WalkResult{ .expr = .{ .void = .{} } }
3169 else
3170 try self.walkInstruction(file, scope, decl_src, value_index, true);
3193 const walk_result = try self.walkInstruction(file, scope, decl_src, value_index, true);
31713194
31723195 if (is_pub) {
31733196 try decl_indexes.append(self.arena, decls_slot_index);
......@@ -3193,7 +3216,6 @@ fn walkDecls(
31933216 self.decls.items[decls_slot_index] = .{
31943217 ._analyzed = true,
31953218 .name = name,
3196 .isTest = is_test,
31973219 .src = ast_node_index,
31983220 //.typeRef = decl_type_ref,
31993221 .value = walk_result,