]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: recognize known C typedefs as types
authorIan Lance Taylor <iant@golang.org>
Wed, 24 Feb 2016 00:11:46 +0000 (16:11 -0800)
committerIan Lance Taylor <iant@golang.org>
Sat, 27 Feb 2016 22:51:51 +0000 (22:51 +0000)
Fixes #14483.

Change-Id: I0cddfe27fd8d00ba85659d0b618410e39ebf45cb
Reviewed-on: https://go-review.googlesource.com/19860
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/cgo/errors/ptr.go
src/cmd/cgo/gcc.go

index 834cde9199175de2df55b3223bc4ff4f549c0394..bbcaaabecb7bd4fb4ebed2eb48a70fe89564047e 100644 (file)
@@ -122,6 +122,16 @@ var ptrTests = []ptrTest{
                body:    `i := 0; p := &S{p:&i}; s := p.a[:]; C.f(unsafe.Pointer(&s[0]))`,
                fail:    false,
        },
+       {
+               // Passing the address of a slice of an array that is
+               // an element in a struct, with a type conversion.
+               name:    "slice-ok-4",
+               c:       `typedef void* PV; void f(PV p) {}`,
+               imports: []string{"unsafe"},
+               support: `type S struct { p *int; a [4]byte }`,
+               body:    `i := 0; p := &S{p:&i}; C.f(C.PV(unsafe.Pointer(&p.a[0])))`,
+               fail:    false,
+       },
        {
                // Passing the address of a static variable with no
                // pointers doesn't matter.
index 5bfdef785c10556dfb12dc88a1b3786b6628a63a..fe9af1769dc7ff1a0e781270dfe2f91503980c5c 100644 (file)
@@ -819,14 +819,17 @@ func (p *Package) hasSideEffects(f *File, x ast.Expr) bool {
 func (p *Package) isType(t ast.Expr) bool {
        switch t := t.(type) {
        case *ast.SelectorExpr:
-               if t.Sel.Name != "Pointer" {
-                       return false
-               }
                id, ok := t.X.(*ast.Ident)
                if !ok {
                        return false
                }
-               return id.Name == "unsafe"
+               if id.Name == "unsafe" && t.Sel.Name == "Pointer" {
+                       return true
+               }
+               if id.Name == "C" && typedef["_Ctype_"+t.Sel.Name] != nil {
+                       return true
+               }
+               return false
        case *ast.Ident:
                // TODO: This ignores shadowing.
                switch t.Name {