github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/misc/cgo/test/issue17537.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Issue 17537.  The void* cast introduced by cgo to avoid problems
     6  // with const/volatile qualifiers breaks C preprocessor macros that
     7  // emulate functions.
     8  
     9  package cgotest
    10  
    11  /*
    12  #include <stdlib.h>
    13  
    14  typedef struct {
    15  	int i;
    16  } S17537;
    17  
    18  int I17537(S17537 *p);
    19  
    20  #define I17537(p) ((p)->i)
    21  
    22  // Calling this function used to fail without the cast.
    23  const int F17537(const char **p) {
    24  	return **p;
    25  }
    26  
    27  // Calling this function used to trigger an error from the C compiler
    28  // (issue 18298).
    29  void F18298(const void *const *p) {
    30  }
    31  
    32  // Test that conversions between typedefs work as they used to.
    33  typedef const void *T18298_1;
    34  struct S18298 { int i; };
    35  typedef const struct S18298 *T18298_2;
    36  void G18298(T18298_1 t) {
    37  }
    38  */
    39  import "C"
    40  
    41  import "testing"
    42  
    43  func test17537(t *testing.T) {
    44  	v := C.S17537{i: 17537}
    45  	if got, want := C.I17537(&v), C.int(17537); got != want {
    46  		t.Errorf("got %d, want %d", got, want)
    47  	}
    48  
    49  	p := (*C.char)(C.malloc(1))
    50  	*p = 17
    51  	if got, want := C.F17537(&p), C.int(17); got != want {
    52  		t.Errorf("got %d, want %d", got, want)
    53  	}
    54  
    55  	C.F18298(nil)
    56  	var v18298 C.T18298_2
    57  	C.G18298(C.T18298_1(v18298))
    58  }