From 84d59c28738b8f84c0581de4f6bba01c1f1e40ef Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 27 Jul 2015 22:20:00 +0100 Subject: [PATCH] py: For viper compile errors, add traceback with function and filename. ViperTypeError now includes filename and function name where the error occurred. The line number is the line number of the start of the function definition, which is the best that can be done without a lot more work. Partially addresses issue #1381. --- py/compile.c | 23 +++++++++++++++++------ py/emitnative.c | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/py/compile.c b/py/compile.c index 141cd2db99..12134d4a9d 100644 --- a/py/compile.c +++ b/py/compile.c @@ -108,16 +108,20 @@ typedef struct _compiler_t { #endif } compiler_t; -STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) { - mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg); - // we don't have a 'block' name, so just pass the NULL qstr to indicate this +STATIC void compile_error_add_traceback(compiler_t *comp, mp_parse_node_t pn) { + mp_uint_t line; if (MP_PARSE_NODE_IS_STRUCT(pn)) { - mp_obj_exception_add_traceback(exc, comp->source_file, (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line, comp->scope_cur->simple_name); + line = (mp_uint_t)((mp_parse_node_struct_t*)pn)->source_line; } else { // we don't have a line number, so just pass 0 - mp_obj_exception_add_traceback(exc, comp->source_file, 0, comp->scope_cur->simple_name); + line = 0; } - comp->compile_error = exc; + mp_obj_exception_add_traceback(comp->compile_error, comp->source_file, line, comp->scope_cur->simple_name); +} + +STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) { + comp->compile_error = mp_obj_new_exception_msg(&mp_type_SyntaxError, msg); + compile_error_add_traceback(comp, pn); } #if MICROPY_COMP_MODULE_CONST @@ -3818,6 +3822,13 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is if (comp->compile_error == MP_OBJ_NULL) { compile_scope(comp, s, MP_PASS_EMIT); } + + #if MICROPY_EMIT_NATIVE + // if viper had an error then add traceback + if (comp->compile_error != MP_OBJ_NULL && s->emit_options == MP_EMIT_OPT_VIPER) { + compile_error_add_traceback(comp, s->pn); + } + #endif } } diff --git a/py/emitnative.c b/py/emitnative.c index 37dbd56b3e..2a521c229c 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -677,7 +677,7 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop // right now we have a restriction of maximum of 4 arguments if (scope->num_pos_args >= 5) { - EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments (while compiling '%q')", scope->simple_name); return; + EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments"); return; }