diff options
author | Russ Cox <rsc@golang.org> | 2013-11-07 15:24:51 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2013-11-07 15:24:51 -0500 |
commit | b8365ed0bf1e36a60b0e69805c1b49da77010607 (patch) | |
tree | 79ddd69917dbb676e21a2a9e2f2cfbfbca565cad /src/cmd/cgo | |
parent | af665d035591a9a1e3be311d8f483c62582b27b6 (diff) | |
download | go-b8365ed0bf1e36a60b0e69805c1b49da77010607.tar.gz |
cmd/cgo: fix handling of array of pointers when using clang
Clang does not record the "size" field for pointer types,
so we must insert the size ourselves. We were already
doing this, but only for the case of pointer types.
For an array of pointer types, the setting of the size for
the nested pointer type was happening after the computation
of the size of the array type, meaning that the array type
was always computed as 0 bytes. Delay the size computation.
This bug happens on all Clang systems, not just FreeBSD.
Our test checked that cgo wrote something, not that it was correct.
FreeBSD's default clang rejects array[0] as a C struct field,
so it noticed the incorrect sizes. But the sizes were incorrect
everywhere.
Update testcdefs to check the output has the right semantics.
Fixes issue 6292.
R=golang-dev, iant
CC=golang-dev
https://codereview.appspot.com/22840043
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r-- | src/cmd/cgo/gcc.go | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index f2a109d34..3e1837ebf 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -1046,21 +1046,11 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type { } t := new(Type) - t.Size = dtype.Size() + t.Size = dtype.Size() // note: wrong for array of pointers, corrected below t.Align = -1 t.C = &TypeRepr{Repr: dtype.Common().Name} c.m[dtype] = t - if t.Size < 0 { - // Unsized types are [0]byte - t.Size = 0 - t.Go = c.Opaque(0) - if t.C.Empty() { - t.C.Set("void") - } - return t - } - switch dt := dtype.(type) { default: fatalf("%s: unexpected type: %s", lineno(pos), dtype) @@ -1207,6 +1197,9 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type { return t case *dwarf.StructType: + if dt.ByteSize < 0 { // opaque struct + break + } // Convert to Go struct, being careful about alignment. // Have to give it a name to simulate C "struct foo" references. tag := dt.StructName @@ -1325,6 +1318,25 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type { } } + if t.Size <= 0 { + // Clang does not record the size of a pointer in its DWARF entry, + // so if dtype is an array, the call to dtype.Size at the top of the function + // computed the size as the array length * 0 = 0. + // The type switch called Type (this function) recursively on the pointer + // entry, and the code near the top of the function updated the size to + // be correct, so calling dtype.Size again will produce the correct value. + t.Size = dtype.Size() + if t.Size < 0 { + // Unsized types are [0]byte + t.Size = 0 + t.Go = c.Opaque(0) + if t.C.Empty() { + t.C.Set("void") + } + return t + } + } + if t.C.Empty() { fatalf("%s: internal error: did not create C name for %s", lineno(pos), dtype) } |