From eaaac220beb5d6388398de7af46d3fc4f2dea44f Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 13 Feb 2021 15:47:09 +0200 Subject: [PATCH] checker: make -skip-unused more conservative to make `v test vlib/builtin` pass --- vlib/v/checker/mark_used.v | 57 ++++++++++++++++++++++-- vlib/v/checker/mark_used_walker/walker.v | 24 ++++++++-- vlib/v/gen/c/fn.v | 7 ++- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/vlib/v/checker/mark_used.v b/vlib/v/checker/mark_used.v index e53231b57c..841042f705 100644 --- a/vlib/v/checker/mark_used.v +++ b/vlib/v/checker/mark_used.v @@ -39,6 +39,7 @@ fn (mut c Checker) mark_used(ast_files []ast.File) { 'main.main', '__new_array', '__new_array_with_default', + '__new_array_with_array_default', 'new_array_from_c_array', 'memdup', 'vstrlen', @@ -46,19 +47,41 @@ fn (mut c Checker) mark_used(ast_files []ast.File) { 'tos2', 'tos3', 'isnil', + 'opt_ok2', /* 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', '4.vstring', '4.vstring_with_len', + '5.str', /* i8_str */ + '6.str', /* i16_str */ + '7.str', /* int_str */ + '8.str', /* i64_str */ + '9.str', /* byte_str */ + '10.str', /* u16_str */ + '11.str', /* u32_str */ + '12.str', /* u64_str */ + '13.str', /* f32_str */ + '14.str', /* f64_str */ + '15.str', /* char_str */ + '16.str', /* bool_str */ + /* byte. methods */ + '9.str_escaped', /* string. methods */ '18.add', '18.trim_space', '18.replace', '18.clone', + '18.clone_static', '18.trim', '18.substr', '18.at', @@ -78,16 +101,31 @@ fn (mut c Checker) mark_used(ast_files []ast.File) { '19.le', '19.ge', '19.add', + '19.str', + '20.str', /* other array methods */ '21.get', '21.set', '21.get_unsafe', '21.set_unsafe', + '21.clone_static', + '21.first', + '21.last', + '21.reverse', + '21.repeat', '21.slice', '21.slice2', + '26.str', /* float_literal_str */ + '27.str', /* int_literal_str */ + '29.str', /* array_string_str */ '59.get', '59.set', + '65557.last', + '65557.pop', '65557.push', + '65557.insert_many', + '65557.prepend_many', + '65557.reverse', '65557.set', '65557.set_unsafe', /* TODO: process the _vinit const initializations automatically too */ @@ -108,9 +146,19 @@ fn (mut c Checker) mark_used(ast_files []ast.File) { if k.ends_with('.free') { all_fn_root_names << k } - if c.pref.is_test && (k.starts_with('test_') || k.contains('.test_')) { + if k.ends_with('.lock') || k.ends_with('.unlock') || k.ends_with('.rlock') + || k.ends_with('.runlock') { all_fn_root_names << k } + if c.pref.is_test { + if k.starts_with('test_') || k.contains('.test_') { + all_fn_root_names << k + } + if k.starts_with('testsuite_') || k.contains('.testsuite_') { + eprintln('>>> test suite: $k') + all_fn_root_names << k + } + } if sb_mut_type != '' && k.starts_with(sb_mut_type) { all_fn_root_names << k } @@ -144,8 +192,11 @@ fn (mut c Checker) mark_used(ast_files []ast.File) { } if walker.n_maps > 0 { for k, mut mfn in all_fns { - if k == 'new_map_2' || k.starts_with('map_') || k.ends_with('set_1') - || k.ends_with('exists_1') || k.ends_with('get_1') { + if k in ['new_map_2', 'new_map_init_2'] + || (k.starts_with('map_') || k.ends_with('clone') || k.ends_with('exists_1') + || k.ends_with('keys') || k.ends_with('keys_1') || k.ends_with('get_1') + || k.ends_with('set_1') || k.ends_with('key') || k.ends_with('value') + || k.ends_with('has_index') || k.ends_with('expand') || k.ends_with('zeros_to_end')) { walker.fn_decl(mut mfn) } } diff --git a/vlib/v/checker/mark_used_walker/walker.v b/vlib/v/checker/mark_used_walker/walker.v index 726f2b8902..af8a4a32a0 100644 --- a/vlib/v/checker/mark_used_walker/walker.v +++ b/vlib/v/checker/mark_used_walker/walker.v @@ -204,8 +204,17 @@ fn (mut w Walker) expr(node ast.Expr) { } } ast.Ident { - if node.kind == .constant { - w.mark_const_as_used(node.name) + match node.kind { + .constant { + w.mark_const_as_used(node.name) + } + .function { + w.fn_by_name(node.name) + } + else { + // `.unresolved`, `.blank_ident`, `.variable`, `.global`, `.function` + // println('>>> else, ast.Ident kind: $node.kind') + } } } ast.Likely { @@ -347,15 +356,24 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) { if w.used_fns[fn_name] { return } + w.mark_fn_as_used(fn_name) stmt := w.all_fns[fn_name] or { return } if stmt.name == node.name { if !node.is_method || (node.receiver_type == stmt.receiver.typ) { - w.mark_fn_as_used(fn_name) w.stmts(stmt.stmts) } } } +pub fn (mut w Walker) fn_by_name(fn_name string) { + if w.used_fns[fn_name] { + return + } + stmt := w.all_fns[fn_name] or { return } + w.mark_fn_as_used(fn_name) + w.stmts(stmt.stmts) +} + pub fn (mut w Walker) struct_fields(sfields []ast.StructField) { for sf in sfields { if sf.has_default_expr { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index b3b778687e..59964fce9a 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -17,9 +17,14 @@ fn (mut g Gen) is_used_by_main(node ast.FnDecl) bool { } if !is_used_by_main { $if trace_skip_unused_fns_in_c_code ? { - g.writeln('// fn $node.name UNUSED') + g.writeln('// trace_skip_unused_fns_in_c_code, $node.name, fkey: $fkey') } } + } else { + $if trace_skip_unused_fns_in_c_code ? { + fkey := if node.is_method { '${int(node.receiver.typ)}.$node.name' } else { node.name } + g.writeln('// trace_skip_unused_fns_in_c_code, $node.name, fkey: $fkey') + } } return is_used_by_main }