Skip to content

Commit

Permalink
Improved resolving of some C types. Fixes #372 (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 authored and elliotchance committed Nov 26, 2017
1 parent a7ac059 commit 9aa8212
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
16 changes: 16 additions & 0 deletions ast/function_decl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ func TestFunctionDecl(t *testing.T) {
IsInline: true,
ChildNodes: []Node{},
},
`0x21c3da0 <line:8201:1, line:8786:1> line:8201:25 used insertvertex 'enum insertvertexresult (struct mesh *, struct behavior *, vertex, struct otri *, struct osub *, int, int)'`: &FunctionDecl{
Addr: 0x21c3da0,
Pos: NewPositionFromString("line:8201:1, line:8786:1"),
Prev: "",
Position2: "line:8201:25",
Name: "insertvertex",
Type: "enum insertvertexresult (struct mesh *, struct behavior *, vertex, struct otri *, struct osub *, int, int)",
Type2: "",
IsExtern: false,
IsImplicit: false,
IsUsed: true,
IsReferenced: false,
IsStatic: false,
IsInline: false,
ChildNodes: []Node{},
},
}

runNodeTests(t, nodes)
Expand Down
3 changes: 3 additions & 0 deletions types/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func GetArrayTypeAndSize(s string) (string, int) {
// FILE where those function probably exist (or should exist) in the noarch
// package.
func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (goast.Expr, error) {
cFromType = CleanCType(cFromType)
cToType = CleanCType(cToType)

fromType := cFromType
toType := cToType

Expand Down
39 changes: 32 additions & 7 deletions types/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,7 @@ var simpleResolveTypes = map[string]string{
// transpiler to step over type errors and put something as a placeholder
// until a more suitable solution is found for those cases.
func ResolveType(p *program.Program, s string) (string, error) {
// Remove any whitespace or attributes that are not relevant to Go.
s = strings.Replace(s, "const ", "", -1)
s = strings.Replace(s, "volatile ", "", -1)
s = strings.Replace(s, "*__restrict", "*", -1)
s = strings.Replace(s, "*restrict", "*", -1)
s = strings.Replace(s, "*const", "*", -1)
s = strings.Trim(s, " \t\n\r")
s = CleanCType(s)

// FIXME: This is a hack to avoid casting in some situations.
if s == "" {
Expand Down Expand Up @@ -320,3 +314,34 @@ func ParseFunction(s string) (f []string, r []string, err error) {

return
}

// CleanCType - remove from C type not Go type
func CleanCType(s string) (out string) {
out = s
out = strings.Replace(out, "()", "", -1)
out = strings.Replace(out, "(*)", "", -1)

// Remove any whitespace or attributes that are not relevant to Go.
out = strings.Replace(out, "const", "", -1)
out = strings.Replace(out, "volatile", "", -1)
out = strings.Replace(out, "__restrict", "", -1)
out = strings.Replace(out, "restrict", "", -1)
out = strings.Replace(out, "\t", "", -1)
out = strings.Replace(out, "\n", "", -1)
out = strings.Replace(out, "\r", "", -1)

// remove space from pointer symbols
out = strings.Replace(out, "* *", "**", -1)

// remove addition spaces
out = strings.Replace(out, " ", " ", -1)

// remove spaces around
out = strings.TrimSpace(out)

if out != s {
return CleanCType(out)
}

return out
}

0 comments on commit 9aa8212

Please sign in to comment.