1 # -*- buffer-read-only: t -*-
2 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
3 # This file is built by regen/warnings.pl.
4 # Any changes made here will be lost!
10 # Verify that we're called correctly so that warnings will work.
12 unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
13 my (undef, $f, $l) = caller;
14 die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
19 warnings - Perl pragma to control optional warnings
29 use warnings::register;
30 if (warnings::enabled()) {
31 warnings::warn("some warning");
34 if (warnings::enabled("void")) {
35 warnings::warn("void", "some warning");
38 if (warnings::enabled($object)) {
39 warnings::warn($object, "some warning");
42 warnings::warnif("some warning");
43 warnings::warnif("void", "some warning");
44 warnings::warnif($object, "some warning");
48 The C<warnings> pragma is a replacement for the command line flag C<-w>,
49 but the pragma is limited to the enclosing block, while the flag is global.
50 See L<perllexwarn> for more information and the list of built-in warning
53 If no import list is supplied, all possible warnings are either enabled
56 A number of functions are provided to assist module authors.
58 In all the descriptions below, $category can also be a warnings category
59 and ID separated by a colon, such as "experimental:lexical_subs". See
60 L<perllexwarn/Individual Warning IDs>.
64 =item use warnings::register
66 Creates a new warnings category with the same name as the package where
67 the call to the pragma is used.
69 =item warnings::enabled()
71 Use the warnings category with the same name as the current package.
73 Return TRUE if that warnings category is enabled in the calling module.
74 Otherwise returns FALSE.
76 =item warnings::enabled($category)
78 Return TRUE if the warnings category, C<$category>, is enabled in the
80 Otherwise returns FALSE.
82 =item warnings::enabled($object)
84 Use the name of the class for the object reference, C<$object>, as the
87 Return TRUE if that warnings category is enabled in the first scope
88 where the object is used.
89 Otherwise returns FALSE.
91 =item warnings::fatal_enabled()
93 Return TRUE if the warnings category with the same name as the current
94 package has been set to FATAL in the calling module.
95 Otherwise returns FALSE.
97 =item warnings::fatal_enabled($category)
99 Return TRUE if the warnings category C<$category> has been set to FATAL in
101 Otherwise returns FALSE.
103 =item warnings::fatal_enabled($object)
105 Use the name of the class for the object reference, C<$object>, as the
108 Return TRUE if that warnings category has been set to FATAL in the first
109 scope where the object is used.
110 Otherwise returns FALSE.
112 =item warnings::warn($message)
114 Print C<$message> to STDERR.
116 Use the warnings category with the same name as the current package.
118 If that warnings category has been set to "FATAL" in the calling module
119 then die. Otherwise return.
121 =item warnings::warn($category, $message)
123 Print C<$message> to STDERR.
125 If the warnings category, C<$category>, has been set to "FATAL" in the
126 calling module then die. Otherwise return.
128 =item warnings::warn($object, $message)
130 Print C<$message> to STDERR.
132 Use the name of the class for the object reference, C<$object>, as the
135 If that warnings category has been set to "FATAL" in the scope where C<$object>
136 is first used then die. Otherwise return.
139 =item warnings::warnif($message)
143 if (warnings::enabled())
144 { warnings::warn($message) }
146 =item warnings::warnif($category, $message)
150 if (warnings::enabled($category))
151 { warnings::warn($category, $message) }
153 =item warnings::warnif($object, $message)
157 if (warnings::enabled($object))
158 { warnings::warn($object, $message) }
160 =item warnings::register_categories(@names)
162 This registers warning categories for the given names and is primarily for
163 use by the warnings::register pragma, for which see L<perllexwarn>.
167 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
173 # Warnings Categories added in Perl 5.008
216 'uninitialized' => 82,
222 # Warnings Categories added in Perl 5.011
225 'illegalproto' => 94,
227 # Warnings Categories added in Perl 5.013
233 # Warnings Categories added in Perl 5.017
235 'experimental' => 102,
236 'experimental:lexical_subs'=> 104,
240 'all' => "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x01", # [0..52]
241 'ambiguous' => "\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00", # [29]
242 'bareword' => "\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00", # [30]
243 'closed' => "\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [6]
244 'closure' => "\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [1]
245 'debugging' => "\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00", # [22]
246 'deprecated' => "\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [2]
247 'digit' => "\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00", # [31]
248 'exec' => "\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [7]
249 'exiting' => "\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [3]
250 'experimental' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x01", # [51,52]
251 'experimental:lexical_subs'=> "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", # [52]
252 'glob' => "\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [4]
253 'illegalproto' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00", # [47]
254 'imprecision' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00", # [46]
255 'inplace' => "\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00", # [23]
256 'internal' => "\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00", # [24]
257 'io' => "\x00\x54\x55\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [5..11]
258 'layer' => "\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [8]
259 'malloc' => "\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00", # [25]
260 'misc' => "\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [12]
261 'newline' => "\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [9]
262 'non_unicode' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00", # [48]
263 'nonchar' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00", # [49]
264 'numeric' => "\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [13]
265 'once' => "\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [14]
266 'overflow' => "\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [15]
267 'pack' => "\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [16]
268 'parenthesis' => "\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00", # [32]
269 'pipe' => "\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [10]
270 'portable' => "\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [17]
271 'precedence' => "\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00", # [33]
272 'printf' => "\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00", # [34]
273 'prototype' => "\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00", # [35]
274 'qw' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00", # [36]
275 'recursion' => "\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [18]
276 'redefine' => "\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [19]
277 'regexp' => "\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00", # [20]
278 'reserved' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00", # [37]
279 'semicolon' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00", # [38]
280 'severe' => "\x00\x00\x00\x00\x00\x54\x05\x00\x00\x00\x00\x00\x00\x00", # [21..25]
281 'signal' => "\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00", # [26]
282 'substr' => "\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00", # [27]
283 'surrogate' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00", # [50]
284 'syntax' => "\x00\x00\x00\x00\x00\x00\x00\x55\x55\x15\x00\x40\x00\x00", # [28..38,47]
285 'taint' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00", # [39]
286 'threads' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00", # [40]
287 'uninitialized' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00", # [41]
288 'unopened' => "\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [11]
289 'unpack' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00", # [42]
290 'untie' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00", # [43]
291 'utf8' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x15\x00", # [44,48..50]
292 'void' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00", # [45]
296 'all' => "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\x02", # [0..52]
297 'ambiguous' => "\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00", # [29]
298 'bareword' => "\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00", # [30]
299 'closed' => "\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [6]
300 'closure' => "\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [1]
301 'debugging' => "\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00", # [22]
302 'deprecated' => "\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [2]
303 'digit' => "\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00", # [31]
304 'exec' => "\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [7]
305 'exiting' => "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [3]
306 'experimental' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x02", # [51,52]
307 'experimental:lexical_subs'=> "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02", # [52]
308 'glob' => "\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [4]
309 'illegalproto' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00", # [47]
310 'imprecision' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00", # [46]
311 'inplace' => "\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00", # [23]
312 'internal' => "\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00", # [24]
313 'io' => "\x00\xa8\xaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [5..11]
314 'layer' => "\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [8]
315 'malloc' => "\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00", # [25]
316 'misc' => "\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [12]
317 'newline' => "\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [9]
318 'non_unicode' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00", # [48]
319 'nonchar' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00", # [49]
320 'numeric' => "\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [13]
321 'once' => "\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [14]
322 'overflow' => "\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [15]
323 'pack' => "\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [16]
324 'parenthesis' => "\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00", # [32]
325 'pipe' => "\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [10]
326 'portable' => "\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [17]
327 'precedence' => "\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00", # [33]
328 'printf' => "\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00", # [34]
329 'prototype' => "\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00", # [35]
330 'qw' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00", # [36]
331 'recursion' => "\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [18]
332 'redefine' => "\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [19]
333 'regexp' => "\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00", # [20]
334 'reserved' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00", # [37]
335 'semicolon' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00", # [38]
336 'severe' => "\x00\x00\x00\x00\x00\xa8\x0a\x00\x00\x00\x00\x00\x00\x00", # [21..25]
337 'signal' => "\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00\x00\x00\x00\x00", # [26]
338 'substr' => "\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00", # [27]
339 'surrogate' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00", # [50]
340 'syntax' => "\x00\x00\x00\x00\x00\x00\x00\xaa\xaa\x2a\x00\x80\x00\x00", # [28..38,47]
341 'taint' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00\x00", # [39]
342 'threads' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00", # [40]
343 'uninitialized' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00", # [41]
344 'unopened' => "\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", # [11]
345 'unpack' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x00\x00\x00", # [42]
346 'untie' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x00\x00", # [43]
347 'utf8' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x2a\x00", # [44,48..50]
348 'void' => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00", # [45]
351 $NONE = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
352 $DEFAULT = "\x10\x01\x00\x00\x00\x50\x04\x00\x00\x00\x00\x00\x00\x01", # [2,52,4,22,23,25]
356 $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
360 require Carp; # this initializes %CarpInternal
361 local $Carp::CarpInternal{'warnings'};
362 delete $Carp::CarpInternal{'warnings'};
372 foreach my $word ( @_ ) {
373 if ($word eq 'FATAL') {
377 elsif ($word eq 'NONFATAL') {
381 elsif ($catmask = $Bits{$word}) {
383 $mask |= $DeadBits{$word} if $fatal ;
384 $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
387 { Croaker("Unknown warnings category '$word'")}
395 # called from B::Deparse.pm
396 push @_, 'all' unless @_ ;
397 return _bits(undef, @_) ;
404 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
406 if (vec($mask, $Offsets{'all'}, 1)) {
407 $mask |= $Bits{'all'} ;
408 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
411 # Empty @_ is equivalent to @_ = 'all' ;
412 ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
420 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
422 if (vec($mask, $Offsets{'all'}, 1)) {
423 $mask |= $Bits{'all'} ;
424 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
427 push @_, 'all' unless @_;
429 foreach my $word ( @_ ) {
430 if ($word eq 'FATAL') {
433 elsif ($catmask = $Bits{$word}) {
434 $mask &= ~($catmask | $DeadBits{$word} | $All);
437 { Croaker("Unknown warnings category '$word'")}
440 ${^WARNING_BITS} = $mask ;
443 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
445 sub MESSAGE () { 4 };
455 my $has_message = $wanted & MESSAGE;
457 unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
458 my $sub = (caller 1)[3];
459 my $syntax = $has_message ? "[category,] 'message'" : '[category]';
460 Croaker("Usage: $sub($syntax)");
463 my $message = pop if $has_message;
466 # check the category supplied.
468 if (my $type = ref $category) {
469 Croaker("not an object")
470 if exists $builtin_type{$type};
474 $offset = $Offsets{$category};
475 Croaker("Unknown warnings category '$category'")
476 unless defined $offset;
479 $category = (caller(1))[0] ;
480 $offset = $Offsets{$category};
481 Croaker("package '$category' not registered for warnings")
482 unless defined $offset ;
490 while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
491 last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
496 $i = _error_loc(); # see where Carp will allocate the error
499 # Default to 0 if caller returns nothing. Default to $DEFAULT if it
500 # explicitly returns undef.
501 my(@callers_bitmask) = (caller($i))[9] ;
502 my $callers_bitmask =
503 @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ;
506 foreach my $type (FATAL, NORMAL) {
507 next unless $wanted & $type;
509 push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
510 vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
513 # &enabled and &fatal_enabled
514 return $results[0] unless $has_message;
516 # &warnif, and the category is neither enabled as warning nor as fatal
517 return if $wanted == (NORMAL | FATAL | MESSAGE)
518 && !($results[0] || $results[1]);
521 Carp::croak($message) if $results[0];
522 # will always get here for &warn. will only get here for &warnif if the
523 # category is enabled
524 Carp::carp($message);
532 vec($mask, $bit, 1) = 1;
536 sub register_categories
540 for my $name (@names) {
541 if (! defined $Bits{$name}) {
542 $Bits{$name} = _mkMask($LAST_BIT);
543 vec($Bits{'all'}, $LAST_BIT, 1) = 1;
544 $Offsets{$name} = $LAST_BIT ++;
545 foreach my $k (keys %Bits) {
546 vec($Bits{$k}, $LAST_BIT, 1) = 0;
548 $DeadBits{$name} = _mkMask($LAST_BIT);
549 vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
556 goto &Carp::short_error_loc; # don't introduce another stack frame
561 return __chk(NORMAL, @_);
566 return __chk(FATAL, @_);
571 return __chk(FATAL | MESSAGE, @_);
576 return __chk(NORMAL | FATAL | MESSAGE, @_);
579 # These are not part of any public interface, so we can delete them to save
581 delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);