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

native, markused: implement -skip-unused (#18036)

This commit is contained in:
Spydr 2023-04-24 10:41:12 +02:00 committed by GitHub
parent d8167b8966
commit 7ac7020192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 114 deletions

View File

@ -945,13 +945,25 @@ pub fn (mut g Gen) gen_print_from_expr(expr ast.Expr, typ ast.Type, name string)
}
}
fn (mut g Gen) is_used_by_main(node ast.FnDecl) bool {
mut used := true
if g.pref.skip_unused {
fkey := node.fkey()
used = g.table.used_fns[fkey]
}
return used
}
fn (mut g Gen) fn_decl(node ast.FnDecl) {
name := if node.is_method {
'${g.table.get_type_name(node.receiver.typ)}.${node.name}'
} else {
node.name
}
if node.no_body {
if node.no_body || !g.is_used_by_main(node) {
if g.pref.is_verbose {
println(term.italic(term.green('\n-> skipping unused function `${name}`')))
}
return
}
if g.pref.is_verbose {

View File

@ -40,7 +40,7 @@ fn test_native() {
work_test_path := '${wrkdir}/${test_file_name}'
exe_test_path := '${wrkdir}/${test_file_name}.exe'
tmperrfile := '${dir}/${test}.tmperr'
cmd := '${os.quoted_path(vexe)} -o ${os.quoted_path(exe_test_path)} -b native ${os.quoted_path(full_test_path)} -d custom_define 2> ${os.quoted_path(tmperrfile)}'
cmd := '${os.quoted_path(vexe)} -o ${os.quoted_path(exe_test_path)} -b native -skip-unused ${os.quoted_path(full_test_path)} -d custom_define 2> ${os.quoted_path(tmperrfile)}'
if is_verbose {
println(cmd)
}

View File

@ -14,118 +14,123 @@ pub fn mark_used(mut table ast.Table, pref_ &pref.Preferences, ast_files []&ast.
util.timing_measure(@METHOD)
}
// Functions that must be generated and can't be skipped
mut all_fn_root_names := [
'main.main',
'__new_array',
'str_intp',
'format_sb',
'__new_array_with_default',
'__new_array_with_multi_default',
'__new_array_with_array_default',
'init_global_allocator' /* needed for linux_bare and wasm_bare */,
'v_realloc' /* needed for _STR */,
'malloc',
'malloc_noscan',
'vcalloc',
'vcalloc_noscan',
'new_array_from_c_array',
'v_fixed_index',
'memdup',
'memdup_uncollectable',
'vstrlen',
'__as_cast',
'tos',
'tos2',
'tos3',
'isnil',
'_option_ok',
'_result_ok',
'error',
// utf8_str_visible_length is used by c/str.v
'utf8_str_visible_length',
'compare_ints',
'compare_u64s',
'compare_strings',
'compare_ints_reverse',
'compare_u64s_reverse',
'compare_strings_reverse',
'builtin_init',
// byteptr and charptr
'3.vstring',
'3.vstring_with_len',
'3.vstring_literal',
'4.vstring',
'4.vstring_with_len',
'4.vstring_literal',
// byte. methods
'10.str_escaped',
// string. methods
'20.add',
'20.trim_space',
'20.repeat',
'20.replace',
'20.clone',
'20.clone_static',
'20.trim',
'20.substr',
'20.substr_ni',
'20.at',
'20.at_with_check',
'20.index_kmp',
// string. ==, !=, etc...
'20.eq',
'20.ne',
'20.lt',
'20.gt',
'20.le',
'20.ge',
'fast_string_eq',
// other array methods
'22.get',
'22.set',
'22.get_unsafe',
'22.set_unsafe',
'22.get_with_check' /* used for `x := a[i] or {}` */,
'22.clone_static_to_depth',
'22.clone_to_depth',
'22.first',
'22.last',
'22.pointers' /* TODO: handle generic methods calling array primitives more precisely in pool_test.v */,
'22.reverse',
'22.repeat_to_depth',
'22.slice',
'22.slice_ni',
'22.slice2',
'61.get',
'61.set',
'65558.last',
'65558.pop',
'65558.push',
'65558.insert_many',
'65558.prepend_many',
'65558.reverse',
'65558.set',
'65558.set_unsafe',
// TODO: process the _vinit const initializations automatically too
'json.decode_string',
'json.decode_int',
'json.decode_bool',
'json.decode_u64',
'json.encode_int',
'json.encode_string',
'json.encode_bool',
'json.encode_u64',
'json.json_print',
'json.json_parse',
'main.nasserts',
'main.vtest_init',
'main.vtest_new_metainfo',
'main.vtest_new_filemetainfo',
'os.getwd',
'os.init_os_args',
'os.init_os_args_wide',
'v.embed_file.find_index_entry_by_path',
]
mut all_fn_root_names := if pref_.backend == .native {
// Note: this is temporary, until the native backend supports more features!
['main.main']
} else {
[
'main.main',
'__new_array',
'str_intp',
'format_sb',
'__new_array_with_default',
'__new_array_with_multi_default',
'__new_array_with_array_default',
'init_global_allocator' /* needed for linux_bare and wasm_bare */,
'v_realloc' /* needed for _STR */,
'malloc',
'malloc_noscan',
'vcalloc',
'vcalloc_noscan',
'new_array_from_c_array',
'v_fixed_index',
'memdup',
'memdup_uncollectable',
'vstrlen',
'__as_cast',
'tos',
'tos2',
'tos3',
'isnil',
'_option_ok',
'_result_ok',
'error',
// utf8_str_visible_length is used by c/str.v
'utf8_str_visible_length',
'compare_ints',
'compare_u64s',
'compare_strings',
'compare_ints_reverse',
'compare_u64s_reverse',
'compare_strings_reverse',
'builtin_init',
// byteptr and charptr
'3.vstring',
'3.vstring_with_len',
'3.vstring_literal',
'4.vstring',
'4.vstring_with_len',
'4.vstring_literal',
// byte. methods
'10.str_escaped',
// string. methods
'20.add',
'20.trim_space',
'20.repeat',
'20.replace',
'20.clone',
'20.clone_static',
'20.trim',
'20.substr',
'20.substr_ni',
'20.at',
'20.at_with_check',
'20.index_kmp',
// string. ==, !=, etc...
'20.eq',
'20.ne',
'20.lt',
'20.gt',
'20.le',
'20.ge',
'fast_string_eq',
// other array methods
'22.get',
'22.set',
'22.get_unsafe',
'22.set_unsafe',
'22.get_with_check' /* used for `x := a[i] or {}` */,
'22.clone_static_to_depth',
'22.clone_to_depth',
'22.first',
'22.last',
'22.pointers' /* TODO: handle generic methods calling array primitives more precisely in pool_test.v */,
'22.reverse',
'22.repeat_to_depth',
'22.slice',
'22.slice_ni',
'22.slice2',
'61.get',
'61.set',
'65558.last',
'65558.pop',
'65558.push',
'65558.insert_many',
'65558.prepend_many',
'65558.reverse',
'65558.set',
'65558.set_unsafe',
// TODO: process the _vinit const initializations automatically too
'json.decode_string',
'json.decode_int',
'json.decode_bool',
'json.decode_u64',
'json.encode_int',
'json.encode_string',
'json.encode_bool',
'json.encode_u64',
'json.json_print',
'json.json_parse',
'main.nasserts',
'main.vtest_init',
'main.vtest_new_metainfo',
'main.vtest_new_filemetainfo',
'os.getwd',
'os.init_os_args',
'os.init_os_args_wide',
'v.embed_file.find_index_entry_by_path',
]
}
if pref_.is_bare {
all_fn_root_names << [