]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo,cmd/fix,misc/cgo: map the EGLConfig C type to uintptr in Go
authorElias Naur <mail@eliasnaur.com>
Sat, 30 May 2020 14:34:23 +0000 (16:34 +0200)
committerElias Naur <mail@eliasnaur.com>
Sun, 31 May 2020 09:48:08 +0000 (09:48 +0000)
Similarly to EGLDisplay, EGLConfig is declared as a pointer but may
contain non-pointer values.

I believe this is the root cause of https://todo.sr.ht/~eliasnaur/gio/121.

Change-Id: I412c4fbc2eef4aa028534d68bda95db98e3a365d
Reviewed-on: https://go-review.googlesource.com/c/go/+/235817
Run-TryBot: Elias Naur <mail@eliasnaur.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/test/testdata/issue27054/egl.h
misc/cgo/test/testdata/issue27054/test27054.go
src/cmd/cgo/doc.go
src/cmd/cgo/gcc.go
src/cmd/fix/egltype.go
src/cmd/fix/egltype_test.go

index 33a759ea2a831704bfee6fd41b5d3e495130c07c..30796273e80b96fa401ac5d6cf2f734813093db4 100644 (file)
@@ -5,3 +5,4 @@
 // This is the relevant part of EGL/egl.h.
 
 typedef void *EGLDisplay;
+typedef void *EGLConfig;
index 186f5bd602078d7ac9b1c3f635c73c239771a69b..01bf43a913b6e0f427b7eb406f2bae90289613f0 100644 (file)
@@ -13,5 +13,9 @@ import (
 )
 
 func Test27054(t *testing.T) {
-       var _ C.EGLDisplay = 0 // Note: 0, not nil. That makes sure we use uintptr for this type.
+       var (
+               // Note: 0, not nil. That makes sure we use uintptr for these types.
+               _ C.EGLDisplay = 0
+               _ C.EGLConfig  = 0
+       )
 }
index 8c3bf81bf78925ec0b1bf35ef3cc4bd955c3c4f3..4366df4b55ef70ee59e317ed568e6fd5e068d8df 100644 (file)
@@ -413,7 +413,7 @@ type in Go are instead represented by a uintptr. Those include:
        jobjectArray
        jweak
 
-3. The EGLDisplay type from the EGL API.
+3. The EGLDisplay and EGLConfig types from the EGL API.
 
 These types are uintptr on the Go side because they would otherwise
 confuse the Go garbage collector; they are sometimes not really
@@ -429,11 +429,16 @@ from Go 1.9 and earlier, use the cftype or jni rewrites in the Go fix tool:
 
 It will replace nil with 0 in the appropriate places.
 
-The EGLDisplay case were introduced in Go 1.12. Use the egl rewrite
+The EGLDisplay case was introduced in Go 1.12. Use the egl rewrite
 to auto-update code from Go 1.11 and earlier:
 
        go tool fix -r egl <pkg>
 
+The EGLConfig case was introduced in Go 1.15. Use the eglconf rewrite
+to auto-update code from Go 1.14 and earlier:
+
+       go tool fix -r eglconf <pkg>
+
 Using cgo directly
 
 Usage:
index edcbd8d2d1422f229fbcc9ac6fd2945dbffd77a3..d903a7afb569e3cbabc770a93cbf43856826d587 100644 (file)
@@ -3029,7 +3029,7 @@ func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
        if c.badJNI(dt) {
                return true
        }
