-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: copy parameter names to exported C file where possible #37746
Comments
Thanks for filing the issue. This doesn't need to be a proposal. If someone wants to modify cgo as you suggest, that would be fine. |
Change https://golang.org/cl/222619 mentions this issue: |
Submitted a PR for this and tested with the code in the official documentation. Seems to work now. extern GoInt64 MyFunction(GoInt arg1, GoInt arg2, GoString arg3);
/* Return type for MyFunction2 */
struct MyFunction2_return {
GoInt64 r0;
char* r1;
};
extern struct MyFunction2_return MyFunction2(GoInt arg1, GoInt arg2, GoString arg3); |
Change https://golang.org/cl/222622 mentions this issue: |
Updates #37746 Change-Id: Ib64abe3995f310cd50ede47b0d3d159572901000 Reviewed-on: https://go-review.googlesource.com/c/go/+/222622 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This seems to have caused #44974. |
The Problem
According to this cgo documentation, when exporting functions using cgo, the resulting header file should retain your function parameters names, however it practice it does not.
It States
However, when I compile that exact code it gave, i get this result:
Based on this stack overflow thread I had posted a while back, it seems that the reason for this is that "Go allows arbitrary rune names, e.g., instead of
input
you might call the variableπ
. C does not allow such names."My Proposal
Instead of avoiding this using parameter names such as
p0
,p1
,p2
, etc, we first detect if the function name is using a non-ASCII character in its name using something like this (taken from this SO thread)And then we could do one of several things.
p0
,p1
, etc, but allow parameter names that are proper ASCII to use their original names.This normally wouldn't be an issue, but when a functions documentation block doesn't line up properly with the parameter names for the function, you wind up with incorrect documentation in either the source file or in the exported header, and it interferes with consumption of the exported header file.
Here is a basic example that somewhat demonstrates how the documentation can become misaligned.
results in
Consuming that function externally might not be incredibly difficult if you infer the position of the parameters, but when you have functions with much more complex signatures or functions with parameter documentation out of order, things get hairy and it tends to result in you maintaining documentation that does not line up either in source or does not line up in implementation.
I personally think the best solution would be to remove the allowance of runes in functions that are exported through cgo specifically. This way function parameters will line up in the C header and the Go source file and add a much needed congruity.
The text was updated successfully, but these errors were encountered: