Skip to content

Commit 7f2d731

Browse files
committed
cgen: fix dump(c_struct), where c_struct has fields of type &&char
1 parent 9569c05 commit 7f2d731

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

vlib/v/gen/c/auto_str_methods.v

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,10 @@ fn (g &Gen) type_to_fmt(typ ast.Type) StrIntpType {
773773
// return '%C\\000' // a C string
774774
return .si_s
775775
}
776+
typ_nr_muls := typ.nr_muls()
777+
if typ_nr_muls > 1 {
778+
return .si_p
779+
}
776780
sym := g.table.sym(typ)
777781
if typ.is_ptr() && (typ.is_int_valptr() || typ.is_float_valptr()) {
778782
return .si_s
@@ -902,7 +906,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
902906
g.get_str_fn(ftyp_noshared)
903907
}
904908

905-
// manage the fact hat with float we use always the g representation
909+
// with floats we use always the g representation:
906910
if sym.kind !in [.f32, .f64] {
907911
fn_body.write_string('{_SLIT("$quote_str"), ${int(base_fmt)}, {.${data_str(base_fmt)}=')
908912
} else {
@@ -913,7 +917,8 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
913917
mut funcprefix := ''
914918
mut func, mut caller_should_free := struct_auto_str_func(sym, field.typ, field_styp_fn_name,
915919
field.name, sym_has_str_method, str_method_expects_ptr)
916-
if field.typ in ast.cptr_types {
920+
ftyp_nr_muls := field.typ.nr_muls()
921+
if ftyp_nr_muls > 1 || field.typ in ast.cptr_types {
917922
func = '(voidptr) it.$field.name'
918923
caller_should_free = false
919924
} else if ftyp_noshared.is_ptr() {

vlib/v/tests/dump_c_structs/dump_c_struct_test.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "@VMODROOT/epoll.h"
2+
#include "@VMODROOT/netdb.h"
23

34
pub struct C.epoll_event {
45
mut:
@@ -19,6 +20,14 @@ struct Epoll {
1920
ev C.epoll_event
2021
}
2122

23+
struct C.hostent {
24+
h_name &char
25+
h_aliases &&char
26+
h_addrtype int
27+
h_length int
28+
h_addr_list &&char
29+
}
30+
2231
fn test_dump_c_struct() {
2332
ev := C.epoll_event{}
2433
unsafe { C.memset(&ev, 0, sizeof(ev)) }
@@ -30,5 +39,12 @@ fn test_dump_c_struct() {
3039
}
3140
dump(e)
3241
println(e)
42+
//
43+
mut hostent := &C.hostent{
44+
h_addr_list: unsafe { nil }
45+
h_aliases: unsafe { nil }
46+
h_name: unsafe { nil }
47+
}
48+
dump(hostent)
3349
assert true
3450
}

vlib/v/tests/dump_c_structs/netdb.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
/* Description of data base entry for a single host. */
3+
struct hostent {
4+
char *h_name; /* Official name of host. */
5+
char **h_aliases; /* Alias list. */
6+
int h_addrtype; /* Host address type. */
7+
int h_length; /* Length of address. */
8+
char **h_addr_list; /* List of addresses from name server. */
9+
};

0 commit comments

Comments
 (0)