cmd/cgo: move typedefs and typedefList out of Package

Theyre moved into a new fileTypedefs type so we can better keep track of
their lifetime.

For #75167

Change-Id: I6a6a696491d00eb4b1cc56dfcb9e94ed53573a7a
Reviewed-on: https://go-review.googlesource.com/c/go/+/699015
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
matloob 2025-08-25 16:50:06 -04:00 committed by Michael Matloob
parent 1d459c4357
commit a7d9d5a80a
2 changed files with 27 additions and 25 deletions

View file

@ -195,13 +195,12 @@ func (p *Package) Translate(f *File) {
var conv typeConv var conv typeConv
conv.Init(p.PtrSize, p.IntSize) conv.Init(p.PtrSize, p.IntSize)
p.typedefs = map[string]bool{} ft := fileTypedefs{typedefs: make(map[string]bool)}
p.typedefList = nil
numTypedefs := -1 numTypedefs := -1
for len(p.typedefs) > numTypedefs { for len(ft.typedefs) > numTypedefs {
numTypedefs = len(p.typedefs) numTypedefs = len(ft.typedefs)
// Also ask about any typedefs we've seen so far. // Also ask about any typedefs we've seen so far.
for _, info := range p.typedefList { for _, info := range ft.typedefList {
if f.Name[info.typedef] != nil { if f.Name[info.typedef] != nil {
continue continue
} }
@ -214,7 +213,7 @@ func (p *Package) Translate(f *File) {
} }
needType := p.guessKinds(f) needType := p.guessKinds(f)
if len(needType) > 0 { if len(needType) > 0 {
p.loadDWARF(f, &conv, needType) p.loadDWARF(f, &ft, &conv, needType)
} }
// In godefs mode we're OK with the typedefs, which // In godefs mode we're OK with the typedefs, which
@ -523,7 +522,7 @@ func (p *Package) guessKinds(f *File) []*Name {
// loadDWARF parses the DWARF debug information generated // loadDWARF parses the DWARF debug information generated
// by gcc to learn the details of the constants, variables, and types // by gcc to learn the details of the constants, variables, and types
// being referred to as C.xxx. // being referred to as C.xxx.
func (p *Package) loadDWARF(f *File, conv *typeConv, names []*Name) { func (p *Package) loadDWARF(f *File, ft *fileTypedefs, conv *typeConv, names []*Name) {
// Extract the types from the DWARF section of an object // Extract the types from the DWARF section of an object
// from a well-formed C program. Gcc only generates DWARF info // from a well-formed C program. Gcc only generates DWARF info
// for symbols in the object file, so it is not enough to print the // for symbols in the object file, so it is not enough to print the
@ -637,7 +636,7 @@ func (p *Package) loadDWARF(f *File, conv *typeConv, names []*Name) {
fatalf("malformed __cgo__ name: %s", name) fatalf("malformed __cgo__ name: %s", name)
} }
types[i] = t.Type types[i] = t.Type
p.recordTypedefs(t.Type, f.NamePos[names[i]]) ft.recordTypedefs(t.Type, f.NamePos[names[i]])
} }
if e.Tag != dwarf.TagCompileUnit { if e.Tag != dwarf.TagCompileUnit {
r.SkipChildren() r.SkipChildren()
@ -702,12 +701,17 @@ func (p *Package) loadDWARF(f *File, conv *typeConv, names []*Name) {
} }
} }
// recordTypedefs remembers in p.typedefs all the typedefs used in dtypes and its children. type fileTypedefs struct {
func (p *Package) recordTypedefs(dtype dwarf.Type, pos token.Pos) { typedefs map[string]bool // type names that appear in the types of the objects we're interested in
p.recordTypedefs1(dtype, pos, map[dwarf.Type]bool{}) typedefList []typedefInfo
} }
func (p *Package) recordTypedefs1(dtype dwarf.Type, pos token.Pos, visited map[dwarf.Type]bool) { // recordTypedefs remembers in ft.typedefs all the typedefs used in dtypes and its children.
func (ft *fileTypedefs) recordTypedefs(dtype dwarf.Type, pos token.Pos) {
ft.recordTypedefs1(dtype, pos, map[dwarf.Type]bool{})
}
func (ft *fileTypedefs) recordTypedefs1(dtype dwarf.Type, pos token.Pos, visited map[dwarf.Type]bool) {
if dtype == nil { if dtype == nil {
return return
} }
@ -721,25 +725,25 @@ func (p *Package) recordTypedefs1(dtype dwarf.Type, pos token.Pos, visited map[d
// Don't look inside builtin types. There be dragons. // Don't look inside builtin types. There be dragons.
return return
} }
if !p.typedefs[dt.Name] { if !ft.typedefs[dt.Name] {
p.typedefs[dt.Name] = true ft.typedefs[dt.Name] = true
p.typedefList = append(p.typedefList, typedefInfo{dt.Name, pos}) ft.typedefList = append(ft.typedefList, typedefInfo{dt.Name, pos})
p.recordTypedefs1(dt.Type, pos, visited) ft.recordTypedefs1(dt.Type, pos, visited)
} }
case *dwarf.PtrType: case *dwarf.PtrType:
p.recordTypedefs1(dt.Type, pos, visited) ft.recordTypedefs1(dt.Type, pos, visited)
case *dwarf.ArrayType: case *dwarf.ArrayType:
p.recordTypedefs1(dt.Type, pos, visited) ft.recordTypedefs1(dt.Type, pos, visited)
case *dwarf.QualType: case *dwarf.QualType:
p.recordTypedefs1(dt.Type, pos, visited) ft.recordTypedefs1(dt.Type, pos, visited)
case *dwarf.FuncType: case *dwarf.FuncType:
p.recordTypedefs1(dt.ReturnType, pos, visited) ft.recordTypedefs1(dt.ReturnType, pos, visited)
for _, a := range dt.ParamType { for _, a := range dt.ParamType {
p.recordTypedefs1(a, pos, visited) ft.recordTypedefs1(a, pos, visited)
} }
case *dwarf.StructType: case *dwarf.StructType:
for _, f := range dt.Field { for _, l := range dt.Field {
p.recordTypedefs1(f.Type, pos, visited) ft.recordTypedefs1(l.Type, pos, visited)
} }
} }
} }

View file

@ -49,8 +49,6 @@ type Package struct {
GoFiles []string // list of Go files GoFiles []string // list of Go files
GccFiles []string // list of gcc output files GccFiles []string // list of gcc output files
Preamble string // collected preamble for _cgo_export.h Preamble string // collected preamble for _cgo_export.h
typedefs map[string]bool // type names that appear in the types of the objects we're interested in
typedefList []typedefInfo
noCallbacks map[string]bool // C function names with #cgo nocallback directive noCallbacks map[string]bool // C function names with #cgo nocallback directive
noEscapes map[string]bool // C function names with #cgo noescape directive noEscapes map[string]bool // C function names with #cgo noescape directive
} }