From 382f87abac469db7cbc4968ca03e02cc1af5f6ee Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Mon, 14 Jun 2021 22:09:07 +0600 Subject: [PATCH 1/2] Rewrite printErrMsgToFile to use Message struct from Compilation --- src/main.zig | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/main.zig b/src/main.zig index 512828bd77ac..2adecd95429b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3227,17 +3227,9 @@ fn printErrMsgToFile( file: fs.File, color: Color, ) !void { - const color_on = switch (color) { - .auto => file.isTty(), - .on => true, - .off => false, - }; const lok_token = parse_error.token; - - const token_starts = tree.tokens.items(.start); - const token_tags = tree.tokens.items(.tag); - const first_token_start = token_starts[lok_token]; const start_loc = tree.tokenLocation(0, lok_token); + const source_line = tree.source[start_loc.line_start..start_loc.line_end]; var text_buf = std.ArrayList(u8).init(gpa); defer text_buf.deinit(); @@ -3245,26 +3237,24 @@ fn printErrMsgToFile( try tree.renderError(parse_error, writer); const text = text_buf.items; - const stream = file.writer(); - try stream.print("{s}:{d}:{d}: error: {s}\n", .{ path, start_loc.line + 1, start_loc.column + 1, text }); + const message: Compilation.AllErrors.Message = .{ + .src = .{ + .src_path = path, + .msg = text, + .byte_offset = @intCast(u32, start_loc.line_start), + .line = @intCast(u32, start_loc.line), + .column = @intCast(u32, start_loc.column), + .source_line = source_line, + }, + }; - if (!color_on) return; + const ttyconf: std.debug.TTY.Config = switch (color) { + .auto => std.debug.detectTTYConfig(), + .on => .escape_codes, + .off => .no_color, + }; - // Print \r and \t as one space each so that column counts line up - for (tree.source[start_loc.line_start..start_loc.line_end]) |byte| { - try stream.writeByte(switch (byte) { - '\r', '\t' => ' ', - else => byte, - }); - } - try stream.writeByte('\n'); - try stream.writeByteNTimes(' ', start_loc.column); - if (token_tags[lok_token].lexeme()) |lexeme| { - try stream.writeByteNTimes('~', lexeme.len); - try stream.writeByte('\n'); - } else { - try stream.writeAll("^\n"); - } + message.renderToStdErr(ttyconf); } pub const info_zen = From 3150458f2f4aa06c6cd99cb6ec06791f1ca05505 Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Mon, 14 Jun 2021 22:13:32 +0600 Subject: [PATCH 2/2] Rename printErrMsgToFile->printErrMsgToStdErr and remove `file` argument Compilation.AllError.Message interface does not provide configurable file handle. And since all uses of this function only use stderr, this can be added later. --- src/main.zig | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2adecd95429b..7c1eb527cdc8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2940,7 +2940,6 @@ const Fmt = struct { }; pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { - const stderr_file = io.getStdErr(); var color: Color = .auto; var stdin_flag: bool = false; var check_flag: bool = false; @@ -2998,7 +2997,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { defer tree.deinit(gpa); for (tree.errors) |parse_error| { - try printErrMsgToFile(gpa, parse_error, tree, "", stderr_file, color); + try printErrMsgToStdErr(gpa, parse_error, tree, "", color); } if (tree.errors.len != 0) { process.exit(1); @@ -3143,7 +3142,7 @@ fn fmtPathFile( defer tree.deinit(fmt.gpa); for (tree.errors) |parse_error| { - try printErrMsgToFile(fmt.gpa, parse_error, tree, file_path, std.io.getStdErr(), fmt.color); + try printErrMsgToStdErr(fmt.gpa, parse_error, tree, file_path, fmt.color); } if (tree.errors.len != 0) { fmt.any_error = true; @@ -3219,12 +3218,11 @@ fn fmtPathFile( } } -fn printErrMsgToFile( +fn printErrMsgToStdErr( gpa: *mem.Allocator, parse_error: ast.Error, tree: ast.Tree, path: []const u8, - file: fs.File, color: Color, ) !void { const lok_token = parse_error.token; @@ -3770,7 +3768,7 @@ pub fn cmdAstCheck( defer file.tree.deinit(gpa); for (file.tree.errors) |parse_error| { - try printErrMsgToFile(gpa, parse_error, file.tree, file.sub_file_path, io.getStdErr(), color); + try printErrMsgToStdErr(gpa, parse_error, file.tree, file.sub_file_path, color); } if (file.tree.errors.len != 0) { process.exit(1); @@ -3891,7 +3889,7 @@ pub fn cmdChangelist( defer file.tree.deinit(gpa); for (file.tree.errors) |parse_error| { - try printErrMsgToFile(gpa, parse_error, file.tree, old_source_file, io.getStdErr(), .auto); + try printErrMsgToStdErr(gpa, parse_error, file.tree, old_source_file, .auto); } if (file.tree.errors.len != 0) { process.exit(1); @@ -3928,7 +3926,7 @@ pub fn cmdChangelist( defer new_tree.deinit(gpa); for (new_tree.errors) |parse_error| { - try printErrMsgToFile(gpa, parse_error, new_tree, new_source_file, io.getStdErr(), .auto); + try printErrMsgToStdErr(gpa, parse_error, new_tree, new_source_file, .auto); } if (new_tree.errors.len != 0) { process.exit(1);