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;...@@ -2271,10 +2271,10 @@ var zigAnalysis;
2271 let decl = getDecl(decls[i]);2271 let decl = getDecl(decls[i]);
2272 let declValue = resolveValue(decl.value);2272 let declValue = resolveValue(decl.value);
22732273
2274 if (decl.isTest) {2274 // if (decl.isTest) {
2275 testsList.push(decl);2275 // testsList.push(decl);
2276 continue;2276 // continue;
2277 }2277 // }
22782278
2279 if (decl.kind === "var") {2279 if (decl.kind === "var") {
2280 varsList.push(decl);2280 varsList.push(decl);
...@@ -3522,10 +3522,9 @@ var zigAnalysis;...@@ -3522,10 +3522,9 @@ var zigAnalysis;
3522 return {3522 return {
3523 name: decl[0],3523 name: decl[0],
3524 kind: decl[1],3524 kind: decl[1],
3525 isTest: decl[2],3525 src: decl[2],
3526 src: decl[3],3526 value: decl[3],
3527 value: decl[4],3527 decltest: decl[4],
3528 decltest: decl[5],
3529 };3528 };
3530 }3529 }
3531 3530
src/Autodoc.zig+83-61
...@@ -471,7 +471,6 @@ const DocData = struct {...@@ -471,7 +471,6 @@ const DocData = struct {
471 const Decl = struct {471 const Decl = struct {
472 name: []const u8,472 name: []const u8,
473 kind: []const u8,473 kind: []const u8,
474 isTest: bool,
475 src: usize, // index into astNodes474 src: usize, // index into astNodes
476 value: WalkResult,475 value: WalkResult,
477 // The index in astNodes of the `test declname { }` node476 // The index in astNodes of the `test declname { }` node
...@@ -2522,14 +2521,22 @@ fn walkInstruction(...@@ -2522,14 +2521,22 @@ fn walkInstruction(
2522 // even if we haven't fully analyzed the decl yet.2521 // even if we haven't fully analyzed the decl yet.
2523 {2522 {
2524 var it = file.zir.declIterator(@intCast(u32, inst_index));2523 var it = file.zir.declIterator(@intCast(u32, inst_index));
2525 try self.decls.resize(self.arena, decls_first_index + it.decls_len);2524 while (it.next()) |d| {
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) {
2531 const decl_name_index = file.zir.extra[d.sub_index + 5];2525 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);
2533 }2540 }
2534 }2541 }
25352542
...@@ -2642,14 +2649,22 @@ fn walkInstruction(...@@ -2642,14 +2649,22 @@ fn walkInstruction(
2642 // even if we haven't fully analyzed the decl yet.2649 // even if we haven't fully analyzed the decl yet.
2643 {2650 {
2644 var it = file.zir.declIterator(@intCast(u32, inst_index));2651 var it = file.zir.declIterator(@intCast(u32, inst_index));
2645 try self.decls.resize(self.arena, decls_first_index + it.decls_len);2652 while (it.next()) |d| {
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) {
2651 const decl_name_index = file.zir.extra[d.sub_index + 5];2653 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);
2653 }2668 }
2654 }2669 }
26552670
...@@ -2773,14 +2788,22 @@ fn walkInstruction(...@@ -2773,14 +2788,22 @@ fn walkInstruction(
2773 // even if we haven't fully analyzed the decl yet.2788 // even if we haven't fully analyzed the decl yet.
2774 {2789 {
2775 var it = file.zir.declIterator(@intCast(u32, inst_index));2790 var it = file.zir.declIterator(@intCast(u32, inst_index));
2776 try self.decls.resize(self.arena, decls_first_index + it.decls_len);2791 while (it.next()) |d| {
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) {
2782 const decl_name_index = file.zir.extra[d.sub_index + 5];2792 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);
2784 }2807 }
2785 }2808 }
27862809
...@@ -2925,14 +2948,22 @@ fn walkInstruction(...@@ -2925,14 +2948,22 @@ fn walkInstruction(
2925 // even if we haven't fully analyzed the decl yet.2948 // even if we haven't fully analyzed the decl yet.
2926 {2949 {
2927 var it = file.zir.declIterator(@intCast(u32, inst_index));2950 var it = file.zir.declIterator(@intCast(u32, inst_index));
2928 try self.decls.resize(self.arena, decls_first_index + it.decls_len);2951 while (it.next()) |d| {
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) {
2934 const decl_name_index = file.zir.extra[d.sub_index + 5];2952 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);
2936 }2967 }
2937 }2968 }
29382969
...@@ -3034,7 +3065,7 @@ fn walkDecls(...@@ -3034,7 +3065,7 @@ fn walkDecls(
3034 scope: *Scope,3065 scope: *Scope,
3035 parent_src: SrcLocInfo,3066 parent_src: SrcLocInfo,
3036 decls_first_index: usize,3067 decls_first_index: usize,
3037 decls_len: u32,3068 decls_len: usize,
3038 decl_indexes: *std.ArrayListUnmanaged(usize),3069 decl_indexes: *std.ArrayListUnmanaged(usize),
3039 priv_decl_indexes: *std.ArrayListUnmanaged(usize),3070 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
3040 extra_start: usize,3071 extra_start: usize,
...@@ -3046,8 +3077,12 @@ fn walkDecls(...@@ -3046,8 +3077,12 @@ fn walkDecls(
3046 var cur_bit_bag: u32 = undefined;3077 var cur_bit_bag: u32 = undefined;
3047 var decl_i: u32 = 0;3078 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;
3049 while (decl_i < decls_len) : (decl_i += 1) {3084 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
3052 if (decl_i % 8 == 0) {3087 if (decl_i % 8 == 0) {
3053 cur_bit_bag = file.zir.extra[bit_bag_index];3088 cur_bit_bag = file.zir.extra[bit_bag_index];
...@@ -3056,6 +3091,7 @@ fn walkDecls(...@@ -3056,6 +3091,7 @@ fn walkDecls(
3056 const is_pub = @truncate(u1, cur_bit_bag) != 0;3091 const is_pub = @truncate(u1, cur_bit_bag) != 0;
3057 cur_bit_bag >>= 1;3092 cur_bit_bag >>= 1;
3058 const is_exported = @truncate(u1, cur_bit_bag) != 0;3093 const is_exported = @truncate(u1, cur_bit_bag) != 0;
3094 _ = is_exported;
3059 cur_bit_bag >>= 1;3095 cur_bit_bag >>= 1;
3060 const has_align = @truncate(u1, cur_bit_bag) != 0;3096 const has_align = @truncate(u1, cur_bit_bag) != 0;
3061 cur_bit_bag >>= 1;3097 cur_bit_bag >>= 1;
...@@ -3101,15 +3137,10 @@ fn walkDecls(...@@ -3101,15 +3137,10 @@ fn walkDecls(
3101 const value_pl_node = data[value_index].pl_node;3137 const value_pl_node = data[value_index].pl_node;
3102 const decl_src = try self.srcLocInfo(file, value_pl_node.src_node, parent_src);3138 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 name3140 const name: []const u8 = switch (decl_name_index) {
3105 const name: []const u8 = blk: {3141 0, 1 => continue, // comptime or usingnamespace decl
3106 if (decl_name_index == 0) {3142 2 => {
3107 break :blk if (is_exported) "usingnamespace" else "comptime";3143 // decl test
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
3113 const decl_being_tested = scope.resolveDeclName(doc_comment_index);3144 const decl_being_tested = scope.resolveDeclName(doc_comment_index);
3114 const func_index = getBlockInlineBreak(file.zir, value_index);3145 const func_index = getBlockInlineBreak(file.zir, value_index);
31153146
...@@ -3126,26 +3157,21 @@ fn walkDecls(...@@ -3126,26 +3157,21 @@ fn walkDecls(
3126 .code = test_source_code,3157 .code = test_source_code,
3127 });3158 });
3128 self.decls.items[decl_being_tested].decltest = ast_node_index;3159 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 };
3137 continue;3160 continue;
3138 } else {3161 },
3139 const raw_decl_name = file.zir.nullTerminatedString(decl_name_index);3162 else => blk: {
3140 if (raw_decl_name.len == 0) {3163 if (file.zir.string_bytes[decl_name_index] == 0) {
3141 is_test = true;3164 // test decl
3142 break :blk file.zir.nullTerminatedString(decl_name_index + 1);3165 continue;
3143 } else {
3144 break :blk raw_decl_name;
3145 }3166 }
3146 }3167 break :blk file.zir.nullTerminatedString(decl_name_index);
3168 },
3147 };3169 };
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
3149 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)3175 const doc_comment: ?[]const u8 = if (doc_comment_index != 0)
3150 file.zir.nullTerminatedString(doc_comment_index)3176 file.zir.nullTerminatedString(doc_comment_index)
3151 else3177 else
...@@ -3164,10 +3190,7 @@ fn walkDecls(...@@ -3164,10 +3190,7 @@ fn walkDecls(
3164 break :idx idx;3190 break :idx idx;
3165 };3191 };
31663192
3167 const walk_result = if (is_test) // TODO: decide if tests should show up at all3193 const walk_result = try self.walkInstruction(file, scope, decl_src, value_index, true);
3168 DocData.WalkResult{ .expr = .{ .void = .{} } }
3169 else
3170 try self.walkInstruction(file, scope, decl_src, value_index, true);
31713194
3172 if (is_pub) {3195 if (is_pub) {
3173 try decl_indexes.append(self.arena, decls_slot_index);3196 try decl_indexes.append(self.arena, decls_slot_index);
...@@ -3193,7 +3216,6 @@ fn walkDecls(...@@ -3193,7 +3216,6 @@ fn walkDecls(
3193 self.decls.items[decls_slot_index] = .{3216 self.decls.items[decls_slot_index] = .{
3194 ._analyzed = true,3217 ._analyzed = true,
3195 .name = name,3218 .name = name,
3196 .isTest = is_test,
3197 .src = ast_node_index,3219 .src = ast_node_index,
3198 //.typeRef = decl_type_ref,3220 //.typeRef = decl_type_ref,
3199 .value = walk_result,3221 .value = walk_result,