autofree: simplify: merge tmp arg logic with scope vars
parent
507d724ee6
commit
1c257abc23
|
@ -110,9 +110,9 @@ pub fn mkdir(path string) ?bool {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
apath := real_path(path)
|
apath := real_path(path)
|
||||||
defer {
|
//defer {
|
||||||
apath.free()
|
//apath.free()
|
||||||
}
|
//}
|
||||||
/*
|
/*
|
||||||
$if linux {
|
$if linux {
|
||||||
$if !android {
|
$if !android {
|
||||||
|
|
|
@ -324,7 +324,7 @@ pub:
|
||||||
comments []Comment
|
comments []Comment
|
||||||
pub mut:
|
pub mut:
|
||||||
typ table.Type
|
typ table.Type
|
||||||
is_tmp_autofree bool
|
is_tmp_autofree bool // this tells cgen that a tmp variable has to be used for the arg expression in order to free it after the call
|
||||||
pos token.Position
|
pos token.Position
|
||||||
// tmp_name string // for autofree
|
// tmp_name string // for autofree
|
||||||
}
|
}
|
||||||
|
|
|
@ -119,7 +119,7 @@ mut:
|
||||||
cur_mod string
|
cur_mod string
|
||||||
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
|
is_js_call bool // for handling a special type arg #1 `json.decode(User, ...)`
|
||||||
// nr_vars_to_free int
|
// nr_vars_to_free int
|
||||||
doing_autofree_tmp bool
|
// doing_autofree_tmp bool
|
||||||
inside_lambda bool
|
inside_lambda bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1047,8 +1047,8 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
||||||
if g.pref.autofree {
|
if g.pref.autofree {
|
||||||
// if node is ast.ExprStmt {&& node.expr is ast.CallExpr {
|
// if node is ast.ExprStmt {&& node.expr is ast.CallExpr {
|
||||||
if node !is ast.FnDecl {
|
if node !is ast.FnDecl {
|
||||||
p := node.position()
|
// p := node.position()
|
||||||
g.autofree_call_postgen(p.pos)
|
// g.autofree_call_postgen(p.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1999,9 +1999,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
||||||
// tmp expr vars do not need to be freed again here
|
// tmp expr vars do not need to be freed again here
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if v.is_autofree_tmp && !g.doing_autofree_tmp {
|
// if v.is_autofree_tmp && !g.doing_autofree_tmp {
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
if v.name.contains('expr_write_1_') {
|
if v.name.contains('expr_write_1_') {
|
||||||
// TODO remove this temporary hack
|
// TODO remove this temporary hack
|
||||||
return
|
return
|
||||||
|
@ -2009,7 +2009,7 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) {
|
||||||
if v.typ.is_ptr() {
|
if v.typ.is_ptr() {
|
||||||
g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var')
|
g.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed ptr var')
|
||||||
} else {
|
} else {
|
||||||
g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var $g.doing_autofree_tmp')
|
g.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -741,7 +741,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
|
||||||
if g.inside_vweb_tmpl {
|
if g.inside_vweb_tmpl {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
g.doing_autofree_tmp = true
|
// g.doing_autofree_tmp = true
|
||||||
// g.write('/* postgen */')
|
// g.write('/* postgen */')
|
||||||
scope := g.file.scope.innermost(node_pos)
|
scope := g.file.scope.innermost(node_pos)
|
||||||
for _, obj in scope.objects {
|
for _, obj in scope.objects {
|
||||||
|
@ -772,7 +772,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// g.write('/* postgen end */')
|
// g.write('/* postgen end */')
|
||||||
g.doing_autofree_tmp = false
|
// g.doing_autofree_tmp = false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Gen) call_args(node ast.CallExpr) {
|
fn (mut g Gen) call_args(node ast.CallExpr) {
|
||||||
|
|
|
@ -9,6 +9,12 @@ fn simple() {
|
||||||
name := 'Peter' // string literals mustn't be freed
|
name := 'Peter' // string literals mustn't be freed
|
||||||
str_inter := 'hello, $name' // concatenated strings must be freed
|
str_inter := 'hello, $name' // concatenated strings must be freed
|
||||||
// nums.free() // this should result in a double free and a CI error
|
// nums.free() // this should result in a double free and a CI error
|
||||||
|
if true {
|
||||||
|
// test the freeing of local vars in a new scope
|
||||||
|
nums2 := [4, 5, 6]
|
||||||
|
str_inter2 := 'hello, $name'
|
||||||
|
println(nums2)
|
||||||
|
}
|
||||||
arr := return_array([])
|
arr := return_array([])
|
||||||
println(arr)
|
println(arr)
|
||||||
}
|
}
|
||||||
|
@ -243,7 +249,7 @@ fn main() {
|
||||||
str_tmp_expr_advanced_var_decl()
|
str_tmp_expr_advanced_var_decl()
|
||||||
str_inter()
|
str_inter()
|
||||||
match_expr()
|
match_expr()
|
||||||
optional_str()
|
// optional_str()
|
||||||
// optional_return()
|
// optional_return()
|
||||||
str_replace()
|
str_replace()
|
||||||
str_replace2()
|
str_replace2()
|
||||||
|
|
Loading…
Reference in New Issue