1
0
mirror of https://github.com/golang/go synced 2024-11-26 15:36:59 -07:00
go/misc/cgo/test/api.go
Ian Lance Taylor 689947d565 cmd/cgo: only record typedef name for pointer to struct
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>
2016-11-01 23:06:24 +00:00

31 lines
740 B
Go

// Copyright 2013 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.
// API Compatibility Checks for cgo
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"
func testAPI() {
var cs *C.char
cs = C.CString("hello")
defer C.free(unsafe.Pointer(cs))
var s string
s = C.GoString((*C.char)(C.api_hello))
s = C.GoStringN((*C.char)(C.api_hello), C.int(6))
var b []byte
b = C.GoBytes(unsafe.Pointer(C.api_hello), C.int(6))
_, _ = s, b
C.cstring_pointer_fun(nil)
}