Turn warnings and hints on or off: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | There are several ways to control the behavior of warnings in both the Lazarus IDE and in the source code itself. Some warnings and hints are not very useful in a particular case so you want to hide them from view. On the other hand, if you would hide warnings or hints globally, you may want to turn a particular warning or hint explicitly on for a particular piece of your code. | |
| 2 | 2 | ||
| 3 | 3 | ``` pascal | |
| 4 | 4 | procedure TForm.Button1Click(sender:Tobject); | |
| 5 | 5 | begin | |
| 6 | 6 | // Do something that does not use sender | |
| 7 | 7 | Form1.Button1.Caption := 'Test'; | |
| 8 | 8 | end; | |
| 9 | 9 | ``` | |
| 10 | 10 | ||
| 11 | 11 | This will generate a hint that parameter sender is not used. Doh. I can see that! Problem is that this hint turns up all over the place and I know what it means. How do I hide them? | |
| 12 | 12 | ||
| 13 | 13 | # Lazarus IDE options | |
| 14 | 14 | ||
| 15 | 15 | The Lazarus IDE can control hints and warnings in two ways: | |
| 16 | 16 | ||
| 17 | 17 | - Globally through the Project Options menu | |
| 18 | 18 | - Using an IDE macro | |
| 19 | 19 | ||
| 20 | 20 | ## Global IDE option | |
| 21 | 21 | ||
| 22 | 22 | Go to the Project Options-\>Compiler Options-\>Messages and uncheck 'Hint:Paramater "\$1" not used'. | |
| 23 | 23 | ||
| 24 | 24 | This will turn of this particular hint for your whole project.\ | |
| 25 | 25 | If you would like to turn off this hint for all your projects, you can check the left bottom checkbox 'Set compiler options as default'. | |
| 26 | 26 | ||
| 27 | 27 |  | |
| 28 | 28 | ||
| 29 | 29 | ## Local IDE option | |
| 30 | 30 | ||
| 31 | 31 | In the Lazarus IDE you can make use of the {%H-} IDE macro to turn hints off for a particular parameter.\ | |
| 32 | 32 | Of course {%H+} will turn hints on. | |
| 33 | 33 | ||
| 34 | 34 | ``` pascal | |
| 35 | 35 | procedure FormMouseDown(Sender: TObject; {%H-}Button: TMouseButton; | |
| 36 | 36 | {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer); | |
| 37 | 37 | ``` | |
| 38 | 38 | ||
| 39 | 39 | Note that this turns off ALL hints for that parameter, not just the 'is not used' hint, so be careful. | |
| 40 | 40 | ||
| 41 | 41 | # Source code and compiler options | |
| 42 | 42 | ||
| 43 | 43 | The options as described above will only work within the Lazarus IDE. | |
| 44 | 44 | ||
| 45 | 45 | It won't work in any other IDE, not even de FPC textmode IDE. The compiler output will still emit warnings and hints. | |
| 46 | 46 | ||
| 47 | 47 | Luckily there are several other options to control hints and warnings in the source code itself. | |
| 48 | 48 | ||
| 49 | 49 | ## Control all warnings per unit: | |
| 50 | 50 | ||
| 51 | 51 | Add | |
| 52 | 52 | ||
| 53 | 53 | ``` pascal | |
| 54 | 54 | {$warnings off} | |
| 55 | 55 | ``` | |
| 56 | 56 | ||
| 57 | 57 | or resp. | |
| 58 | 58 | ||
| 59 | 59 | ``` pascal | |
| 60 | 60 | {$warnings on} | |
| 61 | 61 | ``` | |
| 62 | 62 | ||
| 63 | 63 | just after the unit name. | |
| 64 | 64 | ||
| 65 | 65 | ## Control all hints per unit: | |
| 66 | 66 | ||
| 67 | 67 | Add | |
| 68 | 68 | ||
| 69 | 69 | ``` pascal | |
| 70 | 70 | {$hints off} | |
| 71 | 71 | ``` | |
| 72 | 72 | ||
| 73 | 73 | or resp. | |
| 74 | 74 | ||
| 75 | 75 | ``` pascal | |
| 76 | 76 | {$hints on} | |
| 77 | 77 | ``` | |
| 78 | 78 | ||
| 79 | 79 | just after the unit name. | |
| 80 | 80 | ||
| 81 | 81 | ## Control specific warnings: | |
| 82 | 82 | ||
| 83 | 83 | Add | |
| 84 | 84 | ||
| 85 | 85 | ``` pascal | |
| 86 | 86 | {$warn <number> off} | |
| 87 | 87 | ``` | |
| 88 | 88 | ||
| 89 | 89 | just after the unit name. See the example below. You can also use this construct if you want all warnings off but some specific warning(s) on: | |
| 90 | 90 | ||
| 91 | 91 | ``` pascal | |
| 92 | 92 | {$warnings off}{$warn <number> on} | |
| 93 | 93 | ``` | |
| 94 | 94 | ||
| 95 | 95 | ## Control Specific Warnings or hints per piece of code: | |
| 96 | 96 | ||
| 97 | 97 | You can control this very finely grained by using {\$push} and {\$pop} around a block of code. This does not have to be a method body as long it is a block, e.g. begin/end,repeat/until,/while do, try/finally etc. | |
| 98 | 98 | ||
| 99 | 99 | ``` pascal | |
| 100 | 100 | procedure withunusedparameter(const testme:integer); | |
| 101 | 101 | {$push}{$warn 5024 off} | |
| 102 | 102 | begin | |
| 103 | 103 | // | |
| 104 | 104 | end; | |
| 105 | 105 | {$pop} | |
| 106 | 106 | begin | |
| 107 | 107 | withunusedparameter(100); | |
| 108 | 108 | end. | |
| 109 | 109 | ``` | |
| 110 | 110 | ||
| 111 | 111 | # Obtaining the warnings and hints numbers to suppress | |
| 112 | 112 | ||
| 113 | 113 | You can obtain the needed numbers by compiling your code once with line info on and with -vwhilq | |
| 114 | 114 | ||
| 115 | 115 | This will show both the numbers and the messages and the line numbers where they are found. | |
| 116 | 116 | ||
| 117 | 117 | The other way to obtain hint, notice or warning number is to search inside FP repository for source of your current FPC version. | |
| 118 | 118 | ||
| 119 | 119 | All of the messages with its own description is stored in \<FPC source dir\>/msg/error\<language\>.msg | |
| 120 | 120 | ||
| 121 | 121 | This file is also have header, which provides explanation how error/notice/hint/warning constants are constructed. | |
| 122 | 122 | ||
| 123 | 123 | For the English language it's contains following: | |
| 124 | 124 | ||
| 125 | 125 | ``` pascal | |
| 126 | 126 | # CodePage 20127 | |
| 127 | 127 | ||
| 128 | 128 | # | |
| 129 | 129 | # The constants are build in the following order: | |
| 130 | 130 | # <part>_<type>_<txtidentifier> | |
| 131 | 131 | # | |
| 132 | 132 | # <part> is the part of the compiler the message is used | |
| 133 | 133 | # asmr_ assembler parsing | |
| 134 | 134 | # asmw_ assembler writing/binary writers | |
| 135 | 135 | # unit_ unit handling | |
| 136 | 136 | # option_ command line parameter parsing | |
| 137 | 137 | # scan_ scanner | |
| 138 | 138 | # parser_ parser | |
| 139 | 139 | # type_ type checking | |
| 140 | 140 | # general_ general info | |
| 141 | 141 | # exec_ calls to assembler, external linker, binder | |
| 142 | 142 | # link_ internal linker | |
| 143 | 143 | # package_ package handling | |
| 144 | 144 | # | |
| 145 | 145 | # <type> the type of the message it should normally used for | |
| 146 | 146 | # f_ fatal error | |
| 147 | 147 | # e_ error | |
| 148 | 148 | # w_ warning | |
| 149 | 149 | # n_ note | |
| 150 | 150 | # h_ hint | |
| 151 | 151 | # i_ info | |
| 152 | 152 | # l_ add linenumber | |
| 153 | 153 | # u_ used | |
| 154 | 154 | # t_ tried | |
| 155 | 155 | # c_ conditional | |
| 156 | 156 | # d_ debug message | |
| 157 | 157 | # x_ executable informations | |
| 158 | 158 | # o_ normal (e.g., "press enter to continue") | |
| 159 | 159 | # | |
| 160 | 160 | # <type> can contain a minus sign at the beginning to mark that | |
| 161 | 161 | # the message is off by default. Look at type_w_explicit_string_cast | |
| 162 | 162 | # for example. | |
| 163 | 163 | ``` | |
| 164 | 164 | ||
| 165 | 165 | So, to find your warning/notice/etc, you just need to search the message body. For the example, it can be \<unit "Pas2Js.pas" not used in main.pas\>, to find this warning, you need to replace all of the local data in message body with \$\< data number \>, | |
| 166 | 166 | ||
| 167 | - | ||
| 167 | + | ``` pascal | |
| 168 | + | for this situation it will be: <Unit "$1" not used in $2>; | |
| 169 | + | ``` | |
| 168 | 170 | ||
| 169 | 171 | After that, you can find the required message just by finding its body in errors\<language\>.msg file, you will find the following: | |
| 170 | 172 | ||
| 171 | 173 | ``` pascal | |
| 172 | 174 | sym_n_unit_not_used=05023_H_Unit "$1" not used in $2 | |
| 173 | 175 | % The unit referenced in the \var{uses} clause is not used. | |
| 174 | 176 | ``` | |
| 175 | 177 | ||
| 176 | 178 | So, the actual notice number is 05023. | |
| 177 | 179 | ||
| 178 | 180 | Based on that number you can decide to suppress the warning or hint globally, per unit or per routine, depending on what you need. | |
| 179 | 181 | ||
| 180 | 182 | For example, if you want to suppress unused parameters you can subsequently compile with -vm5024. | |
| 181 | 183 | ||
| 182 | 184 | With the -vm compiler option you can use multiple warning and hint numbers separated by a comma. Or you can write: | |
| 183 | 185 | ||
| 184 | 186 | ``` pascal | |
| 185 | 187 | {$warn 5024 off} | |
| 186 | 188 | ``` | |
| 187 | 189 | ||
| 188 | 190 | This needs to be done for each number separately. | |
| 189 | 191 | ||
| 190 | 192 | See also: | |
| 191 | 193 | ||
| 192 | 194 | --- | |
| 193 | 195 | *From the Free Pascal wiki, page "Turn warnings and hints on or off", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
