diff --git a/misc/cgo/testcdefs/cdefstest.c b/misc/cgo/testcdefs/cdefstest.c new file mode 100644 index 0000000000..10cdd66b65 --- /dev/null +++ b/misc/cgo/testcdefs/cdefstest.c @@ -0,0 +1,8 @@ +// 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. + +#include "runtime.h" +#include "cdefstest.h" + +struct CdefsTest test; diff --git a/misc/cgo/testcdefs/cdefstest.go b/misc/cgo/testcdefs/cdefstest.go new file mode 100644 index 0000000000..e6305b77d7 --- /dev/null +++ b/misc/cgo/testcdefs/cdefstest.go @@ -0,0 +1,41 @@ +// 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. +// +// +build ignore + +package cgotest + +/* +// This file tests a bug found in the cgo -cdefs tool that incorrectly +// translated Go pointer arrays generated by the cgo godefs tool back into C +// pointer arrays. +// +// The comments below show how the type is translated from gcc-style C into Go +// and back into C for both the buggy version and the correct version + +struct cdefsTest { + // This was already being handled correctly + // Correct: -> Array [20]int8 -> int8 array[20] + char array1[20]; + + // Buggy: -> Array [20][20]int8 -> [20]int8 array[20] + // Correct: -> Array [20][20]int8 -> int8 array[20][20] + char array2[20][20]; + + // Buggy: -> Array [20]*int8 -> *int8 array[20] + // Correct: -> Array [20]*int8 -> int8 *array[20] + char *array3[20]; + + // Buggy: -> Array [20][20]*int8 -> [20]*int8 array[20] + // Correct: -> Array [20]**int8 -> int8 *array[20][20] + char *array4[20][20]; + + // Buggy: -> Array [20][20]**int8 -> [20]**int8 array[20] + // Correct: -> Array [20][20]**int8 -> int8 **array[20][20] + char **array5[20][20]; +}; +*/ +import "C" + +type CdefsTest C.struct_cdefsTest diff --git a/misc/cgo/testcdefs/main.go b/misc/cgo/testcdefs/main.go new file mode 100644 index 0000000000..864b4b2a7f --- /dev/null +++ b/misc/cgo/testcdefs/main.go @@ -0,0 +1,8 @@ +// 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. + +package cgotest + +// This file only exists so we can run 'go build' and build our .c files +func test() {} diff --git a/misc/cgo/testcdefs/test.bash b/misc/cgo/testcdefs/test.bash new file mode 100755 index 0000000000..cbfa9b27d9 --- /dev/null +++ b/misc/cgo/testcdefs/test.bash @@ -0,0 +1,16 @@ +# 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. + +# Just add issue file prefixes to this list if more issues come up +FILE_PREFIXES="cdefstest" + +for FP in $FILE_PREFIXES +do + go tool cgo -cdefs ${FP}.go > ${FP}.h +done + +go build . +EXIT=$? +rm -rf _obj main *.h +exit $EXIT diff --git a/src/cmd/cgo/godefs.go b/src/cmd/cgo/godefs.go index 3dfedcb282..ce5ac2736c 100644 --- a/src/cmd/cgo/godefs.go +++ b/src/cmd/cgo/godefs.go @@ -261,17 +261,17 @@ func cdecl(name, typ string) string { if strings.HasPrefix(typ, "*[0]") { typ = "*void" } - // X *byte -> *X byte - if strings.HasPrefix(typ, "*") { - name = "*" + name - typ = typ[1:] - } // X [4]byte -> X[4] byte - if strings.HasPrefix(typ, "[") { + for strings.HasPrefix(typ, "[") { i := strings.Index(typ, "]") + 1 name = name + typ[:i] typ = typ[i:] } + // X *byte -> *X byte + for strings.HasPrefix(typ, "*") { + name = "*" + name + typ = typ[1:] + } // X T -> T X // Handle the special case: 'unsafe.Pointer' is 'void *' if typ == "unsafe.Pointer" { diff --git a/src/run.bash b/src/run.bash index 0324634969..4d8d04bfb3 100755 --- a/src/run.bash +++ b/src/run.bash @@ -124,6 +124,11 @@ freebsd-386 | freebsd-amd64 | linux-386 | linux-amd64 | netbsd-386 | netbsd-amd6 esac ) || exit $? +[ "$CGO_ENABLED" != 1 ] || +(xcd ../misc/cgo/testcdefs +./test.bash || exit 1 +) || exit $? + [ "$CGO_ENABLED" != 1 ] || [ "$GOHOSTOS" == windows ] || (xcd ../misc/cgo/testso