Skip to content

Commit 097d642

Browse files
[release-branch.go1.10] cmd/cgo: don't resolve typedefs in -godefs mode
In -godefs mode any typedefs that appear in struct fields and the like will presumably be defined in the input file. If we resolve to the base type, those cross-references will not work. So for -godefs mode, keep the Go 1.10 behavior and don't resolve the typedefs in a loop. Fixes #26644 Change-Id: I48cf72d9eb5016353c43074e6aff6495af326f35 Reviewed-on: https://go-review.googlesource.com/125995 Reviewed-by: Keith Randall <khr@golang.org> (cherry picked from commit ead59f4) Reviewed-on: https://go-review.googlesource.com/128395 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 0fae49d commit 097d642

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

misc/cgo/testgodefs/fieldtypedef.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Go Authors. All rights reserve d.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
//
5+
// +build ignore
6+
7+
package main
8+
9+
/*
10+
struct S1 { int f1; };
11+
struct S2 { struct S1 s1; };
12+
typedef struct S1 S1Type;
13+
typedef struct S2 S2Type;
14+
*/
15+
import "C"
16+
17+
type S1 C.S1Type
18+
type S2 C.S2Type

misc/cgo/testgodefs/test.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# We are testing cgo -godefs, which translates Go files that use
88
# import "C" into Go files with Go definitions of types defined in the
99
# import "C" block. Add more tests here.
10-
FILE_PREFIXES="anonunion issue8478"
10+
FILE_PREFIXES="anonunion issue8478 fieldtypedef"
1111

1212
RM=
1313
for FP in $FILE_PREFIXES

src/cmd/cgo/gcc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ func (p *Package) Translate(f *File) {
180180
if len(needType) > 0 {
181181
p.loadDWARF(f, needType)
182182
}
183+
184+
// In godefs mode we're OK with the typedefs, which
185+
// will presumably also be defined in the file, we
186+
// don't want to resolve them to their base types.
187+
if *godefs {
188+
break
189+
}
183190
}
184191
if p.rewriteCalls(f) {
185192
// Add `import _cgo_unsafe "unsafe"` after the package statement.

0 commit comments

Comments
 (0)