WARNINGS ON/OFF not working quite as expected. lazarus / 64-FPC development ---------------------------------------------------------------------- 440bx · 2018-10-08 17:43 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$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 · 2018-10-08 18:56 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   {$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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   SomePointer^ := 0;  writeln(sizeof(SomePointer^)); Or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   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 · 2018-10-08 19:48 Note this goes for other things too: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   {.$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 · 2018-10-08 20:15 @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 · 2018-10-08 20:18 You better pay attention. I just explained how and why. Sorry to mock you again about a clear lack of knowledge... (regarding your silly modeswitch dances....< >) For your reference, from globals.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     const     delphimodeswitches =         [m_delphi,m_class,m_objpas,m_result,m_string_pchar,          m_pointe r_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistrin g,          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_ob jfpc,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_pa ra,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_neste d_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. ---------------------------------------------------------------------- 440bx · 2018-10-08 20:41 Quote from: Thaddy on October 08, 2018, 10:18:33 pmYou better pay attention. I just explained how and why. Sorry to mock you again about a clear lack of knowledge... (regarding your silly modeswitch dances....< >) For your reference, from globals.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     const     delphimodeswitches =         [m_delphi,m_class,m_objpas,m_result,m_string_pchar,          m_pointe r_2_procedure,m_autoderef,m_tp_procvar,m_initfinal,m_default_ansistrin g,          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_ob jfpc,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_pa ra,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_neste d_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. As I said in the previous post... you don't get tired of babbling nonsense away.  If you could focus that dedication into learning about computer programming, you might eventually become decent at it. ---------------------------------------------------------------------- ASerge · 2018-10-09 01:01 Let's take a shorter example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE CONSOLE}{$WARNINGS ON}var  SomePointer: PInteger;begin  // Bad to dereference a nil pointer!  Writeln(SomePointer^); // This line generates warning, it's OK  Writeln(SomePointer^); // But if you comment out the line above, THIS line generates a warning. Typically, warnings are not re-generated. This behavior is compatible with Delphi  Writeln(SizeOf(SomePointer^)); // It also generates a warning when the lines above are commented out, but should only generate a hint about the unused variable. I think this is a bug  Readln;end. ---------------------------------------------------------------------- 440bx · 2018-10-09 03:22 Quote from: ASerge on October 09, 2018, 03:01:00 am Typically, warnings are not re-generated. This behavior is compatible with Delphi That's reasonable and useful since it doesn't drown the programmer in repeated warnings for the same thing. It's unfortunate that the compiler's warning system doesn't realize that sizeof(apointer^) isn't actually dereferencing the pointer, which leads to a superfluous warning when the pointer isn't initialized and, no warning when the uninitialized pointer is used because it considers it a repetition, when it actually isn't one, since in one case the pointer isn't really dereferenced while it actually is dereferenced in the second case. It would be nice (and useful) if the compiler could be made aware of the difference. ---------------------------------------------------------------------- Thaddy · 2018-10-09 15:58 Since a pascal compiler first evaluate use of the inner statement which is a reference to that pointer, it is unlikely that any Pascal compiler will solve your issue. To me it is obvious, to some it may not. It is the programmer that decides to ignore warnings. As this example shows, usually the compiler is right and the programmer is wrong. BTW this is not side effect: this is as intended. Aside: do you understand now why your over-use and mis-use of compiler directives makes me laugh and running all over the floor before I can even reply to one of your questions? I am not the only one... trust me... This is not about downgrading you, it is about you realizing you lack some knowledge as I do in some parts (but that is only since wikipedia.. ) , but not here... ---------------------------------------------------------------------- 440bx · 2018-10-09 16:17 Quote from: Thaddy on October 09, 2018, 05:58:27 pmAside: do you understand now why your over-use and mis-use of compiler directives makes me laugh and running all over the floor before I can even reply to one of your questions? Too bad it doesn't make you laugh enough to stay on the floor away from the keyboard.