Stop dropping errors from clang
* Refactor the error-writing code to be more compact and flexible
This commit is contained in:
@@ -16,51 +16,49 @@ enum ErrType {
|
||||
};
|
||||
|
||||
static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) {
|
||||
const char *path = buf_ptr(err->path);
|
||||
size_t line = err->line_start + 1;
|
||||
size_t col = err->column_start + 1;
|
||||
const char *text = buf_ptr(err->msg);
|
||||
|
||||
bool is_tty = os_stderr_tty();
|
||||
if (color == ErrColorOn || (color == ErrColorAuto && is_tty)) {
|
||||
if (err_type == ErrTypeError) {
|
||||
os_stderr_set_color(TermColorBold);
|
||||
fprintf(stderr, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ": ", path, line, col);
|
||||
os_stderr_set_color(TermColorRed);
|
||||
fprintf(stderr, "error:");
|
||||
os_stderr_set_color(TermColorBold);
|
||||
fprintf(stderr, " %s", text);
|
||||
os_stderr_set_color(TermColorReset);
|
||||
fprintf(stderr, "\n");
|
||||
} else if (err_type == ErrTypeNote) {
|
||||
os_stderr_set_color(TermColorBold);
|
||||
fprintf(stderr, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ": ", path, line, col);
|
||||
os_stderr_set_color(TermColorCyan);
|
||||
fprintf(stderr, "note:");
|
||||
os_stderr_set_color(TermColorBold);
|
||||
fprintf(stderr, " %s", text);
|
||||
os_stderr_set_color(TermColorReset);
|
||||
fprintf(stderr, "\n");
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
bool use_colors = color == ErrColorOn || (color == ErrColorAuto && is_tty);
|
||||
|
||||
// Show the error location, if available
|
||||
if (err->path != nullptr) {
|
||||
const size_t line = err->line_start + 1;
|
||||
const size_t col = err->column_start + 1;
|
||||
|
||||
if (use_colors) os_stderr_set_color(TermColorBold);
|
||||
fprintf(stderr, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ": ", buf_ptr(err->path), line, col);
|
||||
}
|
||||
|
||||
// Write out the error type
|
||||
switch (err_type) {
|
||||
case ErrTypeError:
|
||||
if (use_colors) os_stderr_set_color(TermColorRed);
|
||||
fprintf(stderr, "error: ");
|
||||
break;
|
||||
case ErrTypeNote:
|
||||
if (use_colors) os_stderr_set_color(TermColorCyan);
|
||||
fprintf(stderr, "note: ");
|
||||
break;
|
||||
default:
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
// Write out the error message
|
||||
if (use_colors) os_stderr_set_color(TermColorBold);
|
||||
fputs(buf_ptr(err->msg), stderr);
|
||||
if (use_colors) os_stderr_set_color(TermColorReset);
|
||||
fputc('\n', stderr);
|
||||
|
||||
if (buf_len(&err->line_buf) != 0){
|
||||
// Show the referenced line
|
||||
fprintf(stderr, "%s\n", buf_ptr(&err->line_buf));
|
||||
for (size_t i = 0; i < err->column_start; i += 1) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
os_stderr_set_color(TermColorGreen);
|
||||
// Draw the caret
|
||||
if (use_colors) os_stderr_set_color(TermColorGreen);
|
||||
fprintf(stderr, "^");
|
||||
os_stderr_set_color(TermColorReset);
|
||||
if (use_colors) os_stderr_set_color(TermColorReset);
|
||||
fprintf(stderr, "\n");
|
||||
} else {
|
||||
if (err_type == ErrTypeError) {
|
||||
fprintf(stderr, "%s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ": error: %s\n", path, line, col, text);
|
||||
} else if (err_type == ErrTypeNote) {
|
||||
fprintf(stderr, " %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ": note: %s\n", path, line, col, text);
|
||||
} else {
|
||||
zig_unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < err->notes.length; i += 1) {
|
||||
@@ -86,6 +84,12 @@ ErrorMsg *err_msg_create_with_offset(Buf *path, size_t line, size_t column, size
|
||||
err_msg->column_start = column;
|
||||
err_msg->msg = msg;
|
||||
|
||||
if (source == nullptr) {
|
||||
// Must initialize the buffer anyway
|
||||
buf_init_from_str(&err_msg->line_buf, "");
|
||||
return err_msg;
|
||||
}
|
||||
|
||||
size_t line_start_offset = offset;
|
||||
for (;;) {
|
||||
if (line_start_offset == 0) {
|
||||
|
||||
Reference in New Issue
Block a user