Get the number of bits of a bit field with libclang?

Is it possible to get the number of bits of a bit field with libclang?

struct Foo
{
     int a : 1;
};

I noticed I could traverse to the "value" of "a" and the type of the cursor was CXCursor_IntegerLiteral. But is it possible to get the actual value?

Is it possible to get the number of bits of a bit field with libclang?

struct Foo
{
    int a : 1;
};

I noticed I could traverse to the "value" of "a" and the type of the
cursor was CXCursor_IntegerLiteral. But is it possible to get the actual
value?

No, are you interested in submitting a patch ? I suggest something like "clang_Cursor_getBitWidthValue(CXCursor)" similar to how clang_Cursor_getNumArguments(CXCursor C) works, with the difference that it can return "unsigned" and 0 if the cursor is not a bitfield.

Unfortunately, C99 6.7.2.1p3 states that unnamed bit fields may have 0 width, and 6.7.2.1p11 explains that it's a way to separate packed units.

int a : 1;
int : 0;
int b: 1;
// a and b will be in different bytes, possibly different words.

You'll need another sentinel value, a dual return, or maybe just an assert to make this work.

Jordy

Probably in the long run if no one else does it. But right now I have other things on my todo-list for my tool with higher priority.

Would two functions be a good idea, one for signed and one for unsigned values? Just like "clang_getEnumConstantDeclValue" and "clang_getEnumConstantDeclUnsignedValue".

Is it possible to get the number of bits of a bit field with libclang?

struct Foo
{
  int a : 1;
};

I noticed I could traverse to the "value" of "a" and the type of the
cursor was CXCursor_IntegerLiteral. But is it possible to get the actual
value?

No, are you interested in submitting a patch ? I suggest something like "clang_Cursor_getBitWidthValue(CXCursor)" similar to how clang_Cursor_getNumArguments(CXCursor C) works, with the difference that it can return "unsigned" and 0 if the cursor is not a bitfield.

Unfortunately, C99 6.7.2.1p3 states that unnamed bit fields may have 0 width, and 6.7.2.1p11 explains that it's a way to separate packed units.

int a : 1;
int : 0;
int b: 1;
// a and b will be in different bytes, possibly different words.

You'll need another sentinel value, a dual return, or maybe just an assert to make this work.

Good point, I suggest same behavior as clang_Cursor_getNumArguments (int with -1 for non bitfield).

Is it possible to get the number of bits of a bit field with libclang?

struct Foo
{
    int a : 1;
};

I noticed I could traverse to the "value" of "a" and the type of the
cursor was CXCursor_IntegerLiteral. But is it possible to get the actual
value?

No, are you interested in submitting a patch ? I suggest something like "clang_Cursor_getBitWidthValue(CXCursor)" similar to how clang_Cursor_getNumArguments(CXCursor C) works, with the difference that it can return "unsigned" and 0 if the cursor is not a bitfield.

Probably in the long run if no one else does it. But right now I have
other things on my todo-list for my tool with higher priority.

Would two functions be a good idea, one for signed and one for unsigned
values? Just like "clang_getEnumConstantDeclValue" and
"clang_getEnumConstantDeclUnsignedValue".

No, negative values are not allowed for bitfield width.

Right. I was thinking that the field could be either signed or unsigned but that has nothing to do with the width of the bit field.