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 {...@@ -67,17 +67,15 @@ pub fn generateZirData(self: *Autodoc) !void {
67 }67 }
68 }68 }
6969
70 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;70 const root_src_dir = self.module.main_pkg.root_src_directory;
71 const dir =71 const root_src_path = self.module.main_pkg.root_src_path;
72 if (self.module.main_pkg.root_src_directory.path) |rp|72 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
73 std.os.realpath(rp, &buf) catch unreachable73 defer self.arena.free(joined_src_path);
74 else74
75 std.os.getcwd(&buf) catch unreachable;75 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
76 const root_file_path = self.module.main_pkg.root_src_path;76 defer self.arena.free(abs_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).?;
8077
78 const file = self.module.import_table.get(abs_root_src_path).?; // file is expected to be present in the import table
81 // Append all the types in Zir.Inst.Ref.79 // Append all the types in Zir.Inst.Ref.
82 {80 {
83 try self.types.append(self.arena, .{81 try self.types.append(self.arena, .{
...@@ -229,44 +227,44 @@ pub fn generateZirData(self: *Autodoc) !void {...@@ -229,44 +227,44 @@ pub fn generateZirData(self: *Autodoc) !void {
229 self.doc_location.basename,227 self.doc_location.basename,
230 ) catch |e| switch (e) {228 ) catch |e| switch (e) {
231 error.PathAlreadyExists => {},229 error.PathAlreadyExists => {},
232 else => unreachable,230 else => |err| return err,
233 };231 };
234 } else {232 } else {
235 self.module.zig_cache_artifact_directory.handle.makeDir(233 self.module.zig_cache_artifact_directory.handle.makeDir(
236 self.doc_location.basename,234 self.doc_location.basename,
237 ) catch |e| switch (e) {235 ) catch |e| switch (e) {
238 error.PathAlreadyExists => {},236 error.PathAlreadyExists => {},
239 else => unreachable,237 else => |err| return err,
240 };238 };
241 }239 }
242 const output_dir = if (self.doc_location.directory) |d|240 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, .{})
244 else242 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, .{});
246 {244 {
247 const data_js_f = output_dir.createFile("data.js", .{}) catch unreachable;245 const data_js_f = try output_dir.createFile("data.js", .{});
248 defer data_js_f.close();246 defer data_js_f.close();
249 var buffer = std.io.bufferedWriter(data_js_f.writer());247 var buffer = std.io.bufferedWriter(data_js_f.writer());
250248
251 const out = buffer.writer();249 const out = buffer.writer();
252 out.print(250 try out.print(
253 \\ /** @type {{DocData}} */251 \\ /** @type {{DocData}} */
254 \\ var zigAnalysis=252 \\ var zigAnalysis=
255 , .{}) catch unreachable;253 , .{});
256 std.json.stringify(254 try std.json.stringify(
257 data,255 data,
258 .{256 .{
259 .whitespace = .{},257 .whitespace = .{},
260 .emit_null_optional_fields = false,258 .emit_null_optional_fields = false,
261 },259 },
262 out,260 out,
263 ) catch unreachable;261 );
264 out.print(";", .{}) catch unreachable;262 try out.print(";", .{});
265263
266 // last thing (that can fail) that we do is flush264 // last thing (that can fail) that we do is flush
267 buffer.flush() catch unreachable;265 try buffer.flush();
268 }266 }
269 267
270 // copy main.js, index.html268 // copy main.js, index.html
271 var docs_dir = try self.module.comp.zig_lib_directory.handle.openDir("docs", .{});269 var docs_dir = try self.module.comp.zig_lib_directory.handle.openDir("docs", .{});
272 defer docs_dir.close();270 defer docs_dir.close();
...@@ -694,6 +692,12 @@ const DocData = struct {...@@ -694,6 +692,12 @@ const DocData = struct {
694 };692 };
695};693};
696694
695const AutodocErrors = error{
696 OutOfMemory,
697 CurrentWorkingDirectoryUnlinked,
698 Unexpected,
699};
700
697/// Called when we need to analyze a Zir instruction.701/// Called when we need to analyze a Zir instruction.
698/// For example it gets called by `generateZirData` on instruction 0,702/// For example it gets called by `generateZirData` on instruction 0,
699/// which represents the top-level struct corresponding to the root file.703/// which represents the top-level struct corresponding to the root file.
...@@ -708,7 +712,7 @@ fn walkInstruction(...@@ -708,7 +712,7 @@ fn walkInstruction(
708 parent_scope: *Scope,712 parent_scope: *Scope,
709 inst_index: usize,713 inst_index: usize,
710 need_type: bool, // true if the caller needs us to provide also a typeRef714 need_type: bool, // true if the caller needs us to provide also a typeRef
711) error{OutOfMemory}!DocData.WalkResult {715) AutodocErrors!DocData.WalkResult {
712 const tags = file.zir.instructions.items(.tag);716 const tags = file.zir.instructions.items(.tag);
713 const data = file.zir.instructions.items(.data);717 const data = file.zir.instructions.items(.data);
714718
...@@ -778,16 +782,15 @@ fn walkInstruction(...@@ -778,16 +782,15 @@ fn walkInstruction(
778 // TODO: Add this package as a dependency to the current pakcage782 // TODO: Add this package as a dependency to the current pakcage
779 // TODO: this seems something that could be done in bulk783 // TODO: this seems something that could be done in bulk
780 // at the beginning or the end, or something.784 // at the beginning or the end, or something.
781 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;785 const root_src_dir = other_package.root_src_directory;
782 const dir =786 const root_src_path = other_package.root_src_path;
783 if (other_package.root_src_directory.path) |rp|787 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
784 std.os.realpath(rp, &buf) catch unreachable788 defer self.arena.free(joined_src_path);
785 else789
786 std.os.getcwd(&buf) catch unreachable;790 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
787 const root_file_path = other_package.root_src_path;791 defer self.arena.free(abs_root_src_path);
788 const abs_root_path = try std.fs.path.join(self.arena, &.{ dir, root_file_path });792
789 defer self.arena.free(abs_root_path);793 const new_file = self.module.import_table.get(abs_root_src_path).?;
790 const new_file = self.module.import_table.get(abs_root_path).?;
791794
792 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };795 var root_scope = Scope{ .parent = null, .enclosing_type = main_type_index };
793 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });796 try self.ast_nodes.append(self.arena, .{ .name = "(root)" });
...@@ -2630,7 +2633,7 @@ fn walkDecls(...@@ -2630,7 +2633,7 @@ fn walkDecls(
2630 decl_indexes: *std.ArrayListUnmanaged(usize),2633 decl_indexes: *std.ArrayListUnmanaged(usize),
2631 priv_decl_indexes: *std.ArrayListUnmanaged(usize),2634 priv_decl_indexes: *std.ArrayListUnmanaged(usize),
2632 extra_start: usize,2635 extra_start: usize,
2633) error{OutOfMemory}!usize {2636) AutodocErrors!usize {
2634 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;2637 const bit_bags_count = std.math.divCeil(usize, decls_len, 8) catch unreachable;
2635 var extra_index = extra_start + bit_bags_count;2638 var extra_index = extra_start + bit_bags_count;
2636 var bit_bag_index: usize = extra_start;2639 var bit_bag_index: usize = extra_start;
...@@ -2886,7 +2889,7 @@ fn tryResolveRefPath(...@@ -2886,7 +2889,7 @@ fn tryResolveRefPath(
2886 file: *File,2889 file: *File,
2887 inst_index: usize, // used only for panicWithContext2890 inst_index: usize, // used only for panicWithContext
2888 path: []DocData.Expr,2891 path: []DocData.Expr,
2889) error{OutOfMemory}!void {2892) AutodocErrors!void {
2890 var i: usize = 0;2893 var i: usize = 0;
2891 outer: while (i < path.len - 1) : (i += 1) {2894 outer: while (i < path.len - 1) : (i += 1) {
2892 const parent = path[i];2895 const parent = path[i];
...@@ -3189,7 +3192,7 @@ fn analyzeFancyFunction(...@@ -3189,7 +3192,7 @@ fn analyzeFancyFunction(
3189 inst_index: usize,3192 inst_index: usize,
3190 self_ast_node_index: usize,3193 self_ast_node_index: usize,
3191 type_slot_index: usize,3194 type_slot_index: usize,
3192) error{OutOfMemory}!DocData.WalkResult {3195) AutodocErrors!DocData.WalkResult {
3193 const tags = file.zir.instructions.items(.tag);3196 const tags = file.zir.instructions.items(.tag);
3194 const data = file.zir.instructions.items(.data);3197 const data = file.zir.instructions.items(.data);
3195 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));3198 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
...@@ -3405,7 +3408,7 @@ fn analyzeFunction(...@@ -3405,7 +3408,7 @@ fn analyzeFunction(
3405 inst_index: usize,3408 inst_index: usize,
3406 self_ast_node_index: usize,3409 self_ast_node_index: usize,
3407 type_slot_index: usize,3410 type_slot_index: usize,
3408) error{OutOfMemory}!DocData.WalkResult {3411) AutodocErrors!DocData.WalkResult {
3409 const tags = file.zir.instructions.items(.tag);3412 const tags = file.zir.instructions.items(.tag);
3410 const data = file.zir.instructions.items(.data);3413 const data = file.zir.instructions.items(.data);
3411 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));3414 const fn_info = file.zir.getFnInfo(@intCast(u32, inst_index));
...@@ -3726,7 +3729,7 @@ fn walkRef(...@@ -3726,7 +3729,7 @@ fn walkRef(
3726 parent_scope: *Scope,3729 parent_scope: *Scope,
3727 ref: Ref,3730 ref: Ref,
3728 need_type: bool, // true when the caller needs also a typeRef for the return value3731 need_type: bool, // true when the caller needs also a typeRef for the return value
3729) !DocData.WalkResult {3732) AutodocErrors!DocData.WalkResult {
3730 const enum_value = @enumToInt(ref);3733 const enum_value = @enumToInt(ref);
3731 if (enum_value <= @enumToInt(Ref.anyerror_void_error_union_type)) {3734 if (enum_value <= @enumToInt(Ref.anyerror_void_error_union_type)) {
3732 // We can just return a type that indexes into `types` with the3735 // We can just return a type that indexes into `types` with the