v.gen.c: implement `-d trace_autofree` for conditional tracing comments about autofree

pull/10086/head
Delyan Angelov 2021-05-11 19:29:06 +03:00
parent 274c817028
commit 066374bae4
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 17 additions and 12 deletions

View File

@ -1034,8 +1034,8 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
stmt := stmts[0] stmt := stmts[0]
// stmt := stmts[stmts.len-1] // stmt := stmts[stmts.len-1]
if stmt !is ast.FnDecl && g.inside_ternary == 0 { if stmt !is ast.FnDecl && g.inside_ternary == 0 {
// g.writeln('// autofree scope') // g.trace_autofree('// autofree scope')
// g.writeln('// autofree_scope_vars($stmt.pos.pos) | ${typeof(stmt)}') // g.trace_autofree('// autofree_scope_vars($stmt.pos.pos) | ${typeof(stmt)}')
// go back 1 position is important so we dont get the // go back 1 position is important so we dont get the
// internal scope of for loops and possibly other nodes // internal scope of for loops and possibly other nodes
// g.autofree_scope_vars(stmt.pos.pos - 1) // g.autofree_scope_vars(stmt.pos.pos - 1)
@ -1116,7 +1116,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} else { } else {
// continue or break // continue or break
if g.is_autofree && !g.is_builtin_mod { if g.is_autofree && !g.is_builtin_mod {
g.writeln('// free before continue/break') g.trace_autofree('// free before continue/break')
g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true, g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true,
g.branch_parent_pos) g.branch_parent_pos)
} }
@ -2719,11 +2719,16 @@ fn (mut g Gen) autofree_scope_vars_stop(pos int, line_nr int, free_parent_scopes
// TODO why can scope.pos be 0? (only outside fns?) // TODO why can scope.pos be 0? (only outside fns?)
return return
} }
g.writeln('// autofree_scope_vars(pos=$pos line_nr=$line_nr scope.pos=$scope.start_pos scope.end_pos=$scope.end_pos)') g.trace_autofree('// autofree_scope_vars(pos=$pos line_nr=$line_nr scope.pos=$scope.start_pos scope.end_pos=$scope.end_pos)')
g.autofree_scope_vars2(scope, scope.start_pos, scope.end_pos, line_nr, free_parent_scopes, g.autofree_scope_vars2(scope, scope.start_pos, scope.end_pos, line_nr, free_parent_scopes,
stop_pos) stop_pos)
} }
[if trace_autofree]
fn (mut g Gen) trace_autofree(line string) {
g.writeln(line)
}
// fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, end_pos int) { // fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, end_pos int) {
fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int, line_nr int, free_parent_scopes bool, stop_pos int) { fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int, line_nr int, free_parent_scopes bool, stop_pos int) {
if isnil(scope) { if isnil(scope) {
@ -2732,20 +2737,20 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
for _, obj in scope.objects { for _, obj in scope.objects {
match obj { match obj {
ast.Var { ast.Var {
g.writeln('// var "$obj.name" var.pos=$obj.pos.pos var.line_nr=$obj.pos.line_nr') g.trace_autofree('// var "$obj.name" var.pos=$obj.pos.pos var.line_nr=$obj.pos.line_nr')
if obj.name == g.returned_var_name { if obj.name == g.returned_var_name {
g.writeln('// skipping returned var') g.trace_autofree('// skipping returned var')
continue continue
} }
if obj.is_or { if obj.is_or {
// Skip vars inited with the `or {}`, since they are generated // Skip vars inited with the `or {}`, since they are generated
// after the or block in C. // after the or block in C.
g.writeln('// skipping `or{}` var "$obj.name"') g.trace_autofree('// skipping `or{}` var "$obj.name"')
continue continue
} }
if obj.is_tmp { if obj.is_tmp {
// Skip for loop vars // Skip for loop vars
g.writeln('// skipping tmp var "$obj.name"') g.trace_autofree('// skipping tmp var "$obj.name"')
continue continue
} }
// if var.typ == 0 { // if var.typ == 0 {
@ -2778,7 +2783,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
// if !isnil(scope.parent) && line_nr > 0 { // if !isnil(scope.parent) && line_nr > 0 {
if free_parent_scopes && !isnil(scope.parent) if free_parent_scopes && !isnil(scope.parent)
&& (stop_pos == -1 || scope.parent.start_pos >= stop_pos) { && (stop_pos == -1 || scope.parent.start_pos >= stop_pos) {
g.writeln('// af parent scope:') g.trace_autofree('// af parent scope:')
g.autofree_scope_vars2(scope.parent, start_pos, end_pos, line_nr, true, stop_pos) g.autofree_scope_vars2(scope.parent, start_pos, end_pos, line_nr, true, stop_pos)
} }
} }
@ -2801,7 +2806,7 @@ fn (mut g Gen) autofree_variable(v ast.Var) {
// Don't free simple string literals. // Don't free simple string literals.
match v.expr { match v.expr {
ast.StringLiteral { ast.StringLiteral {
g.writeln('// str literal') g.trace_autofree('// str literal')
} }
else { else {
// NOTE/TODO: assign_stmt multi returns variables have no expr // NOTE/TODO: assign_stmt multi returns variables have no expr
@ -4771,7 +4776,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
g.writeln('return ($styp){0};') g.writeln('return ($styp){0};')
} else { } else {
if g.is_autofree { if g.is_autofree {
g.writeln('// free before return (no values returned)') g.trace_autofree('// free before return (no values returned)')
g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true) g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true)
} }
g.writeln('return;') g.writeln('return;')

View File

@ -65,7 +65,7 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) {
} }
return return
} }
g.writeln('// \$method call. sym="$node.sym.name"') g.trace_autofree('// \$method call. sym="$node.sym.name"')
if node.method_name == 'method' { if node.method_name == 'method' {
// `app.$method()` // `app.$method()`
m := node.sym.find_method(g.comp_for_method) or { return } m := node.sym.find_method(g.comp_for_method) or { return }