Skip to content

Commit

Permalink
cgen: fix autofree inserting string declarations for multiple functio…
Browse files Browse the repository at this point in the history
…ns calls (#18723)
  • Loading branch information
walkingdevel committed Jul 3, 2023
1 parent e01d973 commit c48ae86
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -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 {
Expand All @@ -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 = []
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/fn.v
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/gen/c/text_manipulation.v
Expand Up @@ -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
}
}
}
13 changes: 13 additions & 0 deletions vlib/v/slow_tests/valgrind/multiple_fn_calls.v
@@ -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'))
}
1 change: 1 addition & 0 deletions vlib/v/slow_tests/valgrind/valgrind_test.v
Expand Up @@ -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',
Expand Down

0 comments on commit c48ae86

Please sign in to comment.