Skip to content

Commit

Permalink
Remove strdup/strndup when cloning Go strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Oct 16, 2022
1 parent f825adc commit aeacff3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
5 changes: 2 additions & 3 deletions c_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,8 @@ func (e *SliceExpr) HasSideEffects() bool {
return false
}

func (e *SliceExpr) CType(types.Type) types.Type {
arr := types.Unwrap(e.Expr.CType(nil)).(types.ArrayType)
return types.SliceT(arr.Elem())
func (e *SliceExpr) CType(exp types.Type) types.Type {
return e.Expr.CType(exp)
}

func (e *SliceExpr) AsExpr() GoExpr {
Expand Down
14 changes: 14 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ func (g *translator) NewCCallExpr(fnc FuncExpr, args []Expr) Expr {
}
}
}
case g.env.C().StrdupFunc():
// strdup(string) -> string
if len(args) == 1 {
if args[0].CType(nil) == g.env.Go().String() {
return args[0]
}
}
case g.env.C().StrndupFunc():
// strndup(string, n) -> string[:n]
if len(args) == 2 {
if args[0].CType(nil) == g.env.Go().String() {
return &SliceExpr{Expr: args[0], High: args[1]}
}
}
case g.env.Go().SliceFunc():
// _slice(arr, -1, 1) -> arr[:1]
if len(args) == 3 || len(args) == 4 {
Expand Down
2 changes: 2 additions & 0 deletions libs/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ void* malloc(_cxgo_go_int);
c.C().MemmoveFunc(),
c.C().MemcpyFunc(),
c.C().MemsetFunc(),
c.C().StrdupFunc(),
c.C().StrndupFunc(),
c.NewIdent("_cxgo_func_name", "libc.FuncName", libc.FuncName, c.FuncTT(c.Go().String())),
c.NewIdent("__builtin_strcpy", "libc.StrCpy", libc.StrCpy, c.FuncTT(charP, charP, charP)),
c.NewIdent("__sync_lock_test_and_set", "atomic.SwapInt32", atomic.SwapInt32, c.FuncTT(int32T, c.PtrT(int32T), int32T)),
Expand Down
6 changes: 2 additions & 4 deletions libs/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func init() {
"strtok": c.NewIdent("strtok", "libc.StrTok", libc.StrTok, c.FuncTT(cstrT, cstrT, cstrT)),
"strspn": c.NewIdent("strspn", "libc.StrSpn", libc.StrSpn, c.FuncTT(gintT, cstrT, cstrT)),
"strcspn": c.NewIdent("strcspn", "libc.StrCSpn", libc.StrCSpn, c.FuncTT(gintT, cstrT, cstrT)),
"strdup": c.NewIdent("strdup", "libc.StrDup", libc.StrDup, c.FuncTT(cstrT, cstrT)),
"strndup": c.NewIdent("strndup", "libc.StrNDup", libc.StrNDup, c.FuncTT(cstrT, cstrT, gintT)),
},
Header: `
#include <` + BuiltinH + `>
Expand All @@ -59,15 +57,15 @@ _cxgo_go_int strcoll(const char *, const char *);
//int strcoll_l(const char *, const char *, locale_t);
char *strcpy(char *restrict, const char *restrict);
_cxgo_go_int strcspn(const char *, const char *);
char *strdup(const char *);
#define strdup __builtin_strdup
#define strndup __builtin_strndup
char *strerror(int);
//char *strerror_l(int, locale_t);
int strerror_r(int, char *, size_t);
_cxgo_go_int strlen(const char *);
char *strncat(char *restrict, const char *restrict, _cxgo_go_int);
_cxgo_go_int strncmp(const char *, const char *, _cxgo_go_int);
char *strncpy(char *restrict, const char *restrict, _cxgo_go_int);
char *strndup(const char *, _cxgo_go_int);
_cxgo_go_int strnlen(const char *, _cxgo_go_int);
char *strpbrk(const char *, const char *);
char *strrchr(const char *, _cxgo_go_byte);
Expand Down
15 changes: 15 additions & 0 deletions types/env_c.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type C struct {
memmoveF *Ident
memcpyF *Ident
memsetF *Ident
strdupF *Ident
strndupF *Ident
}

// C returns a package containing builtin C types.
Expand Down Expand Up @@ -60,12 +62,15 @@ func (c *C) init() {
c.pkg.NewAlias("_QWORD", "", UintT(8))

unsafePtr := g.UnsafePtr()
cstring := c.String()
c.mallocF = NewIdentGo("__builtin_malloc", "libc.Malloc", c.e.FuncTT(unsafePtr, g.Int()))
c.freeF = NewIdentGo("free", "libc.Free", c.e.FuncTT(nil, unsafePtr))
c.callocF = NewIdentGo("calloc", "libc.Calloc", c.e.FuncTT(unsafePtr, g.Int(), g.Int()))
c.memmoveF = NewIdentGo("__builtin_memmove", "libc.MemMove", c.e.FuncTT(unsafePtr, unsafePtr, unsafePtr, g.Int()))
c.memcpyF = NewIdentGo("__builtin_memcpy", "libc.MemCpy", c.e.FuncTT(unsafePtr, unsafePtr, unsafePtr, g.Int()))
c.memsetF = NewIdentGo("__builtin_memset", "libc.MemSet", c.e.FuncTT(unsafePtr, unsafePtr, g.Byte(), g.Int()))
c.strdupF = NewIdentGo("__builtin_strdup", "libc.StrDup", c.e.FuncTT(cstring, cstring))
c.strndupF = NewIdentGo("__builtin_strndup", "libc.StrNDup", c.e.FuncTT(cstring, cstring, g.Int()))
}

func (c *C) WCharSize() int {
Expand Down Expand Up @@ -234,3 +239,13 @@ func (c *C) MemcpyFunc() *Ident {
func (c *C) MemsetFunc() *Ident {
return c.memsetF
}

// StrdupFunc returns C strdup function ident.
func (c *C) StrdupFunc() *Ident {
return c.strdupF
}

// StrndupFunc returns C strndup function ident.
func (c *C) StrndupFunc() *Ident {
return c.strndupF
}

0 comments on commit aeacff3

Please sign in to comment.