Skip to content

Commit 92d1fb7

Browse files
nathan-fiscalettiianlancetaylor
authored andcommitted
cmd/cgo: updated exported function parameter names
Functions that are exported through cgo will now retain their parameter names provided that their parameter names use only ASCII characters. Fixes #37746 Change-Id: Ia5f643e7d872312e81c224febd1f81ce14425c32 GitHub-Last-Rev: 220959b GitHub-Pull-Request: #37750 Reviewed-on: https://go-review.googlesource.com/c/go/+/222619 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a79ac2b commit 92d1fb7

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/cmd/cgo/out.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"regexp"
2323
"sort"
2424
"strings"
25+
"unicode"
2526
)
2627

2728
var (
@@ -802,6 +803,24 @@ func (p *Package) packedAttribute() string {
802803
return s + "))"
803804
}
804805

806+
// exportParamName returns the value of param as it should be
807+
// displayed in a c header file. If param contains any non-ASCII
808+
// characters, this function will return the character p followed by
809+
// the value of position; otherwise, this function will return the
810+
// value of param.
811+
func exportParamName(param string, position int) string {
812+
pname := param
813+
814+
for i := 0; i < len(param); i++ {
815+
if param[i] > unicode.MaxASCII {
816+
pname = fmt.Sprintf("p%d", position)
817+
break
818+
}
819+
}
820+
821+
return pname
822+
}
823+
805824
// Write out the various stubs we need to support functions exported
806825
// from Go so that they are callable from C.
807826
func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
@@ -915,7 +934,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
915934
if i > 0 || fn.Recv != nil {
916935
s += ", "
917936
}
918-
s += fmt.Sprintf("%s p%d", p.cgoType(atype).C, i)
937+
s += fmt.Sprintf("%s %s", p.cgoType(atype).C, exportParamName(aname, i))
919938
})
920939
s += ")"
921940

@@ -932,28 +951,28 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
932951
fmt.Fprintf(fgcc, "\n%s\n", s)
933952
fmt.Fprintf(fgcc, "{\n")
934953
fmt.Fprintf(fgcc, "\t__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();\n")
935-
fmt.Fprintf(fgcc, "\t%s %v a;\n", ctype, p.packedAttribute())
954+
fmt.Fprintf(fgcc, "\t%s %v _cgo_a;\n", ctype, p.packedAttribute())
936955
if gccResult != "void" && (len(fntype.Results.List) > 1 || len(fntype.Results.List[0].Names) > 1) {
937956
fmt.Fprintf(fgcc, "\t%s r;\n", gccResult)
938957
}
939958
if fn.Recv != nil {
940-
fmt.Fprintf(fgcc, "\ta.recv = recv;\n")
959+
fmt.Fprintf(fgcc, "\t_cgo_a.recv = recv;\n")
941960
}
942961
forFieldList(fntype.Params,
943962
func(i int, aname string, atype ast.Expr) {
944-
fmt.Fprintf(fgcc, "\ta.p%d = p%d;\n", i, i)
963+
fmt.Fprintf(fgcc, "\t_cgo_a.p%d = %s;\n", i, exportParamName(aname, i))
945964
})
946965
fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
947-
fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off)
966+
fmt.Fprintf(fgcc, "\tcrosscall2(_cgoexp%s_%s, &_cgo_a, %d, _cgo_ctxt);\n", cPrefix, exp.ExpName, off)
948967
fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
949968
fmt.Fprintf(fgcc, "\t_cgo_release_context(_cgo_ctxt);\n")
950969
if gccResult != "void" {
951970
if len(fntype.Results.List) == 1 && len(fntype.Results.List[0].Names) <= 1 {
952-
fmt.Fprintf(fgcc, "\treturn a.r0;\n")
971+
fmt.Fprintf(fgcc, "\treturn _cgo_a.r0;\n")
953972
} else {
954973
forFieldList(fntype.Results,
955974
func(i int, aname string, atype ast.Expr) {
956-
fmt.Fprintf(fgcc, "\tr.r%d = a.r%d;\n", i, i)
975+
fmt.Fprintf(fgcc, "\tr.r%d = _cgo_a.r%d;\n", i, i)
957976
})
958977
fmt.Fprintf(fgcc, "\treturn r;\n")
959978
}

0 commit comments

Comments
 (0)