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>
diff --git a/src/cmd/fix/cftype.go b/src/cmd/fix/cftype.go
index 5e742a4..3341348 100644
--- a/src/cmd/fix/cftype.go
+++ b/src/cmd/fix/cftype.go
@@ -30,19 +30,17 @@
// and similar for other *Ref types.
// This fix finds nils initializing these types and replaces the nils with 0s.
func cftypefix(f *ast.File) bool {
- var tc TypeConfig
- return typefix(f, &tc, func(s string) bool {
- return strings.HasPrefix(s, "C.") && strings.HasSuffix(s, "Ref") &&
- (s == "C.CFTypeRef" || tc.External[s[:len(s)-3]+"GetTypeID"] == "func() C.CFTypeID")
+ return typefix(f, func(s string) bool {
+ return strings.HasPrefix(s, "C.") && strings.HasSuffix(s, "Ref") && s != "C.CFAllocatorRef"
})
}
// 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") {
return false
}
- typeof, _ := typecheck(tc, f)
+ typeof, _ := typecheck(&TypeConfig{}, f)
// step 1: Find all the nils with the offending types.
// Compute their replacement.
diff --git a/src/cmd/fix/jnitype.go b/src/cmd/fix/jnitype.go
index 75ae570..29abe0f 100644
--- a/src/cmd/fix/jnitype.go
+++ b/src/cmd/fix/jnitype.go
@@ -27,8 +27,7 @@
// and similar for subtypes of jobject.
// This fix finds nils initializing these types and replaces the nils with 0s.
func jnifix(f *ast.File) bool {
- var tc TypeConfig
- return typefix(f, &tc, func(s string) bool {
+ return typefix(f, func(s string) bool {
switch s {
case "C.jobject":
return true
diff --git a/src/cmd/fix/typecheck.go b/src/cmd/fix/typecheck.go
index a52a542..c5900d8 100644
--- a/src/cmd/fix/typecheck.go
+++ b/src/cmd/fix/typecheck.go
@@ -152,6 +152,7 @@
// If we import "C", add types of cgo objects.
cfg.External = map[string]string{}
+ cfg1.External = cfg.External
if imports(f, "C") {
// Run cgo on gofmtFile(f)
// Parse, extract decls from _cgo_gotypes.go