1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:04:43 -07:00

cmd/cgo: for godefs, don't let field prefix removal cause duplicates

Fixes #48396

Change-Id: Idd7cb66536ef513806c472d394a929bc271fc26b
Reviewed-on: https://go-review.googlesource.com/c/go/+/350159
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Ian Lance Taylor 2021-09-15 16:11:37 -07:00
parent 4efdaa7bc7
commit 265b59aefd
4 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,18 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// +build ignore
package main
/*
// from <linux/kcm.h>
struct issue48396 {
int fd;
int bpf_fd;
};
*/
import "C"
type Issue48396 C.struct_issue48396

View File

@ -28,6 +28,9 @@ var v7 = S{}
// Test that #define'd type is fully defined
var _ = issue38649{X: 0}
// Test that prefixes do not cause duplicate field names.
var _ = Issue48396{Fd: 1, Bpf_fd: 2}
func main() {
pass := true

View File

@ -25,6 +25,7 @@ var filePrefixes = []string{
"issue37621",
"issue38649",
"issue39534",
"issue48396",
}
func TestGoDefs(t *testing.T) {

View File

@ -3030,6 +3030,31 @@ func upper(s string) string {
// so that all fields are exported.
func godefsFields(fld []*ast.Field) {
prefix := fieldPrefix(fld)
// Issue 48396: check for duplicate field names.
if prefix != "" {
names := make(map[string]bool)
fldLoop:
for _, f := range fld {
for _, n := range f.Names {
name := n.Name
if name == "_" {
continue
}
if name != prefix {
name = strings.TrimPrefix(n.Name, prefix)
}
name = upper(name)
if names[name] {
// Field name conflict: don't remove prefix.
prefix = ""
break fldLoop
}
names[name] = true
}
}
}
npad := 0
for _, f := range fld {
for _, n := range f.Names {