better output when @cImport generates invalid zig

master
Andrew Kelley 2017-10-26 00:32:30 -04:00
parent 300c83d893
commit c7053bea20
2 changed files with 26 additions and 6 deletions

View File

@ -27,9 +27,17 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type);
static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type);
ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
// if this assert fails, then parsec generated code that
// failed semantic analysis, which isn't supposed to happen
assert(!node->owner->c_import_node);
if (node->owner->c_import_node != nullptr) {
// if this happens, then parsec generated code that
// failed semantic analysis, which isn't supposed to happen
ErrorMsg *err = add_node_error(g, node->owner->c_import_node,
buf_sprintf("compiler bug: @cImport generated invalid zig code"));
add_error_note(g, err, node, msg);
g->errors.append(err);
return err;
}
ErrorMsg *err = err_msg_create_with_line(node->owner->path, node->line, node->column,
node->owner->source_code, node->owner->line_offsets, msg);
@ -39,9 +47,20 @@ ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
}
ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, AstNode *node, Buf *msg) {
// if this assert fails, then parsec generated code that
// failed semantic analysis, which isn't supposed to happen
assert(!node->owner->c_import_node);
if (node->owner->c_import_node != nullptr) {
// if this happens, then parsec generated code that
// failed semantic analysis, which isn't supposed to happen
Buf *note_path = buf_create_from_str("?.c");
Buf *note_source = buf_create_from_str("TODO: remember C source location to display here ");
ZigList<size_t> note_line_offsets = {0};
note_line_offsets.append(0);
ErrorMsg *note = err_msg_create_with_line(note_path, 0, 0,
note_source, &note_line_offsets, msg);
err_msg_add_note(parent_msg, note);
return note;
}
ErrorMsg *err = err_msg_create_with_line(node->owner->path, node->line, node->column,
node->owner->source_code, node->owner->line_offsets, msg);

View File

@ -123,6 +123,7 @@ ErrorMsg *err_msg_create_with_line(Buf *path, size_t line, size_t column,
size_t end_line = line + 1;
size_t line_end_offset = (end_line >= line_offsets->length) ? buf_len(source) : line_offsets->at(line + 1);
size_t len = (line_end_offset + 1 > line_start_offset) ? (line_end_offset - line_start_offset - 1) : 0;
if (len == SIZE_MAX) len = 0;
buf_init_from_mem(&err_msg->line_buf, buf_ptr(source) + line_start_offset, len);