Skip to content

Commit 7dbbb5b

Browse files
committed
cmd/cgo,cmd/fix,misc/cgo: map the EGLConfig C type to uintptr in Go
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>
1 parent f1f8f9a commit 7dbbb5b

File tree

6 files changed

+117
-76
lines changed

6 files changed

+117
-76
lines changed

misc/cgo/test/testdata/issue27054/egl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
// This is the relevant part of EGL/egl.h.
66

77
typedef void *EGLDisplay;
8+
typedef void *EGLConfig;

misc/cgo/test/testdata/issue27054/test27054.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,9 @@ import (
1313
)
1414

1515
func Test27054(t *testing.T) {
16-
var _ C.EGLDisplay = 0 // Note: 0, not nil. That makes sure we use uintptr for this type.
16+
var (
17+
// Note: 0, not nil. That makes sure we use uintptr for these types.
18+
_ C.EGLDisplay = 0
19+
_ C.EGLConfig = 0
20+
)
1721
}

src/cmd/cgo/doc.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ type in Go are instead represented by a uintptr. Those include:
413413
jobjectArray
414414
jweak
415415
416-
3. The EGLDisplay type from the EGL API.
416+
3. The EGLDisplay and EGLConfig types from the EGL API.
417417
418418
These types are uintptr on the Go side because they would otherwise
419419
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:
429429
430430
It will replace nil with 0 in the appropriate places.
431431
432-
The EGLDisplay case were introduced in Go 1.12. Use the egl rewrite
432+
The EGLDisplay case was introduced in Go 1.12. Use the egl rewrite
433433
to auto-update code from Go 1.11 and earlier:
434434
435435
go tool fix -r egl <pkg>
436436
437+
The EGLConfig case was introduced in Go 1.15. Use the eglconf rewrite
438+
to auto-update code from Go 1.14 and earlier:
439+
440+
go tool fix -r eglconf <pkg>
441+
437442
Using cgo directly
438443
439444
Usage:

