]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: only record typedef name for pointer to struct
authorIan Lance Taylor <iant@golang.org>
Tue, 1 Nov 2016 21:13:34 +0000 (14:13 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 1 Nov 2016 23:06:24 +0000 (23:06 +0000)
In a function argument, we handle a typedef for a pointer specially,
using the pointer type rather than the typedef, to permit the Go calls
to match the laxer type conversions permitted in C. We record the
typedef so that we use that type in the C code, in case it has a special
attribute. However, using the typedef is wrong when using a pointer to a
basic type, because the C code may sometimes use the typedef and
sometimes not, and using the typedef in all cases will cause incorrect
type errors on the Go side. Fortunately we only really need to use the
typedef when pointing to a struct/union/class, and in such a case
confusion is unlikely.

Fixes #17723.

Change-Id: Id2eaeb156faeaf2e8eb9cf0b8f95b44caf8cfbd2
Reviewed-on: https://go-review.googlesource.com/32536
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
misc/cgo/test/api.go
src/cmd/cgo/gcc.go

index b4ae3dda4b26eabdb3c161d9287e20371083cbaa..d2b09cbeffedbc8fc46a89aabd38aeaa7f9d583b 100644 (file)
@@ -7,6 +7,11 @@
 package cgotest
 
 // #include <stdlib.h>
+//
+// // Test for issue 17723.
+// typedef char *cstring_pointer;
+// static void cstring_pointer_fun(cstring_pointer dummy) { }
+//
 // const char *api_hello = "hello!";
 import "C"
 import "unsafe"
@@ -21,4 +26,5 @@ func testAPI() {
        var b []byte
        b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
        _, _ = s, b
+       C.cstring_pointer_fun(nil)
 }
index 450120f83cb5c4fe8a83505b2cb3eefc1bc7c9fe..de87df0798b0f44daba8701fe7de3b9ee41e5f3c 100644 (file)
@@ -1937,9 +1937,12 @@ func (c *typeConv) FuncArg(dtype dwarf.Type, pos token.Pos) *Type {
                                return nil
                        }
 
-                       // Remember the C spelling, in case the struct
-                       // has __attribute__((unavailable)) on it. See issue 2888.
-                       t.Typedef = dt.Name
+                       // For a struct/union/class, remember the C spelling,
+                       // in case it has __attribute__((unavailable)).
+                       // See issue 2888.
+                       if isStructUnionClass(t.Go) {
+                               t.Typedef = dt.Name
+                       }
                }
        }
        return t