Skip to content

Commit b528395

Browse files
committed
builtin,markused,pref,cgen: improve markused for small programs
1 parent 90c638e commit b528395

File tree

11 files changed

+38
-50
lines changed

11 files changed

+38
-50
lines changed

vlib/builtin/builtin.c.v

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ fn C._vinit(argc int, argv &&char)
1212

1313
fn C._vcleanup()
1414

15-
@[markused]
1615
fn v_segmentation_fault_handler(signal_number i32) {
1716
$if freestanding {
1817
eprintln('signal 11: segmentation fault')

vlib/builtin/builtin_nix.c.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn break_if_debugger_attached() {
1818
}
1919
}
2020

21-
@[markused; noreturn]
21+
@[noreturn]
2222
pub fn panic_lasterr(base string) {
2323
// TODO: use strerror_r and errno
2424
panic(base + ' unknown')

vlib/builtin/option.v

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ struct _option {
2121
// derived _option_xxx types
2222
}
2323

24-
@[markused]
2524
fn _option_none(data voidptr, mut option _option, size int) {
2625
unsafe {
2726
*option = _option{

vlib/builtin/string.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ fn (a string) clone_static() string {
332332

333333
// option_clone_static returns an independent copy of a given array when lhs is an option type.
334334
// It should be used only in -autofree generated code.
335-
@[inline; markused]
335+
@[inline]
336336
fn (a string) option_clone_static() ?string {
337337
return ?string(a.clone())
338338
}

vlib/time/format.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub fn (t Time) format_ss_nano() string {
142142
// 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)
143143
// RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar.
144144
// It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols.
145-
@[manualfree; markused]
145+
@[manualfree]
146146
pub fn (t Time) format_rfc3339() string {
147147
mut buf := [u8(`0`), `0`, `0`, `0`, `-`, `0`, `0`, `-`, `0`, `0`, `T`, `0`, `0`, `:`, `0`,
148148
`0`, `:`, `0`, `0`, `.`, `0`, `0`, `0`, `Z`]

vlib/v/gen/c/cgen.v

+1-7
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
343343
enum_data_type: table.find_type('EnumData')
344344
variant_data_type: table.find_type('VariantData')
345345
is_cc_msvc: pref_.ccompiler == 'msvc'
346-
use_segfault_handler: !('no_segfault_handler' in pref_.compile_defines
347-
|| pref_.os in [.wasm32, .wasm32_emscripten])
346+
use_segfault_handler: pref_.should_use_segfault_handler()
348347
static_modifier: if pref_.parallel_cc { 'static ' } else { '' }
349348
static_non_parallel: if !pref_.parallel_cc { 'static ' } else { '' }
350349
has_reflection: 'v.reflection' in table.modules
@@ -2018,11 +2017,6 @@ pub fn (mut g Gen) new_global_tmp_var() string {
20182017
return prefix_with_counter('_t', g.global_tmp_count)
20192018
}
20202019

2021-
pub fn (mut g Gen) new_tmp_declaration_name() string {
2022-
g.tmp_count_declarations++
2023-
return prefix_with_counter('_d', g.tmp_count_declarations)
2024-
}
2025-
20262020
pub fn (mut g Gen) current_tmp_var() string {
20272021
return prefix_with_counter('_t', g.tmp_count)
20282022
}

vlib/v/gen/c/coutput_test.v

+9-5
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ fn test_out_files() {
5252
alloptions := '-o ${os.quoted_path(pexe)} ${file_options.vflags}'
5353
label := mj('v', file_options.vflags, 'run', relpath) + ' == ${mm(out_relpath)} '
5454
//
55+
compile_cmd := '${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}'
5556
sw_compile := time.new_stopwatch()
56-
compilation := os.execute('${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}')
57+
compilation := os.execute(compile_cmd)
5758
compile_ms := sw_compile.elapsed().milliseconds()
58-
ensure_compilation_succeeded(compilation)
59+
ensure_compilation_succeeded(compilation, compile_cmd)
5960
//
6061
sw_run := time.new_stopwatch()
6162
res := os.execute(os.quoted_path(pexe))
@@ -129,12 +130,12 @@ fn test_c_must_have_files() {
129130
}
130131
file_options := get_file_options(path)
131132
alloptions := '-o - ${file_options.vflags}'
132-
description := mj('v', alloptions, relpath) + ' matches ${mm(must_have_relpath)} '
133+
mut description := mj('v', alloptions, relpath) + ' matches ${mm(must_have_relpath)} '
133134
cmd := '${os.quoted_path(vexe)} ${alloptions} ${os.quoted_path(path)}'
134135
sw_compile := time.new_stopwatch()
135136
compilation := os.execute(cmd)
136137
compile_ms := sw_compile.elapsed().milliseconds()
137-
ensure_compilation_succeeded(compilation)
138+
ensure_compilation_succeeded(compilation, cmd)
138139
expected_lines := os.read_lines(must_have_path) or { [] }
139140
generated_c_lines := compilation.output.split_into_lines()
140141
mut nmatches := 0
@@ -145,6 +146,7 @@ fn test_c_must_have_files() {
145146
// eprintln('> testing: $must_have_path has line: $eline')
146147
} else {
147148
failed_patterns << eline
149+
description += '\n failed pattern: `${eline}`'
148150
println('${term.red('FAIL')} C:${compile_ms:5}ms ${description}')
149151
eprintln('${must_have_path}:${idx_expected_line + 1}: expected match error:')
150152
eprintln('`${cmd}` did NOT produce expected line:')
@@ -207,11 +209,13 @@ fn vroot_relative(opath string) string {
207209
return npath.replace(nvroot, '')
208210
}
209211

210-
fn ensure_compilation_succeeded(compilation os.Result) {
212+
fn ensure_compilation_succeeded(compilation os.Result, cmd string) {
211213
if compilation.exit_code < 0 {
214+
eprintln('> cmd exit_code < 0, cmd: ${cmd}')
212215
panic(compilation.output)
213216
}
214217
if compilation.exit_code != 0 {
218+
eprintln('> cmd exit_code != 0, cmd: ${cmd}')
215219
panic('compilation failed: ${compilation.output}')
216220
}
217221
}

vlib/v/gen/c/fn.v

+1-22
Original file line numberDiff line numberDiff line change
@@ -754,25 +754,9 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic
754754
g.write('void')
755755
}
756756
}
757-
// mut is_implicit_ctx := false
758-
// Veb actions defined by user can have implicit context
759-
/*
760-
if g.cur_fn != unsafe { nil } && g.cur_fn.is_method && g.cur_mod.name != 'veb' {
761-
typ_veb_result := g.table.find_type('veb.Result')
762-
// if params.len == 3 {
763-
// println(g.cur_fn)
764-
//}
765-
if g.cur_fn.return_type == typ_veb_result {
766-
// is_implicit_ctx = true
767-
if !g.inside_c_extern {
768-
g.write('/*veb*/')
769-
}
770-
}
771-
}
772-
*/
773757
for i, param in params {
774758
mut caname := if param.name == '_' {
775-
g.new_tmp_declaration_name()
759+
'_d${i + 1}'
776760
} else {
777761
c_name(param.name)
778762
}
@@ -830,11 +814,6 @@ fn (mut g Gen) fn_decl_params(params []ast.Param, scope &ast.Scope, is_variadic
830814
}
831815
g.definitions.write_string(', ')
832816
}
833-
834-
// if is_implicit_ctx && i == 0 && params[1].name != 'ctx' {
835-
// g.writeln('veb__Context* ctx,')
836-
// g.definitions.write_string('veb__Context* ctx,')
837-
//}
838817
}
839818
if (g.pref.translated && is_variadic) || is_c_variadic {
840819
if !g.inside_c_extern {

vlib/v/markused/markused.v

+14-10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
8686
}
8787
if pref_.autofree {
8888
core_fns << string_idx_str + '.clone_static'
89+
core_fns << string_idx_str + '.option_clone_static'
8990
}
9091
if table.used_features.auto_str || pref_.is_shared {
9192
include_panic_deps = true
@@ -243,6 +244,9 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
243244
charptr_idx_str + '.vstring_literal',
244245
]
245246
}
247+
if pref_.should_use_segfault_handler() {
248+
core_fns << 'v_segmentation_fault_handler'
249+
}
246250
all_fn_root_names << core_fns
247251
}
248252
if pref_.is_bare {
@@ -512,27 +516,27 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
512516
}
513517
}
514518

515-
if trace_skip_unused_fn_names {
516-
for key, _ in walker.used_fns {
517-
println('> used fn key: ${key}')
518-
}
519-
}
520-
521519
for kcon, con in all_consts {
522520
if pref_.is_shared && con.is_pub {
523521
walker.mark_const_as_used(kcon)
524522
continue
525523
}
526-
if !pref_.is_shared && con.is_pub && con.name.starts_with('main.') {
527-
walker.mark_const_as_used(kcon)
528-
continue
529-
}
530524
if pref_.translated && con.attrs.any(it.name == 'export') {
531525
walker.mark_const_as_used(kcon)
532526
continue
533527
}
534528
}
535529

530+
if walker.used_none > 0 || table.used_features.auto_str {
531+
walker.mark_fn_as_used('_option_none')
532+
}
533+
534+
if trace_skip_unused_fn_names {
535+
for key, _ in walker.used_fns {
536+
println('> used fn key: ${key}')
537+
}
538+
}
539+
536540
table.used_features.used_fns = walker.used_fns.move()
537541
table.used_features.used_consts = walker.used_consts.move()
538542
table.used_features.used_globals = walker.used_globals.move()

vlib/v/markused/walker.v

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mut:
1616
used_globals map[string]bool
1717
used_structs map[string]bool
1818
used_fields map[string]bool
19+
used_none int
1920
n_asserts int
2021
pref &pref.Preferences = unsafe { nil }
2122
mut:
@@ -364,6 +365,7 @@ fn (mut w Walker) expr(node_ ast.Expr) {
364365
}
365366
ast.SpawnExpr {
366367
w.expr(node.call_expr)
368+
w.fn_by_name('tos3')
367369
if w.pref.os == .windows {
368370
w.fn_by_name('panic_lasterr')
369371
w.fn_by_name('winapi_lasterr_str')
@@ -482,7 +484,9 @@ fn (mut w Walker) expr(node_ ast.Expr) {
482484
w.stmts(b.stmts)
483485
}
484486
}
485-
ast.None {}
487+
ast.None {
488+
w.used_none++
489+
}
486490
ast.Nil {}
487491
ast.ParExpr {
488492
w.expr(node.expr)

vlib/v/pref/pref.v

+5
Original file line numberDiff line numberDiff line change
@@ -1280,3 +1280,8 @@ pub fn supported_test_runners_list() string {
12801280
pub fn (pref &Preferences) should_trace_fn_name(fname string) bool {
12811281
return pref.trace_fns.any(fname.match_glob(it))
12821282
}
1283+
1284+
pub fn (pref &Preferences) should_use_segfault_handler() bool {
1285+
return !('no_segfault_handler' in pref.compile_defines
1286+
|| pref.os in [.wasm32, .wasm32_emscripten])
1287+
}

0 commit comments

Comments
 (0)