cmd/cgo: explicitly use void for functions with no parameters

Currently, exported Go functions with no parameters generate C functions
with an empty parameter list. In C, a function with an empty parameter
list can accept any number of arguments, whereas a function with a single
void parameter explicitly declares that it takes no arguments.

To align the generated C functions with their Go prototypes, update the
code generation to explicitly include a void parameter for functions
with no parameters.

Fixes #68411

Change-Id: Iab9456aa0236200bf21d1181a2e18e82869df63f
GitHub-Last-Rev: 6ff21a98df
GitHub-Pull-Request: golang/go#70981
Reviewed-on: https://go-review.googlesource.com/c/go/+/638635
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Mauri de Souza Meneguzzo 2025-02-17 13:57:12 +00:00 committed by Gopher Robot
parent a73c6545d2
commit 896de17f80
3 changed files with 68 additions and 7 deletions

View file

@ -880,3 +880,44 @@ func TestIssue36233(t *testing.T) {
t.Error("missing functions")
}
}
func TestIssue68411(t *testing.T) {
globalSkip(t)
testenv.MustHaveCGO(t)
t.Parallel()
// Test that the export header uses a void function parameter for
// exported Go functions with no parameters.
tmpdir := t.TempDir()
const exportHeader = "issue68411.h"
run(t, nil, "go", "tool", "cgo", "-exportheader", exportHeader, "-objdir", tmpdir, "./issue68411/issue68411.go")
data, err := os.ReadFile(exportHeader)
if err != nil {
t.Fatal(err)
}
funcs := []struct{ name, signature string }{
{"exportFuncWithNoParams", "void exportFuncWithNoParams(void)"},
{"exportFuncWithParams", "exportFuncWithParams(GoInt a, GoInt b)"},
}
var found int
for line := range bytes.Lines(data) {
for _, fn := range funcs {
if bytes.Contains(line, []byte(fn.name)) {
found++
if !bytes.Contains(line, []byte(fn.signature)) {
t.Errorf("function signature mismatch; got %q, want %q", line, fn.signature)
}
}
}
}
if found != len(funcs) {
t.Error("missing functions")
}
}