]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: ignore top-level qualifiers in function args/results
authorIan Lance Taylor <iant@golang.org>
Wed, 16 Nov 2016 17:55:24 +0000 (09:55 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 17 Nov 2016 19:03:55 +0000 (19:03 +0000)
The top-level qualifiers are unimportant for our purposes. If a C
function is defined as `const int f(const int i)`, the `const`s are
meaningless to C, and we want to avoid using them in the struct we
create where the `const` has a completely different meaning.

This unwinds https://golang.org/cl/33097 with regard to top-level
qualifiers.

Change-Id: I3d66b0eb43b6d9a586d9cdedfae5a2306b46d96c
Reviewed-on: https://go-review.googlesource.com/33325
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
misc/cgo/test/issue17537.go
src/cmd/cgo/gcc.go

index a1558bc5edfa0ce5f0a5b379d4074ec0658aa861..debdbfe4c505b10902dc372cfc5e9f04f0643def 100644 (file)
@@ -20,7 +20,7 @@ int I17537(S17537 *p);
 #define I17537(p) ((p)->i)
 
 // Calling this function used to fail without the cast.
-int F17537(const char **p) {
+const int F17537(const char **p) {
        return **p;
 }
 */
index 5ee06f7f406e00badded6f4612a97e4364b08fec..f6ddfbeceb3fcdec81dd253cbe00b04ecec59364 100644 (file)
@@ -1476,6 +1476,19 @@ func base(dt dwarf.Type) dwarf.Type {
        return dt
 }
 
+// unqual strips away qualifiers from a DWARF type.
+// In general we don't care about top-level qualifiers.
+func unqual(dt dwarf.Type) dwarf.Type {
+       for {
+               if d, ok := dt.(*dwarf.QualType); ok {
+                       dt = d.Type
+               } else {
+                       break
+               }
+       }
+       return dt
+}
+
 // Map from dwarf text names to aliases we use in package "C".
 var dwarfToName = map[string]string{
        "long int":               "long",
@@ -1930,7 +1943,7 @@ func isStructUnionClass(x ast.Expr) bool {
 // FuncArg returns a Go type with the same memory layout as
 // dtype when used as the type of a C function argument.
 func (c *typeConv) FuncArg(dtype dwarf.Type, pos token.Pos) *Type {
-       t := c.Type(dtype, pos)
+       t := c.Type(unqual(dtype), pos)
        switch dt := dtype.(type) {
        case *dwarf.ArrayType:
                // Arrays are passed implicitly as pointers in C.
@@ -1994,7 +2007,7 @@ func (c *typeConv) FuncType(dtype *dwarf.FuncType, pos token.Pos) *FuncType {
        if _, ok := dtype.ReturnType.(*dwarf.VoidType); ok {
                gr = []*ast.Field{{Type: c.goVoid}}
        } else if dtype.ReturnType != nil {
-               r = c.Type(dtype.ReturnType, pos)
+               r = c.Type(unqual(dtype.ReturnType), pos)
                gr = []*ast.Field{{Type: r.Go}}
        }
        return &FuncType{