Skip to content

Commit

Permalink
Fix bitfields not generating for all types
Browse files Browse the repository at this point in the history
  • Loading branch information
PMunch committed Aug 29, 2024
1 parent 03b6265 commit f963f97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/opir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,9 @@ proc genStructDecl(struct: CXCursor): JsonNode =
else:
mainObj[]["fields"].add val
else:
if CursorIsBitField(field) != 0:
mainObj[]["fields"].add %*{"name": field.getName, "type": field.toNimType, "bitsize": field.getFieldDeclBitWidth}
else:
mainObj[]["fields"].add %*{"name": field.getName, "type": field.toNimType}
mainObj[]["fields"].add %*{"name": field.getName, "type": field.toNimType}
if CursorIsBitField(field) != 0:
mainObj[]["fields"][^1]["bitsize"] = %field.getFieldDeclBitWidth
of CXCursor_PackedAttr:
mainObj[]["packed"] = %true
of CXCursor_AlignedAttr:
Expand Down
20 changes: 20 additions & 0 deletions tests/tbitfields.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdint.h>

typedef struct {
int one : 1;
int two : 1;
int three : 2;
int four : 4;
} bitmap;

struct bitmap2
{
uint8_t frame_id : 1;
uint8_t end_of_frame : 1;
uint8_t presentation_time : 1;
uint8_t source_clock : 1;
uint8_t reserved : 1;
uint8_t still_image : 1;
uint8_t error : 1;
uint8_t end_of_header : 1;
};
19 changes: 19 additions & 0 deletions tests/tbitfields.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "../src/futhark"

importc:
path "."
"tbitfields.h"

{.emit: "#include \"tbitfields.h\"".}
proc getCsizeofBitmap(): cint =
{.emit: [result, " = sizeof(bitmap);"].}

proc getCsizeofHeaderInfoBitmap(): cint =
{.emit: [result, " = sizeof(struct bitmap2);"].}

echo getCsizeofBitmap()
echo sizeof(bitmap)
doAssert sizeof(bitmap) == getCsizeofBitmap()
echo getCsizeofHeaderInfoBitmap()
echo sizeof(structBitmap2)
doAssert sizeof(structBitmap2) == getCsizeofHeaderInfoBitmap()

0 comments on commit f963f97

Please sign in to comment.