Recent

Author Topic: FPC feature suggestion  (Read 902 times)

440bx

  • Hero Member
  • *****
  • Posts: 948
FPC feature suggestion
« on: December 25, 2018, 07:18:14 am »
hello,

FPC is very good about issuing hints mentioning the fact that some variable being used may not have been initialized.  This can be very useful.

Unfortunately, at this time, there is no way to tell FPC that either, the variable is already initialized or, that it will be initialized by the procedure/function it is being passed to.  For instance, consider the code snippet below:
Code: Pascal  [Select]
  1. function SomeFunc(parm1 : integer; var parm2 : somerecord) : Boolean;
  2. begin
  3.    RtlZeroMemory(@parm2, sizeof(parm2));  // initialize parm2
  4.   { perform binary magic here }
  5.   result := everything_is_great;
  6. end;
  7.  
  8. ...
  9. ...
  10. ...
  11. var
  12.   MyParm : somerecord;
  13.  
  14. begin
  15.   { use SomeFunc }
  16.  
  17.   r := SomeFunc(v, MyParm);   { compiler issues a hint that MyParm is not initialized }
  18. end.
  19.  

It would be very nice to have something along the lines of
Code: Pascal  [Select]
  1. {$initializes parm2}
that could be placed before SomeFunc's begin statement to inform the compiler that SomeFunc is responsible for initializing parm2.  That way, the compiler would not emit superfluous hints about the variable not being initialized every time SomeFunc is used in the program's body.

Thanks for reading. :)



using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

bytebites

  • Full Member
  • ***
  • Posts: 183
Re: FPC feature suggestion
« Reply #1 on: December 25, 2018, 09:15:36 am »
Does this help?
Code: Pascal  [Select]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;

Thaddy

  • Hero Member
  • *****
  • Posts: 8179
Re: FPC feature suggestion
« Reply #2 on: December 25, 2018, 09:39:38 am »
You can suppress the message with {$push}{$warn xxxx off}.. declaration --- {$pop} xxxx is the number of the warning.
Also note rtlxxx fiunctions are platform. Why not
Code: Pascal  [Select]
  1. parm2 := Default(SomeRecord);
« Last Edit: December 25, 2018, 10:18:51 am by Thaddy »
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

ASerge

  • Hero Member
  • *****
  • Posts: 1300
Re: FPC feature suggestion
« Reply #3 on: December 25, 2018, 11:28:00 am »
Code: Pascal  [Select]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;
+1

440bx

  • Hero Member
  • *****
  • Posts: 948
Re: FPC feature suggestion
« Reply #4 on: December 25, 2018, 12:53:14 pm »
Does this help?
Code: Pascal  [Select]
  1. function SomeFunc(parm1 : integer; out parm2 : somerecord) : Boolean;
Not only that helps, that is the perfect way of doing it.

Thank you, I much appreciate your pointing out the right way of doing it.
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.