3 # Regenerate (overwriting only if changed):
9 # from information hardcoded into this script (the $tree hash), plus the
10 # template for warnings.pm in the DATA section. Only part of
11 # pod/perllexwarn.pod (the warnings category hierarchy) is generated,
12 # the other parts remaining untouched.
14 # When changing the number of warnings, t/op/caller.t should change to
15 # correspond with the value of $BYTES in lib/warnings.pm
17 # With an argument of 'tree', just dump the contents of $tree and exits.
18 # Also accepts the standard regen_lib -q and -v args.
20 # This script is normally invoked from regen.pl.
25 require 'regen/regen_lib.pl';
30 sub DEFAULT_ON () { 1 }
31 sub DEFAULT_OFF () { 2 }
37 'pipe' => [ 5.008, DEFAULT_OFF],
38 'unopened' => [ 5.008, DEFAULT_OFF],
39 'closed' => [ 5.008, DEFAULT_OFF],
40 'newline' => [ 5.008, DEFAULT_OFF],
41 'exec' => [ 5.008, DEFAULT_OFF],
42 'layer' => [ 5.008, DEFAULT_OFF],
43 'syscalls' => [ 5.019, DEFAULT_OFF],
45 'syntax' => [ 5.008, {
46 'ambiguous' => [ 5.008, DEFAULT_OFF],
47 'semicolon' => [ 5.008, DEFAULT_OFF],
48 'precedence' => [ 5.008, DEFAULT_OFF],
49 'bareword' => [ 5.008, DEFAULT_OFF],
50 'reserved' => [ 5.008, DEFAULT_OFF],
51 'digit' => [ 5.008, DEFAULT_OFF],
52 'parenthesis' => [ 5.008, DEFAULT_OFF],
53 'printf' => [ 5.008, DEFAULT_OFF],
54 'prototype' => [ 5.008, DEFAULT_OFF],
55 'qw' => [ 5.008, DEFAULT_OFF],
56 'illegalproto' => [ 5.011, DEFAULT_OFF],
58 'severe' => [ 5.008, {
59 'inplace' => [ 5.008, DEFAULT_ON],
60 'internal' => [ 5.008, DEFAULT_OFF],
61 'debugging' => [ 5.008, DEFAULT_ON],
62 'malloc' => [ 5.008, DEFAULT_ON],
64 'deprecated' => [ 5.008, DEFAULT_ON],
65 'void' => [ 5.008, DEFAULT_OFF],
66 'recursion' => [ 5.008, DEFAULT_OFF],
67 'redefine' => [ 5.008, DEFAULT_OFF],
68 'numeric' => [ 5.008, DEFAULT_OFF],
69 'uninitialized' => [ 5.008, DEFAULT_OFF],
70 'once' => [ 5.008, DEFAULT_OFF],
71 'misc' => [ 5.008, DEFAULT_OFF],
72 'regexp' => [ 5.008, DEFAULT_OFF],
73 'glob' => [ 5.008, DEFAULT_ON],
74 'untie' => [ 5.008, DEFAULT_OFF],
75 'substr' => [ 5.008, DEFAULT_OFF],
76 'taint' => [ 5.008, DEFAULT_OFF],
77 'signal' => [ 5.008, DEFAULT_OFF],
78 'closure' => [ 5.008, DEFAULT_OFF],
79 'overflow' => [ 5.008, DEFAULT_OFF],
80 'portable' => [ 5.008, DEFAULT_OFF],
82 'surrogate' => [ 5.013, DEFAULT_OFF],
83 'nonchar' => [ 5.013, DEFAULT_OFF],
84 'non_unicode' => [ 5.013, DEFAULT_OFF],
86 'exiting' => [ 5.008, DEFAULT_OFF],
87 'pack' => [ 5.008, DEFAULT_OFF],
88 'unpack' => [ 5.008, DEFAULT_OFF],
89 'threads' => [ 5.008, DEFAULT_OFF],
90 'imprecision' => [ 5.011, DEFAULT_OFF],
91 'experimental' => [ 5.017, {
92 'experimental::lexical_subs' =>
93 [ 5.017, DEFAULT_ON ],
94 'experimental::regex_sets' =>
95 [ 5.017, DEFAULT_ON ],
96 'experimental::lexical_topic' =>
97 [ 5.017, DEFAULT_ON ],
98 'experimental::smartmatch' =>
99 [ 5.017, DEFAULT_ON ],
102 #'default' => [ 5.008, DEFAULT_ON ],
120 foreach $k (sort keys %$tre) {
122 die "duplicate key $k\n" if defined $list{$k} ;
123 die "Value associated with key '$k' is not an ARRAY reference"
124 if !ref $v || ref $v ne 'ARRAY' ;
126 my ($ver, $rest) = @{ $v } ;
127 push @{ $v_list{$ver} }, $k;
130 { valueWalk ($rest) }
139 foreach my $ver ( sort { $a <=> $b } keys %v_list ) {
140 foreach my $name (@{ $v_list{$ver} } ) {
141 $ValueToName{ $index } = [ uc $name, $ver ] ;
142 $NameToValue{ uc $name } = $index ++ ;
149 ###########################################################################
157 foreach $k (sort keys %$tre) {
159 die "duplicate key $k\n" if defined $list{$k} ;
160 die "Can't find key '$k'"
161 if ! defined $NameToValue{uc $k} ;
162 push @{ $list{$k} }, $NameToValue{uc $k} ;
163 die "Value associated with key '$k' is not an ARRAY reference"
164 if !ref $v || ref $v ne 'ARRAY' ;
166 my ($ver, $rest) = @{ $v } ;
168 { push (@{ $list{$k} }, walk ($rest)) }
169 elsif ($rest == DEFAULT_ON)
170 { push @def, $NameToValue{uc $k} }
172 push @list, @{ $list{$k} } ;
178 ###########################################################################
185 for my $i (1 .. @a - 1) {
187 if $a[$i] == $a[$i - 1] + 1
188 && ($i >= @a - 1 || $a[$i] + 1 == $a[$i + 1] );
190 $out[-1] = $a[-1] if $out[-1] eq "..";
192 my $out = join(",",@out);
194 $out =~ s/,(\.\.,)+/../g ;
198 ###########################################################################
205 my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ;
206 my @keys = sort keys %$tre ;
208 while ($k = shift @keys) {
210 die "Value associated with key '$k' is not an ARRAY reference"
211 if !ref $v || ref $v ne 'ARRAY' ;
215 print $prefix . "|\n" ;
216 print $prefix . "+- $k" ;
217 $offset = ' ' x ($max + 4) ;
220 print $prefix . "$k" ;
221 $offset = ' ' x ($max + 1) ;
224 my ($ver, $rest) = @{ $v } ;
227 my $bar = @keys ? "|" : " ";
228 print " -" . "-" x ($max - length $k ) . "+\n" ;
229 printTree ($rest, $prefix . $bar . $offset )
237 ###########################################################################
241 my ($f, $max, @a) = @_ ;
242 my $mask = "\x00" x $max ;
246 vec($mask, $_, 1) = 1 ;
249 foreach (unpack("C*", $mask)) {
251 $string .= '\x' . sprintf("%2.2x", $_)
254 $string .= '\\' . sprintf("%o", $_)
263 return mkHexOct("x", $max, @a);
269 return mkHexOct("o", $max, @a);
272 ###########################################################################
274 if (@ARGV && $ARGV[0] eq "tree")
276 printTree($tree, " ") ;
280 my ($warn, $pm) = map {
281 open_new($_, '>', { by => 'regen/warnings.pl' });
282 } 'warnings.h', 'lib/warnings.pm';
286 #define Off(x) ((x) / 8)
287 #define Bit(x) (1 << ((x) % 8))
288 #define IsSet(a, x) ((a)[Off(x)] & Bit(x))
291 #define G_WARN_OFF 0 /* $^W == 0 */
292 #define G_WARN_ON 1 /* -w flag and $^W != 0 */
293 #define G_WARN_ALL_ON 2 /* -W flag */
294 #define G_WARN_ALL_OFF 4 /* -X flag */
295 #define G_WARN_ONCE 8 /* set if 'once' ever enabled */
296 #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF)
298 #define pWARN_STD NULL
299 #define pWARN_ALL (((STRLEN*)0)+1) /* use warnings 'all' */
300 #define pWARN_NONE (((STRLEN*)0)+2) /* no warnings 'all' */
302 #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \
305 /* if PL_warnhook is set to this value, then warnings die */
306 #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
312 my $index = orderValues();
314 die <<EOM if $index > 255 ;
315 Too many warnings categories -- max is 255
316 rewrite packWARN* & unpackWARN* macros
322 my $warn_size = int($index / 8) + ($index % 8 != 0) ;
326 foreach $k (sort { $a <=> $b } keys %ValueToName) {
327 my ($name, $version) = @{ $ValueToName{$k} };
328 print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
329 if $last_ver != $version ;
331 print $warn tab(5, "#define WARN_$name"), " $k\n" ;
332 $last_ver = $version ;
336 print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
337 #print WARN tab(5, '#define WARN_ALLstring'), '"', ('\377' x $warn_size) , "\"\n" ;
338 print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
339 print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
343 #define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD)
344 #define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD)
345 #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE))
346 #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)))
347 #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1))
349 #define DUP_WARNINGS(p) \
350 (specialWARN(p) ? (STRLEN*)(p) \
351 : (STRLEN*)CopyD(p, PerlMemShared_malloc(sizeof(*p)+*p), sizeof(*p)+*p, \
354 #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w))
355 #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2))
356 #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3))
357 #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4))
359 #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w))
360 #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2))
361 #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3))
362 #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4))
366 #define packWARN(a) (a )
367 #define packWARN2(a,b) ((a) | ((b)<<8) )
368 #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) )
369 #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24))
371 #define unpackWARN1(x) ((x) & 0xFF)
372 #define unpackWARN2(x) (((x) >>8) & 0xFF)
373 #define unpackWARN3(x) (((x) >>16) & 0xFF)
374 #define unpackWARN4(x) (((x) >>24) & 0xFF)
377 ( ! specialWARN(PL_curcop->cop_warnings) && \
378 ( isWARNf_on(PL_curcop->cop_warnings, WARN_ALL) || \
379 isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \
380 isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \
381 isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \
382 isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))
384 /* end of file warnings.h */
387 read_only_bottom_close_and_rename($warn);
390 last if /^KEYWORDS$/ ;
395 print $pm "our %Offsets = (\n" ;
396 foreach my $k (sort { $a <=> $b } keys %ValueToName) {
397 my ($name, $version) = @{ $ValueToName{$k} };
400 if ( $last_ver != $version ) {
402 print $pm tab(4, " # Warnings Categories added in Perl $version");
405 print $pm tab(4, " '$name'"), "=> $k,\n" ;
406 $last_ver = $version;
409 print $pm " );\n\n" ;
411 print $pm "our %Bits = (\n" ;
412 foreach $k (sort keys %list) {
415 my @list = sort { $a <=> $b } @$v ;
417 print $pm tab(4, " '$k'"), '=> "',
418 mkHex($warn_size, map $_ * 2 , @list),
419 '", # [', mkRange(@list), "]\n" ;
422 print $pm " );\n\n" ;
424 print $pm "our %DeadBits = (\n" ;
425 foreach $k (sort keys %list) {
428 my @list = sort { $a <=> $b } @$v ;
430 print $pm tab(4, " '$k'"), '=> "',
431 mkHex($warn_size, map $_ * 2 + 1 , @list),
432 '", # [', mkRange(@list), "]\n" ;
435 print $pm " );\n\n" ;
436 print $pm '$NONE = "', ('\0' x $warn_size) , "\";\n" ;
437 print $pm '$DEFAULT = "', mkHex($warn_size, map $_ * 2, @def),
438 '", # [', mkRange(@def), "]\n" ;
439 print $pm '$LAST_BIT = ' . "$index ;\n" ;
440 print $pm '$BYTES = ' . "$warn_size ;\n" ;
445 read_only_bottom_close_and_rename($pm);
447 my $lexwarn = open_new 'pod/perllexwarn.pod', '>';
448 open my $oldlexwarn, "pod/perllexwarn.pod"
449 or die "$0 cannot open pod/perllexwarn.pod for reading: $!";
450 select +(select($lexwarn), do {
451 while(<$oldlexwarn>) {
453 last if /=for warnings.pl begin/;
456 printTree($tree, " ") ;
458 while(<$oldlexwarn>) {
459 last if /=for warnings.pl end/;
461 do { print } while <$oldlexwarn>;
464 close_and_rename($lexwarn);
469 our $VERSION = '1.19';
471 # Verify that we're called correctly so that warnings will work.
472 # see also strict.pm.
473 unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
474 my (undef, $f, $l) = caller;
475 die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
480 warnings - Perl pragma to control optional warnings
490 use warnings::register;
491 if (warnings::enabled()) {
492 warnings::warn("some warning");
495 if (warnings::enabled("void")) {
496 warnings::warn("void", "some warning");
499 if (warnings::enabled($object)) {
500 warnings::warn($object, "some warning");
503 warnings::warnif("some warning");
504 warnings::warnif("void", "some warning");
505 warnings::warnif($object, "some warning");
509 The C<warnings> pragma is a replacement for the command line flag C<-w>,
510 but the pragma is limited to the enclosing block, while the flag is global.
511 See L<perllexwarn> for more information and the list of built-in warning
514 If no import list is supplied, all possible warnings are either enabled
517 A number of functions are provided to assist module authors.
521 =item use warnings::register
523 Creates a new warnings category with the same name as the package where
524 the call to the pragma is used.
526 =item warnings::enabled()
528 Use the warnings category with the same name as the current package.
530 Return TRUE if that warnings category is enabled in the calling module.
531 Otherwise returns FALSE.
533 =item warnings::enabled($category)
535 Return TRUE if the warnings category, C<$category>, is enabled in the
537 Otherwise returns FALSE.
539 =item warnings::enabled($object)
541 Use the name of the class for the object reference, C<$object>, as the
544 Return TRUE if that warnings category is enabled in the first scope
545 where the object is used.
546 Otherwise returns FALSE.
548 =item warnings::fatal_enabled()
550 Return TRUE if the warnings category with the same name as the current
551 package has been set to FATAL in the calling module.
552 Otherwise returns FALSE.
554 =item warnings::fatal_enabled($category)
556 Return TRUE if the warnings category C<$category> has been set to FATAL in
558 Otherwise returns FALSE.
560 =item warnings::fatal_enabled($object)
562 Use the name of the class for the object reference, C<$object>, as the
565 Return TRUE if that warnings category has been set to FATAL in the first
566 scope where the object is used.
567 Otherwise returns FALSE.
569 =item warnings::warn($message)
571 Print C<$message> to STDERR.
573 Use the warnings category with the same name as the current package.
575 If that warnings category has been set to "FATAL" in the calling module
576 then die. Otherwise return.
578 =item warnings::warn($category, $message)
580 Print C<$message> to STDERR.
582 If the warnings category, C<$category>, has been set to "FATAL" in the
583 calling module then die. Otherwise return.
585 =item warnings::warn($object, $message)
587 Print C<$message> to STDERR.
589 Use the name of the class for the object reference, C<$object>, as the
592 If that warnings category has been set to "FATAL" in the scope where C<$object>
593 is first used then die. Otherwise return.
596 =item warnings::warnif($message)
600 if (warnings::enabled())
601 { warnings::warn($message) }
603 =item warnings::warnif($category, $message)
607 if (warnings::enabled($category))
608 { warnings::warn($category, $message) }
610 =item warnings::warnif($object, $message)
614 if (warnings::enabled($object))
615 { warnings::warn($object, $message) }
617 =item warnings::register_categories(@names)
619 This registers warning categories for the given names and is primarily for
620 use by the warnings::register pragma, for which see L<perllexwarn>.
624 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
630 $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
634 require Carp; # this initializes %CarpInternal
635 local $Carp::CarpInternal{'warnings'};
636 delete $Carp::CarpInternal{'warnings'};
646 foreach my $word ( @_ ) {
647 if ($word eq 'FATAL') {
651 elsif ($word eq 'NONFATAL') {
655 elsif ($catmask = $Bits{$word}) {
657 $mask |= $DeadBits{$word} if $fatal ;
658 $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
661 { Croaker("Unknown warnings category '$word'")}
669 # called from B::Deparse.pm
670 push @_, 'all' unless @_ ;
671 return _bits(undef, @_) ;
678 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
680 if (vec($mask, $Offsets{'all'}, 1)) {
681 $mask |= $Bits{'all'} ;
682 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
685 # Empty @_ is equivalent to @_ = 'all' ;
686 ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
694 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
696 if (vec($mask, $Offsets{'all'}, 1)) {
697 $mask |= $Bits{'all'} ;
698 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
701 push @_, 'all' unless @_;
703 foreach my $word ( @_ ) {
704 if ($word eq 'FATAL') {
707 elsif ($catmask = $Bits{$word}) {
708 $mask &= ~($catmask | $DeadBits{$word} | $All);
711 { Croaker("Unknown warnings category '$word'")}
714 ${^WARNING_BITS} = $mask ;
717 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
719 sub MESSAGE () { 4 };
729 my $has_message = $wanted & MESSAGE;
731 unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
732 my $sub = (caller 1)[3];
733 my $syntax = $has_message ? "[category,] 'message'" : '[category]';
734 Croaker("Usage: $sub($syntax)");
737 my $message = pop if $has_message;
740 # check the category supplied.
742 if (my $type = ref $category) {
743 Croaker("not an object")
744 if exists $builtin_type{$type};
748 $offset = $Offsets{$category};
749 Croaker("Unknown warnings category '$category'")
750 unless defined $offset;
753 $category = (caller(1))[0] ;
754 $offset = $Offsets{$category};
755 Croaker("package '$category' not registered for warnings")
756 unless defined $offset ;
764 while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
765 last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
770 $i = _error_loc(); # see where Carp will allocate the error
773 # Default to 0 if caller returns nothing. Default to $DEFAULT if it
774 # explicitly returns undef.
775 my(@callers_bitmask) = (caller($i))[9] ;
776 my $callers_bitmask =
777 @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ;
780 foreach my $type (FATAL, NORMAL) {
781 next unless $wanted & $type;
783 push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
784 vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
787 # &enabled and &fatal_enabled
788 return $results[0] unless $has_message;
790 # &warnif, and the category is neither enabled as warning nor as fatal
791 return if $wanted == (NORMAL | FATAL | MESSAGE)
792 && !($results[0] || $results[1]);
795 Carp::croak($message) if $results[0];
796 # will always get here for &warn. will only get here for &warnif if the
797 # category is enabled
798 Carp::carp($message);
806 vec($mask, $bit, 1) = 1;
810 sub register_categories
814 for my $name (@names) {
815 if (! defined $Bits{$name}) {
816 $Bits{$name} = _mkMask($LAST_BIT);
817 vec($Bits{'all'}, $LAST_BIT, 1) = 1;
818 $Offsets{$name} = $LAST_BIT ++;
819 foreach my $k (keys %Bits) {
820 vec($Bits{$k}, $LAST_BIT, 1) = 0;
822 $DeadBits{$name} = _mkMask($LAST_BIT);
823 vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
830 goto &Carp::short_error_loc; # don't introduce another stack frame
835 return __chk(NORMAL, @_);
840 return __chk(FATAL, @_);
845 return __chk(FATAL | MESSAGE, @_);
850 return __chk(NORMAL | FATAL | MESSAGE, @_);
853 # These are not part of any public interface, so we can delete them to save
855 delete @warnings::{qw(NORMAL FATAL MESSAGE)};