Skip to content

Commit a371bc2

Browse files
cmd/cgo: don't report inconsistency error for incomplete typedef
In CLs 122575 and 123177 the cgo tool started explicitly looking up typedefs. When there are two Go files using import "C", and the first one has an incomplete typedef and the second one has a complete version of the same typedef, then we will now record a version of the first typedef which will not match the recorded version of the second typedef, producing an "inconsistent definitions" error. Fix this by silently merging incomplete typedefs with complete ones. Fixes #26430 Change-Id: I9e629228783b866dd29b5c3a31acd48f6e410a2d Reviewed-on: https://go-review.googlesource.com/124575 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 0d93758 commit a371bc2

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

misc/cgo/test/issue26430.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2018 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 26430: incomplete typedef leads to inconsistent typedefs error.
6+
// No runtime test; just make sure it compiles.
7+
8+
package cgotest
9+
10+
import _ "./issue26430"

misc/cgo/test/issue26430/a.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 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 a
6+
7+
// typedef struct S ST;
8+
// static ST* F() { return 0; }
9+
import "C"
10+
11+
func F1() {
12+
C.F()
13+
}

misc/cgo/test/issue26430/b.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 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 a
6+
7+
// typedef struct S ST;
8+
// struct S { int f; };
9+
import "C"
10+
11+
func F2(p *C.ST) {
12+
p.f = 1
13+
}

src/cmd/cgo/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ func (p *Package) Record(f *File) {
399399
for k, v := range f.Name {
400400
if p.Name[k] == nil {
401401
p.Name[k] = v
402+
} else if p.incompleteTypedef(p.Name[k].Type) {
403+
p.Name[k] = v
404+
} else if p.incompleteTypedef(v.Type) {
405+
// Nothing to do.
402406
} else if !reflect.DeepEqual(p.Name[k], v) {
403407
error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
404408
}
@@ -411,3 +415,9 @@ func (p *Package) Record(f *File) {
411415
}
412416
p.Decl = append(p.Decl, f.AST.Decls...)
413417
}
418+
419+
// incompleteTypedef reports whether t appears to be an incomplete
420+
// typedef definition.
421+
func (p *Package) incompleteTypedef(t *Type) bool {
422+
return t == nil || (t.Size == 0 && t.Align == -1)
423+
}

0 commit comments

Comments
 (0)