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

Fixing https://github.com/xlab/c-for-go/issues/132 #134

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion translator/ast_walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func NewTestTranslator(t *testing.T, code string, useSimplePredefs bool) *Transl
sources = append(sources, cc.Source{Value: code})
ast, err := cc.Translate(config, sources)
assert.Equal(t, nil, err)
translator, err := New(&Config{})
translator, err := New(
&Config{
Rules: Rules(map[RuleTarget][]RuleSpec{"const": {{From: "^TEST", Action: "accept"}}}),
},
)
assert.Equal(t, nil, err)
translator.Learn(ast)
return translator
Expand Down Expand Up @@ -767,3 +771,20 @@ func TestStructNestedTypedef(t *testing.T) {
assert.Equal(t, true, typedefFound)
assert.Equal(t, true, baseTypedefFound)
}

func TestDefineConst(t *testing.T) {
translator := NewTestTranslator(t, "#define TEST_ABC 10", true)
defineFound := false
for _, d := range translator.defines {
if d.Name == "TEST_ABC" {
assert.False(t, defineFound)
defineFound = true
assert.Equal(t, false, d.IsStatic)
assert.Equal(t, false, d.IsTypedef)
assert.Equal(t, true, d.IsDefine)
assert.Equal(t, "10", d.Expression)
assert.Nil(t, d.Spec) // there is no spec for macros
}
}
assert.Equal(t, true, defineFound)
}
4 changes: 2 additions & 2 deletions translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (t *Translator) collectDefines(declares []*CDecl, defines map[string]*cc.Ma
} else if macro.IsFnLike {
continue
}
name := string(macro.Name.Name())
name := string(macro.Name.SrcStr())
if !t.IsAcceptableName(TargetConst, name) {
continue
}
Expand All @@ -357,7 +357,7 @@ func (t *Translator) collectDefines(declares []*CDecl, defines map[string]*cc.Ma
if t.IsTokenIgnored(macro.Position()) {
continue
}
name := string(macro.Name.Name())
name := string(macro.Name.SrcStr())
if _, ok := seen[name]; !ok {
continue
}
Expand Down