Skip to content

stage2: Unify compiler error printing #9117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 23 additions & 35 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "<stdin>", stderr_file, color);
try printErrMsgToStdErr(gpa, parse_error, tree, "<stdin>", color);
}
if (tree.errors.len != 0) {
process.exit(1);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3219,52 +3218,41 @@ 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 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();
const writer = text_buf.writer();
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 =
Expand Down Expand Up @@ -3780,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);
Expand Down Expand Up @@ -3901,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);
Expand Down Expand Up @@ -3938,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);
Expand Down