1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix autofree inserting string declarations for multiple functions calls (#18723)

This commit is contained in:
Mark aka walkingdevel 2023-07-03 05:01:55 +00:00 committed by GitHub
parent e01d973c27
commit c48ae86132
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 3 deletions

View File

@ -3121,7 +3121,12 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.writeln('(${shared_styp}*)__dup${shared_styp}(&(${shared_styp}){.mtx = {0}, .val =')
}
}
last_stmt_pos := if g.stmt_path_pos.len > 0 { g.stmt_path_pos.last() } else { 0 }
stmt_before_call_expr_pos := if g.stmt_path_pos.len > 0 {
g.stmt_path_pos.last()
} else {
0
}
g.call_expr(node)
if g.is_autofree && !g.is_builtin_mod && !g.is_js_call && g.strs_to_free0.len == 0
&& !g.inside_lambda {
@ -3130,7 +3135,8 @@ fn (mut g Gen) expr(node_ ast.Expr) {
// so just skip it
g.autofree_call_pregen(node)
if g.strs_to_free0.len > 0 {
g.insert_at(last_stmt_pos, g.strs_to_free0.join('\n') + '/* inserted before */')
g.insert_at(stmt_before_call_expr_pos, g.strs_to_free0.join('\n') +
'/* inserted before */')
}
g.strs_to_free0 = []
}

View File

@ -2021,7 +2021,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
fn_name := node.name.replace('.', '_')
// name := '_tt${g.tmp_count_af}_arg_expr_${fn_name}_$i'
name := '_arg_expr_${fn_name}_${i + 1}_${node.pos.pos}'
g.write('/*af arg*/' + name)
g.write('/*autofree arg*/' + name)
}
} else {
g.ref_or_deref_arg(arg, expected_types[i], node.language)

View File

@ -84,4 +84,14 @@ fn (mut g Gen) insert_at(pos int, s string) {
// g.out_parallel[g.out_idx].cut_to(pos)
g.writeln(s)
g.write(cur_line)
// After modifying the code in the buffer, we need to adjust the positions of the statements
// to account for the added line of code.
// This is necessary to ensure that autofree can properly insert string declarations
// in the correct positions, considering the surgically made changes.
for index, stmt_pos in g.stmt_path_pos {
if stmt_pos >= pos {
g.stmt_path_pos[index] += s.len + 1
}
}
}

View File

@ -0,0 +1,13 @@
import strconv
fn color_code_to_rgb(color string) []int {
clr := color.replace('#', '')
return [int(strconv.parse_int(clr[0..2], 16, 0) or { return [0, 0, 0] }),
int(strconv.parse_int(clr[2..4], 16, 0) or { return [0, 0, 0] }),
int(strconv.parse_int(clr[4..6],
16, 0) or { return [0, 0, 0] })]
}
fn main() {
dump(color_code_to_rgb('#abcdef'))
}

View File

@ -27,6 +27,7 @@ const skip_valgrind_files = [
'vlib/v/slow_tests/valgrind/struct_field.v',
'vlib/v/slow_tests/valgrind/fn_returning_string_param.v',
'vlib/v/slow_tests/valgrind/fn_with_return_should_free_local_vars.v',
'vlib/v/slow_tests/valgrind/multiple_fn_calls.v',
'vlib/v/slow_tests/valgrind/option_simple.v',
'vlib/v/slow_tests/valgrind/string_plus_string_plus.v',
'vlib/v/slow_tests/valgrind/import_x_json2.v',