Skip to content
Closed
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
8 changes: 8 additions & 0 deletions InteropGenerator.Runtime/Attributes/BitFieldAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace InteropGenerator.Runtime.Attributes;

[AttributeUsage(AttributeTargets.Field, AllowMultiple = true)]
public sealed class BitFieldAttribute<T>(string name, int index, int length = 1) : Attribute {
public string Name { get; } = name;
public int Index { get; } = index;
public int Length { get; } = length;
}
43 changes: 43 additions & 0 deletions InteropGenerator.Runtime/BitOps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Numerics;
using System.Runtime.CompilerServices;

namespace InteropGenerator.Runtime;

public static class BitOps {
/// <summary>
/// Creates a mask with <paramref name="length"/> number of set bits starting from the LSB.<br/>
/// Example: <code>CreateLowBitMask&lt;byte&gt;(3)</code> results in <code>0b0000_0111</code>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T CreateLowBitMask<T>(int length)
where T : unmanaged, IBinaryInteger<T> {
if (length <= 0) return T.Zero;
if (length >= Unsafe.SizeOf<T>() * 8) return T.AllBitsSet;
return (T.One << length) - T.One;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetBit<T>(T value, int index)
where T : unmanaged, IBinaryInteger<T> {
return ((value >> index) & T.One) != T.Zero;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SetBit<T>(T value, int index, bool enable)
where T : unmanaged, IBinaryInteger<T> {
T mask = T.One << index;
return enable ? value | mask : value & ~mask;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetBits<T>(T value, int shift, T mask)
where T : unmanaged, IBinaryInteger<T> {
return (value >> shift) & mask;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T SetBits<T>(T value, int shift, T mask, T newValue)
where T : unmanaged, IBinaryInteger<T> {
return (value & ~(mask << shift)) | ((newValue & mask) << shift);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using InteropGenerator.Diagnostics.Analyzers;
using InteropGenerator.Tests.Helpers;
using Xunit;

namespace InteropGenerator.Tests.Analyzer;

public class BitFieldAttributeIsValidAnalyzerTests {
[Fact]
public async Task BitFieldAttributeIsValid_NoWarn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=30)]
[GenerateInterop]
public partial struct TestStruct
{
[BitField<bool>("TestBool1", 0)]
[BitField<byte>("TestByte1", 1)]
[BitField<byte>("TestByte2", 2, 3)]
[BitField<short>("TestShort1", 5, 2)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;

[BitField<bool>("TestBool2", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal ushort _testField2;

[BitField<bool>("TestBool3", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal uint _testField3;

[BitField<bool>("TestBool4", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal ulong _testField4;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeStartBitNegative_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[{|CSIG0401:BitField<short>("TestShort", -1, 1)|}]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeLengthNegative_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[{|CSIG0402:BitField<short>("TestShort", 0, -1)|}]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeLengthZero_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[{|CSIG0402:BitField<short>("TestShort", 0, 0)|}]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeExceedsFieldSize_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[{|CSIG0403:BitField<short>("TestShort", 0, 9)|}]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeEnumDoesNotExceedFieldSize_NoWarn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[BitField<TestEnum>("TestEnum1", 2, 2)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;

[Flags]
public enum TestEnum {
TestValue1 = 1 << 0,
TestValue2 = 1 << 1,
}
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeInvalidBackingFieldType_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[BitField<bool>("TestBool1", 0)]
[BitField<byte>("TestByte1", 1)]
[BitField<byte>("TestByte2", 2, 3)]
[BitField<short>("TestShort1", 5, 2)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal sbyte {|CSIG0405:_testField1|};

[BitField<bool>("TestBool2", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal short {|CSIG0405:_testField2|};

[BitField<bool>("TestBool3", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal int {|CSIG0405:_testField3|};

[BitField<bool>("TestBool4", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal long {|CSIG0405:_testField4|};

[BitField<bool>("TestBool5", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal TestEnum {|CSIG0405:_testField5|};

[BitField<TestEnum>("TestEnum1", 0, 2)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal TestEnum {|CSIG0405:_testField6|};

[BitField<bool>("TestBool6", 0)]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal DateTime {|CSIG0405:_testField7|};

[Flags]
public enum TestEnum {
TestValue1 = 1 << 0,
TestValue2 = 1 << 1,
}
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}

[Fact]
public async Task BitFieldAttributeInvalidBooleanLength_Warn() {
const string code = """
[global::System.Runtime.InteropServices.StructLayout(global::System.Runtime.InteropServices.LayoutKind.Explicit, Size=1)]
[GenerateInterop]
public partial struct TestStruct
{
[{|CSIG0406:BitField<bool>("TestBool1", 0, 2)|}]
[global::System.Runtime.InteropServices.FieldOffsetAttribute(0)] internal byte _testField1;
}
""";
await AnalyzerVerifier<BitFieldAttributeIsValidAnalyzer>.VerifyAnalyzerAsync(code);
}
}
Loading