Skip to content

Commit

Permalink
better output when @cImport generates invalid zig
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 26, 2017
1 parent 300c83d commit c7053be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/analyze.cpp
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/errmsg.cpp
Expand Up @@ -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);

Expand Down

0 comments on commit c7053be

Please sign in to comment.