Skip to content

Commit 2d22b84

Browse files
ianlancetayloradg
authored andcommitted
cmd/cgo: recognize known C typedefs as types
Fixes #14483. Change-Id: I0cddfe27fd8d00ba85659d0b618410e39ebf45cb Reviewed-on: https://go-review.googlesource.com/19860 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-on: https://go-review.googlesource.com/22041 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e44998b commit 2d22b84

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

misc/cgo/errors/ptr.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ var ptrTests = []ptrTest{
122122
body: `i := 0; p := &S{p:&i}; s := p.a[:]; C.f(unsafe.Pointer(&s[0]))`,
123123
fail: false,
124124
},
125+
{
126+
// Passing the address of a slice of an array that is
127+
// an element in a struct, with a type conversion.
128+
name: "slice-ok-4",
129+
c: `typedef void* PV; void f(PV p) {}`,
130+
imports: []string{"unsafe"},
131+
support: `type S struct { p *int; a [4]byte }`,
132+
body: `i := 0; p := &S{p:&i}; C.f(C.PV(unsafe.Pointer(&p.a[0])))`,
133+
fail: false,
134+
},
125135
{
126136
// Passing the address of a static variable with no
127137
// pointers doesn't matter.

src/cmd/cgo/gcc.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,14 +819,17 @@ func (p *Package) hasSideEffects(f *File, x ast.Expr) bool {
819819
func (p *Package) isType(t ast.Expr) bool {
820820
switch t := t.(type) {
821821
case *ast.SelectorExpr:
822-
if t.Sel.Name != "Pointer" {
823-
return false
824-
}
825822
id, ok := t.X.(*ast.Ident)
826823
if !ok {
827824
return false
828825
}
829-
return id.Name == "unsafe"
826+
if id.Name == "unsafe" && t.Sel.Name == "Pointer" {
827+
return true
828+
}
829+
if id.Name == "C" && typedef["_Ctype_"+t.Sel.Name] != nil {
830+
return true
831+
}
832+
return false
830833
case *ast.Ident:
831834
// TODO: This ignores shadowing.
832835
switch t.Name {

0 commit comments

Comments
 (0)