Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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