golang/go

cmd/cgo: C code with function taking pointer typedef leads to C compiler warning

Open

#19,832 opened on Apr 4, 2017

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsFixcompiler/runtimehelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Building this file gets a warning from the C compiler:

package main

// typedef struct { int i; } *PS;
// void F(PS p) {}
import "C"

func main() {
	C.F(nil)
}

The warning I see is:

# command-line-arguments
cgo-gcc-prolog: In function ‘_cgo_0164fa09626e_Cfunc_F’:
cgo-gcc-prolog:37:2: warning: passing argument 1 of ‘F’ from incompatible pointer type [enabled by default]
/home/iant/foo8.go:4:7: note: expected ‘PS’ but argument is of type ‘struct <anonymous> *’
 // void F(PS p) {}
       ^

The code generated by cgo looks like:

CGO_NO_SANITIZE_THREAD
void
_cgo_0164fa09626e_Cfunc_F(void *v)
{
	struct {
		struct {int i; }* p0;
	} __attribute__((__packed__, __gcc_struct__)) *a = v;
	_cgo_tsan_acquire();
	F(a->p0);
	_cgo_tsan_release();
}

The C function expects PS* but we are passing struct{int i;}*. That is, we aren't using the typedef.

Changing the Go code to use C.PS(unsafe.Pointer(nil)) also fails in the same way.

Contributor guide