cmd/cgo: bug fixes

* disallow embedding of C type (Fixes issue 2552)
* detect 0-length array (Fixes issue 2806)
* use typedefs when possible, to avoid attribute((unavailable)) (Fixes issue 2888)
* print Go types constructed from C types using original C types (Fixes issue 2612)

This fix changes _cgo_export.h to repeat the preamble from import "C".
Otherwise the fix to issue 2612 is impossible, since it cannot refer to
types that have not been defined.  If people are using //export and
putting non-header information in the preamble, they will need to
refactor their code.

R=golang-dev, r, r
CC=golang-dev
https://golang.org/cl/5672080
This commit is contained in:
Russ Cox 2012-02-19 13:32:55 -05:00
parent 72fb81eeb6
commit 1a0c8fe9bb
14 changed files with 157 additions and 35 deletions

View file

@ -16,8 +16,8 @@ the pseudo-package "C" and then refers to types such as C.size_t,
variables such as C.stdout, or functions such as C.putchar.
If the import of "C" is immediately preceded by a comment, that
comment is used as a header when compiling the C parts of
the package. For example:
comment, called the preamble, is used as a header when compiling
the C parts of the package. For example:
// #include <stdio.h>
// #include <errno.h>
@ -57,6 +57,8 @@ The C type void* is represented by Go's unsafe.Pointer.
To access a struct, union, or enum type directly, prefix it with
struct_, union_, or enum_, as in C.struct_stat.
Go structs cannot embed fields with C types.
Any C function that returns a value may be called in a multiple
assignment context to retrieve both the return value and the
C errno variable as an error. For example:
@ -100,7 +102,8 @@ They will be available in the C code as:
extern int64 MyFunction(int arg1, int arg2, GoString arg3);
extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);
found in _cgo_export.h generated header. Functions with multiple
found in _cgo_export.h generated header, after any preambles
copied from the cgo input files. Functions with multiple
return values are mapped to functions returning a struct.
Not all Go types can be mapped to C types in a useful way.