Skip to content

Commit

Permalink
cgen: add support for -d trace_cgen_stmt, document it in CONTRIBUTI…
Browse files Browse the repository at this point in the history
…NG.md
  • Loading branch information
spytheman committed Sep 13, 2023
1 parent 4d8b2e9 commit 5d1f87c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
40 changes: 27 additions & 13 deletions CONTRIBUTING.md
Expand Up @@ -192,20 +192,25 @@ which tests have failed and then fix them by making more changes. Just use
run with your updated code. Use `hub ci-status --verbose` to monitor
their status.

## Flags
## Compiler flags, useful while debugging the compiler itself:

V allows you to pass custom flags using `-d my_flag` that can then be checked
at compile time (see the documentation about
[compile-time if](https://github.com/vlang/v/blob/master/doc/docs.md#compile-time-if)).
There are numerous flags that can be passed when building the compiler
with `v self` or when creating a copy of the compiler, that will help
you when debugging.

Beware that the flags must be passed when building the compiler,
not the program, so do for example: `v -d time_parsing cmd/v` or
`v -d trace_checker self`.
Some flags can make the compiler very verbose, so it is recommended
to create a copy of the compiler rather than replacing it with `v self`.
Since the compiler is *also* an ordinary V program, there are numerous flags that can be
passed when building the compiler itself with `v self`, or when creating a copy of the
compiler, that will help you when debugging the compiler.

Note: beware that the flags below must be passed, when building the compiler, *not the program*,
so do for example:
`./v -o w -d time_parsing cmd/v`
or
`./v -o w -d trace_checker self`
... then use `./w file.v`, instead of `./v file.v`, to compile your program.

Note: some of the flags can make the compiler *very verbose*, so it is recommended to create
a copy of the compiler rather than replacing it with `v self`.

| Flag | Usage |
|-----------------------------------|---------------------------------------------------------------------------------------------------------------------|
Expand All @@ -216,12 +221,21 @@ to create a copy of the compiler rather than replacing it with `v self`.
| `print_vweb_template_expansions` | Prints vweb compiled HTML files |
| `time_checking` | Prints the time spent checking files and other related information |
| `time_parsing` | Prints the time spent parsing files and other related information |
| | |
| `trace_parser` | Prints details about parsed statements and expressions. Very verbose. Use it for panics in the parser. |
| `trace_ccoptions` | Prints options passed down to the C compiler |
| | |
| `trace_checker` | Prints details about the statements being checked. Very verbose. Use it for panics in the checker. |
| | |
| `trace_gen` | Prints all the strings written to the generated C file. Very verbose. |
| `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, |
| | otherwise by default cgen uses several threads, and the lines that are printed are out of order. |
| | |
| `trace_autofree` | Prints details about how/when -autofree puts free() calls |
| `trace_autostr` | Prints details about `.str()` method auto-generated by the compiler during C generation |
| `trace_ccoptions` | Prints options passed down to the C compiler |
| `trace_checker` | Prints details about the statements being checked |
| `trace_gen` | Prints strings written to the generated C file. Beware, this flag is very verbose |
| `trace_parser` | Prints details about parsed statements and expressions |
| | |
| `trace_thirdparty_obj_files` | Prints details about built thirdparty obj files |
| `trace_usecache` | Prints details when -usecache is used |
| `trace_embed_file` | Prints details when $embed_file is used |
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/cgen.v
Expand Up @@ -2006,6 +2006,14 @@ fn (mut g Gen) write_v_source_line_info(pos token.Pos) {
}

fn (mut g Gen) stmt(node ast.Stmt) {
$if trace_cgen_stmt ? {
ntype := typeof(node).replace('v.ast.', '')
if g.file == unsafe { nil } {
eprintln('cgen: <nil> | pos: ${node.pos.line_str():-39} | node: ${ntype} | ${node}')
} else {
eprintln('cgen: ${g.file.path:-30} | pos: ${node.pos.line_str():-39} | node: ${ntype} | ${node}')
}
}
g.inside_call = false
if !g.skip_stmt_pos {
g.set_current_pos_as_last_stmt_pos()
Expand Down

0 comments on commit 5d1f87c

Please sign in to comment.