-       if c.badEGLDisplay(dt) {
+       if c.badEGLType(dt) {
                return true
        }
        return false
@@ -3168,11 +3168,11 @@ func (c *typeConv) badJNI(dt *dwarf.TypedefType) bool {
        return false
 }
 
-func (c *typeConv) badEGLDisplay(dt *dwarf.TypedefType) bool {
-       if dt.Name != "EGLDisplay" {
+func (c *typeConv) badEGLType(dt *dwarf.TypedefType) bool {
+       if dt.Name != "EGLDisplay" && dt.Name != "EGLConfig" {
                return false
        }
-       // Check that the typedef is "typedef void *EGLDisplay".
+       // Check that the typedef is "typedef void *<name>".
        if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
                if _, ok := ptr.Type.(*dwarf.VoidType); ok {
                        return true
index c8c4f03e97f2bb2eea2bc9938525216626947550..cb0f7a73de1ddd20a4e8dcf8307d200116f2cb43 100644 (file)
@@ -9,13 +9,14 @@ import (
 )
 
 func init() {
-       register(eglFix)
+       register(eglFixDisplay)
+       register(eglFixConfig)
 }
 
-var eglFix = fix{
+var eglFixDisplay = fix{
        name:     "egl",
        date:     "2018-12-15",
-       f:        eglfix,
+       f:        eglfixDisp,
        desc:     `Fixes initializers of EGLDisplay`,
        disabled: false,
 }
@@ -25,8 +26,27 @@ var eglFix = fix{
 // New state:
 //   type EGLDisplay uintptr
 // This fix finds nils initializing these types and replaces the nils with 0s.
-func eglfix(f *ast.File) bool {
+func eglfixDisp(f *ast.File) bool {
        return typefix(f, func(s string) bool {
                return s == "C.EGLDisplay"
        })
 }
+
+var eglFixConfig = fix{
+       name:     "eglconf",
+       date:     "2020-05-30",
+       f:        eglfixConfig,
+       desc:     `Fixes initializers of EGLConfig`,
+       disabled: false,
+}
+
+// Old state:
+//   type EGLConfig unsafe.Pointer
+// New state:
+//   type EGLConfig uintptr
+// This fix finds nils initializing these types and replaces the nils with 0s.
+func eglfixConfig(f *ast.File) bool {
+       return typefix(f, func(s string) bool {
+               return s == "C.EGLConfig"
+       })
+}
index 35ffe925958a8cf36cccf6e7c6678b3b32c35394..9b64a7c20b4676d3f86de14b9a72726165bf0190 100644 (file)
 
 package main
 
+import "strings"
+
 func init() {
-       addTestCases(eglTests, eglfix)
+       addTestCases(eglTestsFor("EGLDisplay"), eglfixDisp)
+       addTestCases(eglTestsFor("EGLConfig"), eglfixConfig)
 }
 
-var eglTests = []testCase{
-       {
-               Name: "egl.localVariable",
-               In: `package main
+func eglTestsFor(tname string) []testCase {
+       var eglTests = []testCase{
+               {
+                       Name: "egl.localVariable",
+                       In: `package main
 
 import "C"
 
 func f() {
-       var x C.EGLDisplay = nil
+       var x C.$EGLTYPE = nil
        x = nil
        x, x = nil, nil
 }
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
 func f() {
-       var x C.EGLDisplay = 0
+       var x C.$EGLTYPE = 0
        x = 0
        x, x = 0, 0
 }
 `,
-       },
-       {
-               Name: "egl.globalVariable",
-               In: `package main
+               },
+               {
+                       Name: "egl.globalVariable",
+                       In: `package main
 
 import "C"
 
-var x C.EGLDisplay = nil
+var x C.$EGLTYPE = nil
 
 func f() {
        x = nil
 }
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x C.EGLDisplay = 0
+var x C.$EGLTYPE = 0
 
 func f() {
        x = 0
 }
 `,
-       },
-       {
-               Name: "egl.EqualArgument",
-               In: `package main
+               },
+               {
+                       Name: "egl.EqualArgument",
+                       In: `package main
 
 import "C"
 
-var x C.EGLDisplay
+var x C.$EGLTYPE
 var y = x == nil
 var z = x != nil
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x C.EGLDisplay
+var x C.$EGLTYPE
 var y = x == 0
 var z = x != 0
 `,
-       },
-       {
-               Name: "egl.StructField",
-               In: `package main
+               },
+               {
+                       Name: "egl.StructField",
+                       In: `package main
 
 import "C"
 
 type T struct {
-       x C.EGLDisplay
+       x C.$EGLTYPE
 }
 
 var t = T{x: nil}
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
 type T struct {
-       x C.EGLDisplay
+       x C.$EGLTYPE
 }
 
 var t = T{x: 0}
 `,
-       },
-       {
-               Name: "egl.FunctionArgument",
-               In: `package main
+               },
+               {
+                       Name: "egl.FunctionArgument",
+                       In: `package main
 
 import "C"
 
-func f(x C.EGLDisplay) {
+func f(x C.$EGLTYPE) {
 }
 
 func g() {
        f(nil)
 }
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-func f(x C.EGLDisplay) {
+func f(x C.$EGLTYPE) {
 }
 
 func g() {
        f(0)
 }
 `,
-       },
-       {
-               Name: "egl.ArrayElement",
-               In: `package main
+               },
+               {
+                       Name: "egl.ArrayElement",
+                       In: `package main
 
 import "C"
 
-var x = [3]C.EGLDisplay{nil, nil, nil}
+var x = [3]C.$EGLTYPE{nil, nil, nil}
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x = [3]C.EGLDisplay{0, 0, 0}
+var x = [3]C.$EGLTYPE{0, 0, 0}
 `,
-       },
-       {
-               Name: "egl.SliceElement",
-               In: `package main
+               },
+               {
+                       Name: "egl.SliceElement",
+                       In: `package main
 
 import "C"
 
-var x = []C.EGLDisplay{nil, nil, nil}
+var x = []C.$EGLTYPE{nil, nil, nil}
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x = []C.EGLDisplay{0, 0, 0}
+var x = []C.$EGLTYPE{0, 0, 0}
 `,
-       },
-       {
-               Name: "egl.MapKey",
-               In: `package main
+               },
+               {
+                       Name: "egl.MapKey",
+                       In: `package main
 
 import "C"
 
-var x = map[C.EGLDisplay]int{nil: 0}
+var x = map[C.$EGLTYPE]int{nil: 0}
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x = map[C.EGLDisplay]int{0: 0}
+var x = map[C.$EGLTYPE]int{0: 0}
 `,
-       },
-       {
-               Name: "egl.MapValue",
-               In: `package main
+               },
+               {
+                       Name: "egl.MapValue",
+                       In: `package main
 
 import "C"
 
-var x = map[int]C.EGLDisplay{0: nil}
+var x = map[int]C.$EGLTYPE{0: nil}
 `,
-               Out: `package main
+                       Out: `package main
 
 import "C"
 
-var x = map[int]C.EGLDisplay{0: 0}
+var x = map[int]C.$EGLTYPE{0: 0}
 `,
-       },
+               },
+       }
+       for i := range eglTests {
+               t := &eglTests[i]
+               t.In = strings.ReplaceAll(t.In, "$EGLTYPE", tname)
+               t.Out = strings.ReplaceAll(t.Out, "$EGLTYPE", tname)
+       }
+       return eglTests
 }