From b528395039f7c5952b11df625e035408df258fce Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 18 Mar 2025 19:41:16 +0200 Subject: [PATCH] builtin,markused,pref,cgen: improve markused for small programs --- vlib/builtin/builtin.c.v | 1 - vlib/builtin/builtin_nix.c.v | 2 +- vlib/builtin/option.v | 1 - vlib/builtin/string.v | 2 +- vlib/time/format.v | 2 +- vlib/v/gen/c/cgen.v | 8 +------- vlib/v/gen/c/coutput_test.v | 14 +++++++++----- vlib/v/gen/c/fn.v | 23 +---------------------- vlib/v/markused/markused.v | 24 ++++++++++++++---------- vlib/v/markused/walker.v | 6 +++++- vlib/v/pref/pref.v | 5 +++++ 11 files changed, 38 insertions(+), 50 deletions(-) diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 05ce0fabb5..ad424a3ea1 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -12,7 +12,6 @@ fn C._vinit(argc int, argv &&char) fn C._vcleanup() -@[markused] fn v_segmentation_fault_handler(signal_number i32) { $if freestanding { eprintln('signal 11: segmentation fault') diff --git a/vlib/builtin/builtin_nix.c.v b/vlib/builtin/builtin_nix.c.v index ec90230fd4..68bbff8ad4 100644 --- a/vlib/builtin/builtin_nix.c.v +++ b/vlib/builtin/builtin_nix.c.v @@ -18,7 +18,7 @@ fn break_if_debugger_attached() { } } -@[markused; noreturn] +@[noreturn] pub fn panic_lasterr(base string) { // TODO: use strerror_r and errno panic(base + ' unknown') diff --git a/vlib/builtin/option.v b/vlib/builtin/option.v index c11b4a4c6a..45aed33314 100644 --- a/vlib/builtin/option.v +++ b/vlib/builtin/option.v @@ -21,7 +21,6 @@ struct _option { // derived _option_xxx types } -@[markused] fn _option_none(data voidptr, mut option _option, size int) { unsafe { *option = _option{ diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 14c3cb9bc1..85090c3510 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -332,7 +332,7 @@ fn (a string) clone_static() string { // option_clone_static returns an independent copy of a given array when lhs is an option type. // It should be used only in -autofree generated code. -@[inline; markused] +@[inline] fn (a string) option_clone_static() ?string { return ?string(a.clone()) } diff --git a/vlib/time/format.v b/vlib/time/format.v index f04c15c91f..e7cfc4f560 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -142,7 +142,7 @@ pub fn (t Time) format_ss_nano() string { // format_rfc3339 returns a date string in "YYYY-MM-DDTHH:mm:ss.123Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html) // RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar. // It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols. -@[manualfree; markused] +@[manualfree] pub fn (t Time) format_rfc3339() string { mut buf := [u8(`0`), `0`, `0`, `0`, `-`, `0`, `0`, `-`, `0`, `0`, `T`, `0`, `0`, `:`, `0`, `0`, `:`, `0`, `0`, `.`, `0`, `0`, `0`, `Z`] diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index ca19cb8851..20211e7285 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -343,8 +343,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO enum_data_type: table.find_type('EnumData') variant_data_type: table.find_type('VariantData') is_cc_msvc: pref_.ccompiler == 'msvc' - use_segfault_handler: !('no_segfault_handler' in pref_.compile_defines - || pref_.os in [.wasm32, .wasm32_emscripten]) + use_segfault_handler: pref_.should_use_segfault_handler() static_modifier: if pref_.parallel_cc { 'static ' } else { '' } static_non_parallel: if !pref_.parallel_cc { 'static ' } else { '' } has_reflection: 'v.reflection' in table.modules @@ -2018,11 +2017,6 @@ pub fn (mut g Gen) new_global_tmp_var() string { return prefix_with_counter('_t', g.global_tmp_count) } -pub fn (mut g Gen) new_tmp_declaration_name() string { - g.tmp_count_declarations++ - return prefix_with_counter('_d', g.tmp_count_declarations) -} - pub fn (mut g Gen) current_tmp_var() string { return prefix_with_counter('_t', g.tmp_count) } diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index 5136ad01b9..2473dcbca0 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -52,10 +52,11 @@ fn test_out_files() { alloptions := '-o ${os.quoted_path(pexe)} ${file_options.vflags}' label := mj('v', file_options.vflags, 'run', relpath) + ' == ${mm(out_relpath)} ' // + compile_cmd := '${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}' sw_compile := time.new_stopwatch() - compilation := os.execute('${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}') + compilation := os.execute(compile_cmd) compile_ms := sw_compile.elapsed().milliseconds() - ensure_compilation_succeeded(compilation) + ensure_compilation_succeeded(compilation, compile_cmd) // sw_run := time.new_stopwatch() res := os.execute(os.quoted_path(pexe)) @@ -129,12 +130,12 @@ fn test_c_must_have_files() { } file_options := get_file_options(path) alloptions := '-o - ${file_options.vflags}' - description := mj('v', alloptions, relpath) + ' matches ${mm(must_have_relpath)} ' + mut description := mj('v', alloptions, relpath) + ' matches ${mm(must_have_relpath)} ' cmd := '${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}' sw_compile := time.new_stopwatch() compilation := os.execute(cmd) compile_ms := sw_compile.elapsed().milliseconds() - ensure_compilation_succeeded(compilation) + ensure_compilation_succeeded(compilation, cmd) expected_lines := os.read_lines(must_have_path) or { [] } generated_c_lines := compilation.output.split_into_lines() mut nmatches := 0 @@ -145,6 +146,7 @@ fn test_c_must_have_files() { // eprintln('> testing: $must_have_path has line: $eline') } else { failed_patterns << eline + description += '\n failed pattern: `${eline}`' println('${term.red('FAIL')} C:${compile_ms:5}ms ${description}') eprintln('${must_have_path}:${idx_expected_line + 1}: expected match error:') eprintln('`${cmd}` did NOT produce expected line:') @@ -207,11 +209,13 @@ fn vroot_relative(opath string) string { return npath.replace(nvroot, '') } -fn ensure_compilation_succeeded(compilation os.Result) { +fn ensure_compilation_succeeded(compilation os.Result, cmd string) { if compilation.exit_code < 0 { + eprintln('> cmd exit_code < 0, cmd: ${cmd}') panic(compilation.output) } if compilation.exit_code != 0 { + eprintln('> cmd exit_code != 0, cmd: ${cmd}') panic('compilation failed: ${compilation.output}') } } diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index a2d0c9d57a..f625428ca7 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -754,25 +754,9 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic g.write('void') } } - // mut is_implicit_ctx := false - // Veb actions defined by user can have implicit context - /* - if g.cur_fn != unsafe { nil } && g.cur_fn.is_method && g.cur_mod.name != 'veb' { - typ_veb_result := g.table.find_type('veb.Result') - // if params.len == 3 { - // println(g.cur_fn) - //} - if g.cur_fn.return_type == typ_veb_result { - // is_implicit_ctx = true - if !g.inside_c_extern { - g.write('/*veb*/') - } - } - } - */ for i, param in params { mut caname := if param.name == '_' { - g.new_tmp_declaration_name() + '_d${i + 1}' } else { c_name(param.name) } @@ -830,11 +814,6 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic } g.definitions.write_string(', ') } - - // if is_implicit_ctx && i == 0 && params[1].name != 'ctx' { - // g.writeln('veb__Context* ctx,') - // g.definitions.write_string('veb__Context* ctx,') - //} } if (g.pref.translated && is_variadic) || is_c_variadic { if !g.inside_c_extern { diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 9b8ced2fd4..b1c97103cb 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -86,6 +86,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a } if pref_.autofree { core_fns << string_idx_str + '.clone_static' + core_fns << string_idx_str + '.option_clone_static' } if table.used_features.auto_str || pref_.is_shared { include_panic_deps = true @@ -243,6 +244,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a charptr_idx_str + '.vstring_literal', ] } + if pref_.should_use_segfault_handler() { + core_fns << 'v_segmentation_fault_handler' + } all_fn_root_names << core_fns } if pref_.is_bare { @@ -512,27 +516,27 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a } } - if trace_skip_unused_fn_names { - for key, _ in walker.used_fns { - println('> used fn key: ${key}') - } - } - for kcon, con in all_consts { if pref_.is_shared && con.is_pub { walker.mark_const_as_used(kcon) continue } - if !pref_.is_shared && con.is_pub && con.name.starts_with('main.') { - walker.mark_const_as_used(kcon) - continue - } if pref_.translated && con.attrs.any(it.name == 'export') { walker.mark_const_as_used(kcon) continue } } + if walker.used_none > 0 || table.used_features.auto_str { + walker.mark_fn_as_used('_option_none') + } + + if trace_skip_unused_fn_names { + for key, _ in walker.used_fns { + println('> used fn key: ${key}') + } + } + table.used_features.used_fns = walker.used_fns.move() table.used_features.used_consts = walker.used_consts.move() table.used_features.used_globals = walker.used_globals.move() diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index e1c127f1cf..ae076e9097 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -16,6 +16,7 @@ pub mut: used_globals map[string]bool used_structs map[string]bool used_fields map[string]bool + used_none int n_asserts int pref &pref.Preferences = unsafe { nil } mut: @@ -364,6 +365,7 @@ fn (mut w Walker) expr(node_ ast.Expr) { } ast.SpawnExpr { w.expr(node.call_expr) + w.fn_by_name('tos3') if w.pref.os == .windows { w.fn_by_name('panic_lasterr') w.fn_by_name('winapi_lasterr_str') @@ -482,7 +484,9 @@ fn (mut w Walker) expr(node_ ast.Expr) { w.stmts(b.stmts) } } - ast.None {} + ast.None { + w.used_none++ + } ast.Nil {} ast.ParExpr { w.expr(node.expr) diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index ad116ebb63..cb6f2a73bb 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -1280,3 +1280,8 @@ pub fn supported_test_runners_list() string { pub fn (pref &Preferences) should_trace_fn_name(fname string) bool { return pref.trace_fns.any(fname.match_glob(it)) } + +pub fn (pref &Preferences) should_use_segfault_handler() bool { + return !('no_segfault_handler' in pref.compile_defines + || pref.os in [.wasm32, .wasm32_emscripten]) +}