Skip to content

Commit c22ed12

Browse files
cmd/cgo: for typedef of untagged struct, use typedef name in C code
Fixes #8148. LGTM=cookieo9, rsc R=rsc, cookieo9 CC=golang-codereviews https://golang.org/cl/103080043
1 parent 81a93ef commit c22ed12

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

misc/cgo/test/issue8148.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2014 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 8148. A typedef of an unnamed struct didn't work when used
6+
// with an exported Go function. No runtime test; just make sure it
7+
// compiles.
8+
9+
package cgotest
10+
11+
/*
12+
typedef struct { int i; } T;
13+
14+
int issue8148Callback(T*);
15+
16+
static int get() {
17+
T t;
18+
t.i = 42;
19+
return issue8148Callback(&t);
20+
}
21+
*/
22+
import "C"
23+
24+
//export issue8148Callback
25+
func issue8148Callback(t *C.T) C.int {
26+
return t.i
27+
}
28+
29+
func Issue8148() int {
30+
return int(C.get())
31+
}

src/cmd/cgo/gcc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,11 @@ func (c *typeConv) Type(dtype dwarf.Type, pos token.Pos) *Type {
12831283
if isStructUnionClass(sub.Go) || *godefs || *cdefs {
12841284
t.Go = sub.Go
12851285

1286+
if isStructUnionClass(sub.Go) {
1287+
// Use the typedef name for C code.
1288+
typedef[sub.Go.(*ast.Ident).Name].C = t.C
1289+
}
1290+
12861291
// If we've seen this typedef before, and it
12871292
// was an anonymous struct/union/class before
12881293
// too, use the old definition.

0 commit comments

Comments
 (0)