Skip to content

Commit d50390c

Browse files
author
Elias Naur
committed
cmd/fix,cmd/cgo,misc/cgo: map the EGLDisplay C type to uintptr in Go
Similar to to macOS' CF* types and JNI's jobject and derived types, the EGLDisplay type is declared as a pointer but can contain non-pointers (see golang#27054). Fix it the same way: map EGLDisplay to uintptr in Go. Fixes golang#27054 RELNOTE=yes Change-Id: I6136f8f8162687c5493b30ed324e29efe55a8fd7 Reviewed-on: https://go-review.googlesource.com/c/154417 Run-TryBot: Elias Naur <elias.naur@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
1 parent 26985ed commit d50390c

File tree

5 files changed

+257
-0
lines changed

5 files changed

+257
-0
lines changed

misc/cgo/test/issue27054/egl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// This is the relevant part of EGL/egl.h.
6+
7+
typedef void *EGLDisplay;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package issue27054
6+
7+
/*
8+
#include "egl.h"
9+
*/
10+
import "C"
11+
import (
12+
"testing"
13+
)
14+
15+
func Test27054(t *testing.T) {
16+
var _ C.EGLDisplay = 0 // Note: 0, not nil. That makes sure we use uintptr for this type.
17+
}

src/cmd/cgo/gcc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,9 @@ func (c *typeConv) badPointerTypedef(dt *dwarf.TypedefType) bool {
30043004
if c.badJNI(dt) {
30053005
return true
30063006
}
3007+
if c.badEGLDisplay(dt) {
3008+
return true
3009+
}
30073010
return false
30083011
}
30093012

@@ -3140,6 +3143,19 @@ func (c *typeConv) badJNI(dt *dwarf.TypedefType) bool {
31403143
return false
31413144
}
31423145

3146+
func (c *typeConv) badEGLDisplay(dt *dwarf.TypedefType) bool {
3147+
if dt.Name != "EGLDisplay" {
3148+
return false
3149+
}
3150+
// Check that the typedef is "typedef void *EGLDisplay".
3151+
if ptr, ok := dt.Type.(*dwarf.PtrType); ok {
3152+
if _, ok := ptr.Type.(*dwarf.VoidType); ok {
3153+
return true
3154+
}
3155+
}
3156+
return false
3157+
}
3158+
31433159
// jniTypes maps from JNI types that we want to be uintptrs, to the underlying type to which
31443160
// they are mapped. The base "jobject" maps to the empty string.
31453161
var jniTypes = map[string]string{

src/cmd/fix/egltype.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"go/ast"
9+
)
10+
11+
func init() {
12+
register(eglFix)
13+
}
14+
15+
var eglFix = fix{
16+
name: "egl",
17+
date: "2018-12-15",
18+
f: eglfix,
19+
desc: `Fixes initializers of EGLDisplay`,
20+
disabled: false,
21+
}
22+
23+
// Old state:
24+
// type EGLDisplay unsafe.Pointer
25+
// New state:
26+
// type EGLDisplay uintptr
27+
// This fix finds nils initializing these types and replaces the nils with 0s.
28+
func eglfix(f *ast.File) bool {
29+
return typefix(f, func(s string) bool {
30+
return s == "C.EGLDisplay"
31+
})
32+
}

src/cmd/fix/egltype_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
func init() {
8+
addTestCases(eglTests, eglfix)
9+
}
10+
11+
var eglTests = []testCase{
12+
{
13+
Name: "egl.localVariable",
14+
In: `package main
15+
16+
import "C"
17+
18+
func f() {
19+
var x C.EGLDisplay = nil
20+
x = nil
21+
x, x = nil, nil
22+
}
23+
`,
24+
Out: `package main
25+
26+
import "C"
27+
28+
func f() {
29+
var x C.EGLDisplay = 0
30+
x = 0
31+
x, x = 0, 0
32+
}
33+
`,
34+
},
35+
{
36+
Name: "egl.globalVariable",
37+
In: `package main
38+
39+
import "C"
40+
41+
var x C.EGLDisplay = nil
42+
43+
func f() {
44+
x = nil
45+
}
46+
`,
47+
Out: `package main
48+
49+
import "C"
50+
51+
var x C.EGLDisplay = 0
52+
53+
func f() {
54+
x = 0
55+
}
56+
`,
57+
},
58+
{
59+
Name: "egl.EqualArgument",
60+
In: `package main
61+
62+
import "C"
63+
64+
var x C.EGLDisplay
65+
var y = x == nil
66+
var z = x != nil
67+
`,
68+
Out: `package main
69+
70+
import "C"
71+
72+
var x C.EGLDisplay
73+
var y = x == 0
74+
var z = x != 0
75+
`,
76+
},
77+
{
78+
Name: "egl.StructField",
79+
In: `package main
80+
81+
import "C"
82+
83+
type T struct {
84+
x C.EGLDisplay
85+
}
86+
87+
var t = T{x: nil}
88+
`,
89+
Out: `package main
90+
91+
import "C"
92+
93+
type T struct {
94+
x C.EGLDisplay
95+
}
96+
97+
var t = T{x: 0}
98+
`,
99+
},
100+
{
101+
Name: "egl.FunctionArgument",
102+
In: `package main
103+
104+
import "C"
105+
106+
func f(x C.EGLDisplay) {
107+
}
108+
109+
func g() {
110+
f(nil)
111+
}
112+
`,
113+
Out: `package main
114+
115+
import "C"
116+
117+
func f(x C.EGLDisplay) {
118+
}
119+
120+
func g() {
121+
f(0)
122+
}
123+
`,
124+
},
125+
{
126+
Name: "egl.ArrayElement",
127+
In: `package main
128+
129+
import "C"
130+
131+
var x = [3]C.EGLDisplay{nil, nil, nil}
132+
`,
133+
Out: `package main
134+
135+
import "C"
136+
137+
var x = [3]C.EGLDisplay{0, 0, 0}
138+
`,
139+
},
140+
{
141+
Name: "egl.SliceElement",
142+
In: `package main
143+
144+
import "C"
145+
146+
var x = []C.EGLDisplay{nil, nil, nil}
147+
`,
148+
Out: `package main
149+
150+
import "C"
151+
152+
var x = []C.EGLDisplay{0, 0, 0}
153+
`,
154+
},
155+
{
156+
Name: "egl.MapKey",
157+
In: `package main
158+
159+
import "C"
160+
161+
var x = map[C.EGLDisplay]int{nil: 0}
162+
`,
163+
Out: `package main
164+
165+
import "C"
166+
167+
var x = map[C.EGLDisplay]int{0: 0}
168+
`,
169+
},
170+
{
171+
Name: "egl.MapValue",
172+
In: `package main
173+
174+
import "C"
175+
176+
var x = map[int]C.EGLDisplay{0: nil}
177+
`,
178+
Out: `package main
179+
180+
import "C"
181+
182+
var x = map[int]C.EGLDisplay{0: 0}
183+
`,
184+
},
185+
}

0 commit comments

Comments
 (0)