builtin,markused,pref,cgen: improve markused for small programs

This commit is contained in:
Delyan Angelov 2025-03-18 19:41:16 +02:00
parent 90c638e649
commit b528395039
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
11 changed files with 38 additions and 50 deletions

View File

@ -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')

View File

@ -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')

View File

@ -21,7 +21,6 @@ struct _option {
// derived _option_xxx types
}
@[markused]
fn _option_none(data voidptr, mut option _option, size int) {
unsafe {
*option = _option{

View File

@ -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())
}

View File

@ -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`]

View File

@ -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)
}

View File

@ -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}')
}
}

View File

@ -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 {

View File

@ -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()

View File

@ -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)

View File

@ -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])
}