This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make dquote_static.c available to ext/re/
[perl5.git] / warnings.pl
index 2f987c5..c201734 100644 (file)
@@ -1,6 +1,19 @@
 #!/usr/bin/perl
+# 
+# Regenerate (overwriting only if changed):
+#
+#    lib/warnings.pm
+#    warnings.h
+#
+# from information hardcoded into this script (the $tree hash), plus the
+# template for warnings.pm in the DATA section.
+#
+# With an argument of 'tree', just dump the contents of $tree and exits.
+# Also accepts the standard regen_lib -q and -v args.
+#
+# This script is normally invoked from regen.pl.
 
-$VERSION = '1.02_02';
+$VERSION = '1.02_03';
 
 BEGIN {
     require 'regen_lib.pl';
@@ -33,6 +46,7 @@ my $tree = {
                                        'printf'        => [ 5.008, DEFAULT_OFF],
                                        'prototype'     => [ 5.008, DEFAULT_OFF],
                                        'qw'            => [ 5.008, DEFAULT_OFF],
+                                'illegalproto'  => [ 5.011, DEFAULT_OFF],
                           }],
                'severe'        => [ 5.008, {   
                                'inplace'       => [ 5.008, DEFAULT_ON],
@@ -81,7 +95,6 @@ my %list ;
 my %Value ;
 my %ValueToName ;
 my %NameToValue ;
-my $index ;
 
 my %v_list = () ;
 
@@ -131,7 +144,6 @@ sub walk
     foreach $k (sort keys %$tre) {
        $v = $tre->{$k};
        die "duplicate key $k\n" if defined $list{$k} ;
-       #$Value{$index} = uc $k ;
        die "Can't find key '$k'"
            if ! defined $NameToValue{uc $k} ;
         push @{ $list{$k} }, $NameToValue{uc $k} ;
@@ -154,13 +166,13 @@ sub mkRange
 {
     my @a = @_ ;
     my @out = @a ;
-    my $i ;
-
 
-    for ($i = 1 ; $i < @a; ++ $i) {
+    for my $i (1 .. @a - 1) {
        $out[$i] = ".."
-          if $a[$i] == $a[$i - 1] + 1 && $a[$i] + 1 == $a[$i + 1] ;
+          if $a[$i] == $a[$i - 1] + 1
+             && ($i >= @a  - 1 || $a[$i] + 1 == $a[$i + 1] );
     }
+    $out[-1] = $a[-1] if $out[-1] eq "..";
 
     my $out = join(",",@out);
 
@@ -286,8 +298,6 @@ EOM
 
 my $offset = 0 ;
 
-$index = $offset ;
-#@{ $list{"all"} } = walk ($tree) ;
 valueWalk ($tree) ;
 my $index = orderValues();
 
@@ -340,6 +350,8 @@ print $warn <<'EOM';
 #define ckWARN3_d(w1,w2,w3)    Perl_ckwarn_d(aTHX_ packWARN3(w1,w2,w3))
 #define ckWARN4_d(w1,w2,w3,w4) Perl_ckwarn_d(aTHX_ packWARN4(w1,w2,w3,w4))
 
+#define WARNshift              8
+
 #define packWARN(a)            (a                                      )
 #define packWARN2(a,b)         ((a) | ((b)<<8)                         )
 #define packWARN3(a,b,c)       ((a) | ((b)<<8) | ((c)<<16)             )
@@ -370,8 +382,6 @@ while (<DATA>) {
     print $pm $_ ;
 }
 
-#$list{'all'} = [ $offset .. 8 * ($warn_size/2) - 1 ] ;
-
 $last_ver = 0;
 print $pm "our %Offsets = (\n" ;
 foreach my $k (sort { $a <=> $b } keys %ValueToName) {
@@ -396,7 +406,6 @@ foreach $k (sort keys  %list) {
     my @list = sort { $a <=> $b } @$v ;
 
     print $pm tab(4, "    '$k'"), '=> "',
-               # mkHex($warn_size, @list),
                mkHex($warn_size, map $_ * 2 , @list),
                '", # [', mkRange(@list), "]\n" ;
 }
@@ -410,7 +419,6 @@ foreach $k (sort keys  %list) {
     my @list = sort { $a <=> $b } @$v ;
 
     print $pm tab(4, "    '$k'"), '=> "',
-               # mkHex($warn_size, @list),
                mkHex($warn_size, map $_ * 2 + 1 , @list),
                '", # [', mkRange(@list), "]\n" ;
 }
@@ -436,7 +444,7 @@ __END__
 
 package warnings;
 
-our $VERSION = '1.06';
+our $VERSION = '1.11';
 
 # Verify that we're called correctly so that warnings will work.
 # see also strict.pm.
@@ -514,6 +522,27 @@ Return TRUE if that warnings category is enabled in the first scope
 where the object is used.
 Otherwise returns FALSE.
 
+=item warnings::fatal_enabled()
+
+Return TRUE if the warnings category with the same name as the current
+package has been set to FATAL in the calling module.
+Otherwise returns FALSE.
+
+=item warnings::fatal_enabled($category)
+
+Return TRUE if the warnings category C<$category> has been set to FATAL in
+the calling module.
+Otherwise returns FALSE.
+
+=item warnings::fatal_enabled($object)
+
+Use the name of the class for the object reference, C<$object>, as the
+warnings category.
+
+Return TRUE if that warnings category has been set to FATAL in the first
+scope where the object is used.
+Otherwise returns FALSE.
+
 =item warnings::warn($message)
 
 Print C<$message> to STDERR.
@@ -562,6 +591,11 @@ Equivalent to:
     if (warnings::enabled($object))
       { warnings::warn($object, $message) }
 
+=item warnings::register_categories(@names)
+
+This registers warning categories for the given names and is primarily for
+use by the warnings::register pragma, for which see L<perllexwarn>.
+
 =back
 
 See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
@@ -574,19 +608,14 @@ $All = "" ; vec($All, $Offsets{'all'}, 2) = 3 ;
 
 sub Croaker
 {
-    require Carp::Heavy; # this initializes %CarpInternal
+    require Carp; # this initializes %CarpInternal
     local $Carp::CarpInternal{'warnings'};
     delete $Carp::CarpInternal{'warnings'};
     Carp::croak(@_);
 }
 
-sub bits
-{
-    # called from B::Deparse.pm
-
-    push @_, 'all' unless @_;
-
-    my $mask;
+sub _bits {
+    my $mask = shift ;
     my $catmask ;
     my $fatal = 0 ;
     my $no_fatal = 0 ;
@@ -612,14 +641,17 @@ sub bits
     return $mask ;
 }
 
+sub bits
+{
+    # called from B::Deparse.pm
+    push @_, 'all' unless @_ ;
+    return _bits(undef, @_) ;
+}
+
 sub import 
 {
     shift;
 
-    my $catmask ;
-    my $fatal = 0 ;
-    my $no_fatal = 0 ;
-
     my $mask = ${^WARNING_BITS} ;
 
     if (vec($mask, $Offsets{'all'}, 1)) {
@@ -627,27 +659,8 @@ sub import
         $mask |= $DeadBits{'all'} if vec($mask, $Offsets{'all'}+1, 1);
     }
     
-    push @_, 'all' unless @_;
-
-    foreach my $word ( @_ ) {
-       if ($word eq 'FATAL') {
-           $fatal = 1;
-           $no_fatal = 0;
-       }
-       elsif ($word eq 'NONFATAL') {
-           $fatal = 0;
-           $no_fatal = 1;
-       }
-       elsif ($catmask = $Bits{$word}) {
-           $mask |= $catmask ;
-           $mask |= $DeadBits{$word} if $fatal ;
-           $mask &= ~($DeadBits{$word}|$All) if $no_fatal ;
-       }
-       else
-          { Croaker("Unknown warnings category '$word'")}
-    }
-
-    ${^WARNING_BITS} = $mask ;
+    # Empty @_ is equivalent to @_ = 'all' ;
+    ${^WARNING_BITS} = @_ ? _bits($mask, @_) : $mask | $Bits{all} ;
 }
 
 sub unimport 
@@ -680,11 +693,25 @@ sub unimport
 
 my %builtin_type; @builtin_type{qw(SCALAR ARRAY HASH CODE REF GLOB LVALUE Regexp)} = ();
 
+sub MESSAGE () { 4 };
+sub FATAL () { 2 };
+sub NORMAL () { 1 };
+
 sub __chk
 {
     my $category ;
     my $offset ;
     my $isobj = 0 ;
+    my $wanted = shift;
+    my $has_message = $wanted & MESSAGE;
+
+    unless (@_ == 1 || @_ == ($has_message ? 2 : 0)) {
+       my $sub = (caller 1)[3];
+       my $syntax = $has_message ? "[category,] 'message'" : '[category]';
+       Croaker("Usage: $sub($syntax)");
+    }
+
+    my $message = pop if $has_message;
 
     if (@_) {
         # check the category supplied.
@@ -706,11 +733,11 @@ sub __chk
            unless defined $offset ;
     }
 
-    my $this_pkg = (caller(1))[0] ;
-    my $i = 2 ;
-    my $pkg ;
+    my $i;
 
     if ($isobj) {
+        my $pkg;
+        $i = 2;
         while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
             last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
         }
@@ -720,61 +747,85 @@ sub __chk
         $i = _error_loc(); # see where Carp will allocate the error
     }
 
-    my $callers_bitmask = (caller($i))[9] ;
-    return ($callers_bitmask, $offset, $i) ;
-}
+    # Defaulting this to 0 reduces complexity in code paths below.
+    my $callers_bitmask = (caller($i))[9] || 0 ;
 
-sub _error_loc {
-    require Carp::Heavy;
-    goto &Carp::short_error_loc; # don't introduce another stack frame
-}                                                             
+    my @results;
+    foreach my $type (FATAL, NORMAL) {
+       next unless $wanted & $type;
 
-sub enabled
-{
-    Croaker("Usage: warnings::enabled([category])")
-       unless @_ == 1 || @_ == 0 ;
+       push @results, (vec($callers_bitmask, $offset + $type - 1, 1) ||
+                       vec($callers_bitmask, $Offsets{'all'} + $type - 1, 1));
+    }
 
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
+    # &enabled and &fatal_enabled
+    return $results[0] unless $has_message;
 
-    return 0 unless defined $callers_bitmask ;
-    return vec($callers_bitmask, $offset, 1) ||
-           vec($callers_bitmask, $Offsets{'all'}, 1) ;
+    # &warnif, and the category is neither enabled as warning nor as fatal
+    return if $wanted == (NORMAL | FATAL | MESSAGE)
+       && !($results[0] || $results[1]);
+
+    require Carp;
+    Carp::croak($message) if $results[0];
+    # will always get here for &warn. will only get here for &warnif if the
+    # category is enabled
+    Carp::carp($message);
 }
 
+sub _mkMask
+{
+    my ($bit) = @_;
+    my $mask = "";
 
-sub warn
+    vec($mask, $bit, 1) = 1;
+    return $mask;
+}
+
+sub register_categories
 {
-    Croaker("Usage: warnings::warn([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
+    my @names = @_;
+
+    for my $name (@names) {
+       if (! defined $Bits{$name}) {
+           $Bits{$name}     = _mkMask($LAST_BIT);
+           vec($Bits{'all'}, $LAST_BIT, 1) = 1;
+           $Offsets{$name}  = $LAST_BIT ++;
+           foreach my $k (keys %Bits) {
+               vec($Bits{$k}, $LAST_BIT, 1) = 0;
+           }
+           $DeadBits{$name} = _mkMask($LAST_BIT);
+           vec($DeadBits{'all'}, $LAST_BIT++, 1) = 1;
+       }
+    }
+}
 
-    my $message = pop ;
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
+sub _error_loc {
     require Carp;
-    Carp::croak($message)
-       if vec($callers_bitmask, $offset+1, 1) ||
-          vec($callers_bitmask, $Offsets{'all'}+1, 1) ;
-    Carp::carp($message) ;
+    goto &Carp::short_error_loc; # don't introduce another stack frame
 }
 
-sub warnif
+sub enabled
 {
-    Croaker("Usage: warnings::warnif([category,] 'message')")
-       unless @_ == 2 || @_ == 1 ;
-
-    my $message = pop ;
-    my ($callers_bitmask, $offset, $i) = __chk(@_) ;
+    return __chk(NORMAL, @_);
+}
 
-    return
-        unless defined $callers_bitmask &&
-               (vec($callers_bitmask, $offset, 1) ||
-               vec($callers_bitmask, $Offsets{'all'}, 1)) ;
+sub fatal_enabled
+{
+    return __chk(FATAL, @_);
+}
 
-    require Carp;
-    Carp::croak($message)
-       if vec($callers_bitmask, $offset+1, 1) ||
-          vec($callers_bitmask, $Offsets{'all'}+1, 1) ;
+sub warn
+{
+    return __chk(FATAL | MESSAGE, @_);
+}
 
-    Carp::carp($message) ;
+sub warnif
+{
+    return __chk(NORMAL | FATAL | MESSAGE, @_);
 }
 
+# These are not part of any public interface, so we can delete them to save
+# space.
+delete $warnings::{$_} foreach qw(NORMAL FATAL MESSAGE);
+
 1;