1
0
mirror of https://github.com/golang/go synced 2024-11-18 00:14:47 -07:00

cmd/fix: don't depend on *GetTypeID functions being present

cgo uses the presence of these functions to determine whether
a given type is in the CFTypeRef hierarchy and thus should be
a uintptr instead of a pointer. But if the *GetTypeID functions
aren't used by the user code, then they won't be present in the
cgo output, and thus cmd/fix won't see them.

Use the simpler rule that anything ending in *Ref should be
rewritten. This could over-rewrite, but I don't see a simpler
solution. Unlike cgo, it is easy to edit the output to fix any
issues. And fix is a much rarer operation than cgo.

This is a revert of portions of CL 87616.

Update #23091

Change-Id: I74ecd9fb25490a3d279b372e107248452bb62185
Reviewed-on: https://go-review.googlesource.com/88075
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Keith Randall 2018-01-17 10:38:10 -08:00
parent 18d527b3f0
commit bd89333426
3 changed files with 6 additions and 8 deletions

View File

@ -30,19 +30,17 @@ var cftypeFix = fix{
// and similar for other *Ref types. // and similar for other *Ref types.
// This fix finds nils initializing these types and replaces the nils with 0s. // This fix finds nils initializing these types and replaces the nils with 0s.
func cftypefix(f *ast.File) bool { func cftypefix(f *ast.File) bool {
var tc TypeConfig return typefix(f, func(s string) bool {
return typefix(f, &tc, func(s string) bool { return strings.HasPrefix(s, "C.") && strings.HasSuffix(s, "Ref") && s != "C.CFAllocatorRef"
return strings.HasPrefix(s, "C.") && strings.HasSuffix(s, "Ref") &&
(s == "C.CFTypeRef" || tc.External[s[:len(s)-3]+"GetTypeID"] == "func() C.CFTypeID")
}) })
} }
// typefix replaces nil with 0 for all nils whose type, when passed to badType, returns true. // typefix replaces nil with 0 for all nils whose type, when passed to badType, returns true.
func typefix(f *ast.File, tc *TypeConfig, badType func(string) bool) bool { func typefix(f *ast.File, badType func(string) bool) bool {
if !imports(f, "C") { if !imports(f, "C") {
return false return false
} }
typeof, _ := typecheck(tc, f) typeof, _ := typecheck(&TypeConfig{}, f)
// step 1: Find all the nils with the offending types. // step 1: Find all the nils with the offending types.
// Compute their replacement. // Compute their replacement.

View File

@ -27,8 +27,7 @@ var jniFix = fix{
// and similar for subtypes of jobject. // and similar for subtypes of jobject.
// This fix finds nils initializing these types and replaces the nils with 0s. // This fix finds nils initializing these types and replaces the nils with 0s.
func jnifix(f *ast.File) bool { func jnifix(f *ast.File) bool {
var tc TypeConfig return typefix(f, func(s string) bool {
return typefix(f, &tc, func(s string) bool {
switch s { switch s {
case "C.jobject": case "C.jobject":
return true return true

View File

@ -152,6 +152,7 @@ func typecheck(cfg *TypeConfig, f *ast.File) (typeof map[interface{}]string, ass
// If we import "C", add types of cgo objects. // If we import "C", add types of cgo objects.
cfg.External = map[string]string{} cfg.External = map[string]string{}
cfg1.External = cfg.External
if imports(f, "C") { if imports(f, "C") {
// Run cgo on gofmtFile(f) // Run cgo on gofmtFile(f)
// Parse, extract decls from _cgo_gotypes.go // Parse, extract decls from _cgo_gotypes.go