New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: godefs shouldnt try to guess which struct field's identifier prefixes preceding _
is okay to strip.
#5256
Comments
The cgo -godefs option is used during the Go bootstrap and is not documented for other uses. We aren't going to change its default behaviour. But I'm fine with changing it to, e.g., not strip a prefix if it causes duplicate field names. Labels changed: added priority-later, removed priority-triage. |
Arguably the bug here is that cgo's "fieldPrefix" function skips over fields that don't contain a "_" character when trying to come up with a "common" prefix. E.g., in the example struct, "rects" does not have an underscore, so fieldPrefix simply skips over it; then the only field it actually considers is "num_rects" and (erroneously) concludes "num_" is a common prefix. It would be easy to tweak fieldPrefix to realize that if a struct has fields without any underscores then it doesn't have a 'common prefix'. Unfortunately, that would cause at least some field names in package syscall to change if they were regenerated; e.g., the "Unit" field in Sysinfo_t would become "Mem_unit". However, now that package syscall is frozen (and presumably won't be regenerated any further), maybe that's not an issue? Or maybe we still wouldn't want to change the struct field names even in the new go.sys package? |
A similar issue occurs when one or more fields have a common numeric suffix following the underscore: struct test {
int type;
int pad_1;
int size;
int pad_2;
int etc;
}; Through cgo -godefs: type Test struct {
Type int32
1 int32 // syntax error: unexpected literal 1, expected field name or embedded type
Size int32
2 int32 // syntax error: unexpected literal 2, expected field name or embedded type
Etc int32
} In this example the 'pad_' prefix is stripped and the suffix is not a valid identifier so the output can't compile. Like with the OP's example, it's difficult to recover from this in an automated way since the original field names are lost in the output. It would be nice to at least have a command-line flag to disable the underscore stripping behavior. |
Do you want to send a patch? |
I was hoping to add support to the unix package for linux/ptp_clock.h, which includes the following struct declarations:
Code is here I wonder if we could avoid backwards compatibility problems using a heuristic such as that the prefix will only be removed if the number of fields with the prefix is greater than the number of fields with no prefix (ignoring fields starting with |
by Mortdeus@gocos2d.org:
The text was updated successfully, but these errors were encountered: