From aebb02834958cb96f948cd97085ba4816b9d2f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 21 Jul 2021 10:46:04 +0200 Subject: [PATCH] gen: fix C keyword escape to _v_ instead of just v_ (#10876) --- vlib/builtin/builtin.c.v | 4 ++-- vlib/v/gen/c/assert.v | 2 +- vlib/v/gen/c/cgen.v | 8 ++++---- vlib/v/gen/c/fn.v | 2 +- vlib/v/gen/c/index.v | 4 ++-- vlib/v/gen/c/json.v | 8 ++++---- .../tests/reserved_keywords_can_be_escaped_with_at_test.v | 7 +++++++ 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 1f6565491b..c8766372c7 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -238,7 +238,7 @@ pub fn malloc(n int) &byte { } $if trace_malloc ? { total_m += n - C.fprintf(C.stderr, c'v_malloc %6d total %10d\n', n, total_m) + C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m) // print_backtrace() } mut res := &byte(0) @@ -285,7 +285,7 @@ pub fn malloc_noscan(n int) &byte { } $if trace_malloc ? { total_m += n - C.fprintf(C.stderr, c'v_malloc %6d total %10d\n', n, total_m) + C.fprintf(C.stderr, c'_v_malloc %6d total %10d\n', n, total_m) // print_backtrace() } mut res := &byte(0) diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index 748ace63bd..bc20b789f5 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -47,7 +47,7 @@ fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) { metaname_panic := g.gen_assert_metainfo(node) g.writeln('\t__print_assert_failure(&$metaname_panic);') g.gen_assert_postfailure_mode(node) - g.writeln('\tv_panic(_SLIT("Assertion failed..."));') + g.writeln('\t_v_panic(_SLIT("Assertion failed..."));') g.writeln('}') } } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 33ee3442e9..8027bd9fb7 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -715,7 +715,7 @@ fn (mut g Gen) register_chan_pop_optional_call(opt_el_type string, styp string) static inline $opt_el_type __Option_${styp}_popval($styp ch) { $opt_el_type _tmp = {0}; if (sync__Channel_try_pop_priv(ch, _tmp.data, false)) { - return ($opt_el_type){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; + return ($opt_el_type){ .state = 2, .err = _v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; } return _tmp; }') @@ -729,7 +729,7 @@ fn (mut g Gen) register_chan_push_optional_call(el_type string, styp string) { g.channel_definitions.writeln(' static inline Option_void __Option_${styp}_pushval($styp ch, $el_type e) { if (sync__Channel_try_push_priv(ch, &e, false)) { - return (Option_void){ .state = 2, .err = v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; + return (Option_void){ .state = 2, .err = _v_error(_SLIT("channel closed")), .data = {EMPTY_STRUCT_INITIALIZATION} }; } return (Option_void){0}; }') @@ -5789,7 +5789,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type ast.Ty fn c_name(name_ string) string { name := util.no_dots(name_) if name in c.c_reserved_map { - return 'v_$name' + return '_v_$name' } return name } @@ -6052,7 +6052,7 @@ fn (mut g Gen) go_expr(node ast.GoExpr) { } else { g.gowrappers.writeln('\tint stat = pthread_join(thread, (void **)$c_ret_ptr_ptr);') } - g.gowrappers.writeln('\tif (stat != 0) { v_panic(_SLIT("unable to join thread")); }') + g.gowrappers.writeln('\tif (stat != 0) { _v_panic(_SLIT("unable to join thread")); }') if g.pref.os == .windows { if node.call_expr.return_type == ast.void_type { g.gowrappers.writeln('\tCloseHandle(thread);') diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index c408f210a3..b8d65c1531 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -33,7 +33,7 @@ fn (mut g Gen) process_fn_decl(node ast.FnDecl) { return } if g.is_builtin_mod && g.pref.gc_mode == .boehm_leak && node.name == 'malloc' { - g.definitions.write_string('#define v_malloc GC_MALLOC\n') + g.definitions.write_string('#define _v_malloc GC_MALLOC\n') return } g.gen_attrs(node.attrs) diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index c2b22d5c72..26dc455570 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -274,7 +274,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) { g.writeln('if ($tmp_opt_ptr) {') g.writeln('\t*(($elem_type_str*)&${tmp_opt}.data) = *(($elem_type_str*)$tmp_opt_ptr);') g.writeln('} else {') - g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = v_error(_SLIT("array index out of range"));') + g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = _v_error(_SLIT("array index out of range"));') g.writeln('}') if !node.is_option { g.or_block(tmp_opt, node.or_expr, elem_type) @@ -441,7 +441,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { g.writeln('if ($tmp_opt_ptr) {') g.writeln('\t*(($elem_type_str*)&${tmp_opt}.data) = *(($elem_type_str*)$tmp_opt_ptr);') g.writeln('} else {') - g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = v_error(_SLIT("array index out of range"));') + g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = _v_error(_SLIT("array index out of range"));') g.writeln('}') if !node.is_option { g.or_block(tmp_opt, node.or_expr, elem_type) diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index b1317c8c98..5926da18da 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -50,7 +50,7 @@ $dec_fn_dec { if (error_ptr != NULL) { // fprintf(stderr, "Error in decode() for $styp error_ptr=: %s\\n", error_ptr); // printf("\\nbad js=%%s\\n", js.str); - return (Option_$styp){.state = 2,.err = v_error(tos2((byteptr)error_ptr)),.data = {0}}; + return (Option_$styp){.state = 2,.err = _v_error(tos2((byteptr)error_ptr)),.data = {0}}; } } ') @@ -181,7 +181,7 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s if field_sym.name == 'time.Time' { // time struct requires special treatment // it has to be encoded as a unix timestamp number - enc.writeln('\tcJSON_AddItemToObject(o, "$name", json__encode_u64(val.${c_name(field.name)}.v_unix));') + enc.writeln('\tcJSON_AddItemToObject(o, "$name", json__encode_u64(val.${c_name(field.name)}._v_unix));') } else { enc.writeln('\tcJSON_AddItemToObject(o, "$name", ${enc_name}(val.${c_name(field.name)}));\n') } @@ -225,7 +225,7 @@ fn (mut g Gen) decode_array(value_type ast.Type) string { noscan := g.check_noscan(value_type) return ' if(root && !cJSON_IsArray(root) && !cJSON_IsNull(root)) { - return (Option_Array_$styp){.state = 2, .err = v_error(string__plus(_SLIT("Json element is not an array: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; + return (Option_Array_$styp){.state = 2, .err = _v_error(string__plus(_SLIT("Json element is not an array: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; } res = __new_array${noscan}(0, 0, sizeof($styp)); const cJSON *jsval = NULL; @@ -269,7 +269,7 @@ fn (mut g Gen) decode_map(key_type ast.Type, value_type ast.Type) string { } return ' if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) { - return (Option_Map_${styp}_$styp_v){ .state = 2, .err = v_error(string__plus(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; + return (Option_Map_${styp}_$styp_v){ .state = 2, .err = _v_error(string__plus(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}}; } res = new_map(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn); cJSON *jsval = NULL; diff --git a/vlib/v/tests/reserved_keywords_can_be_escaped_with_at_test.v b/vlib/v/tests/reserved_keywords_can_be_escaped_with_at_test.v index 7bef16fd40..c03c47c610 100644 --- a/vlib/v/tests/reserved_keywords_can_be_escaped_with_at_test.v +++ b/vlib/v/tests/reserved_keywords_can_be_escaped_with_at_test.v @@ -4,3 +4,10 @@ fn test_reserved_keywords() { @union := 'xyz' assert [@continue, @sizeof, @union] == ['abc', 'def', 'xyz'] } + +fn test_duplicated_name() { + // should just compile + // @for was compiled to v_for before, now it's _v_for + @for := 0 + v_for := 0 +}