src/cmd/cgo/gcc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,7 @@ func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
30293029
if c.badJNI(dt) {
30303030
return true
30313031
}
3032-
if c.badEGLDisplay(dt) {
3032+
if c.badEGLType(dt) {
30333033
return true
30343034
}
30353035
return false
@@ -3168,11 +3168,11 @@ func (c *typeConv) badJNI(dt *dwarf.TypedefType) bool {
31683168
return false
31693169
}
31703170

3171-
func (c *typeConv) badEGLDisplay(dt *dwarf.TypedefType) bool {
3172-
if dt.Name != "EGLDisplay" {
3171+
func (c *typeConv) badEGLType(dt *dwarf.TypedefType) bool {
3172+
if dt.Name != "EGLDisplay" && dt.Name != "EGLConfig" {
31733173
return false
31743174
}
3175-
// Check that the typedef is "typedef void *EGLDisplay".
3175+
// Check that the typedef is "typedef void *<name>".
31763176
if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
31773177
if _, ok := ptr.Type.(*dwarf.VoidType); ok {
31783178
return true

src/cmd/fix/egltype.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
)
1010

1111
func init() {
12-
register(eglFix)
12+
register(eglFixDisplay)
13+
register(eglFixConfig)
1314
}
1415

15-
var eglFix = fix{
16+
var eglFixDisplay = fix{
1617
name: "egl",
1718
date: "2018-12-15",
18-
f: eglfix,
19+
f: eglfixDisp,
1920
desc: `Fixes initializers of EGLDisplay`,
2021
disabled: false,
2122
}
@@ -25,8 +26,27 @@ var eglFix = fix{
2526
// New state:
2627
// type EGLDisplay uintptr
2728
// This fix finds nils initializing these types and replaces the nils with 0s.
28-
func eglfix(f *ast.File) bool {
29+
func eglfixDisp(f *ast.File) bool {
2930
return typefix(f, func(s string) bool {
3031
return s == "C.EGLDisplay"
3132
})
3233
}
34+
35+
var eglFixConfig = fix{
36+
name: "eglconf",
37+
date: "2020-05-30",
38+
f: eglfixConfig,
39+
desc: `Fixes initializers of EGLConfig`,
40+
disabled: false,
41+
}
42+
43+
// Old state:
44+
// type EGLConfig unsafe.Pointer
45+
// New state:
46+
// type EGLConfig uintptr
47+
// This fix finds nils initializing these types and replaces the nils with 0s.
48+
func eglfixConfig(f *ast.File) bool {
49+
return typefix(f, func(s string) bool {
50+
return s == "C.EGLConfig"
51+
})
52+
}

src/cmd/fix/egltype_test.go

Lines changed: 76 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,182 +4,193 @@
44

55
package main
66

7+
import "strings"
8+
79
func init() {
8-
addTestCases(eglTests, eglfix)
10+
addTestCases(eglTestsFor("EGLDisplay"), eglfixDisp)
11+
addTestCases(eglTestsFor("EGLConfig"), eglfixConfig)
912
}
1013

11-
var eglTests = []testCase{
12-
{
13-
Name: "egl.localVariable",
14-
In: `package main
14+
func eglTestsFor(tname string) []testCase {
15+
var eglTests = []testCase{
16+
{
17+
Name: "egl.localVariable",
18+
In: `package main
1519
1620
import "C"
1721
1822
func f() {
19-
var x C.EGLDisplay = nil
23+
var x C.$EGLTYPE = nil
2024
x = nil
2125
x, x = nil, nil
2226
}
2327
`,
24-
Out: `package main
28+
Out: `package main
2529
2630
import "C"
2731
2832
func f() {
29-
var x C.EGLDisplay = 0
33+
var x C.$EGLTYPE = 0
3034
x = 0
3135
x, x = 0, 0
3236
}
3337
`,
34-
},
35-
{
36-
Name: "egl.globalVariable",
37-
In: `package main
38+
},
39+
{
40+
Name: "egl.globalVariable",
41+
In: `package main
3842
3943
import "C"
4044
41-
var x C.EGLDisplay = nil
45+
var x C.$EGLTYPE = nil
4246
4347
func f() {
4448
x = nil
4549
}
4650
`,
47-
Out: `package main
51+
Out: `package main
4852
4953
import "C"
5054
51-
var x C.EGLDisplay = 0
55+
var x C.$EGLTYPE = 0
5256
5357
func f() {
5458
x = 0
5559
}
5660
`,
57-
},
58-
{
59-
Name: "egl.EqualArgument",
60-
In: `package main
61+
},
62+
{
63+
Name: "egl.EqualArgument",
64+
In: `package main
6165
6266
import "C"
6367
64-
var x C.EGLDisplay
68+
var x C.$EGLTYPE
6569
var y = x == nil
6670
var z = x != nil
6771
`,
68-
Out: `package main
72+
Out: `package main
6973
7074
import "C"
7175
72-
var x C.EGLDisplay
76+
var x C.$EGLTYPE
7377
var y = x == 0
7478
var z = x != 0
7579
`,
76-
},
77-
{
78-
Name: "egl.StructField",
79-
In: `package main
80+
},
81+
{
82+
Name: "egl.StructField",
83+
In: `package main
8084
8185
import "C"
8286
8387
type T struct {
84-
x C.EGLDisplay
88+
x C.$EGLTYPE
8589
}
8690
8791
var t = T{x: nil}
8892
`,
89-
Out: `package main
93+
Out: `package main
9094
9195
import "C"
9296
9397
type T struct {
94-
x C.EGLDisplay
98+
x C.$EGLTYPE
9599
}
96100
97101
var t = T{x: 0}
98102
`,
99-
},
100-
{
101-
Name: "egl.FunctionArgument",
102-
In: `package main
103+
},
104+
{
105+
Name: "egl.FunctionArgument",
106+
In: `package main
103107
104108
import "C"
105109
106-
func f(x C.EGLDisplay) {
110+
func f(x C.$EGLTYPE) {
107111
}
108112
109113
func g() {
110114
f(nil)
111115
}
112116
`,
113-
Out: `package main
117+
Out: `package main
114118
115119
import "C"
116120
117-
func f(x C.EGLDisplay) {
121+
func f(x C.$EGLTYPE) {
118122
}
119123
120124
func g() {
121125
f(0)
122126
}
123127
`,
124-
},
125-
{
126-
Name: "egl.ArrayElement",
127-
In: `package main
128+
},
129+
{
130+
Name: "egl.ArrayElement",
131+
In: `package main
128132
129133
import "C"
130134
131-
var x = [3]C.EGLDisplay{nil, nil, nil}
135+
var x = [3]C.$EGLTYPE{nil, nil, nil}
132136
`,
133-
Out: `package main
137+
Out: `package main
134138
135139
import "C"
136140
137-
var x = [3]C.EGLDisplay{0, 0, 0}
141+
var x = [3]C.$EGLTYPE{0, 0, 0}
138142
`,
139-
},
140-
{
141-
Name: "egl.SliceElement",
142-
In: `package main
143+
},
144+
{
145+
Name: "egl.SliceElement",
146+
In: `package main
143147
144148
import "C"
145149
146-
var x = []C.EGLDisplay{nil, nil, nil}
150+
var x = []C.$EGLTYPE{nil, nil, nil}
147151
`,
148-
Out: `package main
152+
Out: `package main
149153
150154
import "C"
151155
152-
var x = []C.EGLDisplay{0, 0, 0}
156+
var x = []C.$EGLTYPE{0, 0, 0}
153157
`,
154-
},
155-
{
156-
Name: "egl.MapKey",
157-
In: `package main
158+
},
159+
{
160+
Name: "egl.MapKey",
161+
In: `package main
158162
159163
import "C"
160164
161-
var x = map[C.EGLDisplay]int{nil: 0}
165+
var x = map[C.$EGLTYPE]int{nil: 0}
162166
`,
163-
Out: `package main
167+
Out: `package main
164168
165169
import "C"
166170
167-
var x = map[C.EGLDisplay]int{0: 0}
171+
var x = map[C.$EGLTYPE]int{0: 0}
168172
`,
169-
},
170-
{
171-
Name: "egl.MapValue",
172-
In: `package main
173+
},
174+
{
175+
Name: "egl.MapValue",
176+
In: `package main
173177
174178
import "C"
175179
176-
var x = map[int]C.EGLDisplay{0: nil}
180+
var x = map[int]C.$EGLTYPE{0: nil}
177181
`,
178-
Out: `package main
182+
Out: `package main
179183
180184
import "C"
181185
182-
var x = map[int]C.EGLDisplay{0: 0}
186+
var x = map[int]C.$EGLTYPE{0: 0}
183187
`,
184-
},
188+
},
189+
}
190+
for i := range eglTests {
191+
t := &eglTests[i]
192+
t.In = strings.ReplaceAll(t.In, "$EGLTYPE", tname)
193+
t.Out = strings.ReplaceAll(t.Out, "$EGLTYPE", tname)
194+
}
195+
return eglTests
185196
}

0 commit comments

Comments
 (0)