Skip to content

Commit 33ff63d

Browse files
cmd/cgo: use consistent tag for a particular struct
For #31891 Fixes #38408 Change-Id: Ie7498c2cab728ae798e66e7168425e16b063520e Reviewed-on: https://go-review.googlesource.com/c/go/+/228102 Reviewed-by: Tobias Klauser <[email protected]>
1 parent cdaf8b6 commit 33ff63d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

misc/cgo/test/testx.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ typedef struct {
124124
} Issue31891B;
125125
126126
void callIssue31891(void);
127+
128+
typedef struct {
129+
int i;
130+
} Issue38408, *PIssue38408;
131+
127132
*/
128133
import "C"
129134

@@ -552,3 +557,8 @@ func useIssue31891B(c *C.Issue31891B) {}
552557
func test31891(t *testing.T) {
553558
C.callIssue31891()
554559
}
560+
561+
// issue 38408
562+
// A typedef pointer can be used as the element type.
563+
// No runtime test; just make sure it compiles.
564+
var _ C.PIssue38408 = &C.Issue38408{i: 1}

src/cmd/cgo/gcc.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,10 @@ var goIdent = make(map[string]*ast.Ident)
20602060
// that may contain a pointer. This is used for cgo pointer checking.
20612061
var unionWithPointer = make(map[ast.Expr]bool)
20622062

2063+
// anonymousStructTag provides a consistent tag for an anonymous struct.
2064+
// The same dwarf.StructType pointer will always get the same tag.
2065+
var anonymousStructTag = make(map[*dwarf.StructType]string)
2066+
20632067
func (c *typeConv) Init(ptrSize, intSize int64) {
20642068
c.ptrSize = ptrSize
20652069
c.intSize = intSize
@@ -2408,8 +2412,12 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ
24082412
break
24092413
}
24102414
if tag == "" {
2411-
tag = "__" + strconv.Itoa(tagGen)
2412-
tagGen++
2415+
tag = anonymousStructTag[dt]
2416+
if tag == "" {
2417+
tag = "__" + strconv.Itoa(tagGen)
2418+
tagGen++
2419+
anonymousStructTag[dt] = tag
2420+
}
24132421
} else if t.C.Empty() {
24142422
t.C.Set(dt.Kind + " " + tag)
24152423
}

0 commit comments

Comments
 (0)