Skip to content

Commit

Permalink
cxgo: Convert string sizeof to len. Fixes #88.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Feb 22, 2025
1 parent d5760bf commit 6a1f069
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions c_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,11 +907,15 @@ func (g *translator) cSizeofE(x Expr) Expr {
return g.newUnaryExpr(UnarySizeof, x)
}
}
switch cUnwrap(x).(type) {
case Bool:
case BoolExpr:
switch x := cUnwrap(x).(type) {
case Bool, BoolExpr:
// workaround for C bools (they should be reported as int in some cases)
return g.SizeofT(g.env.DefIntT(), nil)
case StringLit:
return &CallExpr{
Fun: FuncIdent{g.env.Go().LenFunc()},
Args: []Expr{x},
}
}
return g.SizeofT(x.CType(nil), nil)
}
Expand Down
17 changes: 17 additions & 0 deletions literals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ func foo() {
_ = a
a = libc.CString("abc")
}
`,
},
{
name: "string len",
builtins: true,
src: `
void foo() {
int a;
a = sizeof("abc");
}
`,
exp: `
func foo() {
var a int32
_ = a
a = int32(len("abc"))
}
`,
},
{
Expand Down

0 comments on commit 6a1f069

Please sign in to comment.