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

cgo enum value handling varies depending on whether enum type is referenced #479

Closed
gopherbot opened this issue Dec 30, 2009 · 7 comments
Closed

Comments

@gopherbot
Copy link

by emage@spamcop.net:

What steps will reproduce the problem?
1. Create the following file:

package enum

// typedef enum {a, b, c, d} foo;
import "C"

import "fmt"

func Test() {fmt.Printf("%d %d %d %d\n", C.a, C.b, C.c, C.d); }

2. Run cgo on it and note that it generates references of the form _C_a
referring to an enum.so file
3. Append the following line and rerun cgo:

func Test2() C.foo { return C.a; }

The addition of this line causes constants a, b, c, and d to be defined in
the package, and causes the generated Test() code to refer to those constants.

The latter behavior seems more correct and should probably happen in either
case.

Which revision are you using?  (hg identify)

a6fcf4303b0a release/release.2009-12-22
@dhobsd
Copy link
Contributor

dhobsd commented Dec 30, 2009

Comment 1:

I'm fairly sure after some rather extensive debugging that we're hitting this comment:
                        // TODO(rsc): Can also be TagEnumerationType
                        // but haven't seen that in the wild yet.
I'll see if I can fix.

@dhobsd
Copy link
Contributor

dhobsd commented Dec 30, 2009

Comment 2:

After some more extensive debugging, I'm wrong. Back to the drawing board.

@dhobsd
Copy link
Contributor

dhobsd commented Dec 30, 2009

Comment 3:

It would actually appear that when it's not referenced, there's no way to get dwarf
symbols for it. The 
compiled program that ends up being output for the first one is:
typedef struct { char *p; int n; } _GoString_;
_GoString_ GoString(char *p);
char *CString(_GoString_);
typedef enum {a, b, c, d} foo;
void f(void) {
#line 0 "cgo-test"
c;
d;
a;
b;
}
As you can see here, the enum is never referenced, and thus no dwarf stabs are generated
for it. As soon as 
we reference C.foo:
typedef struct { char *p; int n; } _GoString_;
_GoString_ GoString(char *p);
char *CString(_GoString_);
typedef enum {a, b, c, d} foo;
void f(void) {
#line 0 "cgo-test"
c;
d;
foo;
a;
b;
}
This yields dwarf stabs.
Unfortunately, I'm not entirely sure how to deal with this. This pretty much means that
any unnamed 
enumeration (e.g. enum { ... } as opposed to typedef enum { ... } bla) is unusable,
essentially requiring godefs. 
I'll play around a bit, but I'm not sure I'm coming up with any solutions tonight.

@dhobsd
Copy link
Contributor

dhobsd commented Dec 30, 2009

Comment 4:

iant to the rescue. -fno-eliminate-unused-debug-types.

@dhobsd
Copy link
Contributor

dhobsd commented Dec 30, 2009

Comment 5:

Seems to work. See CL 181102

@rsc
Copy link
Contributor

rsc commented Jan 3, 2010

Comment 6:

Labels changed: added cgo.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Jan 6, 2010

Comment 7:

This issue was closed by revision 690fcac.

Status changed to Fixed.

Merged into issue #-.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants