Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

compileEnumConst: check src enum to _cgoe_enum #157

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cl/blockctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type blockCtx struct {
tyI128 types.Type
tyU128 types.Type
unnameds map[ast.ID]unnamedType
srcenums map[ast.ID]string
gblvars map[string]*gox.VarDefs
public map[string]string
ignored []string
Expand Down
1 change: 1 addition & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func loadFile(p *gox.Package, conf *Config, file *ast.Node) (pi *PkgInfo, err er
ctx := &blockCtx{
pkg: p, cb: p.CB(), fset: p.Fset,
unnameds: make(map[ast.ID]unnamedType),
srcenums: make(map[ast.ID]string),
gblvars: make(map[string]*gox.VarDefs),
ignored: conf.Ignored,
public: conf.Public,
Expand Down
3 changes: 3 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func compileTypeCast(ctx *blockCtx, v *ast.Node, src goast.Node) {

func compileDeclRefExpr(ctx *blockCtx, v *ast.Node, lhs bool) {
name := v.ReferencedDecl.Name
if n, ok := ctx.srcenums[v.ReferencedDecl.ID]; ok {
name = n
}
avoidKeyword(&name)
obj := ctx.lookupParent(name)
if obj == nil {
Expand Down
4 changes: 4 additions & 0 deletions cl/multifiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (p *blockCtx) autoStaticName(name string) string {
return "_cgos_" + name + p.baseOF
}

func (p *blockCtx) srcEnumName(name string) string {
return "_cgoe_" + name + p.baseOF
}

func (p *blockCtx) logFile(node *ast.Node) {
if f := node.Loc.PresumedFile; f != "" {
if p.hasMulti {
Expand Down
7 changes: 6 additions & 1 deletion cl/type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ func compileEnumConst(ctx *blockCtx, cdecl *gox.ConstDefs, v *ast.Node, iotav in
}
return 1
}
cdecl.New(fn, iotav, ctx.goNodePos(v), ctypes.Int, v.Name)
name := v.Name
if ctx.inSrcFile() {
name = ctx.srcEnumName(v.Name)
ctx.srcenums[v.ID] = name
}
cdecl.New(fn, iotav, ctx.goNodePos(v), ctypes.Int, name)
return iotav + 1
}

Expand Down
9 changes: 9 additions & 0 deletions testdata/enum/a.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

enum {
ONE,TWO
};

void test1() {
printf("test1 %d\n",ONE);
}
10 changes: 10 additions & 0 deletions testdata/enum/b.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>

enum {
ONE,TWO
};

void test2()
{
printf("test2 %d\n",TWO);
}
15 changes: 15 additions & 0 deletions testdata/enum/c2go.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"target": {
"dir": "cmd/enum"
},
"source": {
"dirs": ["."]
},
"deps": [
"C",
"github.com/goplus/c2go/testdata/libc"
],
"include": [
"../libc/src", "."
]
}
7 changes: 7 additions & 0 deletions testdata/enum/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern void test1();
extern void test2();

int main() {
test1();
test2();
}