golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.72k stars 17.5k forks source link

cmd/cgo: use void keyword in generated headers on extern'd function declarations with no params #68411

Open sixcorners opened 1 month ago

sixcorners commented 1 month ago

Proposal Details

I propose that the generated header files from --buildmode=c-shared have void in the parenthesis when the extern'd function takes no parameters.
In C, function declarations with an empty list of parameters means that the parameters are unknown. The way to declare a function that takes no parameters is to put the void keyword in the parenthesis. If you omit the void keyword then callers are allowed to specify any number of parameters to the function, although compilers can issue warnings when this happens.

In my case I'm using jextract to generate java bindings for a header file generated by a go project. It's is kind of a new tool. When it sees the empty parameter list it treats the function as variadic which looks slightly different on the java side. This doesn't really matter much though. I'm just including this here for context.

I'm unsure what backwards compatibility concerns something like this would have. In the case of jextract it changes everything about the function call on the java side. I don't know if there are other tools that would be greatly affected by this change.

ianlancetaylor commented 1 month ago

I don't think this has to be a proposal, I think we can just make this change. Want to send a patch? Thanks.

nagalakvenkat commented 1 month ago

Is this still open and needs a patch?

ianlancetaylor commented 1 month ago

Yes. Any patch should appear in this issue, and I don't see one.