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 ],
90 'experimental::regex_sets' =>
91 [ 5.017, DEFAULT_ON ],
94 #'default' => [ 5.008, DEFAULT_ON ],
112 foreach $k (sort keys %$tre) {
114 die "duplicate key $k\n" if defined $list{$k} ;
115 die "Value associated with key '$k' is not an ARRAY reference"
116 if !ref $v || ref $v ne 'ARRAY' ;
118 my ($ver, $rest) = @{ $v } ;
119 push @{ $v_list{$ver} }, $k;
122 { valueWalk ($rest) }
131 foreach my $ver ( sort { $a <=> $b } keys %v_list ) {
132 foreach my $name (@{ $v_list{$ver} } ) {
133 $ValueToName{ $index } = [ uc $name, $ver ] ;
134 $NameToValue{ uc $name } = $index ++ ;
141 ###########################################################################
149 foreach $k (sort keys %$tre) {
151 die "duplicate key $k\n" if defined $list{$k} ;
152 die "Can't find key '$k'"
153 if ! defined $NameToValue{uc $k} ;
154 push @{ $list{$k} }, $NameToValue{uc $k} ;
155 die "Value associated with key '$k' is not an ARRAY reference"
156 if !ref $v || ref $v ne 'ARRAY' ;
158 my ($ver, $rest) = @{ $v } ;
160 { push (@{ $list{$k} }, walk ($rest)) }
161 elsif ($rest == DEFAULT_ON)
162 { push @def, $NameToValue{uc $k} }
164 push @list, @{ $list{$k} } ;
170 ###########################################################################
177 for my $i (1 .. @a - 1) {
179 if $a[$i] == $a[$i - 1] + 1
180 && ($i >= @a - 1 || $a[$i] + 1 == $a[$i + 1] );
182 $out[-1] = $a[-1] if $out[-1] eq "..";
184 my $out = join(",",@out);
186 $out =~ s/,(\.\.,)+/../g ;
190 ###########################################################################
197 my $max = (sort {$a <=> $b} map { length $_ } keys %$tre)[-1] ;
198 my @keys = sort keys %$tre ;
200 while ($k = shift @keys) {
202 die "Value associated with key '$k' is not an ARRAY reference"
203 if !ref $v || ref $v ne 'ARRAY' ;
207 print $prefix . "|\n" ;
208 print $prefix . "+- $k" ;
209 $offset = ' ' x ($max + 4) ;
212 print $prefix . "$k" ;
213 $offset = ' ' x ($max + 1) ;
216 my ($ver, $rest) = @{ $v } ;
219 my $bar = @keys ? "|" : " ";
220 print " -" . "-" x ($max - length $k ) . "+\n" ;
221 printTree ($rest, $prefix . $bar . $offset )
229 ###########################################################################
233 my ($f, $max, @a) = @_ ;
234 my $mask = "\x00" x $max ;
238 vec($mask, $_, 1) = 1 ;
241 foreach (unpack("C*", $mask)) {
243 $string .= '\x' . sprintf("%2.2x", $_)
246 $string .= '\\' . sprintf("%o", $_)
255 return mkHexOct("x", $max, @a);
261 return mkHexOct("o", $max, @a);
264 ###########################################################################
266 if (@ARGV && $ARGV[0] eq "tree")
268 printTree($tree, " ") ;
272 my ($warn, $pm) = map {
273 open_new($_, '>', { by => 'regen/warnings.pl' });
274 } 'warnings.h', 'lib/warnings.pm';
278 #define Off(x) ((x) / 8)
279 #define Bit(x) (1 << ((x) % 8))
280 #define IsSet(a, x) ((a)[Off(x)] & Bit(x))
283 #define G_WARN_OFF 0 /* $^W == 0 */
284 #define G_WARN_ON 1 /* -w flag and $^W != 0 */
285 #define G_WARN_ALL_ON 2 /* -W flag */
286 #define G_WARN_ALL_OFF 4 /* -X flag */
287 #define G_WARN_ONCE 8 /* set if 'once' ever enabled */
288 #define G_WARN_ALL_MASK (G_WARN_ALL_ON|G_WARN_ALL_OFF)
290 #define pWARN_STD NULL
291 #define pWARN_ALL (((STRLEN*)0)+1) /* use warnings 'all' */
292 #define pWARN_NONE (((STRLEN*)0)+2) /* no warnings 'all' */
294 #define specialWARN(x) ((x) == pWARN_STD || (x) == pWARN_ALL || \
297 /* if PL_warnhook is set to this value, then warnings die */
298 #define PERL_WARNHOOK_FATAL (&PL_sv_placeholder)
304 my $index = orderValues();
306 die <<EOM if $index > 255 ;
307 Too many warnings categories -- max is 255
308 rewrite packWARN* & unpackWARN* macros
314 my $warn_size = int($index / 8) + ($index % 8 != 0) ;
318 foreach $k (sort { $a <=> $b } keys %ValueToName) {
319 my ($name, $version) = @{ $ValueToName{$k} };
320 print $warn "\n/* Warnings Categories added in Perl $version */\n\n"
321 if $last_ver != $version ;
323 print $warn tab(5, "#define WARN_$name"), " $k\n" ;
324 $last_ver = $version ;
328 print $warn tab(5, '#define WARNsize'), "$warn_size\n" ;
329 #print WARN tab(5, '#define WARN_ALLstring'), '"', ('\377' x $warn_size) , "\"\n" ;
330 print $warn tab(5, '#define WARN_ALLstring'), '"', ('\125' x $warn_size) , "\"\n" ;
331 print $warn tab(5, '#define WARN_NONEstring'), '"', ('\0' x $warn_size) , "\"\n" ;
335 #define isLEXWARN_on (PL_curcop->cop_warnings != pWARN_STD)
336 #define isLEXWARN_off (PL_curcop->cop_warnings == pWARN_STD)
337 #define isWARN_ONCE (PL_dowarn & (G_WARN_ON|G_WARN_ONCE))
338 #define isWARN_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)))
339 #define isWARNf_on(c,x) (IsSet((U8 *)(c + 1), 2*(x)+1))
341 #define DUP_WARNINGS(p) \
342 (specialWARN(p) ? (STRLEN*)(p) \
343 : (STRLEN*)CopyD(p, PerlMemShared_malloc(sizeof(*p)+*p), sizeof(*p)+*p, \
346 #define ckWARN(w) Perl_ckwarn(aTHX_ packWARN(w))
347 #define ckWARN2(w1,w2) Perl_ckwarn(aTHX_ packWARN2(w1,w2))
348 #define ckWARN3(w1,w2,w3) Perl_ckwarn(aTHX_ packWARN3(w1,w2,w3))
349 #define ckWARN4(w1,w2,w3,w4) Perl_ckwarn(aTHX_ packWARN4(w1,w2,w3,w4))
351 #define ckWARN_d(w) Perl_ckwarn_d(aTHX_ packWARN(w))
352 #define ckWARN2_d(w1,w2) Perl_ckwarn_d(aTHX_ packWARN2(w1,w2))
353 #define ckWARN3_d(w1,w2,w3) Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3))
354 #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4))
358 #define packWARN(a) (a )
359 #define packWARN2(a,b) ((a) | ((b)<<8) )
360 #define packWARN3(a,b,c) ((a) | ((b)<<8) | ((c)<<16) )
361 #define packWARN4(a,b,c,d) ((a) | ((b)<<8) | ((c)<<16) | ((d) <<24))
363 #define unpackWARN1(x) ((x) & 0xFF)
364 #define unpackWARN2(x) (((x) >>8) & 0xFF)
365 #define unpackWARN3(x) (((x) >>16) & 0xFF)
366 #define unpackWARN4(x) (((x) >>24) & 0xFF)
369 ( ! specialWARN(PL_curcop->cop_warnings) && \
370 ( isWARNf_on(PL_curcop->cop_warnings, WARN_ALL) || \
371 isWARNf_on(PL_curcop->cop_warnings, unpackWARN1(x)) || \
372 isWARNf_on(PL_curcop->cop_warnings, unpackWARN2(x)) || \
373 isWARNf_on(PL_curcop->cop_warnings, unpackWARN3(x)) || \
374 isWARNf_on(PL_curcop->cop_warnings, unpackWARN4(x))))
376 /* end of file warnings.h */
379 read_only_bottom_close_and_rename($warn);
382 last if /^KEYWORDS$/ ;
387 print $pm "our %Offsets = (\n" ;
388 foreach my $k (sort { $a <=> $b } keys %ValueToName) {
389 my ($name, $version) = @{ $ValueToName{$k} };
392 if ( $last_ver != $version ) {
394 print $pm tab(4, " # Warnings Categories added in Perl $version");
397 print $pm tab(4, " '$name'"), "=> $k,\n" ;
398 $last_ver = $version;
401 print $pm " );\n\n" ;
403 print $pm "our %Bits = (\n" ;
404 foreach $k (sort keys %list) {
407 my @list = sort { $a <=> $b } @$v ;
409 print $pm tab(4, " '$k'"), '=> "',
410 mkHex($warn_size, map $_ * 2 , @list),
411 '", # [', mkRange(@list), "]\n" ;
414 print $pm " );\n\n" ;
416 print $pm "our %DeadBits = (\n" ;
417 foreach $k (sort keys %list) {
420 my @list = sort { $a <=> $b } @$v ;
422 print $pm tab(4, " '$k'"), '=> "',
423 mkHex($warn_size, map $_ * 2 + 1 , @list),
424 '", # [', mkRange(@list), "]\n" ;
427 print $pm " );\n\n" ;
428 print $pm '$NONE = "', ('\0' x $warn_size) , "\";\n" ;
429 print $pm '$DEFAULT = "', mkHex($warn_size, map $_ * 2, @def),
430 '", # [', mkRange(@def), "]\n" ;
431 print $pm '$LAST_BIT = ' . "$index ;\n" ;
432 print $pm '$BYTES = ' . "$warn_size ;\n" ;
437 read_only_bottom_close_and_rename($pm);
442 our $VERSION = '1.16';
444 # Verify that we're called correctly so that warnings will work.
445 # see also strict.pm.
446 unless ( __FILE__ =~ /(^|[\/\\])\Q${\__PACKAGE__}\E\.pmc?$/ ) {
447 my (undef, $f, $l) = caller;
448 die("Incorrect use of pragma '${\__PACKAGE__}' at $f line $l.\n");
453 warnings - Perl pragma to control optional warnings
463 use warnings::register;
464 if (warnings::enabled()) {
465 warnings::warn("some warning");
468 if (warnings::enabled("void")) {
469 warnings::warn("void", "some warning");
472 if (warnings::enabled($object)) {
473 warnings::warn($object, "some warning");
476 warnings::warnif("some warning");
477 warnings::warnif("void", "some warning");
478 warnings::warnif($object, "some warning");
482 The C<warnings> pragma is a replacement for the command line flag C<-w>,
483 but the pragma is limited to the enclosing block, while the flag is global.
484 See L<perllexwarn> for more information and the list of built-in warning
487 If no import list is supplied, all possible warnings are either enabled
490 A number of functions are provided to assist module authors.
494 =item use warnings::register
496 Creates a new warnings category with the same name as the package where
497 the call to the pragma is used.
499 =item warnings::enabled()
501 Use the warnings category with the same name as the current package.
503 Return TRUE if that warnings category is enabled in the calling module.
504 Otherwise returns FALSE.
506 =item warnings::enabled($category)
508 Return TRUE if the warnings category, C<$category>, is enabled in the
510 Otherwise returns FALSE.
512 =item warnings::enabled($object)
514 Use the name of the class for the object reference, C<$object>, as the
517 Return TRUE if that warnings category is enabled in the first scope
518 where the object is used.
519 Otherwise returns FALSE.
521 =item warnings::fatal_enabled()
523 Return TRUE if the warnings category with the same name as the current
524 package has been set to FATAL in the calling module.
525 Otherwise returns FALSE.
527 =item warnings::fatal_enabled($category)
529 Return TRUE if the warnings category C<$category> has been set to FATAL in
531 Otherwise returns FALSE.
533 =item warnings::fatal_enabled($object)
535 Use the name of the class for the object reference, C<$object>, as the
538 Return TRUE if that warnings category has been set to FATAL in the first
539 scope where the object is used.
540 Otherwise returns FALSE.
542 =item warnings::warn($message)
544 Print C<$message> to STDERR.
546 Use the warnings category with the same name as the current package.
548 If that warnings category has been set to "FATAL" in the calling module
549 then die. Otherwise return.
551 =item warnings::warn($category, $message)
553 Print C<$message> to STDERR.
555 If the warnings category, C<$category>, has been set to "FATAL" in the
556 calling module then die. Otherwise return.
558 =item warnings::warn($object, $message)
560 Print C<$message> to STDERR.
562 Use the name of the class for the object reference, C<$object>, as the
565 If that warnings category has been set to "FATAL" in the scope where C<$object>
566 is first used then die. Otherwise return.
569 =item warnings::warnif($message)
573 if (warnings::enabled())
574 { warnings::warn($message) }
576 =item warnings::warnif($category, $message)
580 if (warnings::enabled($category))
581 { warnings::warn($category, $message) }
583 =item warnings::warnif($object, $message)
587 if (warnings::enabled($object))
588 { warnings::warn($object, $message) }
590 =item warnings::register_categories(@names)
592 This registers warning categories for the given names and is primarily for
593 use by the warnings::register pragma, for which see L<perllexwarn>.
597 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
603 $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
607 require Carp; # this initializes %CarpInternal
608 local $Carp::CarpInternal{'warnings'};
609 delete $Carp::CarpInternal{'warnings'};
619 foreach my $word ( @_ ) {
620 if ($word eq 'FATAL') {
624 elsif ($word eq 'NONFATAL') {
628 elsif ($catmask = $Bits{$word}) {
630 $mask |= $DeadBits{$word} if $fatal ;
631 $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
634 { Croaker("Unknown warnings category '$word'")}
642 # called from B::Deparse.pm
643 push @_, 'all' unless @_ ;
644 return _bits(undef, @_) ;
651 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
653 if (vec($mask, $Offsets{'all'}, 1)) {
654 $mask |= $Bits{'all'} ;
655 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
658 # Empty @_ is equivalent to @_ = 'all' ;
659 ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
667 my $mask = ${^WARNING_BITS} // ($^W ? $Bits{all} : $DEFAULT) ;
669 if (vec($mask, $Offsets{'all'}, 1)) {
670 $mask |= $Bits{'all'} ;
671 $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
674 push @_, 'all' unless @_;
676 foreach my $word ( @_ ) {
677 if ($word eq 'FATAL') {
680 elsif ($catmask = $Bits{$word}) {
681 $mask &= ~($catmask | $DeadBits{$word} | $All);
684 { Croaker("Unknown warnings category '$word'")}
687 ${^WARNING_BITS} = $mask ;
690 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
692 sub MESSAGE () { 4 };
702 my $has_message = $wanted & MESSAGE;
704 unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
705 my $sub = (caller 1)[3];
706 my $syntax = $has_message ? "[category,] 'message'" : '[category]';
707 Croaker("Usage: $sub($syntax)");
710 my $message = pop if $has_message;
713 # check the category supplied.
715 if (my $type = ref $category) {
716 Croaker("not an object")
717 if exists $builtin_type{$type};
721 $offset = $Offsets{$category};
722 Croaker("Unknown warnings category '$category'")
723 unless defined $offset;
726 $category = (caller(1))[0] ;
727 $offset = $Offsets{$category};
728 Croaker("package '$category' not registered for warnings")
729 unless defined $offset ;
737 while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
738 last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
743 $i = _error_loc(); # see where Carp will allocate the error
746 # Default to 0 if caller returns nothing. Default to $DEFAULT if it
747 # explicitly returns undef.
748 my(@callers_bitmask) = (caller($i))[9] ;
749 my $callers_bitmask =
750 @callers_bitmask ? $callers_bitmask[0] // $DEFAULT : 0 ;
753 foreach my $type (FATAL, NORMAL) {
754 next unless $wanted & $type;
756 push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
757 vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
760 # &enabled and &fatal_enabled
761 return $results[0] unless $has_message;
763 # &warnif, and the category is neither enabled as warning nor as fatal
764 return if $wanted == (NORMAL | FATAL | MESSAGE)
765 && !($results[0] || $results[1]);
768 Carp::croak($message) if $results[0];
769 # will always get here for &warn. will only get here for &warnif if the
770 # category is enabled
771 Carp::carp($message);
779 vec($mask, $bit, 1) = 1;
783 sub register_categories
787 for my $name (@names) {
788 if (! defined $Bits{$name}) {
789 $Bits{$name} = _mkMask($LAST_BIT);
790 vec($Bits{'all'}, $LAST_BIT, 1) = 1;
791 $Offsets{$name} = $LAST_BIT ++;
792 foreach my $k (keys %Bits) {
793 vec($Bits{$k}, $LAST_BIT, 1) = 0;
795 $DeadBits{$name} = _mkMask($LAST_BIT);
796 vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
803 goto &Carp::short_error_loc; # don't introduce another stack frame
808 return __chk(NORMAL, @_);
813 return __chk(FATAL, @_);
818 return __chk(FATAL | MESSAGE, @_);
823 return __chk(NORMAL | FATAL | MESSAGE, @_);
826 # These are not part of any public interface, so we can delete them to save
828 delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);