Skip to content

Commit

Permalink
cgen: allow dump(unsafe{nil}) and dump(voidptr(123)) in the same program
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jul 29, 2023
1 parent 2f2dde8 commit c881e72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions vlib/v/gen/c/dumpexpr.v
Expand Up @@ -82,11 +82,13 @@ fn (mut g Gen) dump_expr(node ast.DumpExpr) {
}

fn (mut g Gen) dump_expr_definitions() {
mut dump_already_generated_fns := map[string]bool{}
mut dump_typedefs := map[string]bool{}
mut dump_fns := strings.new_builder(100)
mut dump_fn_defs := strings.new_builder(100)
for dump_type, cname in g.table.dumps {
dump_sym := g.table.sym(dump_type)
// eprintln('>>> dump_type: $dump_type | cname: $cname | dump_sym: $dump_sym.name')
mut name := cname
if dump_sym.language == .c {
name = name[3..]
Expand Down Expand Up @@ -147,6 +149,13 @@ fn (mut g Gen) dump_expr_definitions() {
}
dump_fn_name := '_v_dump_expr_${name}' +
(if is_ptr { '_ptr'.repeat(typ.nr_muls()) } else { '' })

// protect against duplicate declarations:
if dump_already_generated_fns[dump_fn_name] {
continue
}
dump_already_generated_fns[dump_fn_name] = true

dump_fn_defs.writeln('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg);')
if g.writeln_fn_header('${str_dumparg_ret_type} ${dump_fn_name}(string fpath, int line, string sexpr, ${str_dumparg_type} dump_arg)', mut
dump_fns)
Expand Down
10 changes: 10 additions & 0 deletions vlib/v/tests/dump_fns_test.v
Expand Up @@ -67,3 +67,13 @@ fn test_dump_of_type_that_has_no_custom_str_method() {
ps := &StructWithoutStrMethod{456}
assert dump(ps).x == 456
}

fn test_nil_values_and_voidptr_values_can_be_dumped_in_the_same_program() {
// Note, that nil is its own type in the main v repo,
// while dump() generates `_v_dump_expr_voidptr` for *both* `nil` and `voidptr` values.
a := unsafe { nil }
b := voidptr(456)
dump(a)
dump(b)
assert true
}

0 comments on commit c881e72

Please sign in to comment.