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

cgen: fix string frees in calls

This commit is contained in:
Alexander Medvednikov 2020-06-29 20:20:31 +02:00
parent ab37dcaa9c
commit 77e56aa3f9
3 changed files with 6 additions and 3 deletions

View File

@ -92,6 +92,7 @@ mut:
inside_vweb_tmpl bool
inside_return bool
strs_to_free string
inside_call bool
}
const (
@ -598,7 +599,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
defer {
// If have temporary string exprs to free after this statement, do it. e.g.:
// `foo('a' + 'b')` => `tmp := 'a' + 'b'; foo(tmp); string_free(&tmp);`
if false && g.pref.autofree {
if g.pref.autofree {
if g.strs_to_free != '' {
g.writeln(g.strs_to_free)
g.strs_to_free = ''

View File

@ -313,6 +313,8 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
if node.should_be_skipped {
return
}
g.inside_call = true
defer {g.inside_call = false}
gen_or := node.or_block.kind != .absent
cur_line := if gen_or && g.is_assign_rhs {
line := g.go_before_stmt(0)

View File

@ -142,7 +142,7 @@ fn (mut g Gen) string_literal(node ast.StringLiteral) {
fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
mut cur_line := ''
mut tmp := ''
free := g.pref.autofree && !g.inside_return &&
free := g.pref.autofree && g.inside_call && !g.inside_return &&
g.inside_ternary == 0 && g.cur_fn != 0 &&
g.cur_fn.name != ''
if free {
@ -157,7 +157,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
*/
// g.insert_before_stmt('// str tmp var\nstring $tmp = ')
cur_line = g.go_before_stmt(0)
g.writeln('// free _str')
g.writeln('// free _str2 $g.inside_call')
g.write('string $tmp = ')
g.strs_to_free += 'string_free(&$tmp); /*tmp str*/'
}