Skip to content

Commit 0833500

Browse files
committed
Add source file name and line number to error messages.
Byte code has a map from byte-code offset to source-code line number, used to give better error messages.
1 parent aefe798 commit 0833500

24 files changed

Lines changed: 351 additions & 194 deletions

py/bc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state);
2-
bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out);
2+
bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out);

py/builtineval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
2828
qstr parse_exc_id;
2929
const char *parse_exc_msg;
3030
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_EVAL_INPUT, &parse_exc_id, &parse_exc_msg);
31+
qstr source_name = mp_lexer_source_name(lex);
3132
mp_lexer_free(lex);
3233

3334
if (pn == MP_PARSE_NODE_NULL) {
@@ -36,7 +37,7 @@ static mp_obj_t mp_builtin_eval(mp_obj_t o_in) {
3637
}
3738

3839
// compile the string
39-
mp_obj_t module_fun = mp_compile(pn, false);
40+
mp_obj_t module_fun = mp_compile(pn, source_name, false);
4041

4142
if (module_fun == mp_const_none) {
4243
// TODO handle compile error correctly

py/builtinimport.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
5151
qstr parse_exc_id;
5252
const char *parse_exc_msg;
5353
mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT, &parse_exc_id, &parse_exc_msg);
54+
qstr source_name = mp_lexer_source_name(lex);
5455
mp_lexer_free(lex);
5556

5657
if (pn == MP_PARSE_NODE_NULL) {
@@ -61,7 +62,7 @@ mp_obj_t mp_builtin___import__(int n_args, mp_obj_t *args) {
6162
}
6263

6364
// compile the imported script
64-
mp_obj_t module_fun = mp_compile(pn, false);
65+
mp_obj_t module_fun = mp_compile(pn, source_name, false);
6566

6667
if (module_fun == mp_const_none) {
6768
// TODO handle compile error correctly

py/compile.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,7 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
25052505
}
25062506
} else {
25072507
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
2508+
EMIT(set_line_number, pns->source_line);
25082509
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
25092510
if (f == NULL) {
25102511
printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
@@ -3024,7 +3025,7 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
30243025
}
30253026
}
30263027

3027-
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
3028+
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) {
30283029
compiler_t *comp = m_new(compiler_t, 1);
30293030

30303031
comp->is_repl = is_repl;
@@ -3131,7 +3132,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl) {
31313132

31323133
default:
31333134
if (emit_bc == NULL) {
3134-
emit_bc = emit_bc_new(max_num_labels);
3135+
emit_bc = emit_bc_new(source_file, max_num_labels);
31353136
}
31363137
comp->emit = emit_bc;
31373138
comp->emit_method_table = &emit_bc_method_table;

py/compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mp_obj_t mp_compile(mp_parse_node_t pn, bool is_repl);
1+
mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl);

py/emit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct _emit_method_table_t {
2323
bool (*last_emit_was_return_value)(emit_t *emit);
2424
int (*get_stack_size)(emit_t *emit);
2525
void (*set_stack_size)(emit_t *emit, int size);
26+
void (*set_line_number)(emit_t *emit, int line);
2627

2728
void (*load_id)(emit_t *emit, qstr qstr);
2829
void (*store_id)(emit_t *emit, qstr qstr);
@@ -119,7 +120,7 @@ extern const emit_method_table_t emit_native_thumb_method_table;
119120
emit_t *emit_pass1_new(qstr qstr___class__);
120121
void emit_pass1_free(emit_t *emit);
121122
emit_t *emit_cpython_new(uint max_num_labels);
122-
emit_t *emit_bc_new(uint max_num_labels);
123+
emit_t *emit_bc_new(qstr source_file, uint max_num_labels);
123124
emit_t *emit_native_x64_new(uint max_num_labels);
124125
emit_t *emit_native_thumb_new(uint max_num_labels);
125126

0 commit comments

Comments
 (0)