gen: fix C keyword escape to _v_ instead of just v_ (#10876)

pull/10353/head
Daniel Däschle 2021-07-21 10:46:04 +02:00 committed by GitHub
parent 591af866ba
commit aebb028349
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 21 additions and 14 deletions

View File

@ -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)

View File

@ -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('}')
}
}

View File

@ -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);')

View File

@ -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)

View File

@ -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)

View File

@ -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;

View File

@ -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
}