authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-07-21 16:16:45+02:00
committergravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-07-21 16:31:45+02:00
log92966088c2679ef670dc9776408deee98d16486f
treed191b8dfb4b689ae4078a7b5340a5ffadc420c9f
parentd74a49456d91cc5c15789e20301603fccbc12f11

autodoc: cleanup file-related operations


1 files changed, 41 insertions(+), 38 deletions(-)

src/Autodoc.zig+41-38
......@@ -67,17 +67,15 @@ pub fn generateZirData(self: *Autodoc) !void {
6767 }
6868 }
6969
70 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
71 const dir =
72 if (self.module.main_pkg.root_src_directory.path) |rp|
73 std.os.realpath(rp, &buf) catch unreachable
74 else
75 std.os.getcwd(&buf) catch unreachable;
76 const root_file_path = self.module.main_pkg.root_src_path;
77 const abs_root_path = try std.fs.path.join(self.arena, &.{ dir, root_file_path });
78 defer self.arena.free(abs_root_path);
79 const file = self.module.import_table.get(abs_root_path).?;
70 const root_src_dir = self.module.main_pkg.root_src_directory;
71 const root_src_path = self.module.main_pkg.root_src_path;
72 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
73 defer self.arena.free(joined_src_path);
74
75 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
76 defer self.arena.free(abs_root_src_path);
8077
78 const file = self.module.import_table.get(abs_root_src_path).?; // file is expected to be present in the import table
8179 // Append all the types in Zir.Inst.Ref.
8280 {
8381 try self.types.append(self.arena, .{
......@@ -229,44 +227,44 @@ pub fn generateZirData(self: *Autodoc) !void {
229227 self.doc_location.basename,
230228 ) catch |e| switch (e) {
231229 error.PathAlreadyExists => {},
232 else => unreachable,
230 else => |err| return err,
233231 };
234232 } else {
235233 self.module.zig_cache_artifact_directory.handle.makeDir(
236234 self.doc_location.basename,
237235 ) catch |e| switch (e) {
238236 error.PathAlreadyExists => {},
239 else => unreachable,
237 else => |err| return err,
240238 };
241239 }
242240 const output_dir = if (self.doc_location.directory) |d|
243 (d.handle.openDir(self.doc_location.basename, .{}) catch unreachable)
241 try d.handle.openDir(self.doc_location.basename, .{})
244242 else
245 (self.module.zig_cache_artifact_directory.handle.openDir(self.doc_location.basename, .{}) catch unreachable);
243 try self.module.zig_cache_artifact_directory.handle.openDir(self.doc_location.basename, .{});
246244 {
247 const data_js_f = output_dir.createFile("data.js", .{}) catch unreachable;
245 const data_js_f = try output_dir.createFile("data.js", .{});
248246 defer data_js_f.close();
249247 var buffer = std.io.bufferedWriter(data_js_f.writer());
250248
251249 const out = buffer.writer();
252 out.print(
250 try out.print(
253251 \\ /** @type {{DocData}} */
254252 \\ var zigAnalysis=
255 , .{}) catch unreachable;
256 std.json.stringify(
253 , .{});
254 try std.json.stringify(
257255 data,
258256 .{
259257 .whitespace = .{},
260258 .emit_null_optional_fields = false,
261259 },
262260 out,
263 ) catch unreachable;
264 out.print(";", .{}) catch unreachable;
261 );
262 try out.print(";", .{});
265263
266264 // last thing (that can fail) that we do is flush
267 buffer.flush() catch unreachable;
265 try buffer.flush();
268266 }
269
267
270268 // copy main.js, index.html
271269 var docs_dir = try self.module.comp.zig_lib_directory.handle.openDir("docs", .{});
272270 defer docs_dir.close();
......@@ -694,6 +692,12 @@ const DocData = struct {
694692 };
695693};
696694
695const AutodocErrors = error{
696 OutOfMemory,
697 CurrentWorkingDirectoryUnlinked,
698 Unexpected,
699};
700
697701/// Called when we need to analyze a Zir instruction.
698702/// For example it gets called by `generateZirData` on instruction 0,
699703/// which represents the top-level struct corresponding to the root file.
......@@ -708,7 +712,7 @@ fn walkInstruction(
708712 parent_scope: *Scope,
709713 inst_index: usize,
710714 need_type: bool, // true if the caller needs us to provide also a typeRef
711) error{OutOfMemory}!DocData.WalkResult {
715) AutodocErrors!DocData.WalkResult {
712716 const tags = file.zir.instructions.items(.tag);
713717 const data = file.zir.instructions.items(.data);
714718
......@@ -778,16 +782,15 @@ fn walkInstruction(
778782 // TODO: Add this package as a dependency to the current pakcage
779783 // TODO: this seems something that could be done in bulk
780784 // at the beginning or the end, or something.
781 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
782 const dir =
783 if (other_package.root_src_directory.path) |rp|
784 std.os.realpath(rp, &buf) catch unreachable
785 else
786 std.os.getcwd(&buf) catch unreachable;
787 const root_file_path = other_package.root_src_path;
788 const abs_root_path = try std.fs.path.join(self.arena, &.{ dir, root_file_path });
789 defer self.arena.free(abs_root_path);
790 const new_file = self.module.import_table.get(abs_root_path).?;
785 const root_src_dir = other_package.root_src_directory;
786 const root_src_path = other_package.root_src_path;
787 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
788 defer self.arena.free(joined_src_path);
789
790 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
791 defer self.arena.free(abs_root_src_path);
792
793 const new_file = self.module.import_table.get(abs_root_src_path).?;
791794
792795 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };
793796 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
......@@ -2630,7 +2633,7 @@ fn walkDecls(
26302633 decl_indexes: *std.ArrayListUnmanaged(usize),
26312634 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
26322635 extra_start: usize,
2633) error{OutOfMemory}!usize {
2636) AutodocErrors!usize {
26342637 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;
26352638 var extra_index = extra_start + bit_bags_count;
26362639 var bit_bag_index: usize = extra_start;
......@@ -2886,7 +2889,7 @@ fn tryResolveRefPath(
28862889 file: *File,
28872890 inst_index: usize, // used only for panicWithContext
28882891 path: []DocData.Expr,
2889) error{OutOfMemory}!void {
2892) AutodocErrors!void {
28902893 var i: usize = 0;
28912894 outer: while (i < path.len - 1) : (i += 1) {
28922895 const parent = path[i];
......@@ -3189,7 +3192,7 @@ fn analyzeFancyFunction(
31893192 inst_index: usize,
31903193 self_ast_node_index: usize,
31913194 type_slot_index: usize,
3192) error{OutOfMemory}!DocData.WalkResult {
3195) AutodocErrors!DocData.WalkResult {
31933196 const tags = file.zir.instructions.items(.tag);
31943197 const data = file.zir.instructions.items(.data);
31953198 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
......@@ -3405,7 +3408,7 @@ fn analyzeFunction(
34053408 inst_index: usize,
34063409 self_ast_node_index: usize,
34073410 type_slot_index: usize,
3408) error{OutOfMemory}!DocData.WalkResult {
3411) AutodocErrors!DocData.WalkResult {
34093412 const tags = file.zir.instructions.items(.tag);
34103413 const data = file.zir.instructions.items(.data);
34113414 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
......@@ -3726,7 +3729,7 @@ fn walkRef(
37263729 parent_scope: *Scope,
37273730 ref: Ref,
37283731 need_type: bool, // true when the caller needs also a typeRef for the return value
3729) !DocData.WalkResult {
3732) AutodocErrors!DocData.WalkResult {
37303733 const enum_value = @enumToInt(ref);
37313734 if (enum_value <= @enumToInt(Ref.anyerror_void_error_union_type)) {
37323735 // We can just return a type that indexes into `types` with the