Recent

Author Topic: $HINTS directive  (Read 1552 times)

440bx

  • Hero Member
  • *****
  • Posts: 4743
$HINTS directive
« on: March 03, 2024, 08:14:19 am »
Hello,

the documentation states :
Quote
1.2.27 $HINTS : Emit hints

{$HINTS ON} switches the generation of hints on. {$HINTS OFF} switches the generation of hints off. Contrary to the command line option -vh this is a local switch, this is useful for checking parts of the code.
(emphasis mine)

I have an include file (.inc) that generates over a hundred hints which really get in the way of noticing other potentially important hints.

I added "{$HINTS OFF}" at the top of the include file and {$HINTS ON} at the bottom of the include file.  The behavior is as if I had not specified anything.  If I just have the {$HINTS OFF} (and no "ON" anywhere after that) then I don't get the hints but, _all_ other hints are gone too (not just the hints generated by the include file.)

The documentation implies that hints can be turned on and off for parts of the code but, it looks like I'm not doing it right. 

My question is: how do I turn off the generation of hints for what is in the include file and _nothing else_ ?

Thank you for your help.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: $HINTS directive
« Reply #1 on: March 03, 2024, 08:41:09 am »
Do it like this maybe helps:
Code: Pascal  [Select][+][-]
  1. {$HINTS OFF}
  2. {$INCLUDE <your inc>}
  3. {$HINTS ON}
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: $HINTS directive
« Reply #2 on: March 03, 2024, 08:51:29 am »
Thank you for the suggestion Thaddy.

I tried it and it made no difference.  I still got all the hints related to the include file.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Awkward

  • Full Member
  • ***
  • Posts: 144
Re: $HINTS directive
« Reply #3 on: March 03, 2024, 09:39:50 am »
i thinked, it works for {$HINT text} directives only...

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: $HINTS directive
« Reply #4 on: March 03, 2024, 10:28:20 am »
No that is not the case. It used to work. If you use the Lazarus IDE, can it be that in the Lazarus settings hints on is defined? That makes it a global setting
« Last Edit: March 03, 2024, 10:31:47 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: $HINTS directive
« Reply #5 on: March 03, 2024, 10:36:26 am »
If you use the Lazarus IDE, can it be that in the Lazarus settings hints on is defined? That makes it a global setting
I thought about that too and did the compile directly with FPC, still got the unwanted hints in spite of bracketing the include file between {$HINTS OFF} and {$HINTS ON}.

if there is no {$HINTS ON} anywhere then _all_ the hints are gone but, there does not seem to be a way to suppress just the hints that are generated by the code in the include file.

I'm really starting to wonder if the documented "this is a local switch" reflects what it really is.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: $HINTS directive
« Reply #6 on: March 03, 2024, 10:49:15 am »
IIRC then turning Hints off is global for a unit, e.i. it is on or off for the entire unit.

I may be wrong of course.

If you build your program in Lazarus, then you can have the IDE insert {%H-} comments just before the offending code, this will not affect fpc emitting hints, but they will be filtered away by Lazarus and don't show up in the Messages window.

If you have hundreds of them, that's gonna be it bit cumbersome though.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: $HINTS directive
« Reply #7 on: March 03, 2024, 10:55:48 am »
Are the hints all the same or all in just a few categories?
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: $HINTS directive
« Reply #8 on: March 03, 2024, 11:31:39 am »
IIRC then turning Hints off is global for a unit, e.i. it is on or off for the entire unit.
That's what it looks like, global to the unit  which in this case is the entire program since the program does not use any units.

I think the wording in the documentation is misleading.  I wouldn't call an entire unit simply "parts of the code".

As you surmised, using %H- would be too cumbersome.  I'll live with unwanted hints for the time being (maybe I'll turn the .inc into a unit.)




Are the hints all the same or all in just a few categories?
A few categories, I could turn those individual ones off  but, it still applies to a greater extent than what I'd like.

I guess the solution is to turn the .inc file into a unit, that will take care of the "more hints than you can shake a stick at" problem.



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: $HINTS directive
« Reply #9 on: March 04, 2024, 09:12:56 pm »
IIRC then turning Hints off is global for a unit, e.i. it is on or off for the entire unit.
That's what it looks like, global to the unit  which in this case is the entire program since the program does not use any units.

I think the wording in the documentation is misleading.  I wouldn't call an entire unit simply "parts of the code".

