]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: avoid name confliction for C functions
authorHiroshi Ioka <hirochachacha@gmail.com>
Sat, 6 Jan 2018 09:10:30 +0000 (18:10 +0900)
committerIan Lance Taylor <iant@golang.org>
Mon, 18 Jun 2018 18:28:02 +0000 (18:28 +0000)
Use more cryptic names for local variables inside C function wrappers.

Fixes #23356

Change-Id: Ia6a0218f27a13be14f589b1a0facc9683d22ff56
Reviewed-on: https://go-review.googlesource.com/86495
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/test/cgo_test.go
misc/cgo/test/issue23356.go [new file with mode: 0644]
src/cmd/cgo/out.go

index 4462df0059875d7755e0d6c0c25f4f26606c8ec0..4c7f676e0ba763520189034c7ca824ef29c52b45 100644 (file)
@@ -89,6 +89,7 @@ func Test21897(t *testing.T)                 { test21897(t) }
 func Test22906(t *testing.T)                 { test22906(t) }
 func Test24206(t *testing.T)                 { test24206(t) }
 func Test25143(t *testing.T)                 { test25143(t) }
+func Test23356(t *testing.T)                 { test23356(t) }
 
 func BenchmarkCgoCall(b *testing.B)  { benchCgoCall(b) }
 func BenchmarkGoString(b *testing.B) { benchGoString(b) }
diff --git a/misc/cgo/test/issue23356.go b/misc/cgo/test/issue23356.go
new file mode 100644 (file)
index 0000000..1c39012
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2018 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
+
+// int a(void) { return 5; };
+// int r(void) { return 3; };
+import "C"
+import "testing"
+
+func test23356(t *testing.T) {
+       if got, want := C.a(), C.int(5); got != want {
+               t.Errorf("C.a() == %v, expected %v", got, want)
+       }
+       if got, want := C.r(), C.int(3); got != want {
+               t.Errorf("C.r() == %v, expected %v", got, want)
+       }
+}
index e9b79865650f1a3e8f30f6b88775e3e413bd62f8..dbc17d2d56f6556e7e84310eccb827c801a8f9ef 100644 (file)
@@ -609,14 +609,14 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
        // We're trying to write a gcc struct that matches gc's layout.
        // Use packed attribute to force no padding in this struct in case
        // gcc has different packing requirements.
-       fmt.Fprintf(fgcc, "\t%s %v *a = v;\n", ctype, p.packedAttribute())
+       fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
        if n.FuncType.Result != nil {
                // Save the stack top for use below.
-               fmt.Fprintf(fgcc, "\tchar *stktop = _cgo_topofstack();\n")
+               fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
        }
        tr := n.FuncType.Result
        if tr != nil {
-               fmt.Fprintf(fgcc, "\t__typeof__(a->r) r;\n")
+               fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
        }
        fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
        if n.AddError {
@@ -624,9 +624,9 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
        }
        fmt.Fprintf(fgcc, "\t")
        if tr != nil {
-               fmt.Fprintf(fgcc, "r = ")
+               fmt.Fprintf(fgcc, "_cgo_r = ")
                if c := tr.C.String(); c[len(c)-1] == '*' {
-                       fmt.Fprint(fgcc, "(__typeof__(a->r)) ")
+                       fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ")
                }
        }
        if n.Kind == "macro" {
@@ -637,7 +637,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
                        if i > 0 {
                                fmt.Fprintf(fgcc, ", ")
                        }
-                       fmt.Fprintf(fgcc, "a->p%d", i)
+                       fmt.Fprintf(fgcc, "_cgo_a->p%d", i)
                }
                fmt.Fprintf(fgcc, ");\n")
        }
@@ -648,9 +648,9 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
        if n.FuncType.Result != nil {
                // The cgo call may have caused a stack copy (via a callback).
                // Adjust the return value pointer appropriately.
-               fmt.Fprintf(fgcc, "\ta = (void*)((char*)a + (_cgo_topofstack() - stktop));\n")
+               fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")
                // Save the return value.
-               fmt.Fprintf(fgcc, "\ta->r = r;\n")
+               fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n")
        }
        if n.AddError {
                fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n")
@@ -685,12 +685,12 @@ func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
        fmt.Fprintf(fgcc, ")\n")
        fmt.Fprintf(fgcc, "{\n")
        if t := n.FuncType.Result; t != nil {
-               fmt.Fprintf(fgcc, "\t%s r;\n", t.C.String())
+               fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String())
        }
        fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
        fmt.Fprintf(fgcc, "\t")
        if t := n.FuncType.Result; t != nil {
-               fmt.Fprintf(fgcc, "r = ")
+               fmt.Fprintf(fgcc, "_cgo_r = ")
                // Cast to void* to avoid warnings due to omitted qualifiers.
                if c := t.C.String(); c[len(c)-1] == '*' {
                        fmt.Fprintf(fgcc, "(void*)")
@@ -716,7 +716,7 @@ func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
                if c := t.C.String(); c[len(c)-1] == '*' {
                        fmt.Fprintf(fgcc, "(void*)")
                }
-               fmt.Fprintf(fgcc, "r;\n")
+               fmt.Fprintf(fgcc, "_cgo_r;\n")
        }
        fmt.Fprintf(fgcc, "}\n")
        fmt.Fprintf(fgcc, "\n")