Skip to content
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: explicitly use void for functions with no parameters #70981

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/cmd/cgo/internal/testcshared/cshared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Split(data, []byte("\n")) {
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")
}
}
15 changes: 15 additions & 0 deletions src/cmd/cgo/internal/testcshared/testdata/issue68411/issue68411.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import "C"

//export exportFuncWithNoParams
func exportFuncWithNoParams() {}

//export exportFuncWithParams
func exportFuncWithParams(a, b int) {}

func main() {}
19 changes: 12 additions & 7 deletions src/cmd/cgo/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,13 +1012,18 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
s += p.cgoType(fn.Recv.List[0].Type).C.String()
s += " recv"
}
forFieldList(fntype.Params,
func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
s += ", "
}
s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i))
})

if len(fntype.Params.List) > 0 {
forFieldList(fntype.Params,
func(i int, aname string, atype ast.Expr) {
if i > 0 || fn.Recv != nil {
s += ", "
}
s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i))
})
} else {
s += "void"
}
s += ")"

if len(exp.Doc) > 0 {
Expand Down