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 # With an argument of 'tree', just dump the contents of $tree and exits.
12 # Also accepts the standard regen_lib -q and -v args.
14 # This script is normally invoked from regen.pl.
19 require 'regen/regen_lib.pl';
24 sub DEFAULT_ON () { 1 }
25 sub DEFAULT_OFF () { 2 }
31 'pipe' => [ 5.008, DEFAULT_OFF],
32 'unopened' => [ 5.008, DEFAULT_OFF],
33 'closed' => [ 5.008, DEFAULT_OFF],
34 'newline' => [ 5.008, DEFAULT_OFF],
35 'exec' => [ 5.008, DEFAULT_OFF],
36 'layer' => [ 5.008, DEFAULT_OFF],
38 'syntax' => [ 5.008, {
39 'ambiguous' => [ 5.008, DEFAULT_OFF],
40 'semicolon' => [ 5.008, DEFAULT_OFF],
41 'precedence' => [ 5.008, DEFAULT_OFF],
42 'bareword' => [ 5.008, DEFAULT_OFF],
43 'reserved' => [ 5.008, DEFAULT_OFF],
44 'digit' => [ 5.008, DEFAULT_OFF],
45 'parenthesis' => [ 5.008, DEFAULT_OFF],
46 'printf' => [ 5.008, DEFAULT_OFF],
47 'prototype' => [ 5.008, DEFAULT_OFF],
48 'qw' => [ 5.008, DEFAULT_OFF],
49 'illegalproto' => [ 5.011, DEFAULT_OFF],
51 'severe' => [ 5.008, {
52 'inplace' => [ 5.008, DEFAULT_ON],
53 'internal' => [ 5.008, DEFAULT_ON],
54 'debugging' => [ 5.008, DEFAULT_ON],
55 'malloc' => [ 5.008, DEFAULT_ON],
57 'deprecated' => [ 5.008, DEFAULT_OFF],
58 'void' => [ 5.008, DEFAULT_OFF],
59 'recursion' => [ 5.008, DEFAULT_OFF],
60 'redefine' => [ 5.008, DEFAULT_OFF],
61 'numeric' => [ 5.008, DEFAULT_OFF],
62 'uninitialized' => [ 5.008, DEFAULT_OFF],
63 'once' => [ 5.008, DEFAULT_OFF],
64 'misc' => [ 5.008, DEFAULT_OFF],
65 'regexp' => [ 5.008, DEFAULT_OFF],
66 'glob' => [ 5.008, DEFAULT_OFF],
67 'untie' => [ 5.008, DEFAULT_OFF],
68 'substr' => [ 5.008, DEFAULT_OFF],
69 'taint' => [ 5.008, DEFAULT_OFF],
70 'signal' => [ 5.008, DEFAULT_OFF],
71 'closure' => [ 5.008, DEFAULT_OFF],
72 'overflow' => [ 5.008, DEFAULT_OFF],
73 'portable' => [ 5.008, DEFAULT_OFF],
74 'utf8' => [ 5.008, DEFAULT_OFF],
75 'exiting' => [ 5.008, DEFAULT_OFF],
76 'pack' => [ 5.008, DEFAULT_OFF],
77 'unpack' => [ 5.008, DEFAULT_OFF],
78 'threads' => [ 5.008, DEFAULT_OFF],
79 'imprecision' => [ 5.011, DEFAULT_OFF],
81 #'default' => [ 5.008, DEFAULT_ON ],
98 foreach $k (sort keys %$tre) {
100 die "duplicate key $k\n" if defined $list{$k} ;
101 die "Value associated with key '$k' is not an ARRAY reference"
102 if !ref $v || ref $v ne 'ARRAY' ;
104 my ($ver, $rest) = @{ $v } ;
105 push @{ $v_list{$ver} }, $k;
108 { valueWalk ($rest) }
117 foreach my $ver ( sort { $a <=> $b } keys %v_list ) {
118 foreach my $name (@{ $v_list{$ver} } ) {
119 $ValueToName{ $index } = [ uc $name, $ver ] ;
120 $NameToValue{ uc $name } = $index ++ ;
127 ###########################################################################
135 foreach $k (sort keys %$tre) {
137 die "duplicate key $k\n" if defined $list{$k} ;
138 die "Can't find key '$k'"
139 if ! defined $NameToValue{uc $k} ;
140 push @{ $list{$k} }, $NameToValue{uc $k} ;
141 die "Value associated with key '$k' is not an ARRAY reference"
142 if !ref $v || ref $v ne 'ARRAY' ;
144 my ($ver, $rest) = @{ $v } ;
146 { push (@{ $list{$k} }, walk ($rest)) }
148 push @list, @{ $list{$k} } ;
154 ###########################################################################
161 for my $i (1 .. @a - 1) {
163 if $a[$i] == $a[$i - 1] + 1
164 && ($i >= @a - 1 || $a[$i] + 1 == $a[$i + 1] );
166 $out[-1] = $a[-1] if $out[-1] eq "..";
168 my $out = join(",",@out);
170 $out =~ s/,(\.\.,)+/../g ;
174 ###########################################################################
181 my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ;
182 my @keys = sort keys %$tre ;
184 while ($k = shift @keys) {
186 die "Value associated with key '$k' is not an ARRAY reference"
187 if !ref $v || ref $v ne 'ARRAY' ;
191 print $prefix . "|\n" ;
192 print $prefix . "+- $k" ;
193 $offset = ' ' x ($max + 4) ;
196 print $prefix . "$k" ;
197 $offset = ' ' x ($max + 1) ;
200 my ($ver, $rest) = @{ $v } ;
203 my $bar = @keys ? "|" : " ";
204 print " -" . "-" x ($max - length $k ) . "+\n" ;
205 printTree ($rest, $prefix . $bar . $offset )
213 ###########################################################################
217 my ($f, $max, @a) = @_ ;
218 my $mask = "\x00" x $max ;
222 vec($mask, $_, 1) = 1 ;
225 foreach (unpack("C*", $mask)) {
227 $string .= '\x' . sprintf("%2.2x", $_)
230 $string .= '\\' . sprintf("%o", $_)
239 return mkHexOct("x", $max, @a);
245 return mkHexOct("o", $max, @a);
248 ###########################################################################
250 if (@ARGV && $ARGV[0] eq "tree")
252 printTree($tree, " ") ;
256 my $warn = safer_open('warnings.h-new', 'warnings.h');
257 my $pm = safer_open('lib/warnings.pm-new', 'lib/warnings.pm');
259 print $pm read_only_top(lang => 'Perl', by => 'regen/warnings.pl');
260 print $warn read_only_top(lang => 'C', by => 'regen/warnings.pl'), <<'EOM';
262 #define Off(x) ((x) / 8)
263 #define Bit(x) (1 << ((x) % 8))
264 #define IsSet(a, x) ((a)[Off(x)] & Bit(x))
267 #define G_WARN_OFF 0 /* $^W == 0 */
268 #define G_WARN_ON 1 /* -w flag and $^W != 0 */
269 #define G_WARN_ALL_ON 2 /* -W flag */
270 #define G_WARN_ALL_OFF 4 /* -X flag */
271 #define G_WARN_ONCE 8 /* set if 'once' ever enabled */
272 #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF)
274 #define pWARN_STD NULL
275 #define pWARN_ALL (((STRLEN*)0)+1) /* use warnings 'all' */
276 #define pWARN_NONE (((STRLEN*)0)+2) /* no warnings 'all' */
278 #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \
281 /* if PL_warnhook is set to this value, then warnings die */
282 #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
288 my $index = orderValues();
290 die <<EOM if $index > 255 ;
291 Too many warnings categories -- max is 255
292 rewrite packWARN* & unpackWARN* macros
298 my $warn_size = int($index / 8) + ($index % 8 != 0) ;
302 foreach $k (sort { $a <=> $b } keys %ValueToName) {
303 my ($name, $version) = @{ $ValueToName{$k} };
304 print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
305 if $last_ver != $version ;
306 print $warn tab(5, "#define WARN_$name"), "$k\n" ;
307 $last_ver = $version ;
311 print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
312 #print WARN tab(5, '#define WARN_ALLstring'), '"', ('\377' x $warn_size) , "\"\n" ;
313 print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
314 print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
318 #define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD)
319 #define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD)
320 #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE))
321 #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)))
322 #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1))
324 #define DUP_WARNINGS(p) \
325 (specialWARN(p) ? (STRLEN*)(p) \
326 : (STRLEN*)CopyD(p, PerlMemShared_malloc(sizeof(*p)+*p), sizeof(*p)+*p, \
329 #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w))
330 #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2))
331 #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3))
332 #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4))
334 #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w))
335 #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2))
336 #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3))
337 #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4))
341 #define packWARN(a) (a )
342 #define packWARN2(a,b) ((a) | ((b)<<8) )
343 #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) )
344 #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24))
346 #define unpackWARN1(x) ((x) & 0xFF)
347 #define unpackWARN2(x) (((x) >>8) & 0xFF)
348 #define unpackWARN3(x) (((x) >>16) & 0xFF)
349 #define unpackWARN4(x) (((x) >>24) & 0xFF)
352 ( ! specialWARN(PL_curcop->cop_warnings) && \
353 ( isWARNf_on(PL_curcop->cop_warnings, WARN_ALL) || \
354 isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \
355 isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \
356 isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \
357 isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))
359 /* end of file warnings.h */
362 read_only_bottom_close_and_rename($warn);
365 last if /^KEYWORDS$/ ;
370 print $pm "our %Offsets = (\n" ;
371 foreach my $k (sort { $a <=> $b } keys %ValueToName) {
372 my ($name, $version) = @{ $ValueToName{$k} };
375 if ( $last_ver != $version ) {
377 print $pm tab(4, " # Warnings Categories added in Perl $version");
380 print $pm tab(4, " '$name'"), "=> $k,\n" ;
381 $last_ver = $version;
384 print $pm " );\n\n" ;
386 print $pm "our %Bits = (\n" ;
387 foreach $k (sort keys %list) {
390 my @list = sort { $a <=> $b } @$v ;
392 print $pm tab(4, " '$k'"), '=> "',
393 mkHex($warn_size, map $_ * 2 , @list),
394 '", # [', mkRange(@list), "]\n" ;
397 print $pm " );\n\n" ;
399 print $pm "our %DeadBits = (\n" ;
400 foreach $k (sort keys %list) {
403 my @list = sort { $a <=> $b } @$v ;
405 print $pm tab(4, " '$k'"), '=> "',
406 mkHex($warn_size, map $_ * 2 + 1 , @list),
407 '", # [', mkRange(@list), "]\n" ;
410 print $pm " );\n\n" ;
411 print $pm '$NONE = "', ('\0' x $warn_size) , "\";\n" ;
412 print $pm '$LAST_BIT = ' . "$index ;\n" ;
413 print $pm '$BYTES = ' . "$warn_size ;\n" ;
418 read_only_bottom_close_and_rename($pm);
423 our $VERSION = '1.12';
425 # Verify that we're called correctly so that warnings will work.
426 # see also strict.pm.
427 unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
428 my (undef, $f, $l) = caller;
429 die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
434 warnings - Perl pragma to control optional warnings
444 use warnings::register;
445 if (warnings::enabled()) {
446 warnings::warn("some warning");
449 if (warnings::enabled("void")) {
450 warnings::warn("void", "some warning");
453 if (warnings::enabled($object)) {
454 warnings::warn($object, "some warning");
457 warnings::warnif("some warning");
458 warnings::warnif("void", "some warning");
459 warnings::warnif($object, "some warning");
463 The C<warnings> pragma is a replacement for the command line flag C<-w>,
464 but the pragma is limited to the enclosing block, while the flag is global.
465 See L<perllexwarn> for more information.
467 If no import list is supplied, all possible warnings are either enabled
470 A number of functions are provided to assist module authors.
474 =item use warnings::register
476 Creates a new warnings category with the same name as the package where
477 the call to the pragma is used.
479 =item warnings::enabled()
481 Use the warnings category with the same name as the current package.
483 Return TRUE if that warnings category is enabled in the calling module.
484 Otherwise returns FALSE.
486 =item warnings::enabled($category)
488 Return TRUE if the warnings category, C<$category>, is enabled in the
490 Otherwise returns FALSE.
492 =item warnings::enabled($object)
494 Use the name of the class for the object reference, C<$object>, as the
497 Return TRUE if that warnings category is enabled in the first scope
498 where the object is used.
499 Otherwise returns FALSE.
501 =item warnings::fatal_enabled()
503 Return TRUE if the warnings category with the same name as the current
504 package has been set to FATAL in the calling module.
505 Otherwise returns FALSE.
507 =item warnings::fatal_enabled($category)
509 Return TRUE if the warnings category C<$category> has been set to FATAL in
511 Otherwise returns FALSE.
513 =item warnings::fatal_enabled($object)
515 Use the name of the class for the object reference, C<$object>, as the
518 Return TRUE if that warnings category has been set to FATAL in the first
519 scope where the object is used.
520 Otherwise returns FALSE.
522 =item warnings::warn($message)
524 Print C<$message> to STDERR.
526 Use the warnings category with the same name as the current package.
528 If that warnings category has been set to "FATAL" in the calling module
529 then die. Otherwise return.
531 =item warnings::warn($category, $message)
533 Print C<$message> to STDERR.
535 If the warnings category, C<$category>, has been set to "FATAL" in the
536 calling module then die. Otherwise return.
538 =item warnings::warn($object, $message)
540 Print C<$message> to STDERR.
542 Use the name of the class for the object reference, C<$object>, as the
545 If that warnings category has been set to "FATAL" in the scope where C<$object>
546 is first used then die. Otherwise return.
549 =item warnings::warnif($message)
553 if (warnings::enabled())
554 { warnings::warn($message) }
556 =item warnings::warnif($category, $message)
560 if (warnings::enabled($category))
561 { warnings::warn($category, $message) }
563 =item warnings::warnif($object, $message)
567 if (warnings::enabled($object))
568 { warnings::warn($object, $message) }
570 =item warnings::register_categories(@names)
572 This registers warning categories for the given names and is primarily for
573 use by the warnings::register pragma, for which see L<perllexwarn>.
577 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
583 $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
587 require Carp; # this initializes %CarpInternal
588 local $Carp::CarpInternal{'warnings'};
589 delete $Carp::CarpInternal{'warnings'};
599 foreach my $word ( @_ ) {
600 if ($word eq 'FATAL') {
604 elsif ($word eq 'NONFATAL') {
608 elsif ($catmask = $Bits{$word}) {
610 $mask |= $DeadBits{$word} if $fatal ;
611 $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
614 { Croaker("Unknown warnings category '$word'")}
622 # called from B::Deparse.pm
623 push @_, 'all' unless @_ ;
624 return _bits(undef, @_) ;
631 my $mask = ${^WARNING_BITS} ;
633 if (vec($mask, $Offsets{'all'}, 1)) {
634 $mask |= $Bits{'all'} ;
635 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
638 # Empty @_ is equivalent to @_ = 'all' ;
639 ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
647 my $mask = ${^WARNING_BITS} ;
649 if (vec($mask, $Offsets{'all'}, 1)) {
650 $mask |= $Bits{'all'} ;
651 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
654 push @_, 'all' unless @_;
656 foreach my $word ( @_ ) {
657 if ($word eq 'FATAL') {
660 elsif ($catmask = $Bits{$word}) {
661 $mask &= ~($catmask | $DeadBits{$word} | $All);
664 { Croaker("Unknown warnings category '$word'")}
667 ${^WARNING_BITS} = $mask ;
670 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
672 sub MESSAGE () { 4 };
682 my $has_message = $wanted & MESSAGE;
684 unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
685 my $sub = (caller 1)[3];
686 my $syntax = $has_message ? "[category,] 'message'" : '[category]';
687 Croaker("Usage: $sub($syntax)");
690 my $message = pop if $has_message;
693 # check the category supplied.
695 if (my $type = ref $category) {
696 Croaker("not an object")
697 if exists $builtin_type{$type};
701 $offset = $Offsets{$category};
702 Croaker("Unknown warnings category '$category'")
703 unless defined $offset;
706 $category = (caller(1))[0] ;
707 $offset = $Offsets{$category};
708 Croaker("package '$category' not registered for warnings")
709 unless defined $offset ;
717 while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
718 last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
723 $i = _error_loc(); # see where Carp will allocate the error
726 # Defaulting this to 0 reduces complexity in code paths below.
727 my $callers_bitmask = (caller($i))[9] || 0 ;
730 foreach my $type (FATAL, NORMAL) {
731 next unless $wanted & $type;
733 push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
734 vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
737 # &enabled and &fatal_enabled
738 return $results[0] unless $has_message;
740 # &warnif, and the category is neither enabled as warning nor as fatal
741 return if $wanted == (NORMAL | FATAL | MESSAGE)
742 && !($results[0] || $results[1]);
745 Carp::croak($message) if $results[0];
746 # will always get here for &warn. will only get here for &warnif if the
747 # category is enabled
748 Carp::carp($message);
756 vec($mask, $bit, 1) = 1;
760 sub register_categories
764 for my $name (@names) {
765 if (! defined $Bits{$name}) {
766 $Bits{$name} = _mkMask($LAST_BIT);
767 vec($Bits{'all'}, $LAST_BIT, 1) = 1;
768 $Offsets{$name} = $LAST_BIT ++;
769 foreach my $k (keys %Bits) {
770 vec($Bits{$k}, $LAST_BIT, 1) = 0;
772 $DeadBits{$name} = _mkMask($LAST_BIT);
773 vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
780 goto &Carp::short_error_loc; # don't introduce another stack frame
785 return __chk(NORMAL, @_);
790 return __chk(FATAL, @_);
795 return __chk(FATAL | MESSAGE, @_);
800 return __chk(NORMAL | FATAL | MESSAGE, @_);
803 # These are not part of any public interface, so we can delete them to save
805 delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);