cgen: fix string frees in calls

pull/5570/head
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_vweb_tmpl bool
inside_return bool inside_return bool
strs_to_free string strs_to_free string
inside_call bool
} }
const ( const (
@ -598,7 +599,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
defer { defer {
// If have temporary string exprs to free after this statement, do it. e.g.: // 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);` // `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 != '' { if g.strs_to_free != '' {
g.writeln(g.strs_to_free) g.writeln(g.strs_to_free)
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 { if node.should_be_skipped {
return return
} }
g.inside_call = true
defer {g.inside_call = false}
gen_or := node.or_block.kind != .absent gen_or := node.or_block.kind != .absent
cur_line := if gen_or && g.is_assign_rhs { cur_line := if gen_or && g.is_assign_rhs {
line := g.go_before_stmt(0) 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) { fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) {
mut cur_line := '' mut cur_line := ''
mut tmp := '' 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.inside_ternary == 0 && g.cur_fn != 0 &&
g.cur_fn.name != '' g.cur_fn.name != ''
if free { 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 = ') // g.insert_before_stmt('// str tmp var\nstring $tmp = ')
cur_line = g.go_before_stmt(0) cur_line = g.go_before_stmt(0)
g.writeln('// free _str') g.writeln('// free _str2 $g.inside_call')
g.write('string $tmp = ') g.write('string $tmp = ')
g.strs_to_free += 'string_free(&$tmp); /*tmp str*/' g.strs_to_free += 'string_free(&$tmp); /*tmp str*/'
} }