authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-14 16:12:24-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:15-07:00
log363d4a107de3af95620f82e851e8777aa9f576c9
tree14a7536caf8366329f0f2f9286fa539d0fab3db2
parented33901218363b9d1baee4c37e85f573dc034e06

add compile log output to build runner


4 files changed, 38 insertions(+), 19 deletions(-)

lib/std/zig/ErrorBundle.zig+28-1
......@@ -32,6 +32,8 @@ pub const SourceLocationIndex = enum(u32) {
3232pub const ErrorMessageList = struct {
3333 len: u32,
3434 start: u32,
35 /// null-terminated string index. 0 means no compile log text.
36 compile_log_text: u32,
3537};
3638
3739/// Trailing:
......@@ -110,6 +112,10 @@ pub fn getNotes(eb: ErrorBundle, index: MessageIndex) []const MessageIndex {
110112 return @ptrCast([]const MessageIndex, eb.extra[start..][0..notes_len]);
111113}
112114
115pub fn getCompileLogOutput(eb: ErrorBundle) [:0]const u8 {
116 return nullTerminatedString(eb, getErrorMessageList(eb).compile_log_text);
117}
118
113119/// Returns the requested data, as well as the new index which is at the start of the
114120/// trailers for the object.
115121fn extraData(eb: ErrorBundle, comptime T: type, index: usize) struct { data: T, end: usize } {
......@@ -145,6 +151,7 @@ pub const RenderOptions = struct {
145151 ttyconf: std.debug.TTY.Config,
146152 include_reference_trace: bool = true,
147153 include_source_line: bool = true,
154 include_log_text: bool = true,
148155};
149156
150157pub fn renderToStdErr(eb: ErrorBundle, options: RenderOptions) void {
......@@ -158,6 +165,14 @@ pub fn renderToWriter(eb: ErrorBundle, options: RenderOptions, writer: anytype)
158165 for (eb.getMessages()) |err_msg| {
159166 try renderErrorMessageToWriter(eb, options, err_msg, writer, "error", .Red, 0);
160167 }
168
169 if (options.include_log_text) {
170 const log_text = eb.getCompileLogOutput();
171 if (log_text.len != 0) {
172 try writer.writeAll("\nCompile Log Output:\n");
173 try writer.writeAll(log_text);
174 }
175 }
161176}
162177
163178fn renderErrorMessageToWriter(
......@@ -314,6 +329,7 @@ pub const Wip = struct {
314329 assert(0 == try addExtra(wip, ErrorMessageList{
315330 .len = 0,
316331 .start = 0,
332 .compile_log_text = 0,
317333 }));
318334 }
319335
......@@ -325,9 +341,10 @@ pub const Wip = struct {
325341 wip.* = undefined;
326342 }
327343
328 pub fn toOwnedBundle(wip: *Wip) !ErrorBundle {
344 pub fn toOwnedBundle(wip: *Wip, compile_log_text: []const u8) !ErrorBundle {
329345 const gpa = wip.gpa;
330346 if (wip.root_list.items.len == 0) {
347 assert(compile_log_text.len == 0);
331348 // Special encoding when there are no errors.
332349 wip.deinit();
333350 wip.* = .{
......@@ -338,9 +355,19 @@ pub const Wip = struct {
338355 };
339356 return empty;
340357 }
358
359 const compile_log_str_index = if (compile_log_text.len == 0) 0 else str: {
360 const str = @intCast(u32, wip.string_bytes.items.len);
361 try wip.string_bytes.ensureUnusedCapacity(gpa, compile_log_text.len + 1);
362 wip.string_bytes.appendSliceAssumeCapacity(compile_log_text);
363 wip.string_bytes.appendAssumeCapacity(0);
364 break :str str;
365 };
366
341367 wip.setExtra(0, ErrorMessageList{
342368 .len = @intCast(u32, wip.root_list.items.len),
343369 .start = @intCast(u32, wip.extra.items.len),
370 .compile_log_text = compile_log_str_index,
344371 });
345372 try wip.extra.appendSlice(gpa, @ptrCast([]const u32, wip.root_list.items));
346373 wip.root_list.clearAndFree(gpa);
src/Compilation.zig+2-6
......@@ -2705,7 +2705,8 @@ pub fn getAllErrorsAlloc(self: *Compilation) !ErrorBundle {
27052705
27062706 assert(self.totalErrorCount() == bundle.root_list.items.len);
27072707
2708 return bundle.toOwnedBundle();
2708 const compile_log_text = if (self.bin_file.options.module) |m| m.compile_log_text.items else "";
2709 return bundle.toOwnedBundle(compile_log_text);
27092710}
27102711
27112712pub const ErrorNoteHashContext = struct {
......@@ -2954,11 +2955,6 @@ pub fn addZirErrorMessages(eb: *ErrorBundle.Wip, file: *Module.File) !void {
29542955 }
29552956}
29562957
2957pub fn getCompileLogOutput(self: *Compilation) []const u8 {
2958 const module = self.bin_file.options.module orelse return &[0]u8{};
2959 return module.compile_log_text.items;
2960}
2961
29622958pub fn performAllTheWork(
29632959 comp: *Compilation,
29642960 main_progress_node: *std.Progress.Node,
src/Sema.zig+1-1
......@@ -2219,7 +2219,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, err_msg: *Module.ErrorMsg) CompileError {
22192219 wip_errors.init(gpa) catch unreachable;
22202220 Compilation.addModuleErrorMsg(&wip_errors, err_msg.*) catch unreachable;
22212221 std.debug.print("compile error during Sema:\n", .{});
2222 var error_bundle = wip_errors.toOwnedBundle() catch unreachable;
2222 var error_bundle = wip_errors.toOwnedBundle("") catch unreachable;
22232223 error_bundle.renderToStdErr(.{ .ttyconf = .no_color });
22242224 crash_report.compilerPanic("unexpected compile error occurred", null, null);
22252225 }
src/main.zig+7-11
......@@ -3886,10 +3886,6 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
38863886
38873887 if (errors.errorMessageCount() > 0) {
38883888 errors.renderToStdErr(renderOptions(comp.color));
3889 const log_text = comp.getCompileLogOutput();
3890 if (log_text.len != 0) {
3891 std.debug.print("\nCompile Log Output:\n{s}", .{log_text});
3892 }
38933889 return error.SemanticAnalyzeFail;
38943890 } else switch (hook) {
38953891 .none => {},
......@@ -4512,7 +4508,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
45124508 &all_modules,
45134509 );
45144510 if (wip_errors.root_list.items.len > 0) {
4515 var errors = try wip_errors.toOwnedBundle();
4511 var errors = try wip_errors.toOwnedBundle("");
45164512 defer errors.deinit(gpa);
45174513 errors.renderToStdErr(renderOptions(color));
45184514 process.exit(1);
......@@ -4775,7 +4771,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
47754771 try wip_errors.init(gpa);
47764772 defer wip_errors.deinit();
47774773 try Compilation.addZirErrorMessages(&wip_errors, &file);
4778 var error_bundle = try wip_errors.toOwnedBundle();
4774 var error_bundle = try wip_errors.toOwnedBundle("");
47794775 defer error_bundle.deinit(gpa);
47804776 error_bundle.renderToStdErr(renderOptions(color));
47814777 process.exit(2);
......@@ -4981,7 +4977,7 @@ fn fmtPathFile(
49814977 try wip_errors.init(gpa);
49824978 defer wip_errors.deinit();
49834979 try Compilation.addZirErrorMessages(&wip_errors, &file);
4984 var error_bundle = try wip_errors.toOwnedBundle();
4980 var error_bundle = try wip_errors.toOwnedBundle("");
49854981 defer error_bundle.deinit(gpa);
49864982 error_bundle.renderToStdErr(renderOptions(fmt.color));
49874983 fmt.any_error = true;
......@@ -5018,7 +5014,7 @@ fn printAstErrorsToStderr(gpa: Allocator, tree: Ast, path: []const u8, color: Co
50185014
50195015 try putAstErrorsIntoBundle(gpa, tree, path, &wip_errors);
50205016
5021 var error_bundle = try wip_errors.toOwnedBundle();
5017 var error_bundle = try wip_errors.toOwnedBundle("");
50225018 defer error_bundle.deinit(gpa);
50235019 error_bundle.renderToStdErr(renderOptions(color));
50245020}
......@@ -5622,7 +5618,7 @@ pub fn cmdAstCheck(
56225618 try wip_errors.init(gpa);
56235619 defer wip_errors.deinit();
56245620 try Compilation.addZirErrorMessages(&wip_errors, &file);
5625 var error_bundle = try wip_errors.toOwnedBundle();
5621 var error_bundle = try wip_errors.toOwnedBundle("");
56265622 defer error_bundle.deinit(gpa);
56275623 error_bundle.renderToStdErr(renderOptions(color));
56285624 process.exit(1);
......@@ -5739,7 +5735,7 @@ pub fn cmdChangelist(
57395735 try wip_errors.init(gpa);
57405736 defer wip_errors.deinit();
57415737 try Compilation.addZirErrorMessages(&wip_errors, &file);
5742 var error_bundle = try wip_errors.toOwnedBundle();
5738 var error_bundle = try wip_errors.toOwnedBundle("");
57435739 defer error_bundle.deinit(gpa);
57445740 error_bundle.renderToStdErr(renderOptions(color));
57455741 process.exit(1);
......@@ -5774,7 +5770,7 @@ pub fn cmdChangelist(
57745770 try wip_errors.init(gpa);
57755771 defer wip_errors.deinit();
57765772 try Compilation.addZirErrorMessages(&wip_errors, &file);
5777 var error_bundle = try wip_errors.toOwnedBundle();
5773 var error_bundle = try wip_errors.toOwnedBundle("");
57785774 defer error_bundle.deinit(gpa);
57795775 error_bundle.renderToStdErr(renderOptions(color));
57805776 process.exit(1);