cgen: cleanup generating helper comments with -skip-unused

pull/8719/head
Delyan Angelov 2021-02-13 10:31:48 +02:00
parent ebb5305b27
commit 0b60510c9c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 77 additions and 65 deletions

View File

@ -1045,60 +1045,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
} }
} }
ast.FnDecl { ast.FnDecl {
g.gen_attrs(node.attrs) g.process_fn_decl(node)
// g.tmp_count = 0 TODO
mut skip := false
pos := g.out.buf.len
should_bundle_module := util.should_bundle_module(node.mod)
if g.pref.build_mode == .build_module {
// if node.name.contains('parse_text') {
// println('!!! $node.name mod=$node.mod, built=$g.module_built')
// }
// TODO true for not just "builtin"
// TODO: clean this up
mod := if g.is_builtin_mod { 'builtin' } else { node.name.all_before_last('.') }
if (mod != g.module_built && node.mod != g.module_built.after('/'))
|| should_bundle_module {
// Skip functions that don't have to be generated for this module.
// println('skip bm $node.name mod=$node.mod module_built=$g.module_built')
skip = true
}
if g.is_builtin_mod && g.module_built == 'builtin' && node.mod == 'builtin' {
skip = false
}
if !skip && g.pref.is_verbose {
println('build module `$g.module_built` fn `$node.name`')
}
}
if g.pref.use_cache {
// We are using prebuilt modules, we do not need to generate
// their functions in main.c.
if node.mod != 'main' && node.mod != 'help' && !should_bundle_module
&& !g.pref.is_test && node.generic_params.len == 0 {
skip = true
}
}
keep_fn_decl := g.fn_decl
g.fn_decl = &node
if node.name == 'main.main' {
g.has_main = true
}
if node.name == 'backtrace' || node.name == 'backtrace_symbols'
|| node.name == 'backtrace_symbols_fd' {
g.write('\n#ifndef __cplusplus\n')
}
g.gen_fn_decl(node, skip)
if node.name == 'backtrace' || node.name == 'backtrace_symbols'
|| node.name == 'backtrace_symbols_fd' {
g.write('\n#endif\n')
}
g.fn_decl = keep_fn_decl
if skip {
g.out.go_back_to(pos)
}
if node.language != .c {
g.writeln('')
}
} }
ast.ForCStmt { ast.ForCStmt {
prev_branch_parent_pos := g.branch_parent_pos prev_branch_parent_pos := g.branch_parent_pos

View File

@ -7,6 +7,82 @@ import v.ast
import v.table import v.table
import v.util import v.util
fn (mut g Gen) is_used_by_main(node ast.FnDecl) bool {
mut is_used_by_main := true
if g.pref.skip_unused {
fkey := if node.is_method { '${int(node.receiver.typ)}.$node.name' } else { node.name }
is_used_by_main = g.table.used_fns[fkey]
$if trace_skip_unused_fns ? {
println('> is_used_by_main: $is_used_by_main | node.name: $node.name | fkey: $fkey | node.is_method: $node.is_method')
}
if !is_used_by_main {
$if trace_skip_unused_fns_in_c_code ? {
g.writeln('// fn $node.name UNUSED')
}
}
}
return is_used_by_main
}
fn (mut g Gen) process_fn_decl(node ast.FnDecl) {
if !g.is_used_by_main(node) {
return
}
g.gen_attrs(node.attrs)
// g.tmp_count = 0 TODO
mut skip := false
pos := g.out.buf.len
should_bundle_module := util.should_bundle_module(node.mod)
if g.pref.build_mode == .build_module {
// if node.name.contains('parse_text') {
// println('!!! $node.name mod=$node.mod, built=$g.module_built')
// }
// TODO true for not just "builtin"
// TODO: clean this up
mod := if g.is_builtin_mod { 'builtin' } else { node.name.all_before_last('.') }
if (mod != g.module_built && node.mod != g.module_built.after('/')) || should_bundle_module {
// Skip functions that don't have to be generated for this module.
// println('skip bm $node.name mod=$node.mod module_built=$g.module_built')
skip = true
}
if g.is_builtin_mod && g.module_built == 'builtin' && node.mod == 'builtin' {
skip = false
}
if !skip && g.pref.is_verbose {
println('build module `$g.module_built` fn `$node.name`')
}
}
if g.pref.use_cache {
// We are using prebuilt modules, we do not need to generate
// their functions in main.c.
if node.mod != 'main' && node.mod != 'help' && !should_bundle_module && !g.pref.is_test
&& node.generic_params.len == 0 {
skip = true
}
}
keep_fn_decl := g.fn_decl
g.fn_decl = &node
if node.name == 'main.main' {
g.has_main = true
}
if node.name == 'backtrace' || node.name == 'backtrace_symbols'
|| node.name == 'backtrace_symbols_fd' {
g.write('\n#ifndef __cplusplus\n')
}
g.gen_fn_decl(node, skip)
if node.name == 'backtrace' || node.name == 'backtrace_symbols'
|| node.name == 'backtrace_symbols_fd' {
g.write('\n#endif\n')
}
g.fn_decl = keep_fn_decl
if skip {
g.out.go_back_to(pos)
}
if node.language != .c {
g.writeln('')
}
}
fn (mut g Gen) gen_fn_decl(node ast.FnDecl, skip bool) { fn (mut g Gen) gen_fn_decl(node ast.FnDecl, skip bool) {
// TODO For some reason, build fails with autofree with this line // TODO For some reason, build fails with autofree with this line
// as it's only informative, comment it for now // as it's only informative, comment it for now
@ -27,17 +103,6 @@ fn (mut g Gen) gen_fn_decl(node ast.FnDecl, skip bool) {
} }
} }
*/ */
if g.pref.skip_unused {
fkey := if node.is_method { '${int(node.receiver.typ)}.$node.name' } else { node.name }
is_used_by_main := g.table.used_fns[fkey]
$if trace_skip_unused_fns ? {
println('> is_used_by_main: $is_used_by_main | node.name: $node.name | fkey: $fkey | node.is_method: $node.is_method')
}
if !is_used_by_main {
g.writeln('// fn $node.name UNUSED')
return
}
}
g.returned_var_name = '' g.returned_var_name = ''
// //