Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: C Structs with the same field definitions are used interchangeably in generated prototypes #31891

Closed
kesmit13 opened this issue May 7, 2019 · 6 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kesmit13
Copy link

kesmit13 commented May 7, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.1 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nd/fslycqgs2m54077zvlwx158h000464/T/go-build014851518=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

If I use the following struct definitions in functions exported by cgo, the
struct names get mixed up in the generated h file.

Input h File

typedef struct {
    char *objtype;
    unsigned long long obj;
} Connection;

typedef struct {
    char *objtype;
    unsigned long long obj;
} Error;

typedef struct {
    char *objtype;
    unsigned long long obj;
} CASValueList;

Exported Function Definitions

func casConnectionDestroy(c *C.CASConnection) { ... }
func casErrorDestroy(e *C.CASError) { ... }

What did you expect to see?

I expected the generated prototypes to look like the following.

extern void casConnectionDestroy(CASConnection* p0);
extern void casErrorDestroy(CASError* p0);

What did you see instead?

There are the prototypes that were generated instead.

extern void casConnectionDestroy(CASValueList* p0);
extern void casErrorDestroy(CASValueList* p0);

Observation

I suspected that some sort of hashing was being used to alias structs that
had the same fields, so I added a unique field to each of the structs in the
input header file and that did solve the problem.

@ianlancetaylor ianlancetaylor changed the title C Structs with the same field definitions are used interchangeably in generated prototypes cmd/cgo: C Structs with the same field definitions are used interchangeably in generated prototypes May 7, 2019
@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label May 7, 2019
@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone May 7, 2019
@gopherbot
Copy link

Change https://golang.org/cl/181857 mentions this issue: cmd/cgo: ...

@odeke-em
Copy link
Member

Thank you for filing this change @kesmit13 and welcome to the Go project!

Since Ian on CL https://golang.org/cl/181857 added this to wait-release, I shall accordingly move the milestone to Go1.14 so perhaps please except a fix to land after August 2019.

@odeke-em odeke-em modified the milestones: Go1.13, Go1.14 Jun 13, 2019
tklauser added a commit to tklauser/go that referenced this issue Mar 29, 2020
CL 181857 broke the translation of certain C types when using cmd/cgo
-godefs.

Updates golang#31891
Fixes golang#37479
Fixes golang#37621

Change-Id: I301a749ec89585789cb0d213593bb8b7341beb88
@gopherbot
Copy link

Change https://golang.org/cl/226341 mentions this issue: cmd/cgo, misc/cgo: only cache anonymous struct typedefs with parent name

gopherbot pushed a commit that referenced this issue Mar 30, 2020
CL 181857 broke the translation of certain C types using cmd/cgo -godefs
because it stores each typedef, array and qualified type with their
parent type name in the translation cache.

Fix this by only considering the parent type for typedefs of anonymous
structs which is the only case where types might become ambiguous.

Updates #31891
Fixes #37479
Fixes #37621

Change-Id: I301a749ec89585789cb0d213593bb8b7341beb88
Reviewed-on: https://go-review.googlesource.com/c/go/+/226341
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@gopherbot
Copy link

Change https://golang.org/cl/226497 mentions this issue: cmd/cgo, misc/cgo: only cache anonymous struct typedefs with parent name

@gopherbot
Copy link

Change https://golang.org/cl/228102 mentions this issue: cmd/cgo: use consistent tag for a particular struct

gopherbot pushed a commit that referenced this issue Apr 14, 2020
For #31891
Fixes #38408

Change-Id: Ie7498c2cab728ae798e66e7168425e16b063520e
Reviewed-on: https://go-review.googlesource.com/c/go/+/228102
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
@gopherbot
Copy link

Change https://golang.org/cl/228107 mentions this issue: [release-branch.go1.14] cmd/cgo: use consistent tag for a particular struct

gopherbot pushed a commit that referenced this issue Apr 14, 2020
…struct

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 33ff63d)
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>
@golang golang locked and limited conversation to collaborators Apr 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants