cmd/cgo: -godefs doesn't handle embedded struct fields correctly #5253
$ cat test.go
package test
/*
struct A {
unsigned long count;
struct {
long value;
} data[15];
};
*/
import "C"
type A C.struct_A
$ go tool cgo -godefs test.go
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs test.go
package test
type A struct {
Count uint64
Data [15]_Ctype_struct___0
}
@Mortdeus, please provide minimal and standalone test cases (like above)
next time, thanks.
Labels changed: added priority-later, removed priority-triage.
Status changed to Accepted.
mkb218
commented
Sep 23, 2013
gopherbot
commented
Oct 2, 2013
Comment 3 by Mortdeus@gocos2d.org:
I just manually wrote the them in.
gopherbot
commented
Oct 2, 2013
Comment 4 by Mortdeus@gocos2d.org:
I just manually wrote them in. mortdeus/egles@201cb06#diff-1f1850a75cd762fd2b932808ece04736L238
I think this is reasonably fixable. We just need to change the handling of unnamed
dwarf.StructType's to directly return a struct definition rather than indirectly via a
dummy identifier and typedef. E.g., with a minor change on top of my patch for issue
8441 (mostly to make sure assigning to t.Go after the recursive call to c.Struct() is
safe), I get:
$ go tool cgo -godefs issue5253.go
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs issue5253.go
package test
type A struct {
Count uint64
Data [15]struct {
Value int64
}
}
There's a possible issue if C code does something silly like "typedef struct { ... } X,
Y;" then C.X and C.Y would now become distinct types, whereas previously they would be
treated as aliases of type __1. I don't think that's an issue in practice, but it's
probably solvable if needed.
There's also an exponential code size blow up from pathological inputs like:
/*
typedef struct foo {
struct {
struct {
struct {
int x;
} a, b;
} a, b;
} a, b;
} A, B;
*/
import "C"
but that could be fixed by making (*typeConv).Struct() smarter when consecutive fields
reference the same DWARF type. Though again, I don't expect it to be an issue in
practice.
I've put together https://golang.org/cl/122900043 to fix the issue as described in the initial bug report here. E.g., for the input provided by minux in #1, here's the output from patched cgo: <<<< $ go tool cgo -godefs test.go // Created by cgo -godefs - DO NOT EDIT // cgo -godefs test.go package test type A struct { Count uint64 Data [15]struct { Value int64 } } >>>> I'd appreciate if any users affected by this could test that this patch (applied to tip) fixes their issues with cgo -godefs, or if there are more details to address.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by Mortdeus@gocos2d.org: