1 // Copyright 2013 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 package main 6 7 /* 8 #include <stdio.h> 9 10 typedef struct foo foo_t; 11 typedef struct bar bar_t; 12 13 foo_t *foop; 14 15 long double x = 0; 16 17 static int transform(int x) { return x; } 18 19 typedef void v; 20 void F(v** p) {} 21 22 void fvi(void *p, int x) {} 23 24 void fppi(int** p) {} 25 26 int i; 27 void fi(int i) {} 28 */ 29 import "C" 30 import ( 31 "unsafe" 32 ) 33 34 func main() { 35 s := "" 36 _ = s 37 C.malloc(s) // ERROR HERE 38 39 x := (*C.bar_t)(nil) 40 C.foop = x // ERROR HERE 41 42 // issue 13129: used to output error about C.unsignedshort with CC=clang 43 var x1 C.ushort 44 x1 = int(0) // ERROR HERE: C\.ushort 45 46 // issue 13423 47 _ = C.fopen() // ERROR HERE 48 49 // issue 13467 50 var x2 rune = '✈' 51 var _ rune = C.transform(x2) // ERROR HERE: C\.int 52 53 // issue 13635: used to output error about C.unsignedchar. 54 // This test tests all such types. 55 var ( 56 _ C.uchar = "uc" // ERROR HERE: C\.uchar 57 _ C.schar = "sc" // ERROR HERE: C\.schar 58 _ C.ushort = "us" // ERROR HERE: C\.ushort 59 _ C.uint = "ui" // ERROR HERE: C\.uint 60 _ C.ulong = "ul" // ERROR HERE: C\.ulong 61 _ C.longlong = "ll" // ERROR HERE: C\.longlong 62 _ C.ulonglong = "ull" // ERROR HERE: C\.ulonglong 63 _ C.complexfloat = "cf" // ERROR HERE: C\.complexfloat 64 _ C.complexdouble = "cd" // ERROR HERE: C\.complexdouble 65 ) 66 67 // issue 13830 68 // cgo converts C void* to Go unsafe.Pointer, so despite appearances C 69 // void** is Go *unsafe.Pointer. This test verifies that we detect the 70 // problem at build time. 71 { 72 type v [0]byte 73 74 f := func(p **v) { 75 C.F((**C.v)(unsafe.Pointer(p))) // ERROR HERE 76 } 77 var p *v 78 f(&p) 79 } 80 81 // issue 16116 82 _ = C.fvi(1) // ERROR HERE 83 84 // Issue 16591: Test that we detect an invalid call that was being 85 // hidden by a type conversion inserted by cgo checking. 86 { 87 type x *C.int 88 var p *x 89 C.fppi(p) // ERROR HERE 90 } 91 92 // issue 26745 93 _ = func(i int) int { 94 // typecheck reports at column 14 ('+'), but types2 reports at 95 // column 10 ('C'). 96 // TODO(mdempsky): Investigate why, and see if types2 can be 97 // updated to match typecheck behavior. 98 return C.i + 1 // ERROR HERE: \b(10|14)\b 99 } 100 _ = func(i int) { 101 // typecheck reports at column 7 ('('), but types2 reports at 102 // column 8 ('i'). The types2 position is more correct, but 103 // updating typecheck here is fundamentally challenging because of 104 // IR limitations. 105 C.fi(i) // ERROR HERE: \b(7|8)\b 106 } 107 108 C.fi = C.fi // ERROR HERE 109 110 } 111