Skip to content

Commit

Permalink
cgen: implement `-d trace_gen_wanted -d trace_gen_wanted_value="messa…
Browse files Browse the repository at this point in the history
…ge = _SLIT0"`, cleanup text_manipulation.v
  • Loading branch information
spytheman committed Nov 18, 2024
1 parent 791d0d3 commit 61e38b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, |
Expand Down
94 changes: 44 additions & 50 deletions vlib/v/gen/c/text_manipulation.v
Original file line number Diff line number Diff line change
Expand Up @@ -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: <nil> | 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: <nil> | 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))
}
Expand All @@ -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: <nil> | 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: <nil> | 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))
}
Expand All @@ -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: <nil> | 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))
}
Expand All @@ -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: <nil> | 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))
Expand All @@ -84,28 +89,17 @@ 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: <nil> | 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))
}
g.out.writeln(s1)
g.empty_line = true

// expansion for s2
$if trace_gen ? {
if g.file == unsafe { nil } {
eprintln('gen file: <nil> | 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))
}
Expand Down

0 comments on commit 61e38b9

Please sign in to comment.