Skip to content

Commit

Permalink
btf: correctly format 128 bit C types
Browse files Browse the repository at this point in the history
C gives access to 128 bit int types like __uint128_t, which GoFormatter currently outputs as `uint128` even though no such type exists in Go. Instead emit a byte array of the correct size and a comment that indicates the "real" type.
  • Loading branch information
Benjmmi committed Sep 23, 2022
1 parent 03b648c commit bb83b7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions btf/format.go
Expand Up @@ -174,8 +174,6 @@ func (gf *GoFormatter) writeIntLit(i *Int) error {
return fmt.Errorf("bool with size %d", i.Size)
}
gf.w.WriteString("bool")
case Signed:
fmt.Fprintf(&gf.w, "int%d", bits)
case Char:
if i.Size != 1 {
return fmt.Errorf("char with size %d", i.Size)
Expand All @@ -184,8 +182,16 @@ func (gf *GoFormatter) writeIntLit(i *Int) error {
// we are dealing with unsigned, since this works nicely with []byte
// in Go code.
fallthrough
case Unsigned:
fmt.Fprintf(&gf.w, "uint%d", bits)
case Unsigned, Signed:
stem := "uint"
if i.Encoding == Signed {
stem = "int"
}
if i.Size > 8 {
fmt.Fprintf(&gf.w, "[%d]byte /* %s%d */", i.Size, stem, i.Size*8)
} else {
fmt.Fprintf(&gf.w, "%s%d", stem, bits)
}
default:
return fmt.Errorf("can't encode %s", i.Encoding)
}
Expand Down
2 changes: 1 addition & 1 deletion btf/format_test.go
Expand Up @@ -20,7 +20,7 @@ func TestGoTypeDeclaration(t *testing.T) {
{&Int{Size: 4, Encoding: Signed}, "type t int32"},
{&Int{Size: 8}, "type t uint64"},
{&Typedef{Name: "frob", Type: &Int{Size: 8}}, "type t uint64"},
{&Int{Size: 16}, "type t uint128"},
{&Int{Size: 16}, "type t [16]byte /* uint128 */"},
{&Enum{Values: []EnumValue{{"FOO", 32}}, Size: 4}, "type t uint32; const ( tFOO t = 32; )"},
{&Enum{Values: []EnumValue{{"BAR", 1}}, Size: 1, Signed: true}, "type t int8; const ( tBAR t = 1; )"},
{
Expand Down

0 comments on commit bb83b7f

Please sign in to comment.