c-for-go icon indicating copy to clipboard operation
c-for-go copied to clipboard

how to handle zero size array field in struct

Open andypeng2015 opened this issue 2 years ago • 1 comments

go version go1.17.11 darwin/amd64 OS macOS Big Sur 11.4

when I run

| => cd bindings/x86/
| => ~/go/bin/c-for-go gen.yml
| => mv -v ./x86/* .
| => go test .
# github.com/andypeng2015/test/bindings/x86
./cgo_helpers.go:272:13: ref3fccfd0e.coalesced_mmio undefined (type *_Ctype_struct_kvm_coalesced_mmio_ring has no field or method coalesced_mmio)
./cgo_helpers.go:272:53: cannot assign [0]_Ctype_struct_kvm_coalesced_mmio to ref3fccfd0e.coalesced_mmio in multiple assignment
./cgo_helpers.go:298:108: x.ref3fccfd0e.coalesced_mmio undefined (type *_Ctype_struct_kvm_coalesced_mmio_ring has no field or method coalesced_mmio)
./cgo_helpers.go:402:13: refde42b52b.entries undefined (type *_Ctype_struct_kvm_cpuid has no field or method entries)
./cgo_helpers.go:402:39: cannot assign [0]_Ctype_struct_kvm_cpuid_entry to refde42b52b.entries in multiple assignment
./cgo_helpers.go:428:96: x.refde42b52b.entries undefined (type *_Ctype_struct_kvm_cpuid has no field or method entries)
FAIL    github.com/andypeng2015/test/bindings/x86 [build failed]
FAIL

// code generated by c-for-go
// from ~/Library/Caches/go-build

type _Ctype_struct_kvm_coalesced_mmio_ring struct {
	first	_Ctype___u32
	last	_Ctype___u32
}

type _Ctype_struct_kvm_cpuid struct {
	nent	_Ctype___u32
	padding	_Ctype___u32
}

type _Ctype_struct_kvm_cpuid_entry struct {
	function	_Ctype___u32
	eax		_Ctype___u32
	ebx		_Ctype___u32
	ecx		_Ctype___u32
	edx		_Ctype___u32
	padding		_Ctype___u32
}
struct kvm_coalesced_mmio_ring {
	__u32 first, last;
	struct kvm_coalesced_mmio coalesced_mmio[0];
};

struct kvm_cpuid {
	__u32 nent;
	__u32 padding;
	struct kvm_cpuid_entry entries[0];
};

source code attached test.zip

pls advise, thanks

andypeng2015 avatar Jun 17 '22 02:06 andypeng2015

I believe it's caused by zero size array.

I also tried MemTips below, but still got error, not sure how to handle this properly, please kindly advise @xlab thanks

MemTips:
    - {target: "^kvm_coalesced_mmio", self: raw}
    - {target: "^kvm_cpuid", self: raw}
processing gen.yml ⠋[WARN] cannot gofmt x86/cgo_helpers.go: x86/cgo_helpers.go:239:82: expected operand, found '&&' (and 10 more errors)
Screenshot 2022-06-20 at 11 14 07 AM

andypeng2015 avatar Jun 20 '22 03:06 andypeng2015