Skip to content

Commit

Permalink
markused: minor cleanup (#23066)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Dec 5, 2024
1 parent e8e6c76 commit feade24
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
134 changes: 65 additions & 69 deletions vlib/v/markused/markused.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
ref_array_idx_str := '${int(ast.array_type.ref())}'
is_gc_none := pref_.gc_mode == .no_gc

mut include_panic_deps := false
mut core_fns := [
'main.main',
'init_global_allocator', // needed for linux_bare and wasm_bare
'v_realloc', // needed for _STR
'memdup',
'tos',
'tos2',
Expand All @@ -46,26 +46,14 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
$if debug_used_features ? {
dump(table.used_features)
}
panic_deps := [
'__new_array_with_default',
'__new_array_with_default_noscan',
'str_intp',
ref_array_idx_str + '.push',
ref_array_idx_str + '.push_noscan',
string_idx_str + '.substr',
array_idx_str + '.slice',
array_idx_str + '.get',
'v_fixed_index',
charptr_idx_str + '.vstring_literal',
]
if ast.float_literal_type.idx() in table.used_features.print_types
|| ast.f64_type_idx in table.used_features.print_types
|| ast.f32_type_idx in table.used_features.print_types {
core_fns << panic_deps
include_panic_deps = true
}
$if windows {
if 'no_backtrace' !in pref_.compile_defines {
core_fns << panic_deps
include_panic_deps = true
}
} $else {
if 'use_libbacktrace' in pref_.compile_defines {
Expand All @@ -77,13 +65,13 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << ref_array_idx_str + '.pop'
}
if table.used_features.used_modules.len > 0 {
core_fns << panic_deps
include_panic_deps = true
}
if pref_.autofree {
core_fns << string_idx_str + '.clone_static'
}
if table.used_features.as_cast || table.used_features.auto_str || pref_.is_shared {
core_fns << panic_deps
include_panic_deps = true
core_fns << 'isnil'
core_fns << '__new_array'
core_fns << '__new_array_noscan'
Expand All @@ -101,7 +89,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << charptr_idx_str + '.vstring_literal'
}
if table.used_features.index || pref_.is_shared {
core_fns << panic_deps
include_panic_deps = true
core_fns << string_idx_str + '.at_with_check'
core_fns << string_idx_str + '.clone'
core_fns << string_idx_str + '.clone_static'
Expand All @@ -128,8 +116,8 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << 'tos3'
}
if table.used_features.auto_str_ptr {
include_panic_deps = true
core_fns << 'isnil'
core_fns << panic_deps
}
if table.used_features.arr_prepend {
core_fns << ref_array_idx_str + '.prepend_many'
Expand All @@ -147,7 +135,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << array_idx_str + '.last'
}
if table.used_features.arr_delete {
core_fns << panic_deps
include_panic_deps = true
}
if table.used_features.arr_insert {
if is_gc_none {
Expand All @@ -156,13 +144,13 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}
if pref_.ccompiler_type != .tinyc && 'no_backtrace' !in pref_.compile_defines {
// with backtrace on gcc/clang more code needs be generated
core_fns << panic_deps
include_panic_deps = true
}
if table.used_features.interpolation {
core_fns << panic_deps
include_panic_deps = true
}
if table.used_features.dump {
core_fns << panic_deps
include_panic_deps = true
builderptr_idx := int(table.find_type('strings.Builder').ref())
core_fns << [
'${builderptr_idx}.str',
Expand All @@ -171,7 +159,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
]
}
if table.used_features.arr_init || table.used_features.comptime_for {
core_fns << panic_deps
include_panic_deps = true
core_fns << '__new_array'
core_fns << 'new_array_from_c_array'
core_fns << 'new_array_from_c_array_noscan'
Expand All @@ -180,10 +168,10 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << '__new_array_with_array_default'
}
if table.used_features.option_or_result {
include_panic_deps = true
core_fns << '_option_ok'
core_fns << '_result_ok'
core_fns << charptr_idx_str + '.vstring_literal'
core_fns << panic_deps
}
if table.used_features.as_cast {
core_fns << '__as_cast'
Expand All @@ -192,7 +180,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << 'memdup_uncollectable'
}
if table.used_features.arr_map {
core_fns << panic_deps
include_panic_deps = true
core_fns << '__new_array_with_map_default'
core_fns << 'new_map_noscan_key'
core_fns << ref_map_idx_str + '.clone'
Expand All @@ -201,24 +189,38 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
table.used_features.used_maps++
}
if table.used_features.map_update {
core_fns << panic_deps
include_panic_deps = true
core_fns << 'new_map_update_init'
table.used_features.used_maps++
}
if table.used_features.asserts {
core_fns << panic_deps
include_panic_deps = true
core_fns << '__print_assert_failure'
core_fns << 'isnil'
}
if pref_.trace_calls || pref_.trace_fns.len > 0 {
core_fns << panic_deps
include_panic_deps = true
core_fns << 'vgettid'
core_fns << 'C.gettid'
core_fns << 'v.trace_calls.on_c_main'
core_fns << 'v.trace_calls.current_time'
core_fns << 'v.trace_calls.on_call'
}
all_fn_root_names << core_fns
if include_panic_deps {
all_fn_root_names << [
'__new_array_with_default',
'__new_array_with_default_noscan',
'str_intp',
ref_array_idx_str + '.push',
ref_array_idx_str + '.push_noscan',
string_idx_str + '.substr',
array_idx_str + '.slice',
array_idx_str + '.get',
'v_fixed_index',
charptr_idx_str + '.vstring_literal',
]
}
}
if pref_.is_bare {
all_fn_root_names << [
Expand All @@ -238,10 +240,6 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
$if trace_skip_unused_all_fns ? {
println('k: ${k} | mfn: ${mfn.name}')
}
if k in table.used_features.comptime_calls {
all_fn_root_names << k
continue
}
if pref_.translated && mfn.attrs.any(it.name == 'c') {
all_fn_root_names << k
continue
Expand Down Expand Up @@ -287,11 +285,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}

// sync:
if k == 'sync.new_channel_st' {
all_fn_root_names << k
continue
}
if k == 'sync.channel_select' {
if k in ['sync.new_channel_st', 'sync.channel_select'] {
all_fn_root_names << k
continue
}
Expand Down Expand Up @@ -422,13 +416,14 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}
}
for orm_type in orm_connection_implementations {
all_fn_root_names << '${int(orm_type)}.select'
all_fn_root_names << '${int(orm_type)}.insert'
all_fn_root_names << '${int(orm_type)}.update'
all_fn_root_names << '${int(orm_type)}.delete'
all_fn_root_names << '${int(orm_type)}.create'
all_fn_root_names << '${int(orm_type)}.drop'
all_fn_root_names << '${int(orm_type)}.last_id'
typ := int(orm_type)
all_fn_root_names << '${typ}.select'
all_fn_root_names << '${typ}.insert'
all_fn_root_names << '${typ}.update'
all_fn_root_names << '${typ}.delete'
all_fn_root_names << '${typ}.create'
all_fn_root_names << '${typ}.drop'
all_fn_root_names << '${typ}.last_id'
}
}

Expand Down Expand Up @@ -534,8 +529,7 @@ fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[st
mut all_consts := map[string]ast.ConstField{}
mut all_globals := map[string]ast.GlobalField{}
for i in 0 .. ast_files.len {
file := ast_files[i]
for node in file.stmts {
for node in ast_files[i].stmts {
match node {
ast.FnDecl {
fkey := node.fkey()
Expand Down Expand Up @@ -564,37 +558,39 @@ fn all_fn_const_and_global(ast_files []&ast.File) (map[string]ast.FnDecl, map[st

fn mark_all_methods_used(mut table ast.Table, mut all_fn_root_names []string, typ ast.Type) {
sym := table.sym(typ)
styp := '${int(typ)}'
for method in sym.methods {
all_fn_root_names << '${int(typ)}.${method.name}'
all_fn_root_names << '${styp}.${method.name}'
}
}

fn handle_vweb(mut table ast.Table, mut all_fn_root_names []string, result_name string, filter_name string,
context_name string) {
// handle vweb magic router methods:
result_type_idx := table.find_type(result_name)
if result_type_idx != 0 {
all_fn_root_names << filter_name
typ_vweb_context := table.find_type(context_name).set_nr_muls(1)
mark_all_methods_used(mut table, mut all_fn_root_names, typ_vweb_context)
for vgt in table.used_features.used_veb_types {
sym_app := table.sym(vgt)
for m in sym_app.methods {
mut skip := true
if m.name == 'before_request' {
// TODO: handle expansion of method calls in generic functions in a more universal way
skip = false
}
if m.return_type == result_type_idx {
skip = false
}
if skip {
continue
}
pvgt := vgt.set_nr_muls(1)
// eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name')
all_fn_root_names << '${int(pvgt)}.${m.name}'
if result_type_idx == 0 {
return
}
all_fn_root_names << filter_name
typ_vweb_context := table.find_type(context_name).set_nr_muls(1)
mark_all_methods_used(mut table, mut all_fn_root_names, typ_vweb_context)
for vgt in table.used_features.used_veb_types {
sym_app := table.sym(vgt)
pvgt := '${int(vgt.set_nr_muls(1))}'
for m in sym_app.methods {
mut skip := true
if m.name == 'before_request' {
// TODO: handle expansion of method calls in generic functions in a more universal way
skip = false
}
if m.return_type == result_type_idx {
skip = false
}
if skip {
continue
}
// eprintln('vgt: $vgt | pvgt: $pvgt | sym_app.name: $sym_app.name | m.name: $m.name')
all_fn_root_names << '${pvgt}.${m.name}'
}
}
}
3 changes: 3 additions & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ pub fn Walker.new(params Walker) &Walker {
return new_walker
}

@[inline]
pub fn (mut w Walker) mark_fn_as_used(fkey string) {
$if trace_skip_unused_marked ? {
eprintln(' fn > |${fkey}|')
}
w.used_fns[fkey] = true
}

@[inline]
pub fn (mut w Walker) mark_builtin_array_method_as_used(method_name string) {
w.mark_builtin_type_method_as_used('${ast.array_type_idx}.${method_name}', '${int(ast.array_type.ref())}.${method_name}')
}

@[inline]
pub fn (mut w Walker) mark_builtin_map_method_as_used(method_name string) {
w.mark_builtin_type_method_as_used('${ast.map_type_idx}.${method_name}', '${int(ast.map_type.ref())}.${method_name}')
}
Expand Down

0 comments on commit feade24

Please sign in to comment.