Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,15 @@ private void VisitRecordDecl(RecordDecl recordDecl)
{
if (fieldDecl.IsBitField)
{
VisitBitfieldDecl(fieldDecl, bitfieldDescs, recordDecl, contextName: "", ref bitfieldIndex, ref bitfieldPreviousSize, ref bitfieldRemainingBits);
if (fieldDecl.IsZeroLengthBitField)
{
bitfieldPreviousSize = 0;
bitfieldRemainingBits = 0;
}
else
{
VisitBitfieldDecl(fieldDecl, bitfieldDescs, recordDecl, contextName: "", ref bitfieldIndex, ref bitfieldPreviousSize, ref bitfieldRemainingBits);
}
}
else
{
Expand Down
9 changes: 9 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,15 @@ private BitfieldDesc[] GetBitfieldDescs(RecordDecl recordDecl)
continue;
}

if (fieldDecl.IsZeroLengthBitField)
{
// A zero-length bitfield (`int : 0;`) names no storage and only forces the next
// bitfield into a fresh storage unit, so it emits no region and resets the run.
previousSize = 0;
remainingBits = 0;
continue;
}

var type = fieldDecl.Type;
var currentSize = type.Handle.SizeOf;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace ClangSharp.Test
{
public partial struct MyStruct
{
[NativeBitfield("o0_b0_1", offset: 0, length: 1)]
public uint _bitfield1;

[NativeTypeName("unsigned int : 1")]
public uint o0_b0_1
{
get
{
return _bitfield1 & 0x1u;
}

set
{
_bitfield1 = (_bitfield1 & ~0x1u) | (value & 0x1u);
}
}

[NativeBitfield("o4_b0_1", offset: 0, length: 1)]
public uint _bitfield2;

[NativeTypeName("unsigned int : 1")]
public uint o4_b0_1
{
get
{
return _bitfield2 & 0x1u;
}

set
{
_bitfield2 = (_bitfield2 & ~0x1u) | (value & 0x1u);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace ClangSharp.Test
{
public partial struct MyStruct
{
[NativeBitfield("o0_b0_1", offset: 0, length: 1)]
public uint _bitfield1;

[NativeTypeName("unsigned int : 1")]
public uint o0_b0_1
{
readonly get
{
return _bitfield1 & 0x1u;
}

set
{
_bitfield1 = (_bitfield1 & ~0x1u) | (value & 0x1u);
}
}

[NativeBitfield("o4_b0_1", offset: 0, length: 1)]
public uint _bitfield2;

[NativeTypeName("unsigned int : 1")]
public uint o4_b0_1
{
readonly get
{
return _bitfield2 & 0x1u;
}

set
{
_bitfield2 = (_bitfield2 & ~0x1u) | (value & 0x1u);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace ClangSharp.Test
{
public partial struct MyStruct
{
[NativeBitfield("o0_b0_1", offset: 0, length: 1)]
public uint _bitfield1;

[NativeTypeName("unsigned int : 1")]
public uint o0_b0_1
{
readonly get
{
return _bitfield1 & 0x1u;
}

set
{
_bitfield1 = (_bitfield1 & ~0x1u) | (value & 0x1u);
}
}

[NativeBitfield("o4_b0_1", offset: 0, length: 1)]
public uint _bitfield2;

[NativeTypeName("unsigned int : 1")]
public uint o4_b0_1
{
readonly get
{
return _bitfield2 & 0x1u;
}

set
{
_bitfield2 = (_bitfield2 & ~0x1u) | (value & 0x1u);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace ClangSharp.Test
{
public partial struct MyStruct
{
[NativeBitfield("o0_b0_1", offset: 0, length: 1)]
public uint _bitfield1;

[NativeTypeName("unsigned int : 1")]
public uint o0_b0_1
{
readonly get
{
return _bitfield1 & 0x1u;
}

set
{
_bitfield1 = (_bitfield1 & ~0x1u) | (value & 0x1u);
}
}

[NativeBitfield("o4_b0_1", offset: 0, length: 1)]
public uint _bitfield2;

[NativeTypeName("unsigned int : 1")]
public uint o4_b0_1
{
readonly get
{
return _bitfield2 & 0x1u;
}

set
{
_bitfield2 = (_bitfield2 & ~0x1u) | (value & 0x1u);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<bindings>
<namespace name="ClangSharp.Test">
<struct name="MyStruct" access="public">
<field name="_bitfield1" access="public">
<attribute>NativeBitfield("o0_b0_1", offset: 0, length: 1)</attribute>
<type>uint</type>
</field>
<field name="o0_b0_1" access="public">
<type native="unsigned int : 1">uint</type>
<get>
<code>return <bitfieldName>_bitfield1</bitfieldName> &amp; 0x<bitwidthHexStringBacking>1u</bitwidthHexStringBacking>;</code>
</get>
<set>
<code>
<bitfieldName>_bitfield1</bitfieldName> = (_bitfield1 &amp; ~0x<bitwidthHexStringBacking>1u</bitwidthHexStringBacking>) | (value &amp; 0x<bitwidthHexString>1u</bitwidthHexString>);</code>
</set>
</field>
<field name="_bitfield2" access="public">
<attribute>NativeBitfield("o4_b0_1", offset: 0, length: 1)</attribute>
<type>uint</type>
</field>
<field name="o4_b0_1" access="public">
<type native="unsigned int : 1">uint</type>
<get>
<code>return <bitfieldName>_bitfield2</bitfieldName> &amp; 0x<bitwidthHexStringBacking>1u</bitwidthHexStringBacking>;</code>
</get>
<set>
<code>
<bitfieldName>_bitfield2</bitfieldName> = (_bitfield2 &amp; ~0x<bitwidthHexStringBacking>1u</bitwidthHexStringBacking>) | (value &amp; 0x<bitwidthHexString>1u</bitwidthHexString>);</code>
</set>
</field>
</struct>
</namespace>
</bindings>
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ struct MyStruct3
return ValidateAsync(nameof(BitfieldWithNativeBitfieldAttributeTest), inputContents, additionalConfigOptions: PInvokeGeneratorConfigurationOptions.GenerateNativeBitfieldAttribute);
}

[Test]
public Task ZeroLengthBitfieldTest()
{
var inputContents = @"struct MyStruct
{
unsigned int o0_b0_1 : 1;
unsigned int : 0;
unsigned int o4_b0_1 : 1;
};
";

return ValidateAsync(nameof(ZeroLengthBitfieldTest), inputContents, additionalConfigOptions: PInvokeGeneratorConfigurationOptions.GenerateNativeBitfieldAttribute);
}

[Test]
public Task DeclTypeTest()
{
Expand Down
Loading