1
0
mirror of https://github.com/golang/go synced 2024-09-29 14:24:32 -06:00
go/misc/cgo/test/issue17537.go
Ian Lance Taylor fb8c896aff cmd/cgo: don't ignore qualifiers, don't cast to void*
The cgo tool used to simply ignore C type qualifiers. To avoid problems
when a C function expected a qualifier that was not present, cgo emitted
a cast to void* around all pointer arguments. Unfortunately, that broke
code that contains both a function declaration and a macro, when the
macro required the argument to have the right type. To fix this problem,
don't ignore qualifiers. They are easy enough to handle for the limited
set of cases that matter for cgo, in which we don't care about array or
function types.

Fixes #17537.

Change-Id: Ie2988d21db6ee016a3e99b07f53cfb0f1243a020
Reviewed-on: https://go-review.googlesource.com/33097
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2016-11-11 01:31:12 +00:00

43 lines
881 B
Go

// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Issue 17537. The void* cast introduced by cgo to avoid problems
// with const/volatile qualifiers breaks C preprocessor macros that
// emulate functions.
package cgotest
/*
#include <stdlib.h>
typedef struct {
int i;
} S17537;
int I17537(S17537 *p);
#define I17537(p) ((p)->i)
// Calling this function used to fail without the cast.
int F17537(const char **p) {
return **p;
}
*/
import "C"
import "testing"
func test17537(t *testing.T) {
v := C.S17537{i: 17537}
if got, want := C.I17537(&v), C.int(17537); got != want {
t.Errorf("got %d, want %d", got, want)
}
p := (*C.char)(C.malloc(1))
*p = 17
if got, want := C.F17537(&p), C.int(17); got != want {
t.Errorf("got %d, want %d", got, want)
}
}