Commit | Line | Data |
---|---|---|
599cee73 | 1 | #!/usr/bin/perl |
c4a853d1 | 2 | # |
6294c161 DM |
3 | # Regenerate (overwriting only if changed): |
4 | # | |
5 | # lib/warnings.pm | |
6 | # warnings.h | |
7 | # | |
8 | # from information hardcoded into this script (the $tree hash), plus the | |
d2ec25a5 | 9 | # template for warnings.pm in the DATA section. |
6294c161 | 10 | # |
91efc02c KW |
11 | # When changing the number of warnings, t/op/caller.t should change to |
12 | # correspond with the value of $BYTES in lib/warnings.pm | |
8457b38f | 13 | # |
6294c161 DM |
14 | # With an argument of 'tree', just dump the contents of $tree and exits. |
15 | # Also accepts the standard regen_lib -q and -v args. | |
16 | # | |
17 | # This script is normally invoked from regen.pl. | |
599cee73 | 18 | |
573a192d | 19 | $VERSION = '1.48'; |
b75c8c73 | 20 | |
73f0cc2d | 21 | BEGIN { |
3d7c117d | 22 | require './regen/regen_lib.pl'; |
b6b9a099 | 23 | push @INC, './lib'; |
73f0cc2d | 24 | } |
599cee73 PM |
25 | use strict ; |
26 | ||
27 | sub DEFAULT_ON () { 1 } | |
28 | sub DEFAULT_OFF () { 2 } | |
29 | ||
30 | my $tree = { | |
3c3f8cd6 AB |
31 | 'all' => [ 5.008, { |
32 | 'io' => [ 5.008, { | |
33 | 'pipe' => [ 5.008, DEFAULT_OFF], | |
34 | 'unopened' => [ 5.008, DEFAULT_OFF], | |
35 | 'closed' => [ 5.008, DEFAULT_OFF], | |
36 | 'newline' => [ 5.008, DEFAULT_OFF], | |
37 | 'exec' => [ 5.008, DEFAULT_OFF], | |
38 | 'layer' => [ 5.008, DEFAULT_OFF], | |
39 | 'syscalls' => [ 5.019, DEFAULT_OFF], | |
40 | }], | |
41 | 'syntax' => [ 5.008, { | |
42 | 'ambiguous' => [ 5.008, DEFAULT_OFF], | |
43 | 'semicolon' => [ 5.008, DEFAULT_OFF], | |
44 | 'precedence' => [ 5.008, DEFAULT_OFF], | |
45 | 'bareword' => [ 5.008, DEFAULT_OFF], | |
46 | 'reserved' => [ 5.008, DEFAULT_OFF], | |
47 | 'digit' => [ 5.008, DEFAULT_OFF], | |
48 | 'parenthesis' => [ 5.008, DEFAULT_OFF], | |
49 | 'printf' => [ 5.008, DEFAULT_OFF], | |
50 | 'prototype' => [ 5.008, DEFAULT_OFF], | |
51 | 'qw' => [ 5.008, DEFAULT_OFF], | |
52 | 'illegalproto' => [ 5.011, DEFAULT_OFF], | |
53 | }], | |
54 | 'severe' => [ 5.008, { | |
55 | 'inplace' => [ 5.008, DEFAULT_ON], | |
56 | 'internal' => [ 5.008, DEFAULT_OFF], | |
57 | 'debugging' => [ 5.008, DEFAULT_ON], | |
58 | 'malloc' => [ 5.008, DEFAULT_ON], | |
59 | }], | |
60 | 'deprecated' => [ 5.008, DEFAULT_ON], | |
61 | 'void' => [ 5.008, DEFAULT_OFF], | |
62 | 'recursion' => [ 5.008, DEFAULT_OFF], | |
63 | 'redefine' => [ 5.008, DEFAULT_OFF], | |
64 | 'numeric' => [ 5.008, DEFAULT_OFF], | |
65 | 'uninitialized' => [ 5.008, DEFAULT_OFF], | |
66 | 'once' => [ 5.008, DEFAULT_OFF], | |
67 | 'misc' => [ 5.008, DEFAULT_OFF], | |
68 | 'regexp' => [ 5.008, DEFAULT_OFF], | |
69 | 'glob' => [ 5.008, DEFAULT_ON], | |
70 | 'untie' => [ 5.008, DEFAULT_OFF], | |
71 | 'substr' => [ 5.008, DEFAULT_OFF], | |
72 | 'taint' => [ 5.008, DEFAULT_OFF], | |
73 | 'signal' => [ 5.008, DEFAULT_OFF], | |
74 | 'closure' => [ 5.008, DEFAULT_OFF], | |
75 | 'overflow' => [ 5.008, DEFAULT_OFF], | |
76 | 'portable' => [ 5.008, DEFAULT_OFF], | |
77 | 'utf8' => [ 5.008, { | |
78 | 'surrogate' => [ 5.013, DEFAULT_OFF], | |
79 | 'nonchar' => [ 5.013, DEFAULT_OFF], | |
80 | 'non_unicode' => [ 5.013, DEFAULT_OFF], | |
81 | }], | |
82 | 'exiting' => [ 5.008, DEFAULT_OFF], | |
83 | 'pack' => [ 5.008, DEFAULT_OFF], | |
84 | 'unpack' => [ 5.008, DEFAULT_OFF], | |
85 | 'threads' => [ 5.008, DEFAULT_OFF], | |
86 | 'imprecision' => [ 5.011, DEFAULT_OFF], | |
87 | 'experimental' => [ 5.017, { | |
88 | 'experimental::lexical_subs' => | |
89 | [ 5.017, DEFAULT_ON ], | |
90 | 'experimental::regex_sets' => | |
91 | [ 5.017, DEFAULT_ON ], | |
3c3f8cd6 AB |
92 | 'experimental::smartmatch' => |
93 | [ 5.017, DEFAULT_ON ], | |
94 | 'experimental::postderef' => | |
95 | [ 5.019, DEFAULT_ON ], | |
3c3f8cd6 AB |
96 | 'experimental::signatures' => |
97 | [ 5.019, DEFAULT_ON ], | |
98 | 'experimental::win32_perlio' => | |
99 | [ 5.021, DEFAULT_ON ], | |
100 | 'experimental::refaliasing' => | |
101 | [ 5.021, DEFAULT_ON ], | |
102 | 'experimental::re_strict' => | |
103 | [ 5.021, DEFAULT_ON ], | |
104 | 'experimental::const_attr' => | |
105 | [ 5.021, DEFAULT_ON ], | |
9f88e537 FC |
106 | 'experimental::bitwise' => |
107 | [ 5.021, DEFAULT_ON ], | |
88d5dae9 FC |
108 | 'experimental::declared_refs' => |
109 | [ 5.025, DEFAULT_ON ], | |
0d76344b KW |
110 | 'experimental::script_run' => |
111 | [ 5.027, DEFAULT_ON ], | |
948f26d8 KW |
112 | 'experimental::alpha_assertions' => |
113 | [ 5.027, DEFAULT_ON ], | |
21c34e97 KW |
114 | 'experimental::private_use' => |
115 | [ 5.029, DEFAULT_ON ], | |
4fa1c4b6 KW |
116 | 'experimental::uniprop_wildcards' => |
117 | [ 5.029, DEFAULT_ON ], | |
15a9bc0d KW |
118 | 'experimental::vlb' => |
119 | [ 5.029, DEFAULT_ON ], | |
813e85a0 PE |
120 | 'experimental::isa' => |
121 | [ 5.031, DEFAULT_ON ], | |
3c3f8cd6 AB |
122 | }], |
123 | ||
124 | 'missing' => [ 5.021, DEFAULT_OFF], | |
125 | 'redundant' => [ 5.021, DEFAULT_OFF], | |
126 | 'locale' => [ 5.021, DEFAULT_ON], | |
52e3acf8 | 127 | 'shadow' => [ 5.027, DEFAULT_OFF], |
3c3f8cd6 AB |
128 | |
129 | #'default' => [ 5.008, DEFAULT_ON ], | |
ea5519d6 | 130 | }]}; |
599cee73 | 131 | |
7fc874e8 | 132 | my @def ; |
599cee73 PM |
133 | my %list ; |
134 | my %Value ; | |
0d658bf5 PM |
135 | my %ValueToName ; |
136 | my %NameToValue ; | |
599cee73 | 137 | |
0d658bf5 PM |
138 | my %v_list = () ; |
139 | ||
140 | sub valueWalk | |
141 | { | |
142 | my $tre = shift ; | |
143 | my @list = () ; | |
144 | my ($k, $v) ; | |
145 | ||
146 | foreach $k (sort keys %$tre) { | |
147 | $v = $tre->{$k}; | |
148 | die "duplicate key $k\n" if defined $list{$k} ; | |
149 | die "Value associated with key '$k' is not an ARRAY reference" | |
150 | if !ref $v || ref $v ne 'ARRAY' ; | |
151 | ||
152 | my ($ver, $rest) = @{ $v } ; | |
153 | push @{ $v_list{$ver} }, $k; | |
c4a853d1 | 154 | |
0d658bf5 PM |
155 | if (ref $rest) |
156 | { valueWalk ($rest) } | |
157 | ||
158 | } | |
159 | ||
160 | } | |
161 | ||
162 | sub orderValues | |
163 | { | |
164 | my $index = 0; | |
165 | foreach my $ver ( sort { $a <=> $b } keys %v_list ) { | |
166 | foreach my $name (@{ $v_list{$ver} } ) { | |
167 | $ValueToName{ $index } = [ uc $name, $ver ] ; | |
168 | $NameToValue{ uc $name } = $index ++ ; | |
169 | } | |
170 | } | |
171 | ||
172 | return $index ; | |
173 | } | |
174 | ||
175 | ########################################################################### | |
176 | ||
599cee73 PM |
177 | sub walk |
178 | { | |
179 | my $tre = shift ; | |
180 | my @list = () ; | |
181 | my ($k, $v) ; | |
182 | ||
95dfd3ab GS |
183 | foreach $k (sort keys %$tre) { |
184 | $v = $tre->{$k}; | |
599cee73 | 185 | die "duplicate key $k\n" if defined $list{$k} ; |
0d658bf5 PM |
186 | die "Can't find key '$k'" |
187 | if ! defined $NameToValue{uc $k} ; | |
188 | push @{ $list{$k} }, $NameToValue{uc $k} ; | |
189 | die "Value associated with key '$k' is not an ARRAY reference" | |
190 | if !ref $v || ref $v ne 'ARRAY' ; | |
c4a853d1 | 191 | |
0d658bf5 PM |
192 | my ($ver, $rest) = @{ $v } ; |
193 | if (ref $rest) | |
194 | { push (@{ $list{$k} }, walk ($rest)) } | |
7fc874e8 FC |
195 | elsif ($rest == DEFAULT_ON) |
196 | { push @def, $NameToValue{uc $k} } | |
0d658bf5 | 197 | |
599cee73 PM |
198 | push @list, @{ $list{$k} } ; |
199 | } | |
200 | ||
201 | return @list ; | |
599cee73 PM |
202 | } |
203 | ||
204 | ########################################################################### | |
205 | ||
206 | sub mkRange | |
207 | { | |
573a192d DIM |
208 | my @in = @_ ; |
209 | my @out = @in ; | |
599cee73 | 210 | |
573a192d | 211 | for my $i (1 .. @in - 1) { |
0ca4541c | 212 | $out[$i] = ".." |
573a192d DIM |
213 | if $in[$i] == $in[$i - 1] + 1 |
214 | && ($i >= @in - 1 || $in[$i] + 1 == $in[$i + 1] ); | |
599cee73 | 215 | } |
573a192d | 216 | $out[-1] = $in[-1] if $out[-1] eq ".."; |
599cee73 PM |
217 | |
218 | my $out = join(",",@out); | |
219 | ||
220 | $out =~ s/,(\.\.,)+/../g ; | |
221 | return $out; | |
222 | } | |
223 | ||
224 | ########################################################################### | |
e15f14b8 | 225 | sub warningsTree |
e476b1b5 GS |
226 | { |
227 | my $tre = shift ; | |
228 | my $prefix = shift ; | |
e476b1b5 GS |
229 | my ($k, $v) ; |
230 | ||
231 | my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ; | |
0d658bf5 | 232 | my @keys = sort keys %$tre ; |
e476b1b5 | 233 | |
e15f14b8 RS |
234 | my $rv = ''; |
235 | ||
0d658bf5 | 236 | while ($k = shift @keys) { |
e476b1b5 | 237 | $v = $tre->{$k}; |
0d658bf5 PM |
238 | die "Value associated with key '$k' is not an ARRAY reference" |
239 | if !ref $v || ref $v ne 'ARRAY' ; | |
c4a853d1 | 240 | |
0d658bf5 PM |
241 | my $offset ; |
242 | if ($tre ne $tree) { | |
e15f14b8 RS |
243 | $rv .= $prefix . "|\n" ; |
244 | $rv .= $prefix . "+- $k" ; | |
0d658bf5 PM |
245 | $offset = ' ' x ($max + 4) ; |
246 | } | |
247 | else { | |
e15f14b8 | 248 | $rv .= $prefix . "$k" ; |
0d658bf5 PM |
249 | $offset = ' ' x ($max + 1) ; |
250 | } | |
251 | ||
252 | my ($ver, $rest) = @{ $v } ; | |
f1d34ca8 | 253 | if (ref $rest) |
0ca4541c | 254 | { |
0d658bf5 | 255 | my $bar = @keys ? "|" : " "; |
e15f14b8 RS |
256 | $rv .= " -" . "-" x ($max - length $k ) . "+\n" ; |
257 | $rv .= warningsTree ($rest, $prefix . $bar . $offset ) | |
e476b1b5 GS |
258 | } |
259 | else | |
e15f14b8 | 260 | { $rv .= "\n" } |
e476b1b5 GS |
261 | } |
262 | ||
e15f14b8 | 263 | return $rv; |
e476b1b5 GS |
264 | } |
265 | ||
266 | ########################################################################### | |
599cee73 | 267 | |
317ea90d | 268 | sub mkHexOct |
599cee73 | 269 | { |
573a192d | 270 | my ($f, $max, @bits) = @_ ; |
599cee73 PM |
271 | my $mask = "\x00" x $max ; |
272 | my $string = "" ; | |
273 | ||
573a192d | 274 | foreach (@bits) { |
599cee73 PM |
275 | vec($mask, $_, 1) = 1 ; |
276 | } | |
277 | ||
599cee73 | 278 | foreach (unpack("C*", $mask)) { |
317ea90d MS |
279 | if ($f eq 'x') { |
280 | $string .= '\x' . sprintf("%2.2x", $_) | |
281 | } | |
282 | else { | |
283 | $string .= '\\' . sprintf("%o", $_) | |
284 | } | |
599cee73 PM |
285 | } |
286 | return $string ; | |
287 | } | |
288 | ||
317ea90d MS |
289 | sub mkHex |
290 | { | |
573a192d DIM |
291 | my($max, @bits) = @_; |
292 | return mkHexOct("x", $max, @bits); | |
317ea90d MS |
293 | } |
294 | ||
295 | sub mkOct | |
296 | { | |
573a192d DIM |
297 | my($max, @bits) = @_; |
298 | return mkHexOct("o", $max, @bits); | |
317ea90d MS |
299 | } |
300 | ||
599cee73 PM |
301 | ########################################################################### |
302 | ||
e476b1b5 GS |
303 | if (@ARGV && $ARGV[0] eq "tree") |
304 | { | |
3c3f8cd6 | 305 | print warningsTree($tree, " ") ; |
e476b1b5 GS |
306 | exit ; |
307 | } | |
599cee73 | 308 | |
cc49830d NC |
309 | my ($warn, $pm) = map { |
310 | open_new($_, '>', { by => 'regen/warnings.pl' }); | |
311 | } 'warnings.h', 'lib/warnings.pm'; | |
599cee73 | 312 | |
c4a853d1 RS |
313 | my ($index, $warn_size); |
314 | ||
315 | { | |
316 | # generate warnings.h | |
317 | ||
318 | print $warn <<'EOM'; | |
599cee73 | 319 | |
0453d815 PM |
320 | #define Off(x) ((x) / 8) |
321 | #define Bit(x) (1 << ((x) % 8)) | |
599cee73 PM |
322 | #define IsSet(a, x) ((a)[Off(x)] & Bit(x)) |
323 | ||
0453d815 | 324 | |
599cee73 | 325 | #define G_WARN_OFF 0 /* $^W == 0 */ |
0453d815 | 326 | #define G_WARN_ON 1 /* -w flag and $^W != 0 */ |
599cee73 PM |
327 | #define G_WARN_ALL_ON 2 /* -W flag */ |
328 | #define G_WARN_ALL_OFF 4 /* -X flag */ | |
0453d815 | 329 | #define G_WARN_ONCE 8 /* set if 'once' ever enabled */ |
599cee73 PM |
330 | #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF) |
331 | ||
a0714e2c | 332 | #define pWARN_STD NULL |
8c165a32 KW |
333 | #define pWARN_ALL (STRLEN *) &PL_WARN_ALL /* use warnings 'all' */ |
334 | #define pWARN_NONE (STRLEN *) &PL_WARN_NONE /* no warnings 'all' */ | |
599cee73 | 335 | |
d3a7d8c7 GS |
336 | #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \ |
337 | (x) == pWARN_NONE) | |
5f2d9966 DM |
338 | |
339 | /* if PL_warnhook is set to this value, then warnings die */ | |
06dcd5bf | 340 | #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder) |
599cee73 PM |
341 | EOM |
342 | ||
c4a853d1 | 343 | my $offset = 0 ; |
d3a7d8c7 | 344 | |
c4a853d1 RS |
345 | valueWalk ($tree) ; |
346 | $index = orderValues(); | |
599cee73 | 347 | |
c4a853d1 | 348 | die <<EOM if $index > 255 ; |
12bcd1a6 | 349 | Too many warnings categories -- max is 255 |
c4a853d1 | 350 | rewrite packWARN* & unpackWARN* macros |
12bcd1a6 | 351 | EOM |
599cee73 | 352 | |
c4a853d1 | 353 | walk ($tree) ; |
006c1a1d Z |
354 | for (my $i = $index; $i & 3; $i++) { |
355 | push @{$list{all}}, $i; | |
356 | } | |
0d658bf5 | 357 | |
c4a853d1 RS |
358 | $index *= 2 ; |
359 | $warn_size = int($index / 8) + ($index % 8 != 0) ; | |
599cee73 | 360 | |
c4a853d1 RS |
361 | my $k ; |
362 | my $last_ver = 0; | |
a1a5a9c8 | 363 | my @names; |
c4a853d1 RS |
364 | foreach $k (sort { $a <=> $b } keys %ValueToName) { |
365 | my ($name, $version) = @{ $ValueToName{$k} }; | |
366 | print $warn "\n/* Warnings Categories added in Perl $version */\n\n" | |
367 | if $last_ver != $version ; | |
368 | $name =~ y/:/_/; | |
a1a5a9c8 KW |
369 | $name = "WARN_$name"; |
370 | print $warn tab(6, "#define $name"), " $k\n" ; | |
371 | push @names, $name; | |
c4a853d1 RS |
372 | $last_ver = $version ; |
373 | } | |
599cee73 | 374 | |
3c3f8cd6 AB |
375 | print $warn tab(6, '#define WARNsize'), " $warn_size\n" ; |
376 | print $warn tab(6, '#define WARN_ALLstring'), ' "', ('\125' x $warn_size) , "\"\n" ; | |
377 | print $warn tab(6, '#define WARN_NONEstring'), ' "', ('\0' x $warn_size) , "\"\n" ; | |
599cee73 | 378 | |
c4a853d1 | 379 | print $warn <<'EOM'; |
599cee73 | 380 | |
a2637ca0 FC |
381 | #define isLEXWARN_on \ |
382 | cBOOL(PL_curcop && PL_curcop->cop_warnings != pWARN_STD) | |
383 | #define isLEXWARN_off \ | |
384 | cBOOL(!PL_curcop || PL_curcop->cop_warnings == pWARN_STD) | |
d5a71f30 | 385 | #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE)) |
72dc9ed5 NC |
386 | #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x))) |
387 | #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1)) | |
388 | ||
c1e47bad | 389 | #define DUP_WARNINGS(p) Perl_dup_warnings(aTHX_ p) |
d5a71f30 | 390 | |
94c8b9c1 | 391 | #define free_and_set_cop_warnings(cmp,w) STMT_START { \ |
1943af61 DM |
392 | if (!specialWARN((cmp)->cop_warnings)) PerlMemShared_free((cmp)->cop_warnings); \ |
393 | (cmp)->cop_warnings = w; \ | |
94c8b9c1 N |
394 | } STMT_END |
395 | ||
feff94e1 KW |
396 | /* |
397 | ||
398 | =head1 Warning and Dieing | |
399 | ||
bb3eff5d KW |
400 | In all these calls, the C<U32 wI<n>> parameters are warning category |
401 | constants. You can see the ones currently available in | |
402 | L<warnings/Category Hierarchy>, just capitalize all letters in the names | |
403 | and prefix them by C<WARN_>. So, for example, the category C<void> used in a | |
404 | perl program becomes C<WARN_VOID> when used in XS code and passed to one of | |
405 | the calls below. | |
406 | ||
feff94e1 | 407 | =for apidoc Am|bool|ckWARN|U32 w |
0afc88d2 KW |
408 | =for apidoc_item ||ckWARN2|U32 w1|U32 w2 |
409 | =for apidoc_item ||ckWARN3|U32 w1|U32 w2|U32 w3 | |
410 | =for apidoc_item ||ckWARN4|U32 w1|U32 w2|U32 w3|U32 w4 | |
411 | These return a boolean as to whether or not warnings are enabled for any of | |
412 | the warning category(ies) parameters: C<w>, C<w1>, .... | |
feff94e1 | 413 | |
0afc88d2 KW |
414 | Should any of the categories by default be enabled even if not within the |
415 | scope of S<C<use warnings>>, instead use the C<L</ckWARN_d>> macros. | |
feff94e1 | 416 | |
0afc88d2 KW |
417 | The categories must be completely independent, one may not be subclassed from |
418 | the other. | |
feff94e1 | 419 | |
0afc88d2 | 420 | =for apidoc Am|bool|ckWARN_d|U32 w |
b8a2649a KW |
421 | =for apidoc_item ||ckWARN2_d|U32 w1|U32 w2 |
422 | =for apidoc_item ||ckWARN3_d|U32 w1|U32 w2|U32 w3 | |
423 | =for apidoc_item ||ckWARN4_d|U32 w1|U32 w2|U32 w3|U32 w4 | |
feff94e1 | 424 | |
0afc88d2 | 425 | Like C<L</ckWARN>>, but for use if and only if the warning category(ies) is by |
feff94e1 KW |
426 | default enabled even if not within the scope of S<C<use warnings>>. |
427 | ||
b8a2649a KW |
428 | =for apidoc Am|U32|packWARN|U32 w1 |
429 | =for apidoc_item ||packWARN2|U32 w1|U32 w2 | |
430 | =for apidoc_item ||packWARN3|U32 w1|U32 w2|U32 w3 | |
431 | =for apidoc_item ||packWARN4|U32 w1|U32 w2|U32 w3|U32 w4 | |
432 | ||
433 | These macros are used to pack warning categories into a single U32 to pass to | |
434 | macros and functions that take a warning category parameter. The number of | |
435 | categories to pack is given by the name, with a corresponding number of | |
436 | category parameters passed. | |
feff94e1 KW |
437 | |
438 | =cut | |
439 | ||
440 | */ | |
441 | ||
f54ba1c2 | 442 | #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w)) |
7c08c4c5 KW |
443 | |
444 | /* The w1, w2 ... should be independent warnings categories; one shouldn't be | |
445 | * a subcategory of any other */ | |
446 | ||
f54ba1c2 DM |
447 | #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2)) |
448 | #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3)) | |
449 | #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4)) | |
450 | ||
451 | #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w)) | |
452 | #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2)) | |
453 | #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3)) | |
454 | #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4)) | |
12bcd1a6 | 455 | |
98fe6610 NC |
456 | #define WARNshift 8 |
457 | ||
3b9e3074 | 458 | #define packWARN(a) (a ) |
7c08c4c5 KW |
459 | |
460 | /* The a, b, ... should be independent warnings categories; one shouldn't be | |
461 | * a subcategory of any other */ | |
462 | ||
3b9e3074 SH |
463 | #define packWARN2(a,b) ((a) | ((b)<<8) ) |
464 | #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) ) | |
465 | #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24)) | |
12bcd1a6 PM |
466 | |
467 | #define unpackWARN1(x) ((x) & 0xFF) | |
468 | #define unpackWARN2(x) (((x) >>8) & 0xFF) | |
469 | #define unpackWARN3(x) (((x) >>16) & 0xFF) | |
470 | #define unpackWARN4(x) (((x) >>24) & 0xFF) | |
471 | ||
472 | #define ckDEAD(x) \ | |
006c1a1d Z |
473 | (PL_curcop && \ |
474 | !specialWARN(PL_curcop->cop_warnings) && \ | |
475 | (isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \ | |
476 | (unpackWARN2(x) && \ | |
477 | (isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \ | |
478 | (unpackWARN3(x) && \ | |
479 | (isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \ | |
480 | (unpackWARN4(x) && \ | |
481 | isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))))))) | |
12bcd1a6 | 482 | |
599cee73 PM |
483 | EOM |
484 | ||
c286f332 KW |
485 | print $warn "\n\n/*\n" ; |
486 | print $warn map { "=for apidoc Amnh||$_\n" } @names; | |
487 | print $warn "\n=cut\n*/\n\n" ; | |
488 | print $warn "/* end of file warnings.h */\n"; | |
489 | ||
c4a853d1 RS |
490 | read_only_bottom_close_and_rename($warn); |
491 | } | |
599cee73 PM |
492 | |
493 | while (<DATA>) { | |
3d8ff825 TC |
494 | last if /^VERSION$/ ; |
495 | print $pm $_ ; | |
496 | } | |
497 | ||
498 | print $pm qq(our \$VERSION = "$::VERSION";\n); | |
499 | ||
500 | while (<DATA>) { | |
599cee73 | 501 | last if /^KEYWORDS$/ ; |
424a4936 | 502 | print $pm $_ ; |
599cee73 PM |
503 | } |
504 | ||
c4a853d1 | 505 | my $last_ver = 0; |
3c3f8cd6 | 506 | print $pm "our %Offsets = (" ; |
0d658bf5 PM |
507 | foreach my $k (sort { $a <=> $b } keys %ValueToName) { |
508 | my ($name, $version) = @{ $ValueToName{$k} }; | |
509 | $name = lc $name; | |
d3a7d8c7 | 510 | $k *= 2 ; |
0d658bf5 | 511 | if ( $last_ver != $version ) { |
424a4936 | 512 | print $pm "\n"; |
3c3f8cd6 AB |
513 | print $pm tab(6, " # Warnings Categories added in Perl $version"); |
514 | print $pm "\n"; | |
0d658bf5 | 515 | } |
3c3f8cd6 | 516 | print $pm tab(6, " '$name'"), "=> $k,\n" ; |
0d658bf5 | 517 | $last_ver = $version; |
d3a7d8c7 GS |
518 | } |
519 | ||
3c3f8cd6 | 520 | print $pm ");\n\n" ; |
d3a7d8c7 | 521 | |
424a4936 | 522 | print $pm "our %Bits = (\n" ; |
c4a853d1 | 523 | foreach my $k (sort keys %list) { |
599cee73 PM |
524 | |
525 | my $v = $list{$k} ; | |
526 | my @list = sort { $a <=> $b } @$v ; | |
527 | ||
3c3f8cd6 | 528 | print $pm tab(6, " '$k'"), '=> "', |
0ca4541c | 529 | mkHex($warn_size, map $_ * 2 , @list), |
599cee73 PM |
530 | '", # [', mkRange(@list), "]\n" ; |
531 | } | |
532 | ||
3c3f8cd6 | 533 | print $pm ");\n\n" ; |
599cee73 | 534 | |
424a4936 | 535 | print $pm "our %DeadBits = (\n" ; |
c4a853d1 | 536 | foreach my $k (sort keys %list) { |
599cee73 PM |
537 | |
538 | my $v = $list{$k} ; | |
539 | my @list = sort { $a <=> $b } @$v ; | |
540 | ||
3c3f8cd6 | 541 | print $pm tab(6, " '$k'"), '=> "', |
0ca4541c | 542 | mkHex($warn_size, map $_ * 2 + 1 , @list), |
599cee73 PM |
543 | '", # [', mkRange(@list), "]\n" ; |
544 | } | |
545 | ||
3c3f8cd6 AB |
546 | print $pm ");\n\n" ; |
547 | print $pm "# These are used by various things, including our own tests\n"; | |
548 | print $pm tab(6, 'our $NONE'), '= "', ('\0' x $warn_size) , "\";\n" ; | |
549 | print $pm tab(6, 'our $DEFAULT'), '= "', mkHex($warn_size, map $_ * 2, @def), | |
193e9da6 | 550 | '"; # [', mkRange(sort { $a <=> $b } @def), "]\n" ; |
3c3f8cd6 AB |
551 | print $pm tab(6, 'our $LAST_BIT'), '= ' . "$index ;\n" ; |
552 | print $pm tab(6, 'our $BYTES'), '= ' . "$warn_size ;\n" ; | |
599cee73 | 553 | while (<DATA>) { |
effd17dc | 554 | if ($_ eq "=for warnings.pl tree-goes-here\n") { |
3c3f8cd6 | 555 | print $pm warningsTree($tree, " "); |
effd17dc DD |
556 | next; |
557 | } | |
424a4936 | 558 | print $pm $_ ; |
599cee73 PM |
559 | } |
560 | ||
ce716c52 | 561 | read_only_bottom_close_and_rename($pm); |
599cee73 PM |
562 | |
563 | __END__ | |
4438c4b7 | 564 | package warnings; |
599cee73 | 565 | |
3d8ff825 | 566 | VERSION |
f2c3e829 RGS |
567 | |
568 | # Verify that we're called correctly so that warnings will work. | |
67ba812d AP |
569 | # Can't use Carp, since Carp uses us! |
570 | # String regexps because constant folding = smaller optree = less memory vs regexp literal | |
f2c3e829 | 571 | # see also strict.pm. |
67ba812d AP |
572 | die sprintf "Incorrect use of pragma '%s' at %s line %d.\n", __PACKAGE__, +(caller)[1,2] |
573 | if __FILE__ !~ ( '(?x) \b '.__PACKAGE__.' \.pmc? \z' ) | |
574 | && __FILE__ =~ ( '(?x) \b (?i:'.__PACKAGE__.') \.pmc? \z' ); | |
b75c8c73 | 575 | |
effd17dc DD |
576 | KEYWORDS |
577 | ||
effd17dc DD |
578 | sub Croaker |
579 | { | |
580 | require Carp; # this initializes %CarpInternal | |
581 | local $Carp::CarpInternal{'warnings'}; | |
582 | delete $Carp::CarpInternal{'warnings'}; | |
583 | Carp::croak(@_); | |
584 | } | |
585 | ||
006c1a1d Z |
586 | sub _expand_bits { |
587 | my $bits = shift; | |
588 | my $want_len = ($LAST_BIT + 7) >> 3; | |
589 | my $len = length($bits); | |
590 | if ($len != $want_len) { | |
591 | if ($bits eq "") { | |
592 | $bits = "\x00" x $want_len; | |
593 | } elsif ($len > $want_len) { | |
594 | substr $bits, $want_len, $len-$want_len, ""; | |
595 | } else { | |
573a192d DIM |
596 | my $x = vec($bits, $Offsets{all} >> 1, 2); |
597 | $x |= $x << 2; | |
598 | $x |= $x << 4; | |
599 | $bits .= chr($x) x ($want_len - $len); | |
006c1a1d Z |
600 | } |
601 | } | |
602 | return $bits; | |
603 | } | |
604 | ||
effd17dc DD |
605 | sub _bits { |
606 | my $mask = shift ; | |
607 | my $catmask ; | |
608 | my $fatal = 0 ; | |
609 | my $no_fatal = 0 ; | |
610 | ||
006c1a1d | 611 | $mask = _expand_bits($mask); |
effd17dc DD |
612 | foreach my $word ( @_ ) { |
613 | if ($word eq 'FATAL') { | |
614 | $fatal = 1; | |
615 | $no_fatal = 0; | |
616 | } | |
617 | elsif ($word eq 'NONFATAL') { | |
618 | $fatal = 0; | |
619 | $no_fatal = 1; | |
620 | } | |
621 | elsif ($catmask = $Bits{$word}) { | |
622 | $mask |= $catmask ; | |
623 | $mask |= $DeadBits{$word} if $fatal ; | |
006c1a1d | 624 | $mask = ~(~$mask | $DeadBits{$word}) if $no_fatal ; |
effd17dc DD |
625 | } |
626 | else | |
56873d42 | 627 | { Croaker("Unknown warnings category '$word'")} |
effd17dc DD |
628 | } |
629 | ||
630 | return $mask ; | |
631 | } | |
632 | ||
633 | sub bits | |
634 | { | |
635 | # called from B::Deparse.pm | |
636 | push @_, 'all' unless @_ ; | |
006c1a1d | 637 | return _bits("", @_) ; |
effd17dc DD |
638 | } |
639 | ||
640 | sub import | |
641 | { | |
642 | shift; | |
643 | ||
644 | my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; | |
645 | ||
006c1a1d Z |
646 | # append 'all' when implied (empty import list or after a lone |
647 | # "FATAL" or "NONFATAL") | |
648 | push @_, 'all' | |
649 | if !@_ || (@_==1 && ($_[0] eq 'FATAL' || $_[0] eq 'NONFATAL')); | |
effd17dc | 650 | |
006c1a1d | 651 | ${^WARNING_BITS} = _bits($mask, @_); |
effd17dc DD |
652 | } |
653 | ||
654 | sub unimport | |
655 | { | |
656 | shift; | |
657 | ||
658 | my $catmask ; | |
659 | my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ; | |
660 | ||
effd17dc DD |
661 | # append 'all' when implied (empty import list or after a lone "FATAL") |
662 | push @_, 'all' if !@_ || @_==1 && $_[0] eq 'FATAL'; | |
663 | ||
006c1a1d | 664 | $mask = _expand_bits($mask); |
effd17dc DD |
665 | foreach my $word ( @_ ) { |
666 | if ($word eq 'FATAL') { | |
667 | next; | |
668 | } | |
669 | elsif ($catmask = $Bits{$word}) { | |
006c1a1d | 670 | $mask = ~(~$mask | $catmask | $DeadBits{$word}); |
effd17dc DD |
671 | } |
672 | else | |
56873d42 | 673 | { Croaker("Unknown warnings category '$word'")} |
effd17dc DD |
674 | } |
675 | ||
676 | ${^WARNING_BITS} = $mask ; | |
677 | } | |
678 | ||
679 | my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = (); | |
680 | ||
c4583f59 | 681 | sub LEVEL () { 8 }; |
effd17dc DD |
682 | sub MESSAGE () { 4 }; |
683 | sub FATAL () { 2 }; | |
684 | sub NORMAL () { 1 }; | |
685 | ||
686 | sub __chk | |
687 | { | |
688 | my $category ; | |
689 | my $offset ; | |
690 | my $isobj = 0 ; | |
691 | my $wanted = shift; | |
692 | my $has_message = $wanted & MESSAGE; | |
c4583f59 FC |
693 | my $has_level = $wanted & LEVEL ; |
694 | ||
695 | if ($has_level) { | |
696 | if (@_ != ($has_message ? 3 : 2)) { | |
697 | my $sub = (caller 1)[3]; | |
698 | my $syntax = $has_message | |
699 | ? "category, level, 'message'" | |
700 | : 'category, level'; | |
701 | Croaker("Usage: $sub($syntax)"); | |
702 | } | |
703 | } | |
704 | elsif (not @_ == 1 || @_ == ($has_message ? 2 : 0)) { | |
effd17dc DD |
705 | my $sub = (caller 1)[3]; |
706 | my $syntax = $has_message ? "[category,] 'message'" : '[category]'; | |
707 | Croaker("Usage: $sub($syntax)"); | |
708 | } | |
709 | ||
710 | my $message = pop if $has_message; | |
711 | ||
712 | if (@_) { | |
56873d42 DD |
713 | # check the category supplied. |
714 | $category = shift ; | |
715 | if (my $type = ref $category) { | |
716 | Croaker("not an object") | |
717 | if exists $builtin_type{$type}; | |
effd17dc | 718 | $category = $type; |
56873d42 DD |
719 | $isobj = 1 ; |
720 | } | |
721 | $offset = $Offsets{$category}; | |
722 | Croaker("Unknown warnings category '$category'") | |
effd17dc DD |
723 | unless defined $offset; |
724 | } | |
725 | else { | |
56873d42 DD |
726 | $category = (caller(1))[0] ; |
727 | $offset = $Offsets{$category}; | |
728 | Croaker("package '$category' not registered for warnings") | |
effd17dc DD |
729 | unless defined $offset ; |
730 | } | |
731 | ||
732 | my $i; | |
733 | ||
734 | if ($isobj) { | |
56873d42 DD |
735 | my $pkg; |
736 | $i = 2; | |
737 | while (do { { package DB; $pkg = (caller($i++))[0] } } ) { | |
738 | last unless @DB::args && $DB::args[0] =~ /^$category=/ ; | |
739 | } | |
effd17dc DD |
740 | $i -= 2 ; |
741 | } | |
c4583f59 FC |
742 | elsif ($has_level) { |
743 | $i = 2 + shift; | |
744 | } | |
effd17dc | 745 | else { |
56873d42 | 746 | $i = _error_loc(); # see where Carp will allocate the error |
effd17dc DD |
747 | } |
748 | ||
749 | # Default to 0 if caller returns nothing. Default to $DEFAULT if it | |
750 | # explicitly returns undef. | |
751 | my(@callers_bitmask) = (caller($i))[9] ; | |
752 | my $callers_bitmask = | |
753 | @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ; | |
006c1a1d | 754 | length($callers_bitmask) > ($offset >> 3) or $offset = $Offsets{all}; |
effd17dc DD |
755 | |
756 | my @results; | |
757 | foreach my $type (FATAL, NORMAL) { | |
758 | next unless $wanted & $type; | |
759 | ||
006c1a1d | 760 | push @results, vec($callers_bitmask, $offset + $type - 1, 1); |
effd17dc DD |
761 | } |
762 | ||
763 | # &enabled and &fatal_enabled | |
764 | return $results[0] unless $has_message; | |
765 | ||
766 | # &warnif, and the category is neither enabled as warning nor as fatal | |
c4583f59 FC |
767 | return if ($wanted & (NORMAL | FATAL | MESSAGE)) |
768 | == (NORMAL | FATAL | MESSAGE) | |
effd17dc DD |
769 | && !($results[0] || $results[1]); |
770 | ||
c4583f59 FC |
771 | # If we have an explicit level, bypass Carp. |
772 | if ($has_level and @callers_bitmask) { | |
a0da1e16 | 773 | # logic copied from util.c:mess_sv |
c4583f59 | 774 | my $stuff = " at " . join " line ", (caller $i)[1,2]; |
06afc688 FC |
775 | $stuff .= sprintf ", <%s> %s %d", |
776 | *${^LAST_FH}{NAME}, | |
777 | ($/ eq "\n" ? "line" : "chunk"), $. | |
a0da1e16 | 778 | if $. && ${^LAST_FH}; |
c4583f59 FC |
779 | die "$message$stuff.\n" if $results[0]; |
780 | return warn "$message$stuff.\n"; | |
781 | } | |
782 | ||
effd17dc DD |
783 | require Carp; |
784 | Carp::croak($message) if $results[0]; | |
785 | # will always get here for &warn. will only get here for &warnif if the | |
786 | # category is enabled | |
787 | Carp::carp($message); | |
788 | } | |
789 | ||
790 | sub _mkMask | |
791 | { | |
792 | my ($bit) = @_; | |
793 | my $mask = ""; | |
794 | ||
795 | vec($mask, $bit, 1) = 1; | |
796 | return $mask; | |
797 | } | |
798 | ||
799 | sub register_categories | |
800 | { | |
801 | my @names = @_; | |
802 | ||
803 | for my $name (@names) { | |
804 | if (! defined $Bits{$name}) { | |
006c1a1d Z |
805 | $Offsets{$name} = $LAST_BIT; |
806 | $Bits{$name} = _mkMask($LAST_BIT++); | |
807 | $DeadBits{$name} = _mkMask($LAST_BIT++); | |
808 | if (length($Bits{$name}) > length($Bits{all})) { | |
809 | $Bits{all} .= "\x55"; | |
810 | $DeadBits{all} .= "\xaa"; | |
effd17dc | 811 | } |
effd17dc DD |
812 | } |
813 | } | |
814 | } | |
815 | ||
816 | sub _error_loc { | |
817 | require Carp; | |
818 | goto &Carp::short_error_loc; # don't introduce another stack frame | |
819 | } | |
820 | ||
821 | sub enabled | |
822 | { | |
823 | return __chk(NORMAL, @_); | |
824 | } | |
825 | ||
826 | sub fatal_enabled | |
827 | { | |
828 | return __chk(FATAL, @_); | |
829 | } | |
830 | ||
831 | sub warn | |
832 | { | |
833 | return __chk(FATAL | MESSAGE, @_); | |
834 | } | |
835 | ||
836 | sub warnif | |
837 | { | |
838 | return __chk(NORMAL | FATAL | MESSAGE, @_); | |
839 | } | |
840 | ||
c4583f59 FC |
841 | sub enabled_at_level |
842 | { | |
843 | return __chk(NORMAL | LEVEL, @_); | |
844 | } | |
845 | ||
846 | sub fatal_enabled_at_level | |
847 | { | |
848 | return __chk(FATAL | LEVEL, @_); | |
849 | } | |
850 | ||
851 | sub warn_at_level | |
852 | { | |
853 | return __chk(FATAL | MESSAGE | LEVEL, @_); | |
854 | } | |
855 | ||
856 | sub warnif_at_level | |
857 | { | |
858 | return __chk(NORMAL | FATAL | MESSAGE | LEVEL, @_); | |
859 | } | |
860 | ||
effd17dc DD |
861 | # These are not part of any public interface, so we can delete them to save |
862 | # space. | |
c4583f59 | 863 | delete @warnings::{qw(NORMAL FATAL MESSAGE LEVEL)}; |
effd17dc DD |
864 | |
865 | 1; | |
866 | __END__ | |
4bbd41f5 | 867 | |
599cee73 PM |
868 | =head1 NAME |
869 | ||
4438c4b7 | 870 | warnings - Perl pragma to control optional warnings |
599cee73 PM |
871 | |
872 | =head1 SYNOPSIS | |
873 | ||
4438c4b7 JH |
874 | use warnings; |
875 | no warnings; | |
599cee73 | 876 | |
4438c4b7 JH |
877 | use warnings "all"; |
878 | no warnings "all"; | |
599cee73 | 879 | |
d3a7d8c7 GS |
880 | use warnings::register; |
881 | if (warnings::enabled()) { | |
882 | warnings::warn("some warning"); | |
883 | } | |
884 | ||
885 | if (warnings::enabled("void")) { | |
e476b1b5 GS |
886 | warnings::warn("void", "some warning"); |
887 | } | |
888 | ||
7e6d00f8 PM |
889 | if (warnings::enabled($object)) { |
890 | warnings::warn($object, "some warning"); | |
891 | } | |
892 | ||
721f911b PM |
893 | warnings::warnif("some warning"); |
894 | warnings::warnif("void", "some warning"); | |
895 | warnings::warnif($object, "some warning"); | |
7e6d00f8 | 896 | |
599cee73 PM |
897 | =head1 DESCRIPTION |
898 | ||
188c4f6f RS |
899 | The C<warnings> pragma gives control over which warnings are enabled in |
900 | which parts of a Perl program. It's a more flexible alternative for | |
901 | both the command line flag B<-w> and the equivalent Perl variable, | |
902 | C<$^W>. | |
33edcb80 RS |
903 | |
904 | This pragma works just like the C<strict> pragma. | |
905 | This means that the scope of the warning pragma is limited to the | |
906 | enclosing block. It also means that the pragma setting will not | |
907 | leak across files (via C<use>, C<require> or C<do>). This allows | |
908 | authors to independently define the degree of warning checks that will | |
909 | be applied to their module. | |
910 | ||
911 | By default, optional warnings are disabled, so any legacy code that | |
912 | doesn't attempt to control the warnings will work unchanged. | |
913 | ||
3c3f8cd6 | 914 | All warnings are enabled in a block by either of these: |
33edcb80 RS |
915 | |
916 | use warnings; | |
917 | use warnings 'all'; | |
918 | ||
3c3f8cd6 | 919 | Similarly all warnings are disabled in a block by either of these: |
33edcb80 RS |
920 | |
921 | no warnings; | |
922 | no warnings 'all'; | |
923 | ||
924 | For example, consider the code below: | |
925 | ||
926 | use warnings; | |
573a192d | 927 | my @x; |
33edcb80 RS |
928 | { |
929 | no warnings; | |
573a192d | 930 | my $y = @x[0]; |
33edcb80 | 931 | } |
573a192d | 932 | my $z = @x[0]; |
33edcb80 RS |
933 | |
934 | The code in the enclosing block has warnings enabled, but the inner | |
935 | block has them disabled. In this case that means the assignment to the | |
cd2e5170 | 936 | scalar C<$z> will trip the C<"Scalar value @x[0] better written as $x[0]"> |
573a192d | 937 | warning, but the assignment to the scalar C<$y> will not. |
33edcb80 RS |
938 | |
939 | =head2 Default Warnings and Optional Warnings | |
940 | ||
941 | Before the introduction of lexical warnings, Perl had two classes of | |
56873d42 | 942 | warnings: mandatory and optional. |
33edcb80 RS |
943 | |
944 | As its name suggests, if your code tripped a mandatory warning, you | |
945 | would get a warning whether you wanted it or not. | |
946 | For example, the code below would always produce an C<"isn't numeric"> | |
947 | warning about the "2:". | |
948 | ||
573a192d | 949 | my $x = "2:" + 3; |
33edcb80 RS |
950 | |
951 | With the introduction of lexical warnings, mandatory warnings now become | |
952 | I<default> warnings. The difference is that although the previously | |
953 | mandatory warnings are still enabled by default, they can then be | |
954 | subsequently enabled or disabled with the lexical warning pragma. For | |
955 | example, in the code below, an C<"isn't numeric"> warning will only | |
573a192d | 956 | be reported for the C<$x> variable. |
33edcb80 | 957 | |
573a192d | 958 | my $x = "2:" + 3; |
33edcb80 | 959 | no warnings; |
573a192d | 960 | my $y = "2:" + 3; |
33edcb80 RS |
961 | |
962 | Note that neither the B<-w> flag or the C<$^W> can be used to | |
963 | disable/enable default warnings. They are still mandatory in this case. | |
964 | ||
965 | =head2 What's wrong with B<-w> and C<$^W> | |
966 | ||
967 | Although very useful, the big problem with using B<-w> on the command | |
968 | line to enable warnings is that it is all or nothing. Take the typical | |
969 | scenario when you are writing a Perl program. Parts of the code you | |
970 | will write yourself, but it's very likely that you will make use of | |
971 | pre-written Perl modules. If you use the B<-w> flag in this case, you | |
972 | end up enabling warnings in pieces of code that you haven't written. | |
973 | ||
974 | Similarly, using C<$^W> to either disable or enable blocks of code is | |
975 | fundamentally flawed. For a start, say you want to disable warnings in | |
976 | a block of code. You might expect this to be enough to do the trick: | |
977 | ||
978 | { | |
979 | local ($^W) = 0; | |
573a192d DIM |
980 | my $x =+ 2; |
981 | my $y; chop $y; | |
33edcb80 RS |
982 | } |
983 | ||
984 | When this code is run with the B<-w> flag, a warning will be produced | |
573a192d | 985 | for the C<$x> line: C<"Reversed += operator">. |
33edcb80 RS |
986 | |
987 | The problem is that Perl has both compile-time and run-time warnings. To | |
988 | disable compile-time warnings you need to rewrite the code like this: | |
989 | ||
990 | { | |
991 | BEGIN { $^W = 0 } | |
573a192d DIM |
992 | my $x =+ 2; |
993 | my $y; chop $y; | |
33edcb80 RS |
994 | } |
995 | ||
45f87e65 DB |
996 | And note that unlike the first example, this will permanently set C<$^W> |
997 | since it cannot both run during compile-time and be localized to a | |
998 | run-time block. | |
999 | ||
33edcb80 RS |
1000 | The other big problem with C<$^W> is the way you can inadvertently |
1001 | change the warning setting in unexpected places in your code. For example, | |
1002 | when the code below is run (without the B<-w> flag), the second call | |
1003 | to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas | |
1004 | the first will not. | |
1005 | ||
1006 | sub doit | |
1007 | { | |
573a192d | 1008 | my $y; chop $y; |
33edcb80 RS |
1009 | } |
1010 | ||
1011 | doit(); | |
1012 | ||
1013 | { | |
1014 | local ($^W) = 1; | |
1015 | doit() | |
1016 | } | |
1017 | ||
1018 | This is a side-effect of C<$^W> being dynamically scoped. | |
1019 | ||
1020 | Lexical warnings get around these limitations by allowing finer control | |
1021 | over where warnings can or can't be tripped. | |
1022 | ||
1023 | =head2 Controlling Warnings from the Command Line | |
1024 | ||
1025 | There are three Command Line flags that can be used to control when | |
1026 | warnings are (or aren't) produced: | |
1027 | ||
1028 | =over 5 | |
1029 | ||
1030 | =item B<-w> | |
1031 | X<-w> | |
1032 | ||
1033 | This is the existing flag. If the lexical warnings pragma is B<not> | |
677f8b87 | 1034 | used in any of your code, or any of the modules that you use, this flag |
05a64c17 | 1035 | will enable warnings everywhere. See L</Backward Compatibility> for |
33edcb80 RS |
1036 | details of how this flag interacts with lexical warnings. |
1037 | ||
1038 | =item B<-W> | |
1039 | X<-W> | |
1040 | ||
3c3f8cd6 | 1041 | If the B<-W> flag is used on the command line, it will enable all warnings |
33edcb80 RS |
1042 | throughout the program regardless of whether warnings were disabled |
1043 | locally using C<no warnings> or C<$^W =0>. | |
1044 | This includes all files that get | |
1045 | included via C<use>, C<require> or C<do>. | |
1046 | Think of it as the Perl equivalent of the "lint" command. | |
1047 | ||
1048 | =item B<-X> | |
1049 | X<-X> | |
1050 | ||
3c3f8cd6 | 1051 | Does the exact opposite to the B<-W> flag, i.e. it disables all warnings. |
33edcb80 RS |
1052 | |
1053 | =back | |
1054 | ||
1055 | =head2 Backward Compatibility | |
1056 | ||
1057 | If you are used to working with a version of Perl prior to the | |
1058 | introduction of lexically scoped warnings, or have code that uses both | |
1059 | lexical warnings and C<$^W>, this section will describe how they interact. | |
1060 | ||
1061 | How Lexical Warnings interact with B<-w>/C<$^W>: | |
1062 | ||
1063 | =over 5 | |
1064 | ||
1065 | =item 1. | |
1066 | ||
1067 | If none of the three command line flags (B<-w>, B<-W> or B<-X>) that | |
1068 | control warnings is used and neither C<$^W> nor the C<warnings> pragma | |
1069 | are used, then default warnings will be enabled and optional warnings | |
1070 | disabled. | |
1071 | This means that legacy code that doesn't attempt to control the warnings | |
1072 | will work unchanged. | |
1073 | ||
1074 | =item 2. | |
1075 | ||
1076 | The B<-w> flag just sets the global C<$^W> variable as in 5.005. This | |
1077 | means that any legacy code that currently relies on manipulating C<$^W> | |
56873d42 | 1078 | to control warning behavior will still work as is. |
33edcb80 RS |
1079 | |
1080 | =item 3. | |
1081 | ||
1082 | Apart from now being a boolean, the C<$^W> variable operates in exactly | |
1083 | the same horrible uncontrolled global way, except that it cannot | |
1084 | disable/enable default warnings. | |
1085 | ||
1086 | =item 4. | |
1087 | ||
1088 | If a piece of code is under the control of the C<warnings> pragma, | |
1089 | both the C<$^W> variable and the B<-w> flag will be ignored for the | |
1090 | scope of the lexical warning. | |
1091 | ||
1092 | =item 5. | |
1093 | ||
1094 | The only way to override a lexical warnings setting is with the B<-W> | |
1095 | or B<-X> command line flags. | |
1096 | ||
1097 | =back | |
1098 | ||
1099 | The combined effect of 3 & 4 is that it will allow code which uses | |
1100 | the C<warnings> pragma to control the warning behavior of $^W-type | |
1101 | code (using a C<local $^W=0>) if it really wants to, but not vice-versa. | |
1102 | ||
1103 | =head2 Category Hierarchy | |
1104 | X<warning, categories> | |
1105 | ||
1106 | A hierarchy of "categories" have been defined to allow groups of warnings | |
1107 | to be enabled/disabled in isolation. | |
1108 | ||
1109 | The current hierarchy is: | |
1110 | ||
1111 | =for warnings.pl tree-goes-here | |
1112 | ||
1113 | Just like the "strict" pragma any of these categories can be combined | |
1114 | ||
1115 | use warnings qw(void redefine); | |
1116 | no warnings qw(io syntax untie); | |
1117 | ||
1118 | Also like the "strict" pragma, if there is more than one instance of the | |
56873d42 | 1119 | C<warnings> pragma in a given scope the cumulative effect is additive. |
33edcb80 RS |
1120 | |
1121 | use warnings qw(void); # only "void" warnings enabled | |
1122 | ... | |
1123 | use warnings qw(io); # only "void" & "io" warnings enabled | |
1124 | ... | |
1125 | no warnings qw(void); # only "io" warnings enabled | |
1126 | ||
1127 | To determine which category a specific warning has been assigned to see | |
1128 | L<perldiag>. | |
1129 | ||
1130 | Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a | |
1131 | sub-category of the "syntax" category. It is now a top-level category | |
1132 | in its own right. | |
1133 | ||
3664866e AB |
1134 | Note: Before 5.21.0, the "missing" lexical warnings category was |
1135 | internally defined to be the same as the "uninitialized" category. It | |
1136 | is now a top-level category in its own right. | |
1137 | ||
33edcb80 RS |
1138 | =head2 Fatal Warnings |
1139 | X<warning, fatal> | |
1140 | ||
2e4abf26 DG |
1141 | The presence of the word "FATAL" in the category list will escalate |
1142 | warnings in those categories into fatal errors in that lexical scope. | |
1143 | ||
1144 | B<NOTE:> FATAL warnings should be used with care, particularly | |
1145 | C<< FATAL => 'all' >>. | |
1146 | ||
1147 | Libraries using L<warnings::warn|/FUNCTIONS> for custom warning categories | |
1148 | generally don't expect L<warnings::warn|/FUNCTIONS> to be fatal and can wind up | |
1149 | in an unexpected state as a result. For XS modules issuing categorized | |
1150 | warnings, such unanticipated exceptions could also expose memory leak bugs. | |
1151 | ||
1152 | Moreover, the Perl interpreter itself has had serious bugs involving | |
1153 | fatalized warnings. For a summary of resolved and unresolved problems as | |
1154 | of January 2015, please see | |
1155 | L<this perl5-porters post|http://www.nntp.perl.org/group/perl.perl5.porters/2015/01/msg225235.html>. | |
1156 | ||
1157 | While some developers find fatalizing some warnings to be a useful | |
1158 | defensive programming technique, using C<< FATAL => 'all' >> to fatalize | |
1159 | all possible warning categories -- including custom ones -- is particularly | |
1160 | risky. Therefore, the use of C<< FATAL => 'all' >> is | |
1161 | L<discouraged|perlpolicy/discouraged>. | |
1162 | ||
1163 | The L<strictures|strictures/VERSION-2> module on CPAN offers one example of | |
1164 | a warnings subset that the module's authors believe is relatively safe to | |
1165 | fatalize. | |
1166 | ||
1167 | B<NOTE:> users of FATAL warnings, especially those using | |
1168 | C<< FATAL => 'all' >>, should be fully aware that they are risking future | |
1169 | portability of their programs by doing so. Perl makes absolutely no | |
1170 | commitments to not introduce new warnings or warnings categories in the | |
1171 | future; indeed, we explicitly reserve the right to do so. Code that may | |
1172 | not warn now may warn in a future release of Perl if the Perl5 development | |
1173 | team deems it in the best interests of the community to do so. Should code | |
1174 | using FATAL warnings break due to the introduction of a new warning we will | |
1175 | NOT consider it an incompatible change. Users of FATAL warnings should | |
1176 | take special caution during upgrades to check to see if their code triggers | |
1177 | any new warnings and should pay particular attention to the fine print of | |
1178 | the documentation of the features they use to ensure they do not exploit | |
1179 | features that are documented as risky, deprecated, or unspecified, or where | |
1180 | the documentation says "so don't do that", or anything with the same sense | |
1181 | and spirit. Use of such features in combination with FATAL warnings is | |
1182 | ENTIRELY AT THE USER'S RISK. | |
1183 | ||
1184 | The following documentation describes how to use FATAL warnings but the | |
1185 | perl5 porters strongly recommend that you understand the risks before doing | |
1186 | so, especially for library code intended for use by others, as there is no | |
1187 | way for downstream users to change the choice of fatal categories. | |
1188 | ||
1189 | In the code below, the use of C<time>, C<length> | |
33edcb80 RS |
1190 | and C<join> can all produce a C<"Useless use of xxx in void context"> |
1191 | warning. | |
1192 | ||
1193 | use warnings; | |
1194 | ||
1195 | time; | |
1196 | ||
1197 | { | |
1198 | use warnings FATAL => qw(void); | |
1199 | length "abc"; | |
1200 | } | |
1201 | ||
1202 | join "", 1,2,3; | |
1203 | ||
1204 | print "done\n"; | |
1205 | ||
1206 | When run it produces this output | |
1207 | ||
1208 | Useless use of time in void context at fatal line 3. | |
56873d42 | 1209 | Useless use of length in void context at fatal line 7. |
33edcb80 RS |
1210 | |
1211 | The scope where C<length> is used has escalated the C<void> warnings | |
1212 | category into a fatal error, so the program terminates immediately when it | |
1213 | encounters the warning. | |
1214 | ||
1215 | To explicitly turn off a "FATAL" warning you just disable the warning | |
1216 | it is associated with. So, for example, to disable the "void" warning | |
1217 | in the example above, either of these will do the trick: | |
1218 | ||
1219 | no warnings qw(void); | |
1220 | no warnings FATAL => qw(void); | |
1221 | ||
1222 | If you want to downgrade a warning that has been escalated into a fatal | |
1223 | error back to a normal warning, you can use the "NONFATAL" keyword. For | |
1224 | example, the code below will promote all warnings into fatal errors, | |
1225 | except for those in the "syntax" category. | |
1226 | ||
1227 | use warnings FATAL => 'all', NONFATAL => 'syntax'; | |
1228 | ||
1229 | As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can | |
1230 | use: | |
1231 | ||
1232 | use v5.20; # Perl 5.20 or greater is required for the following | |
1233 | use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';" | |
1234 | ||
1235 | If you want your program to be compatible with versions of Perl before | |
1236 | 5.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In | |
1237 | previous versions of Perl, the behavior of the statements | |
1238 | C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and | |
1239 | C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if | |
1240 | they included the C<< => 'all' >> portion. As of 5.20, they do.) | |
1241 | ||
33edcb80 RS |
1242 | =head2 Reporting Warnings from a Module |
1243 | X<warning, reporting> X<warning, registering> | |
1244 | ||
1245 | The C<warnings> pragma provides a number of functions that are useful for | |
1246 | module authors. These are used when you want to report a module-specific | |
1247 | warning to a calling module has enabled warnings via the C<warnings> | |
1248 | pragma. | |
1249 | ||
1250 | Consider the module C<MyMod::Abc> below. | |
1251 | ||
1252 | package MyMod::Abc; | |
1253 | ||
1254 | use warnings::register; | |
1255 | ||
1256 | sub open { | |
1257 | my $path = shift; | |
1258 | if ($path !~ m#^/#) { | |
1259 | warnings::warn("changing relative path to /var/abc") | |
1260 | if warnings::enabled(); | |
1261 | $path = "/var/abc/$path"; | |
1262 | } | |
1263 | } | |
1264 | ||
1265 | 1; | |
1266 | ||
1267 | The call to C<warnings::register> will create a new warnings category | |
1268 | called "MyMod::Abc", i.e. the new category name matches the current | |
1269 | package name. The C<open> function in the module will display a warning | |
1270 | message if it gets given a relative path as a parameter. This warnings | |
1271 | will only be displayed if the code that uses C<MyMod::Abc> has actually | |
1272 | enabled them with the C<warnings> pragma like below. | |
1273 | ||
1274 | use MyMod::Abc; | |
1275 | use warnings 'MyMod::Abc'; | |
1276 | ... | |
1277 | abc::open("../fred.txt"); | |
1278 | ||
1279 | It is also possible to test whether the pre-defined warnings categories are | |
1280 | set in the calling module with the C<warnings::enabled> function. Consider | |
1281 | this snippet of code: | |
1282 | ||
1283 | package MyMod::Abc; | |
1284 | ||
1285 | sub open { | |
4a21999a TC |
1286 | if (warnings::enabled("deprecated")) { |
1287 | warnings::warn("deprecated", | |
1288 | "open is deprecated, use new instead"); | |
1289 | } | |
33edcb80 RS |
1290 | new(@_); |
1291 | } | |
1292 | ||
1293 | sub new | |
1294 | ... | |
1295 | 1; | |
1296 | ||
1297 | The function C<open> has been deprecated, so code has been included to | |
1298 | display a warning message whenever the calling module has (at least) the | |
1299 | "deprecated" warnings category enabled. Something like this, say. | |
1300 | ||
1301 | use warnings 'deprecated'; | |
1302 | use MyMod::Abc; | |
1303 | ... | |
1304 | MyMod::Abc::open($filename); | |
1305 | ||
1306 | Either the C<warnings::warn> or C<warnings::warnif> function should be | |
1307 | used to actually display the warnings message. This is because they can | |
1308 | make use of the feature that allows warnings to be escalated into fatal | |
1309 | errors. So in this case | |
1310 | ||
1311 | use MyMod::Abc; | |
1312 | use warnings FATAL => 'MyMod::Abc'; | |
1313 | ... | |
1314 | MyMod::Abc::open('../fred.txt'); | |
1315 | ||
1316 | the C<warnings::warnif> function will detect this and die after | |
1317 | displaying the warning message. | |
1318 | ||
1319 | The three warnings functions, C<warnings::warn>, C<warnings::warnif> | |
1320 | and C<warnings::enabled> can optionally take an object reference in place | |
1321 | of a category name. In this case the functions will use the class name | |
1322 | of the object as the warnings category. | |
1323 | ||
1324 | Consider this example: | |
1325 | ||
1326 | package Original; | |
1327 | ||
1328 | no warnings; | |
1329 | use warnings::register; | |
1330 | ||
1331 | sub new | |
1332 | { | |
1333 | my $class = shift; | |
1334 | bless [], $class; | |
1335 | } | |
1336 | ||
1337 | sub check | |
1338 | { | |
1339 | my $self = shift; | |
1340 | my $value = shift; | |
1341 | ||
1342 | if ($value % 2 && warnings::enabled($self)) | |
1343 | { warnings::warn($self, "Odd numbers are unsafe") } | |
1344 | } | |
1345 | ||
1346 | sub doit | |
1347 | { | |
1348 | my $self = shift; | |
1349 | my $value = shift; | |
1350 | $self->check($value); | |
1351 | # ... | |
1352 | } | |
1353 | ||
1354 | 1; | |
1355 | ||
1356 | package Derived; | |
1357 | ||
1358 | use warnings::register; | |
1359 | use Original; | |
1360 | our @ISA = qw( Original ); | |
1361 | sub new | |
1362 | { | |
1363 | my $class = shift; | |
1364 | bless [], $class; | |
1365 | } | |
1366 | ||
1367 | ||
1368 | 1; | |
1369 | ||
56873d42 | 1370 | The code below makes use of both modules, but it only enables warnings from |
33edcb80 RS |
1371 | C<Derived>. |
1372 | ||
1373 | use Original; | |
1374 | use Derived; | |
1375 | use warnings 'Derived'; | |
573a192d DIM |
1376 | my $x = Original->new(); |
1377 | $x->doit(1); | |
1378 | my $y = Derived->new(); | |
1379 | $x->doit(1); | |
33edcb80 | 1380 | |
573a192d | 1381 | When this code is run only the C<Derived> object, C<$y>, will generate |
56873d42 | 1382 | a warning. |
33edcb80 RS |
1383 | |
1384 | Odd numbers are unsafe at main.pl line 7 | |
1385 | ||
1386 | Notice also that the warning is reported at the line where the object is first | |
1387 | used. | |
1388 | ||
1389 | When registering new categories of warning, you can supply more names to | |
1390 | warnings::register like this: | |
1391 | ||
1392 | package MyModule; | |
1393 | use warnings::register qw(format precision); | |
1394 | ||
1395 | ... | |
fe2e802c | 1396 | |
33edcb80 | 1397 | warnings::warnif('MyModule::format', '...'); |
599cee73 | 1398 | |
33edcb80 | 1399 | =head1 FUNCTIONS |
e476b1b5 | 1400 | |
c4583f59 FC |
1401 | Note: The functions with names ending in C<_at_level> were added in Perl |
1402 | 5.28. | |
1403 | ||
39b50539 Z |
1404 | =over 4 |
1405 | ||
d3a7d8c7 GS |
1406 | =item use warnings::register |
1407 | ||
7e6d00f8 PM |
1408 | Creates a new warnings category with the same name as the package where |
1409 | the call to the pragma is used. | |
1410 | ||
1411 | =item warnings::enabled() | |
1412 | ||
1413 | Use the warnings category with the same name as the current package. | |
1414 | ||
1415 | Return TRUE if that warnings category is enabled in the calling module. | |
1416 | Otherwise returns FALSE. | |
1417 | ||
1418 | =item warnings::enabled($category) | |
1419 | ||
1420 | Return TRUE if the warnings category, C<$category>, is enabled in the | |
1421 | calling module. | |
1422 | Otherwise returns FALSE. | |
1423 | ||
1424 | =item warnings::enabled($object) | |
1425 | ||
1426 | Use the name of the class for the object reference, C<$object>, as the | |
1427 | warnings category. | |
1428 | ||
1429 | Return TRUE if that warnings category is enabled in the first scope | |
1430 | where the object is used. | |
1431 | Otherwise returns FALSE. | |
1432 | ||
c4583f59 FC |
1433 | =item warnings::enabled_at_level($category, $level) |
1434 | ||
1435 | Like C<warnings::enabled>, but $level specifies the exact call frame, 0 | |
1436 | being the immediate caller. | |
1437 | ||
ec983580 AR |
1438 | =item warnings::fatal_enabled() |
1439 | ||
1440 | Return TRUE if the warnings category with the same name as the current | |
1441 | package has been set to FATAL in the calling module. | |
1442 | Otherwise returns FALSE. | |
1443 | ||
1444 | =item warnings::fatal_enabled($category) | |
1445 | ||
1446 | Return TRUE if the warnings category C<$category> has been set to FATAL in | |
1447 | the calling module. | |
1448 | Otherwise returns FALSE. | |
1449 | ||
1450 | =item warnings::fatal_enabled($object) | |
1451 | ||
1452 | Use the name of the class for the object reference, C<$object>, as the | |
1453 | warnings category. | |
1454 | ||
1455 | Return TRUE if that warnings category has been set to FATAL in the first | |
1456 | scope where the object is used. | |
1457 | Otherwise returns FALSE. | |
1458 | ||
c4583f59 FC |
1459 | =item warnings::fatal_enabled_at_level($category, $level) |
1460 | ||
1461 | Like C<warnings::fatal_enabled>, but $level specifies the exact call frame, | |
1462 | 0 being the immediate caller. | |
1463 | ||
7e6d00f8 PM |
1464 | =item warnings::warn($message) |
1465 | ||
1466 | Print C<$message> to STDERR. | |
1467 | ||
1468 | Use the warnings category with the same name as the current package. | |
1469 | ||
1470 | If that warnings category has been set to "FATAL" in the calling module | |
1471 | then die. Otherwise return. | |
1472 | ||
1473 | =item warnings::warn($category, $message) | |
1474 | ||
1475 | Print C<$message> to STDERR. | |
1476 | ||
1477 | If the warnings category, C<$category>, has been set to "FATAL" in the | |
1478 | calling module then die. Otherwise return. | |
d3a7d8c7 | 1479 | |
7e6d00f8 | 1480 | =item warnings::warn($object, $message) |
e476b1b5 | 1481 | |
7e6d00f8 | 1482 | Print C<$message> to STDERR. |
e476b1b5 | 1483 | |
7e6d00f8 PM |
1484 | Use the name of the class for the object reference, C<$object>, as the |
1485 | warnings category. | |
e476b1b5 | 1486 | |
7e6d00f8 PM |
1487 | If that warnings category has been set to "FATAL" in the scope where C<$object> |
1488 | is first used then die. Otherwise return. | |
599cee73 | 1489 | |
c4583f59 FC |
1490 | =item warnings::warn_at_level($category, $level, $message) |
1491 | ||
1492 | Like C<warnings::warn>, but $level specifies the exact call frame, | |
1493 | 0 being the immediate caller. | |
e476b1b5 | 1494 | |
7e6d00f8 PM |
1495 | =item warnings::warnif($message) |
1496 | ||
1497 | Equivalent to: | |
1498 | ||
1499 | if (warnings::enabled()) | |
1500 | { warnings::warn($message) } | |
1501 | ||
1502 | =item warnings::warnif($category, $message) | |
1503 | ||
1504 | Equivalent to: | |
1505 | ||
1506 | if (warnings::enabled($category)) | |
1507 | { warnings::warn($category, $message) } | |
1508 | ||
1509 | =item warnings::warnif($object, $message) | |
1510 | ||
1511 | Equivalent to: | |
1512 | ||
1513 | if (warnings::enabled($object)) | |
1514 | { warnings::warn($object, $message) } | |
d3a7d8c7 | 1515 | |
c4583f59 FC |
1516 | =item warnings::warnif_at_level($category, $level, $message) |
1517 | ||
1518 | Like C<warnings::warnif>, but $level specifies the exact call frame, | |
1519 | 0 being the immediate caller. | |
1520 | ||
5e7ad92a | 1521 | =item warnings::register_categories(@names) |
13781810 FR |
1522 | |
1523 | This registers warning categories for the given names and is primarily for | |
d2ec25a5 | 1524 | use by the warnings::register pragma. |
13781810 | 1525 | |
e476b1b5 GS |
1526 | =back |
1527 | ||
d2ec25a5 | 1528 | See also L<perlmodlib/Pragmatic Modules> and L<perldiag>. |
599cee73 PM |
1529 | |
1530 | =cut |