Skip to content

Commit

Permalink
builtin: define ptrdiff_t and size_t; fixes #33
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Sep 15, 2021
1 parent 18fb1b5 commit ce9b713
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
13 changes: 12 additions & 1 deletion libs/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libs
import (
"fmt"
"strconv"
"strings"
"sync/atomic"

"github.com/gotranspile/cxgo/runtime/libc"
Expand Down Expand Up @@ -73,6 +74,14 @@ func init() {
#define _ILP32_ 1
`
}
var post strings.Builder
maxIntTypeDefs(&post, "ptr", c.PtrSize()*8)
post.WriteString(`
#define NULL 0
typedef intptr_t ptrdiff_t;
typedef uintptr_t size_t;
`)

l := &Library{
Header: pre + `
#define __ORDER_BIG_ENDIAN__ 4321
Expand Down Expand Up @@ -145,7 +154,7 @@ _cxgo_go_iface_slice _rest;
void* malloc(_cxgo_go_int);
` + includeHacks,
` + post.String() + includeHacks,
Idents: map[string]*types.Ident{
"malloc": c.C().MallocFunc(),
},
Expand All @@ -156,6 +165,8 @@ void* malloc(_cxgo_go_int);
},
Types: map[string]types.Type{
"__builtin_va_list": valistT,
"intptr_t": c.IntPtrT(),
"uintptr_t": c.UintPtrT(),
},
ForceMacros: map[string]bool{
"NULL": true,
Expand Down
10 changes: 5 additions & 5 deletions nums_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,20 @@ func foo(a uint32) {
},
{
name: "named int arithm",
inc: `typedef int size_t;`,
inc: `typedef int my_size_t;`,
src: `
void foo() {
size_t a;
size_t b;
my_size_t a;
my_size_t b;
a = 1 + b;
a = b + 1;
}
`,
exp: `
func foo() {
var a size_t
var a my_size_t
_ = a
var b size_t
var b my_size_t
a = b + 1
a = b + 1
}
Expand Down
20 changes: 17 additions & 3 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,18 @@ func foo(a *int32) int32 {
`,
},
{
name: "sizeof",
inc: `typedef int size_t;`,
name: "sizeof only",
builtins: true,
src: `
int a = sizeof(int);
`,
exp: `
var a int32 = int32(unsafe.Sizeof(int32(0)))
`,
},
{
name: "sizeof",
builtins: true,
src: `
void foo() {
size_t a;
Expand Down Expand Up @@ -784,7 +794,11 @@ func runTestTranslateCase(t *testing.T, c parseCase) {
Predefines: c.builtins,
Sources: srcs,
})
require.NoError(t, err)
if c.skip {
t.SkipNow()
} else {
require.NoError(t, err)
}

tconf := Config{ForwardDecl: true}
for _, f := range c.configFuncs {
Expand Down
6 changes: 3 additions & 3 deletions pointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,14 @@ func foo() *int32 {
},
{
name: "return zero ptr named",
inc: `typedef int size_t;`,
inc: `typedef int my_size_t;`,
src: `
size_t* foo() {
my_size_t* foo() {
return 0;
}
`,
exp: `
func foo() *size_t {
func foo() *my_size_t {
return nil
}
`,
Expand Down

0 comments on commit ce9b713

Please sign in to comment.