gir2pas: Fixed issues when generating bit fields with a null or duplicate masks.

In this case, the code used to create a wrong index with value 255.
This is now detected and fixed.

Also, the code was not able to handle bit fields with duplicates.
Now this is also fixed.
This commit is contained in:
Abou Al Montacir 2023-06-04 18:35:44 +02:00
parent 8ef337b53e
commit aba8308de3

View File

@ -1101,6 +1101,8 @@ begin
Value := AItem.Members.Member[i]^.Value; Value := AItem.Members.Member[i]^.Value;
if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin
UIntValue := UInt64(StrToInt(Value)); UIntValue := UInt64(StrToInt(Value));
if UIntValue = 0 then
Continue;
MSB := BsrQWord(UIntValue); MSB := BsrQWord(UIntValue);
if UIntValue > 1 shl MSB then if UIntValue > 1 shl MSB then
Continue; Continue;
@ -1134,28 +1136,31 @@ procedure TPascalUnit.HandleBitfield(AItem: TgirBitField);
var var
Comma: String[2]; Comma: String[2];
n: Integer; n: Integer;
i: Integer;
StrValue: String;
begin begin
WriteLn(Name, ' = ', Value); WriteLn(Name, ' = ', Value);
if Value = 0 then begin
Exit(IndentText(Name + ' = []; {0 = $00000000}', 2));
end;
Result := IndentText(Name + ' = [', 2, 0); Result := IndentText(Name + ' = [', 2, 0);
Comma := LineEnding; Comma := LineEnding;
for n := BsfDWord(Value) to BsrDword(Value) do begin for n := BsfDWord(Value) to BsrDword(Value) do begin
if Value and (1 << n) <> 0 then begin if Value and (1 << n) <> 0 then begin
if n + 1 < AItem.Members.Count then begin StrValue := IntToStr(1 << n);
Name := AItem.Members.Member[n + 1]^.CIdentifier; Name := TypeName + 'Idx(' + IntToStr(n) + ')';
end else begin for i := 0 to AItem.Members.Count - 1 do begin
Name := TypeName + '(' + IntToStr(n) + ')'; if StrValue = AItem.Members.Member[i]^.Value then begin
end; Name := AItem.Members.Member[i]^.CIdentifier;
Result += Comma + IndentText(Name, 4, 0); Break;
Comma := ',' + LineEnding; end;
end; end;
Result += Comma + IndentText(Name, 4, 0);
Comma := ',' + LineEnding;
end;
end; end;
if Comma[1] = ',' then begin Result += LineEnding;
n := 2; Result += IndentText(']; {' + IntToStr(Value) + ' = $' + IntToHex(Value) + '}', 2);
Result += LineEnding;
end else begin
n := 0;
end;
Result += IndentText(']; {' + IntToStr(Value) + ' = $' + IntToHex(Value) + '}', n);
end; end;
var var
Section: TPDeclarationWithLines; Section: TPDeclarationWithLines;