3 # Regenerate (overwriting only if changed):
8 # from information hardcoded into this script (the $tree hash), plus the
9 # template for warnings.pm in the DATA section.
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
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.
17 # This script is normally invoked from regen.pl.
22 require 'regen/regen_lib.pl';
27 sub DEFAULT_ON () { 1 }
28 sub DEFAULT_OFF () { 2 }
34 'pipe' => [ 5.008, DEFAULT_OFF],
35 'unopened' => [ 5.008, DEFAULT_OFF],
36 'closed' => [ 5.008, DEFAULT_OFF],
37 'newline' => [ 5.008, DEFAULT_OFF],
38 'exec' => [ 5.008, DEFAULT_OFF],
39 'layer' => [ 5.008, DEFAULT_OFF],
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],
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],
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],
78 'surrogate' => [ 5.013, DEFAULT_OFF],
79 'nonchar' => [ 5.013, DEFAULT_OFF],
80 'non_unicode' => [ 5.013, DEFAULT_OFF],
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 ],
92 #'default' => [ 5.008, DEFAULT_ON ],
110 foreach $k (sort keys %$tre) {
112 die "duplicate key $k\n" if defined $list{$k} ;
113 die "Value associated with key '$k' is not an ARRAY reference"
114 if !ref $v || ref $v ne 'ARRAY' ;
116 my ($ver, $rest) = @{ $v } ;
117 push @{ $v_list{$ver} }, $k;
120 { valueWalk ($rest) }
129 foreach my $ver ( sort { $a <=> $b } keys %v_list ) {
130 foreach my $name (@{ $v_list{$ver} } ) {
131 $ValueToName{ $index } = [ uc $name, $ver ] ;
132 $NameToValue{ uc $name } = $index ++ ;
139 ###########################################################################
147 foreach $k (sort keys %$tre) {
149 die "duplicate key $k\n" if defined $list{$k} ;
150 die "Can't find key '$k'"
151 if ! defined $NameToValue{uc $k} ;
152 push @{ $list{$k} }, $NameToValue{uc $k} ;
153 die "Value associated with key '$k' is not an ARRAY reference"
154 if !ref $v || ref $v ne 'ARRAY' ;
156 my ($ver, $rest) = @{ $v } ;
158 { push (@{ $list{$k} }, walk ($rest)) }
159 elsif ($rest == DEFAULT_ON)
160 { push @def, $NameToValue{uc $k} }
162 push @list, @{ $list{$k} } ;
168 ###########################################################################
175 for my $i (1 .. @a - 1) {
177 if $a[$i] == $a[$i - 1] + 1
178 && ($i >= @a - 1 || $a[$i] + 1 == $a[$i + 1] );
180 $out[-1] = $a[-1] if $out[-1] eq "..";
182 my $out = join(",",@out);
184 $out =~ s/,(\.\.,)+/../g ;
188 ###########################################################################
195 my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ;
196 my @keys = sort keys %$tre ;
198 while ($k = shift @keys) {
200 die "Value associated with key '$k' is not an ARRAY reference"
201 if !ref $v || ref $v ne 'ARRAY' ;
205 print $prefix . "|\n" ;
206 print $prefix . "+- $k" ;
207 $offset = ' ' x ($max + 4) ;
210 print $prefix . "$k" ;
211 $offset = ' ' x ($max + 1) ;
214 my ($ver, $rest) = @{ $v } ;
217 my $bar = @keys ? "|" : " ";
218 print " -" . "-" x ($max - length $k ) . "+\n" ;
219 printTree ($rest, $prefix . $bar . $offset )
227 ###########################################################################
231 my ($f, $max, @a) = @_ ;
232 my $mask = "\x00" x $max ;
236 vec($mask, $_, 1) = 1 ;
239 foreach (unpack("C*", $mask)) {
241 $string .= '\x' . sprintf("%2.2x", $_)
244 $string .= '\\' . sprintf("%o", $_)
253 return mkHexOct("x", $max, @a);
259 return mkHexOct("o", $max, @a);
262 ###########################################################################
264 if (@ARGV && $ARGV[0] eq "tree")
266 printTree($tree, " ") ;
270 my ($warn, $pm) = map {
271 open_new($_, '>', { by => 'regen/warnings.pl' });
272 } 'warnings.h', 'lib/warnings.pm';
276 #define Off(x) ((x) / 8)
277 #define Bit(x) (1 << ((x) % 8))
278 #define IsSet(a, x) ((a)[Off(x)] & Bit(x))
281 #define G_WARN_OFF 0 /* $^W == 0 */
282 #define G_WARN_ON 1 /* -w flag and $^W != 0 */
283 #define G_WARN_ALL_ON 2 /* -W flag */
284 #define G_WARN_ALL_OFF 4 /* -X flag */
285 #define G_WARN_ONCE 8 /* set if 'once' ever enabled */
286 #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF)
288 #define pWARN_STD NULL
289 #define pWARN_ALL (((STRLEN*)0)+1) /* use warnings 'all' */
290 #define pWARN_NONE (((STRLEN*)0)+2) /* no warnings 'all' */
292 #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \
295 /* if PL_warnhook is set to this value, then warnings die */
296 #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
302 my $index = orderValues();
304 die <<EOM if $index > 255 ;
305 Too many warnings categories -- max is 255
306 rewrite packWARN* & unpackWARN* macros
312 my $warn_size = int($index / 8) + ($index % 8 != 0) ;
316 foreach $k (sort { $a <=> $b } keys %ValueToName) {
317 my ($name, $version) = @{ $ValueToName{$k} };
318 print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
319 if $last_ver != $version ;
321 print $warn tab(5, "#define WARN_$name"), " $k\n" ;
322 $last_ver = $version ;
326 print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
327 #print WARN tab(5, '#define WARN_ALLstring'), '"', ('\377' x $warn_size) , "\"\n" ;
328 print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
329 print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
333 #define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD)
334 #define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD)
335 #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE))
336 #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)))
337 #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1))
339 #define DUP_WARNINGS(p) \
340 (specialWARN(p) ? (STRLEN*)(p) \
341 : (STRLEN*)CopyD(p, PerlMemShared_malloc(sizeof(*p)+*p), sizeof(*p)+*p, \
344 #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w))
345 #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2))
346 #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3))
347 #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4))
349 #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w))
350 #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2))
351 #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3))
352 #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4))
356 #define packWARN(a) (a )
357 #define packWARN2(a,b) ((a) | ((b)<<8) )
358 #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) )
359 #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24))
361 #define unpackWARN1(x) ((x) & 0xFF)
362 #define unpackWARN2(x) (((x) >>8) & 0xFF)
363 #define unpackWARN3(x) (((x) >>16) & 0xFF)
364 #define unpackWARN4(x) (((x) >>24) & 0xFF)
367 ( ! specialWARN(PL_curcop->cop_warnings) && \
368 ( isWARNf_on(PL_curcop->cop_warnings, WARN_ALL) || \
369 isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \
370 isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \
371 isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \
372 isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))
374 /* end of file warnings.h */
377 read_only_bottom_close_and_rename($warn);
380 last if /^KEYWORDS$/ ;
385 print $pm "our %Offsets = (\n" ;
386 foreach my $k (sort { $a <=> $b } keys %ValueToName) {
387 my ($name, $version) = @{ $ValueToName{$k} };
390 if ( $last_ver != $version ) {
392 print $pm tab(4, " # Warnings Categories added in Perl $version");
395 print $pm tab(4, " '$name'"), "=> $k,\n" ;
396 $last_ver = $version;
399 print $pm " );\n\n" ;
401 print $pm "our %Bits = (\n" ;
402 foreach $k (sort keys %list) {
405 my @list = sort { $a <=> $b } @$v ;
407 print $pm tab(4, " '$k'"), '=> "',
408 mkHex($warn_size, map $_ * 2 , @list),
409 '", # [', mkRange(@list), "]\n" ;
412 print $pm " );\n\n" ;
414 print $pm "our %DeadBits = (\n" ;
415 foreach $k (sort keys %list) {
418 my @list = sort { $a <=> $b } @$v ;
420 print $pm tab(4, " '$k'"), '=> "',
421 mkHex($warn_size, map $_ * 2 + 1 , @list),
422 '", # [', mkRange(@list), "]\n" ;
425 print $pm " );\n\n" ;
426 print $pm '$NONE = "', ('\0' x $warn_size) , "\";\n" ;
427 print $pm '$DEFAULT = "', mkHex($warn_size, map $_ * 2, @def),
428 '", # [', mkRange(@def), "]\n" ;
429 print $pm '$LAST_BIT = ' . "$index ;\n" ;
430 print $pm '$BYTES = ' . "$warn_size ;\n" ;
435 read_only_bottom_close_and_rename($pm);
440 our $VERSION = '1.15';
442 # Verify that we're called correctly so that warnings will work.
443 # see also strict.pm.
444 unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
445 my (undef, $f, $l) = caller;
446 die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
451 warnings - Perl pragma to control optional warnings
461 use warnings::register;
462 if (warnings::enabled()) {
463 warnings::warn("some warning");
466 if (warnings::enabled("void")) {
467 warnings::warn("void", "some warning");
470 if (warnings::enabled($object)) {
471 warnings::warn($object, "some warning");
474 warnings::warnif("some warning");
475 warnings::warnif("void", "some warning");
476 warnings::warnif($object, "some warning");
480 The C<warnings> pragma is a replacement for the command line flag C<-w>,
481 but the pragma is limited to the enclosing block, while the flag is global.
482 See L<perllexwarn> for more information and the list of built-in warning
485 If no import list is supplied, all possible warnings are either enabled
488 A number of functions are provided to assist module authors.
492 =item use warnings::register
494 Creates a new warnings category with the same name as the package where
495 the call to the pragma is used.
497 =item warnings::enabled()
499 Use the warnings category with the same name as the current package.
501 Return TRUE if that warnings category is enabled in the calling module.
502 Otherwise returns FALSE.
504 =item warnings::enabled($category)
506 Return TRUE if the warnings category, C<$category>, is enabled in the
508 Otherwise returns FALSE.
510 =item warnings::enabled($object)
512 Use the name of the class for the object reference, C<$object>, as the
515 Return TRUE if that warnings category is enabled in the first scope
516 where the object is used.
517 Otherwise returns FALSE.
519 =item warnings::fatal_enabled()
521 Return TRUE if the warnings category with the same name as the current
522 package has been set to FATAL in the calling module.
523 Otherwise returns FALSE.
525 =item warnings::fatal_enabled($category)
527 Return TRUE if the warnings category C<$category> has been set to FATAL in
529 Otherwise returns FALSE.
531 =item warnings::fatal_enabled($object)
533 Use the name of the class for the object reference, C<$object>, as the
536 Return TRUE if that warnings category has been set to FATAL in the first
537 scope where the object is used.
538 Otherwise returns FALSE.
540 =item warnings::warn($message)
542 Print C<$message> to STDERR.
544 Use the warnings category with the same name as the current package.
546 If that warnings category has been set to "FATAL" in the calling module
547 then die. Otherwise return.
549 =item warnings::warn($category, $message)
551 Print C<$message> to STDERR.
553 If the warnings category, C<$category>, has been set to "FATAL" in the
554 calling module then die. Otherwise return.
556 =item warnings::warn($object, $message)
558 Print C<$message> to STDERR.
560 Use the name of the class for the object reference, C<$object>, as the
563 If that warnings category has been set to "FATAL" in the scope where C<$object>
564 is first used then die. Otherwise return.
567 =item warnings::warnif($message)
571 if (warnings::enabled())
572 { warnings::warn($message) }
574 =item warnings::warnif($category, $message)
578 if (warnings::enabled($category))
579 { warnings::warn($category, $message) }
581 =item warnings::warnif($object, $message)
585 if (warnings::enabled($object))
586 { warnings::warn($object, $message) }
588 =item warnings::register_categories(@names)
590 This registers warning categories for the given names and is primarily for
591 use by the warnings::register pragma, for which see L<perllexwarn>.
595 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
601 $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
605 require Carp; # this initializes %CarpInternal
606 local $Carp::CarpInternal{'warnings'};
607 delete $Carp::CarpInternal{'warnings'};
617 foreach my $word ( @_ ) {
618 if ($word eq 'FATAL') {
622 elsif ($word eq 'NONFATAL') {
626 elsif ($catmask = $Bits{$word}) {
628 $mask |= $DeadBits{$word} if $fatal ;
629 $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
632 { Croaker("Unknown warnings category '$word'")}
640 # called from B::Deparse.pm
641 push @_, 'all' unless @_ ;
642 return _bits(undef, @_) ;
649 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
651 if (vec($mask, $Offsets{'all'}, 1)) {
652 $mask |= $Bits{'all'} ;
653 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
656 # Empty @_ is equivalent to @_ = 'all' ;
657 ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
665 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
667 if (vec($mask, $Offsets{'all'}, 1)) {
668 $mask |= $Bits{'all'} ;
669 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
672 push @_, 'all' unless @_;
674 foreach my $word ( @_ ) {
675 if ($word eq 'FATAL') {
678 elsif ($catmask = $Bits{$word}) {
679 $mask &= ~($catmask | $DeadBits{$word} | $All);
682 { Croaker("Unknown warnings category '$word'")}
685 ${^WARNING_BITS} = $mask ;
688 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
690 sub MESSAGE () { 4 };
700 my $has_message = $wanted & MESSAGE;
702 unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
703 my $sub = (caller 1)[3];
704 my $syntax = $has_message ? "[category,] 'message'" : '[category]';
705 Croaker("Usage: $sub($syntax)");
708 my $message = pop if $has_message;
711 # check the category supplied.
713 if (my $type = ref $category) {
714 Croaker("not an object")
715 if exists $builtin_type{$type};
719 $offset = $Offsets{$category};
720 Croaker("Unknown warnings category '$category'")
721 unless defined $offset;
724 $category = (caller(1))[0] ;
725 $offset = $Offsets{$category};
726 Croaker("package '$category' not registered for warnings")
727 unless defined $offset ;
735 while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
736 last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
741 $i = _error_loc(); # see where Carp will allocate the error
744 # Default to 0 if caller returns nothing. Default to $DEFAULT if it
745 # explicitly returns undef.
746 my(@callers_bitmask) = (caller($i))[9] ;
747 my $callers_bitmask =
748 @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ;
751 foreach my $type (FATAL, NORMAL) {
752 next unless $wanted & $type;
754 push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
755 vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
758 # &enabled and &fatal_enabled
759 return $results[0] unless $has_message;
761 # &warnif, and the category is neither enabled as warning nor as fatal
762 return if $wanted == (NORMAL | FATAL | MESSAGE)
763 && !($results[0] || $results[1]);
766 Carp::croak($message) if $results[0];
767 # will always get here for &warn. will only get here for &warnif if the
768 # category is enabled
769 Carp::carp($message);
777 vec($mask, $bit, 1) = 1;
781 sub register_categories
785 for my $name (@names) {
786 if (! defined $Bits{$name}) {
787 $Bits{$name} = _mkMask($LAST_BIT);
788 vec($Bits{'all'}, $LAST_BIT, 1) = 1;
789 $Offsets{$name} = $LAST_BIT ++;
790 foreach my $k (keys %Bits) {
791 vec($Bits{$k}, $LAST_BIT, 1) = 0;
793 $DeadBits{$name} = _mkMask($LAST_BIT);
794 vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
801 goto &Carp::short_error_loc; # don't introduce another stack frame
806 return __chk(NORMAL, @_);
811 return __chk(FATAL, @_);
816 return __chk(FATAL | MESSAGE, @_);
821 return __chk(NORMAL | FATAL | MESSAGE, @_);
824 # These are not part of any public interface, so we can delete them to save
826 delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);