It is not global to the compilation module.

Take the following example:

hint.inc

Code: Pascal  [Select][+][-]
  1. procedure PROC1;
  2. var
  3.   arr: array of LongInt;
  4. begin
  5.   SetLength(arr, 10);
  6. end;
  7.  
  8. function PROC2: String;
  9. begin
  10.   SetLength(Result, 10);
  11. end;

thint.pp

Code: Pascal  [Select][+][-]
  1. program thint;
  2.  
  3. {$MODE OBJFPC}
  4. {$MACRO ON}
  5.  
  6. {$WARNING 'Only hints off'}
  7. {$DEFINE PROC1:=Test1}
  8. {$DEFINE PROC2:=Test2}
  9. {$HINTS OFF}
  10. {$I hint.inc}
  11. {$HINTS ON}
  12.  
  13. {$WARNING 'Only notes off'}
  14. {$DEFINE PROC1:=Test3}
  15. {$DEFINE PROC2:=Test4}
  16. {$NOTES OFF}
  17. {$I hint.inc}
  18. {$NOTES ON}
  19.  
  20. {$WARNING 'Notes and hints off'}
  21. {$DEFINE PROC1:=Test5}
  22. {$DEFINE PROC2:=Test6}
  23. {$HINTS OFF}
  24. {$NOTES OFF}
  25. {$I hint.inc}
  26. {$HINTS ON}
  27. {$NOTES ON}
  28.  
  29. begin
  30.   Test1;
  31.   Test2;
  32.   Test3;
  33.   Test4;
  34.   Test5;
  35.   Test6;
  36. end.

The compiler output will be as follows:

Code: [Select]
PS C:\fpc\git> fpc -FEtestoutput -viwnh .\fpctests\thint.pp
Hint: Start of reading config file C:\lazarus\2.2\fpc\3.2.2\bin\x86_64-win64\fpc.cfg
Hint: End of reading config file C:\lazarus\2.2\fpc\3.2.2\bin\x86_64-win64\fpc.cfg
Free Pascal Compiler version 3.2.2 [2022/01/02] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win64 for x64
Compiling .\fpctests\thint.pp
thint.pp(6,2) Warning: User defined: 'Only hints off'
hint.inc(3,3) Note: Local variable "arr" is assigned but never used
thint.pp(13,2) Warning: User defined: 'Only notes off'
hint.inc(5,16) Hint: Local variable "arr" of a managed type does not seem to be initialized
hint.inc(10,19) Hint: Function result variable does not seem to be initialized
thint.pp(20,2) Warning: User defined: 'Notes and hints off'
Linking testoutput\thint.exe
69 lines compiled, 0.1 sec, 36496 bytes code, 1556 bytes data
3 warning(s) issued
4 hint(s) issued
1 note(s) issued

If it doesn't work for you, then please provide an explicit example.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: $HINTS directive
« Reply #10 on: March 05, 2024, 01:30:12 am »
If it doesn't work for you, then please provide an explicit example.
Fair enough... attached is an example with all the necessary files to compile it.

I'd love to be proved wrong... those "unused" hint messages are a pain in the neck!
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: $HINTS directive
« Reply #11 on: March 09, 2024, 06:07:04 pm »
I'd love to be proved wrong... those "unused" hint messages are a pain in the neck!

Okay, those are a problematic one. The thing is the compiler does not know whether this symbol is used at the time the symbol is declared (which is where the $HINTS OFF directive is active, but only when the whole program is compiled (or if they're located in the implementation-section a unit, the unit). However the compiler does not keep track of which verbosity settings had been active at the time the symbol had been declared.

As a workaround you can add a {$WARN 5028 OFF} directly before the end. (or if you're not interested in these message at all, at the top of the program).

Note: you need to do this for each unit with private symbols where you want to suppress the hints.

Thaddy

  • Hero Member
  • *****
  • Posts: 16194
  • Censorship about opinions does not belong here.
Re: $HINTS directive
« Reply #12 on: March 09, 2024, 06:22:45 pm »
And better surround them with {$push}/{$pop}. That is actually my personal workflow.
It has the advantage that you can compile with -Sewh to sanity check your code.
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4743
Re: $HINTS directive
« Reply #13 on: March 09, 2024, 07:28:58 pm »
Thanks PascalDragon.  At least I know it isn't because of something I'm doing wrong.

Good suggestion Thaddy, I normally do that too.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018