]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.14] cmd/cgo: use consistent tag for a particular struct
authorIan Lance Taylor <iant@golang.org>
Tue, 14 Apr 2020 00:47:47 +0000 (17:47 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 14 Apr 2020 22:36:35 +0000 (22:36 +0000)
For #31891
For #38408
Fixes #38426

Change-Id: Ie7498c2cab728ae798e66e7168425e16b063520e
Reviewed-on: https://go-review.googlesource.com/c/go/+/228102
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
(cherry picked from commit 33ff63da4ec9cd456cab65b034b80a2fde4ebdea)
Reviewed-on: https://go-review.googlesource.com/c/go/+/228107
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/test/testx.go
src/cmd/cgo/gcc.go

index 42979b5f4d5092e3771c828faa2fdecbd0fb5f6d..eb9d7fa47d3ecdf70613100fd52850be84da168d 100644 (file)
@@ -124,6 +124,11 @@ typedef struct {
 } Issue31891B;
 
 void callIssue31891(void);
+
+typedef struct {
+       int i;
+} Issue38408, *PIssue38408;
+
 */
 import "C"
 
@@ -552,3 +557,8 @@ func useIssue31891B(c *C.Issue31891B) {}
 func test31891(t *testing.T) {
        C.callIssue31891()
 }
+
+// issue 38408
+// A typedef pointer can be used as the element type.
+// No runtime test; just make sure it compiles.
+var _ C.PIssue38408 = &C.Issue38408{i: 1}
index c4128e9502e4667f0f6a71ee6dc3b9034ffb4257..7f99057a493ca65481a5786dfc5c3647755945c1 100644 (file)
@@ -2060,6 +2060,10 @@ var goIdent = make(map[string]*ast.Ident)
 // that may contain a pointer. This is used for cgo pointer checking.
 var unionWithPointer = make(map[ast.Expr]bool)
 
+// anonymousStructTag provides a consistent tag for an anonymous struct.
+// The same dwarf.StructType pointer will always get the same tag.
+var anonymousStructTag = make(map[*dwarf.StructType]string)
+
 func (c *typeConv) Init(ptrSize, intSize int64) {
        c.ptrSize = ptrSize
        c.intSize = intSize
@@ -2408,8 +2412,12 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ
                        break
                }
                if tag == "" {
-                       tag = "__" + strconv.Itoa(tagGen)
-                       tagGen++
+                       tag = anonymousStructTag[dt]
+                       if tag == "" {
+                               tag = "__" + strconv.Itoa(tagGen)
+                               tagGen++
+                               anonymousStructTag[dt] = tag
+                       }
                } else if t.C.Empty() {
                        t.C.Set(dt.Kind + " " + tag)
                }