By grabbing the length, we can use sv_setpvn here.
[p5sagit/p5-mst-13.2.git] / embed.pl
index 1d816b1..778090f 100755 (executable)
--- a/embed.pl
+++ b/embed.pl
@@ -18,12 +18,13 @@ BEGIN {
 sub do_not_edit ($)
 {
     my $file = shift;
-    
+
     my $years = '1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005';
 
     $years =~ s/1999,/1999,\n  / if length $years > 40;
 
     my $warning = <<EOW;
+ -*- buffer-read-only: t -*-
 
    $file
 
@@ -79,7 +80,7 @@ sub walk_table (&@) {
        $F = $filename;
     }
     else {
-       safer_unlink $filename;
+       safer_unlink $filename if $filename ne '/dev/null';
        open F, ">$filename" or die "Can't open $filename: $!";
        binmode F;
        $F = \*F;
@@ -101,8 +102,8 @@ sub walk_table (&@) {
        else {
            @args = split /\s*\|\s*/, $_;
        }
-        my @outs = &{$function}(@args);
-        print $F @outs; # $function->(@args) is not 5.003
+       my @outs = &{$function}(@args);
+       print $F @outs; # $function->(@args) is not 5.003
     }
     print $F $trailer if $trailer;
     unless (ref $filename) {
@@ -113,14 +114,14 @@ sub walk_table (&@) {
 sub munge_c_files () {
     my $functions = {};
     unless (@ARGV) {
-        warn "\@ARGV empty, nothing to do\n";
+       warn "\@ARGV empty, nothing to do\n";
        return;
     }
     walk_table {
        if (@_ > 1) {
            $functions->{$_[2]} = \@_ if $_[@_-1] =~ /\.\.\./;
        }
-    } '/dev/null', '';
+    } '/dev/null', '', '';
     local $^I = '.bak';
     while (<>) {
 #      if (/^#\s*include\s+"perl.h"/) {
@@ -172,6 +173,8 @@ sub write_protos {
     }
     else {
        my ($flags,$retval,$func,@args) = @_;
+       my @nonnull;
+       my $has_context = ( $flags !~ /n/ );
        $ret .= '/* ' if $flags =~ /m/;
        if ($flags =~ /s/) {
            $retval = "STATIC $retval";
@@ -184,27 +187,53 @@ sub write_protos {
            }
        }
        $ret .= "$retval\t$func(";
-       unless ($flags =~ /n/) {
-           $ret .= "pTHX";
-           $ret .= "_ " if @args;
+       if ( $has_context ) {
+           $ret .= @args ? "pTHX_ " : "pTHX";
        }
        if (@args) {
+           my $n;
+           for my $arg ( @args ) {
+               ++$n;
+               push( @nonnull, $n ) if ( $arg =~ s/\s*\bNN\b\s+// );
+               $arg =~ s/\s*\bNULLOK\b\s+//; # strip NULLOK with no effect
+           }
            $ret .= join ", ", @args;
        }
        else {
-           $ret .= "void" if $flags =~ /n/;
+           $ret .= "void" if !$has_context;
        }
        $ret .= ")";
-       $ret .= " __attribute__((noreturn))" if $flags =~ /r/;
+       my @attrs;
+       if ( $flags =~ /r/ ) {
+           push @attrs, "__attribute__noreturn__";
+       }
+       if ( $flags =~ /a/ ) {
+           push @attrs, "__attribute__malloc__";
+           $flags .= "R"; # All allocing must check return value
+       }
+       if ( $flags =~ /R/ ) {
+           push @attrs, "__attribute__warn_unused_result__";
+       }
+       if ( $flags =~ /P/ ) {
+           push @attrs, "__attribute__pure__";
+       }
        if( $flags =~ /f/ ) {
-           my $prefix = $flags =~ /n/ ? '' : 'pTHX_';
+           my $prefix = $has_context ? 'pTHX_' : '';
            my $args = scalar @args;
-           $ret .= sprintf "\n\t__attribute__format__(__printf__,%s%d,%s%d)",
+           push @attrs, sprintf "__attribute__format__(__printf__,%s%d,%s%d)",
                                    $prefix, $args - 1, $prefix, $args;
        }
+       if ( @nonnull ) {
+           my @pos = map { $has_context ? "pTHX_$_" : $_ } @nonnull;
+           push @attrs, map { sprintf( "__attribute__nonnull__(%s)", $_ ) } @pos;
+       }
+       if ( @attrs ) {
+           $ret .= "\n";
+           $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
+       }
        $ret .= ";";
        $ret .= ' */' if $flags =~ /m/;
-       $ret .= "\n";
+       $ret .= @attrs ? "\n\n" : "\n";
     }
     $ret;
 }
@@ -223,20 +252,20 @@ sub write_global_sym {
     $ret;
 }
 
-walk_table(\&write_protos,     "proto.h", undef);
-walk_table(\&write_global_sym, "global.sym", undef);
+walk_table(\&write_protos,     "proto.h", undef, "/* ex: set ro: */\n");
+walk_table(\&write_global_sym, "global.sym", undef, "# ex: set ro:\n");
 
 # XXX others that may need adding
 #       warnhook
 #       hints
 #       copline
 my @extvars = qw(sv_undef sv_yes sv_no na dowarn
-                 curcop compiling
-                 tainting tainted stack_base stack_sp sv_arenaroot
+                curcop compiling
+                tainting tainted stack_base stack_sp sv_arenaroot
                 no_modify
-                 curstash DBsub DBsingle DBassertion debstash
-                 rsfp
-                 stdingv
+                curstash DBsub DBsingle DBassertion debstash
+                rsfp
+                stdingv
                 defgv
                 errgv
                 rsfp_filters
@@ -344,8 +373,19 @@ print EM do_not_edit ("embed.h"), <<'END';
 
 END
 
+# Try to elimiate lots of repeated
+# #ifdef PERL_CORE
+# foo
+# #endif
+# #ifdef PERL_CORE
+# bar
+# #endif
+# by tracking state and merging foo and bar into one block.
+my $ifdef_state = '';
+
 walk_table {
     my $ret = "";
+    my $new_ifdef_state = '';
     if (@_ == 1) {
        my $arg = shift;
        $ret .= "$arg\n" if $arg =~ /^#\s*(if|ifn?def|else|endif)\b/;
@@ -362,15 +402,31 @@ walk_table {
        }
        if ($ret ne '' && $flags !~ /A/) {
            if ($flags =~ /E/) {
-               $ret = "#if defined(PERL_CORE) || defined(PERL_EXT)\n$ret#endif\n";
-           } else {
-               $ret = "#ifdef PERL_CORE\n$ret#endif\n";
+               $new_ifdef_state
+                   = "#if defined(PERL_CORE) || defined(PERL_EXT)\n";
+           }
+           else {
+               $new_ifdef_state = "#ifdef PERL_CORE\n";
+           }
+
+           if ($new_ifdef_state ne $ifdef_state) {
+               $ret = $new_ifdef_state . $ret;
            }
         }
     }
+    if ($ifdef_state && $new_ifdef_state ne $ifdef_state) {
+       # Close the old one ahead of opening the new one.
+       $ret = "#endif\n$ret";
+    }
+    # Remember the new state.
+    $ifdef_state = $new_ifdef_state;
     $ret;
 } \*EM, "";
 
+if ($ifdef_state) {
+    print EM "#endif\n";
+}
+
 for $sym (sort keys %ppsym) {
     $sym =~ s/^Perl_//;
     print EM hide($sym, "Perl_$sym");
@@ -384,8 +440,10 @@ END
 
 my @az = ('a'..'z');
 
+$ifdef_state = '';
 walk_table {
     my $ret = "";
+    my $new_ifdef_state = '';
     if (@_ == 1) {
        my $arg = shift;
        $ret .= "$arg\n" if $arg =~ /^#\s*(if|ifn?def|else|endif)\b/;
@@ -420,17 +478,33 @@ walk_table {
                $ret .= $alist . ")\n";
            }
        }
-         unless ($flags =~ /A/) {
+       unless ($flags =~ /A/) {
            if ($flags =~ /E/) {
-               $ret = "#if defined(PERL_CORE) || defined(PERL_EXT)\n$ret#endif\n";
-           } else {
-               $ret = "#ifdef PERL_CORE\n$ret#endif\n";
+               $new_ifdef_state
+                   = "#if defined(PERL_CORE) || defined(PERL_EXT)\n";
+           }
+           else {
+               $new_ifdef_state = "#ifdef PERL_CORE\n";
+           }
+
+           if ($new_ifdef_state ne $ifdef_state) {
+               $ret = $new_ifdef_state . $ret;
            }
         }
     }
+    if ($ifdef_state && $new_ifdef_state ne $ifdef_state) {
+       # Close the old one ahead of opening the new one.
+       $ret = "#endif\n$ret";
+    }
+    # Remember the new state.
+    $ifdef_state = $new_ifdef_state;
     $ret;
 } \*EM, "";
 
+if ($ifdef_state) {
+    print EM "#endif\n";
+}
+
 for $sym (sort keys %ppsym) {
     $sym =~ s/^Perl_//;
     if ($sym =~ /^ck_/) {
@@ -529,6 +603,7 @@ print EM <<'END';
 #  define Perl_sv_setpvf_mg_nocontext  Perl_sv_setpvf_mg
 #endif
 
+/* ex: set ro: */
 END
 
 close(EM) or die "Error closing EM: $!";
@@ -638,6 +713,8 @@ for $sym (sort @extvars) {
 print EM <<'END';
 
 #endif /* PERL_POLLUTE */
+
+/* ex: set ro: */
 END
 
 close(EM) or die "Error closing EM: $!";
@@ -718,10 +795,27 @@ EXTCONST void * const PL_force_link_funcs[] = {
 #define PERLVARIC(v,t,i) PERLVAR(v,t)
 #define PERLVARISC(v,i) PERLVAR(v,char)
 
+/* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
+ * cannot cast between void pointers and function pointers without
+ * info level warnings.  The PL_force_link_funcs[] would cause a few
+ * hundred of those warnings.  In code one can circumnavigate this by using
+ * unions that overlay the different pointers, but in declarations one
+ * cannot use this trick.  Therefore we just disable the warning here
+ * for the duration of the PL_force_link_funcs[] declaration. */
+
+#if defined(__DECC) && defined(__osf__)
+#pragma message save
+#pragma message disable (nonstandcast)
+#endif
+
 #include "thrdvar.h"
 #include "intrpvar.h"
 #include "perlvars.h"
 
+#if defined(__DECC) && defined(__osf__)
+#pragma message restore
+#endif
+
 #undef PERLVAR
 #undef PERLVARA
 #undef PERLVARI
@@ -757,6 +851,7 @@ print CAPIH <<'EOT';
 
 #endif /* __perlapi_h__ */
 
+/* ex: set ro: */
 EOT
 close CAPIH or die "Error closing CAPIH: $!";
 
@@ -833,6 +928,8 @@ unsigned char** Perl_Gfold_locale_ptr(pTHX) {
 END_EXTERN_C
 
 #endif /* MULTIPLICITY */
+
+/* ex: set ro: */
 EOT
 
 close(CAPI) or die "Error closing CAPI: $!";
@@ -857,3 +954,5 @@ my %vfuncs = qw(
     Perl_dump_indent           Perl_dump_vindent
     Perl_default_protect       Perl_vdefault_protect
 );
+
+# ex: set ts=8 sts=4 sw=4 noet: