]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: unify cgo output for gc and gccgo
authorMichael Steinert <mike.steinert@gmail.com>
Mon, 17 Jul 2017 16:14:23 +0000 (11:14 -0500)
committerIan Lance Taylor <iant@golang.org>
Wed, 16 Aug 2017 21:57:56 +0000 (21:57 +0000)
When calling a Go function that returns multiple values from C, cgo
generates a structure to hold the values. According to the documentation
this structure is called `struct <function-name>_return`. When compiling
for gccgo the generated structure name is `struct <function-name>_result`.
This change updates the output for gccgo to match the documentation and
output for gc.

Fixes #20910

Change-Id: Iaea8030a695a7aaf9d9f317447fc05615d8e4adc
Reviewed-on: https://go-review.googlesource.com/49350
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/test/cgo_test.go
misc/cgo/test/issue20910.c [new file with mode: 0644]
misc/cgo/test/issue20910.go [new file with mode: 0644]
src/cmd/cgo/out.go

index f7cf6f613c4c2b09f1cc7b6fbdd8f0ded94761a5..a44eff27b856549827ee8ad2f5f2bd6a78ec5b14 100644 (file)
@@ -80,5 +80,6 @@ func Test20369(t *testing.T)                 { test20369(t) }
 func Test18720(t *testing.T)                 { test18720(t) }
 func Test20266(t *testing.T)                 { test20266(t) }
 func Test20129(t *testing.T)                 { test20129(t) }
+func Test20910(t *testing.T)                 { test20910(t) }
 
 func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/issue20910.c b/misc/cgo/test/issue20910.c
new file mode 100644 (file)
index 0000000..e8d623f
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2017 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.
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "_cgo_export.h"
+
+/* Test calling a Go function with multiple return values.  */
+
+void
+callMulti(void)
+{
+       struct multi_return result = multi();
+       assert(strcmp(result.r0, "multi") == 0);
+       assert(result.r1 == 0);
+       free(result.r0);
+}
diff --git a/misc/cgo/test/issue20910.go b/misc/cgo/test/issue20910.go
new file mode 100644 (file)
index 0000000..69d7d92
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2017 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 cgotest
+
+//void callMulti(void);
+import "C"
+
+import "testing"
+
+//export multi
+func multi() (*C.char, C.int) {
+       return C.CString("multi"), 0
+}
+
+func test20910(t *testing.T) {
+       C.callMulti()
+}
index 228f981c9427898b65bfce5b4ea96648f2f6b1db..6e1a47669d39a66cee7ef97659841eee3ac45d3d 100644 (file)
@@ -985,7 +985,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
                default:
                        // Declare a result struct.
                        fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
-                       fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
+                       fmt.Fprintf(fgcch, "struct %s_return {\n", exp.ExpName)
                        forFieldList(fntype.Results,
                                func(i int, aname string, atype ast.Expr) {
                                        t := p.cgoType(atype)
@@ -996,7 +996,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
                                        fmt.Fprint(fgcch, "\n")
                                })
                        fmt.Fprintf(fgcch, "};\n")
-                       fmt.Fprintf(cdeclBuf, "struct %s_result", exp.ExpName)
+                       fmt.Fprintf(cdeclBuf, "struct %s_return", exp.ExpName)
                }
 
                cRet := cdeclBuf.String()
@@ -1022,7 +1022,7 @@ func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
                        fmt.Fprintf(fgcch, "\n%s", exp.Doc)
                }
 
-               fmt.Fprintf(fgcch, "extern %s %s %s;\n", cRet, exp.ExpName, cParams)
+               fmt.Fprintf(fgcch, "extern %s %s%s;\n", cRet, exp.ExpName, cParams)
 
                // We need to use a name that will be exported by the
                // Go code; otherwise gccgo will make it static and we