Forum > FPC development

WARNINGS ON/OFF not working quite as expected.

(1/2) > >>

440bx:
Hello,

The compiler emits a warning when it encounters a "sizeof(anunitializedpointer^)" about the pointer not being initialized. 

To stop the compiler from emitting a warning in that case, turning WARNINGS OFF then back on after the statement should solve that problem and, it does BUT, after that, it will no longer warn if the pointer is _actually_ used without being initialized even though the warnings have been turned back on.

In the short sample program, in case 1, there really isn't any reason to emit a warning.  In case 2, there is good reason to emit a warning.  When the warning is suppressed for case 1, then no warning is given for case 2 (that's when it really should give a warning.)


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC                        } {$MODESWITCH     ADVANCEDRECORDS     }{$MODESWITCH     TYPEHELPERS         }{$MODESWITCH     ALLOWINLINE         }{$MODESWITCH     RESULT              }{$MODESWITCH     PROPERTIES          } {$MODESWITCH     ANSISTRINGS-        }{$MODESWITCH     AUTODEREF-          }{$MODESWITCH     UNICODESTRINGS-     }{$MODESWITCH     POINTERTOPROCVAR-   } {$LONGSTRINGS    OFF                 }{$WRITEABLECONST ON                  }{$TYPEDADDRESS   ON                  }  {$define BadPointer} program SizeofWarning; var  SomePointer : PDWORD; begin  // without the WARNINGS OFF directive the compiler warns about the use of  // SomePointer^ in sizeof.  It would be nice if it didn't, since the pointer  // really isn't being dereferenced.  { case 1 }   {$WARNINGS OFF}    writeln(sizeof(SomePointer^));    // generates spurious warning  {$WARNINGS ON}   // {$WARNINGS ON} above doesn't seem to have the expected effect   // the writeln below really deserves a warning but, none is generated { case 2 }   {$ifdef BadPointer}    writeln('you might get an exception when the next statement is executed');    writeln('Press any key to try your luck today.');    readln;     writeln(SomePointer^);           // this should generate a warning because                                     // the variable is being dereferenced yet,                                     // it does not.  {$endif}   writeln('Press any key to end this program');  Readln;end. 
it would be nice if the compiler was "aware" that dereferencing in a sizeof statement really isn't a dereference and, as such, does not merit a warning.

Thaddy:
Actually,what was the state of warnings? if it was off in the first place (because maybe a compiler switch instead of sourcecode directive) turning it on would be wrong.
As I explained before, you are using conditionals in a, let's say, funny way, using too much or assuming too little.
If you want full control over warnings there are several options, (I wrote the wiki part on those, have a look there http://wiki.freepascal.org/Turn_warnings_and_hints_on_or_off )
Simple example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$push} // don't assume on or off, but save state{$warnings off}// ... do something that normally throws a warning{$pop} // restore state: if it was off it stays off, if it was on, it's on again.Or:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$push}{$warn 5092 off}// .. Do something with uninitialized managed type .... would otherwise throw (5092) Variable "L" of a managed type does not seem to be initialized{$pop}
In Lazarus IDE and only in the Lazarus IDE there's a Lazarus specific macro option as well that doesn't work in other editors, like fp or geany or notepad++. See also the wiki.
That option is therefor not recommended.

To "fix" your code and introduce a bug:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  {$PUSH}{$WARN 5037 OFF}  // Warning: (5037) Variable "SomePointer" does not seem to be initialized  // generates a genuine warning otherwise: the content of SomePointer^ is not known...  // that's a bug!! The compiler will now assume this particular variable to be initialized... everywhere.    writeln(sizeof(SomePointer^));      {$POP} // means further variable, but NOT this one will again raise to the warning
To really fix your code, initialize the content of SomePointer^ as the compiler told you.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  SomePointer^ := 0;  writeln(sizeof(SomePointer^));
Or

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  writeln(SizeOf(PDWORD^));// now the second occurance will throw the warning..
Once you tell the compiler to ignore initialization for a variable it is internally registered as initialized, so the compiler will ignore further occurances.
That is the reason of your confusion the second time it occurs, but that is fully consistent compiler behavior. You marked it as ignore... Any further warning of the same kind for the same variable will be further ignored.
You can't "uninitialize" a variable very easily.... and what you did was telling the compiler, never mind, accept that I mean it is initialized, so that's what the compiler does....

I hope this is clear. I also hope it is clear that it is very dangerous to turn ALL warnings on or off as it can lead to a myriad of hard to find bugs.
But the compiler is consistent and above all: correct...

Thaddy:
Note this goes for other things too:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  {.$packrecords 1}type  Tmyrecord = record  a:integer;  b:byte;  c:longint;  end; var  // how is this packed? {$packrecords 1}  d:TMyrecord;begin  writeln(SizeOf(d));end.

440bx:
@Thaddy,

Your ability to generate and babble pointless, besides the point, nonsense seems to be the only natural talent you have. 

I have to give you credit for tirelessly developing it.

Thaddy:
You better pay attention. I just explained how and why. Sorry to mock you again about a clear lack of knowledge... :D (regarding your silly modeswitch dances....< >:D>)
For your reference, from globals.pas

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    const       delphimodeswitches =         [m_delphi,m_class,m_objpas,m_result,m_string_pchar,          m_pointer_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistring,          m_out,m_default_para,m_duplicate_names,m_hintdirective,          m_property,m_default_inline,m_except,m_advanced_records,          m_array_operators];       delphiunicodemodeswitches = delphimodeswitches + [m_systemcodepage,m_default_unicodestring];       fpcmodeswitches =         [m_fpc,m_string_pchar,m_nested_comment,m_repeat_forward,          m_cvar_support,m_initfinal,m_hintdirective,          m_property,m_default_inline];       objfpcmodeswitches =         [m_objfpc,m_fpc,m_class,m_objpas,m_result,m_string_pchar,m_nested_comment,          m_repeat_forward,m_cvar_support,m_initfinal,m_out,m_default_para,m_hintdirective,          m_property,m_default_inline,m_except];       tpmodeswitches =         [m_tp7,m_tp_procvar,m_duplicate_names];{$ifdef gpc_mode}       gpcmodeswitches =         [m_gpc,m_tp_procvar];{$endif}       macmodeswitches =         [m_mac,m_cvar_support,m_mac_procvar,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_default_inline];       isomodeswitches =         [m_iso,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_isolike_io,          m_isolike_program_para,          m_isolike_mod];       extpasmodeswitches =         [m_extpas,m_tp_procvar,m_duplicate_names,m_nested_procvars,m_non_local_goto,m_isolike_unary_minus,m_isolike_io,          m_isolike_program_para,          m_isolike_mod];
The issue at hand is explained in depth. I hope it helped.

Navigation

[0] Message Index

[#] Next page

Go to full version