[fpc-devel] Wrong docs: not initialized global variables

Ondrej Pokorny lazarus at kluug.net
Tue Jul 3 22:57:27 CEST 2018


On 03.07.2018 22:08, Florian Klämpfl wrote:
> Am 03.07.2018 um 21:30 schrieb Ondrej Pokorny:
>> On 03.07.2018 20:54, Florian Klämpfl wrote:
>> program Project1;
>> type
>>    TMyArray = array[0..10] of Integer;
>>    procedure Init2(var A: TMyArray);
>>    begin
>>      A[2] := 1;
>>    end;
>> var
>>    A: TMyArray;
>> begin
>>    Init2(A);
>> end.
>>
>> should not emit the warning because A is not being read in Init2.
>
> (please note it throws only a hint in this case as A *is* initialized 
> as it is a global variable).
>
> FPC has no global data flow analysis (and I do not plan to do so yet), 
> so this is warning is currently far out of reach.

Doing it for SetLength/FillChar would be a good start.


>> IMPORTANT: the compiler knows that SetLength and FillChar don't 
>> read/use the data so no warning is appropriate there. 
>
> SetLength does somehow :), as the examples showed.

Not really. It keeps the "read/used" data within the variable - the 
possibly uninitialized data is not written or used outside the variable 
itself.


>> What you achieved with SetLength and FillChar emitting a warning is 
>> that you made the compiler more stupid instead of making it more clever.
>
> The compiler has not to be clever, it has to follow rules. And the 
> rules which need to be followed is the meaning of the var parameter. 
> It means: data in/data out.

It's hard to argue like this with all the different hints/warnings/notes 
being emitted on the notification side and different optimizations being 
done on the compiler side. To me it looks more like this: "the compiler 
may be clever only when we want it to be".

---

Probably I am the only one who thinks that the code below is ridiculous...

     procedure TExternalAssemblerOutputFile.AsmWriteFiltered(p: pchar; 
len: longint);
       var
         s: ansistring;
       begin
         MaybeAddLinePrefix;
         s:=''; // <<< ????
         setlength(s,len);
         move(p^,s[1],len);
         AsmWriteAnsiStringUnfiltered(decorator.LineFilter(s));
       end;

---

I have split feelings: I really like to get the warning/hint at places 
where it makes sense so I don't want to disable it globally. But the 
amount of false positives increased significantly :/

Ondrej



More information about the fpc-devel mailing list