From 61e38b9bf1ff40a3f3b22568f67a784205f7c133 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 18 Nov 2024 15:19:11 +0200 Subject: [PATCH] cgen: implement `-d trace_gen_wanted -d trace_gen_wanted_value="message = _SLIT0"`, cleanup text_manipulation.v --- CONTRIBUTING.md | 2 + vlib/v/gen/c/text_manipulation.v | 94 +++++++++++++++----------------- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2bb84ad3587367..cfbc62c9b5f81c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -328,6 +328,8 @@ a copy of the compiler rather than replacing it with `v self`. | `trace_transformer` | Prints details about the statements being transformed. Very verbose. Use it for panics in the transformer stage. | | | | | `trace_gen` | Prints all the strings written to the generated C file. Very verbose. | +| `trace_gen_wanted_value` | Prints a backtrace, when a specific *wanted* value, is part of what is printed in the generated C file. | +| | Use: `v -g -o vgen -d trace_gen_wanted -d trace_gen_wanted_value="message = _SLIT0" cmd/v && ./vgen bug.v` | | `trace_cgen_stmt` | Prints details about the statements that are being processed by cgen. | | | Use it for panics in cgen, to see the closest input V source line, that caused the panic. | | | Note: you need `v -no-parallel -d trace_cgen_stmt -o w cmd/v` to make sense of the output of that, | diff --git a/vlib/v/gen/c/text_manipulation.v b/vlib/v/gen/c/text_manipulation.v index 90bf334476e338..732cd32ecbc10f 100644 --- a/vlib/v/gen/c/text_manipulation.v +++ b/vlib/v/gen/c/text_manipulation.v @@ -5,15 +5,43 @@ module c import v.util +const trace_gen_wanted_value = $d('trace_gen_wanted_value', '') + +@[if trace_gen_wanted ?] +fn (mut g Gen) trace_gen_wanted_context(last_character_len int, s string) { + last_n := g.out.last_n(last_character_len) + eprintln('> trace_gen_wanted, last characters:\n${last_n}\n') + eprintln('> trace_gen_wanted, found wanted cgen string `${trace_gen_wanted_value}` in generated string ${s}') + print_backtrace() +} + +@[if trace_gen_wanted ?] +fn (mut g Gen) trace_gen_wanted(s string) { + if s.contains(trace_gen_wanted_value) { + g.trace_gen_wanted_context(256, s) + } +} + +@[if trace_gen_wanted ?] +fn (mut g Gen) trace_gen_wanted2(s1 string, s2 string) { + if s1.contains(trace_gen_wanted_value) || s2.contains(trace_gen_wanted_value) { + g.trace_gen_wanted_context(256, s1 + s2) + } +} + +@[if trace_gen ?] +fn (mut g Gen) trace_gen(reason string, s string) { + if g.file == unsafe { nil } { + eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | ${reason}: ${s}') + } else { + eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | ${reason}: ${s}') + } +} + @[expand_simple_interpolation] fn (mut g Gen) write(s string) { - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s}') - } - } + g.trace_gen_wanted(s) + g.trace_gen('write', s) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) } @@ -22,27 +50,15 @@ fn (mut g Gen) write(s string) { } fn (mut g Gen) write2(s1 string, s2 string) { - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s1}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s1}') - } - } + g.trace_gen_wanted2(s1, s2) + g.trace_gen('write2 s1', s1) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) } g.out.write_string(s1) g.empty_line = false - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s2}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write: ${s2}') - } - } - + g.trace_gen('write2 s2', s2) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) } @@ -51,13 +67,7 @@ fn (mut g Gen) write2(s1 string, s2 string) { } fn (mut g Gen) write_decimal(x i64) { - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | write_decimal: ${x}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | write_decimal: ${x}') - } - } + g.trace_gen('write_decimal', x.str()) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) } @@ -66,13 +76,8 @@ fn (mut g Gen) write_decimal(x i64) { } fn (mut g Gen) writeln(s string) { - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: ${s}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln: ${s}') - } - } + g.trace_gen_wanted(s) + g.trace_gen('writeln', s) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) // g.out_parallel[g.out_idx].write_string(util.tabs(g.indent)) @@ -84,14 +89,9 @@ fn (mut g Gen) writeln(s string) { } fn (mut g Gen) writeln2(s1 string, s2 string) { + g.trace_gen_wanted2(s1, s2) + g.trace_gen('writeln2 s1', s1) // expansion for s1 - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln2: ${s1}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln2: ${s1}') - } - } if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) } @@ -99,13 +99,7 @@ fn (mut g Gen) writeln2(s1 string, s2 string) { g.empty_line = true // expansion for s2 - $if trace_gen ? { - if g.file == unsafe { nil } { - eprintln('gen file: | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln2: ${s2}') - } else { - eprintln('gen file: ${g.file.path:-30} | last_fn_c_name: ${g.last_fn_c_name:-45} | writeln2: ${s2}') - } - } + g.trace_gen('writeln2 s2', s2) if g.indent > 0 && g.empty_line { g.out.write_string(util.tabs(g.indent)) }