Skip to content

Commit

Permalink
Inline size of byte arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Oct 16, 2022
1 parent 2e3e90d commit f921f98
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions c_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,12 @@ func (e *CSizeofExpr) Uses() []types.Usage {
}

func sizeOf(typ types.Type) GoExpr {
switch typ := types.Unwrap(typ).(type) {
case types.ArrayType:
if !typ.IsSlice() && types.Unwrap(typ.Elem()) == types.UintT(1) {
return intLit(typ.Len())
}
}
x := typ.GoType()
switch types.Unwrap(typ).(type) {
case *types.StructType, types.ArrayType:
Expand Down
5 changes: 5 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,12 @@ var a int32 = int32(unsafe.Sizeof(int32(0)))
void foo() {
size_t a;
int b[10];
unsigned char b2[10];
void* c;
int* d;
int (*e)(void);
a = sizeof(b);
a = sizeof(b2);
a = sizeof(c);
a = sizeof(d);
a = sizeof(e);
Expand All @@ -587,13 +589,16 @@ func foo() {
_ = a
var b [10]int32
_ = b
var b2 [10]uint8
_ = b2
var c unsafe.Pointer
_ = c
var d *int32
_ = d
var e func() int32
_ = e
a = size_t(unsafe.Sizeof([10]int32{}))
a = size_t(10)
a = size_t(unsafe.Sizeof(unsafe.Pointer(nil)))
a = size_t(unsafe.Sizeof((*int32)(nil)))
a = size_t(unsafe.Sizeof(uintptr(0)))
Expand Down

0 comments on commit f921f98

Please sign in to comment.