cmd/cgo: build unique C type cache keys from parent names

When translating C types, cache the in-progress type under its parent
names, so that anonymous structs can also be translated for multiple
typedefs, without clashing.

Standalone types are not affected by this change.

Also updated the test for issue 9026 because the C struct name
generation algorithm has changed.

Fixes #31891

Change-Id: I00cc64852a2617ce33da13f74caec886af05b9f2
Reviewed-on: https://go-review.googlesource.com/c/go/+/181857
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Tai 2019-06-12 04:57:02 +08:00 committed by Ian Lance Taylor
parent 5f4aa5d79f
commit e13a4d9586
5 changed files with 49 additions and 5 deletions

View file

@ -2189,6 +2189,11 @@ func (c *typeConv) FinishType(pos token.Pos) {
// Type returns a *Type with the same memory layout as
// dtype when used as the type of a variable or a struct field.
func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
return c.loadType(dtype, pos, "")
}
// loadType recursively loads the requested dtype and its dependency graph.
func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Type {
// Always recompute bad pointer typedefs, as the set of such
// typedefs changes as we see more types.
checkCache := true
@ -2196,7 +2201,9 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
checkCache = false
}
key := dtype.String()
// The cache key should be relative to its parent.
// See issue https://golang.org/issue/31891
key := parent + " > " + dtype.String()
if checkCache {
if t, ok := c.m[key]; ok {
@ -2236,7 +2243,7 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
// Translate to zero-length array instead.
count = 0
}
sub := c.Type(dt.Type, pos)
sub := c.loadType(dt.Type, pos, key)
t.Align = sub.Align
t.Go = &ast.ArrayType{
Len: c.intExpr(count),
@ -2381,7 +2388,7 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
c.ptrs[key] = append(c.ptrs[key], t)
case *dwarf.QualType:
t1 := c.Type(dt.Type, pos)
t1 := c.loadType(dt.Type, pos, key)
t.Size = t1.Size
t.Align = t1.Align
t.Go = t1.Go
@ -2465,7 +2472,7 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
}
name := c.Ident("_Ctype_" + dt.Name)
goIdent[name.Name] = name
sub := c.Type(dt.Type, pos)
sub := c.loadType(dt.Type, pos, key)
if c.badPointerTypedef(dt) {
// Treat this typedef as a uintptr.
s := *sub