From aba8308de352c8b80469515212fa435515a3b69b Mon Sep 17 00:00:00 2001 From: Abou Al Montacir Date: Sun, 4 Jun 2023 18:35:44 +0200 Subject: [PATCH] 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. --- tools/gir2pascal/girpascalwritertypes.pas | 37 +++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tools/gir2pascal/girpascalwritertypes.pas b/tools/gir2pascal/girpascalwritertypes.pas index d3971f93b6..1e33d644c7 100644 --- a/tools/gir2pascal/girpascalwritertypes.pas +++ b/tools/gir2pascal/girpascalwritertypes.pas @@ -1101,6 +1101,8 @@ begin Value := AItem.Members.Member[i]^.Value; if (goEnumAsSet in FOptions) and (AItem is TgirBitField) then begin UIntValue := UInt64(StrToInt(Value)); + if UIntValue = 0 then + Continue; MSB := BsrQWord(UIntValue); if UIntValue > 1 shl MSB then Continue; @@ -1134,28 +1136,31 @@ procedure TPascalUnit.HandleBitfield(AItem: TgirBitField); var Comma: String[2]; n: Integer; + i: Integer; + StrValue: String; begin WriteLn(Name, ' = ', Value); + if Value = 0 then begin + Exit(IndentText(Name + ' = []; {0 = $00000000}', 2)); + end; Result := IndentText(Name + ' = [', 2, 0); Comma := LineEnding; for n := BsfDWord(Value) to BsrDword(Value) do begin - if Value and (1 << n) <> 0 then begin - if n + 1 < AItem.Members.Count then begin - Name := AItem.Members.Member[n + 1]^.CIdentifier; - end else begin - Name := TypeName + '(' + IntToStr(n) + ')'; - end; - Result += Comma + IndentText(Name, 4, 0); - Comma := ',' + LineEnding; - end; + if Value and (1 << n) <> 0 then begin + StrValue := IntToStr(1 << n); + Name := TypeName + 'Idx(' + IntToStr(n) + ')'; + for i := 0 to AItem.Members.Count - 1 do begin + if StrValue = AItem.Members.Member[i]^.Value then begin + Name := AItem.Members.Member[i]^.CIdentifier; + Break; + end; + end; + Result += Comma + IndentText(Name, 4, 0); + Comma := ',' + LineEnding; + end; end; - if Comma[1] = ',' then begin - n := 2; - Result += LineEnding; - end else begin - n := 0; - end; - Result += IndentText(']; {' + IntToStr(Value) + ' = $' + IntToHex(Value) + '}', n); + Result += LineEnding; + Result += IndentText(']; {' + IntToStr(Value) + ' = $' + IntToHex(Value) + '}', 2); end; var Section: TPDeclarationWithLines;