1 ################################################################################
3 # ppptools.pl -- various utility functions
5 # WARNING: This will be called by old perls. You can't use modern constructs
8 ################################################################################
10 # Version 3.x, Copyright (C) 2004-2013, Marcus Holland-Moritz.
11 # Version 2.x, Copyright (C) 2001, Paul Marquess.
12 # Version 1.x, Copyright (C) 1999, Kenneth Albanowski.
14 # This program is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
17 ################################################################################
19 require './parts/inc/inctools';
23 eval { require File::Spec };
24 return $@ ? join('/', @_) : File::Spec->catfile(@_);
32 opendir DIR, $dir or die "cannot open directory $dir: $!\n";
33 my @files = grep { !-d && !/^\./ } readdir DIR; # no dirs or hidden files
36 return map { cat_file($dir, $_) } sort @files;
41 # Creates a hash with the keys being all symbols found in all the files in
42 # the input directory (default 'parts/todo'), and the values being the perl
43 # versions of each symbol.
45 my $dir = shift || 'parts/todo';
50 for $todo (all_files_in_dir($dir)) {
51 open TODO, $todo or die "cannot open $todo: $!\n";
59 /^\w+$/ or die "invalid identifier: $_\n";
60 exists $todo{$_} and die "duplicate identifier: $_ ($todo{$_} <=> $version)\n";
72 my($r, $v, $s) = parse_version($ver);
73 $r == 5 or die "only Perl revision 5 is supported\n";
74 my $bcdver = sprintf "0x%d%03d%03d", $r, $v, $s;
75 return "(PERL_BCDVERSION $op $bcdver)";
81 my $section = 'implementation';
83 my $vsec = join '|', qw( provides dontwarn implementation
84 xsubs xsinit xsmisc xshead xsboot tests );
88 open F, $file or die "$file: $!\n";
90 /[ \t]+$/ and warn "$file:$.: warning: trailing whitespace\n";
91 if ($section eq 'implementation') {
92 m!//! && !m!(?:=~|s/).*//! && !m!(?:ht|f)tp(?:s)://!
93 and warn "$file:$.: warning: potential C++ comment\n";
98 if (/^=($vsec)(?:\s+(.*))?/) {
102 $options{$section} = eval "{ $opt }";
103 $@ and die "$file:$.: invalid options ($opt) in section $section: $@\n";
107 push @{$data{$section}}, $_;
112 my @v = @{$data{$_}};
113 shift @v while @v && $v[0] =~ /^\s*$/;
114 pop @v while @v && $v[-1] =~ /^\s*$/;
115 $data{$_} = join '', @v;
118 if (! exists $data{provides}) {
119 if ($file =~ /inctools$/) { # This file is special, it doesn't 'provide'
120 # any API, but has subs to use internally
121 $data{provides} = "";
124 $data{provides} = ($file =~ /(\w+)\.?$/)[0];
127 $data{provides} = [$data{provides} =~ /(\S+)/g];
129 if (exists $data{dontwarn}) {
130 $data{dontwarn} = [$data{dontwarn} =~ /(\S+)/g];
136 if (exists $data{tests} && (!exists $data{implementation} || $data{implementation} !~ /\S/)) {
137 $data{implementation} = '';
140 $data{implementation} =~ /\S/ or die "Empty implementation in $file\n";
144 for $p (@{$data{provides}}) {
145 if ($p =~ m#^/.*/\w*$#) {
146 my @tmp = eval "\$data{implementation} =~ ${p}gm";
147 $@ and die "invalid regex $p in $file\n";
148 @tmp or warn "no matches for regex $p in $file\n";
149 push @prov, do { my %h; grep !$h{$_}++, @tmp };
151 elsif ($p eq '__UNDEFINED__') {
152 my @tmp = $data{implementation} =~ /^\s*__UNDEFINED__[^\r\n\S]+(\w+)/gm;
153 @tmp or warn "no __UNDEFINED__ macros in $file\n";
162 if ($data{implementation} !~ /\b\Q$_\E\b/) {
163 warn "$file claims to provide $_, but doesn't seem to do so\n";
167 # scan for prototypes
168 my($proto) = $data{implementation} =~ /
169 ( ^ (?:[\w*]|[^\S\r\n])+
181 exists $proto{$_} and warn "$file: duplicate prototype for $_\n";
186 for $section (qw( implementation xsubs xsinit xsmisc xshead xsboot )) {
187 if (exists $data{$section}) {
188 $data{$section} =~ s/\{\s*version\s*(<|>|==|!=|>=|<=)\s*([\d._]+)\s*\}/expand_version($1, $2)/gei;
192 $data{provides} = \@prov;
193 $data{prototypes} = \%proto;
194 $data{OPTIONS} = \%options;
196 my %prov = map { ($_ => 1) } @prov;
197 my %dontwarn = exists $data{dontwarn} ? map { ($_ => 1) } @{$data{dontwarn}} : ();
198 my @maybeprov = do { my %h;
200 my($nop) = /^Perl_(.*)/;
201 not exists $prov{$_} ||
202 exists $dontwarn{$_} ||
204 (defined $nop && exists $prov{$nop} ) ||
205 (defined $nop && exists $dontwarn{$nop}) ||
208 $data{implementation} =~ /^\s*#\s*define\s+(\w+)/gm };
211 warn "$file seems to provide these macros, but doesn't list them:\n "
212 . join("\n ", @maybeprov) . "\n";
218 sub compare_prototypes
238 push @c, map "!($_)", @{$p->{pre}};
239 defined $p->{cur} and push @c, "($p->{cur})";
245 sub trim_arg # Splits the argument into type and name, returning the
249 my $remove = join '|', qw( NN NULLOK VOL );
251 $in eq '...' and return ($in);
254 my $name; # Work on the name
256 s/[*()]/ /g; # Get rid of this punctuation
257 s/ \[ [^\]]* \] / /xg; # Get rid of dimensions
258 s/\b(?:auto|const|extern|inline|register|static|volatile|restrict)\b//g;
260 s/^\s+//; s/\s+$//; # No leading, trailing space
262 if( /^\b (?:struct|union|enum) \s+ \w+ (?: \s+ ( \w+ ) )? $/x ) {
263 defined $1 and $name = $1; # Extract the name for one of these declarations
266 if( s/\b(?:char|double|float|int|long|short|signed|unsigned|void)\b//g ) {
267 /^ \s* (\w+) \s* $/x and $name = $1; # Similarly for these
269 elsif (/^ \s* " [^"]+ " \s+ (\w+) \s* $/x) { # A literal string (is special)
273 /^ \s* \w+ \s+ (\w+) \s* $/x and $name = $1; # Everything else.
277 $_ = $in; # Now work on the type.
279 # Get rid of the name if we found one
280 defined $name and s/\b$name\b//;
282 # these don't matter at all; note that const does matter
283 s/\b(?:auto|extern|inline|register|static|volatile|restrict)\b//g;
286 while (s/ \* \s+ \* /**/xg) {} # No spaces within pointer sequences
287 s/ \s* ( \*+ ) \s* / $1 /xg; # Normalize pointer sequences to be surrounded
289 s/^\s+//; s/\s+$//; # No leading, trailing spacd
290 s/\s+/ /g; # Collapse multiple space into one
304 open FILE, $file or die "$file: $!\n";
307 while (defined($line = <FILE>)) {
308 while ($line =~ /\\$/ && defined($l = <FILE>)) {
312 next if $line =~ /^\s*:/;
313 $line =~ s/^\s+|\s+$//gs;
314 my($dir, $args) = ($line =~ /^\s*#\s*(\w+)(?:\s*(.*?)\s*)?$/);
315 if (defined $dir and defined $args) {
317 /^ifdef$/ and do { push @pps, { pre => [], cur => "defined($args)" } ; last };
318 /^ifndef$/ and do { push @pps, { pre => [], cur => "!defined($args)" } ; last };
319 /^if$/ and do { push @pps, { pre => [], cur => $args } ; last };
320 /^elif$/ and do { push @{$pps[-1]{pre}}, $pps[-1]{cur}; $pps[-1]{cur} = $args; last };
321 /^else$/ and do { push @{$pps[-1]{pre}}, $pps[-1]{cur}; $pps[-1]{cur} = undef; last };
322 /^endif$/ and do { pop @pps ; last };
323 /^include$/ and last;
326 warn "unhandled preprocessor directive: $dir\n";
330 my @e = split /\s*\|\s*/, $line;
332 my($flags, $ret, $name, @args) = @e;
334 # Skip non-name entries, like
335 # PL_parser-E<gt>linestr
336 # which documents a struct entry rather than a function. We retain
337 # all other entries, so that our caller has full information, and
338 # may skip things like non-public functions.
339 next if $flags =~ /N/;
340 if ($name =~ /^[^\W\d]\w*$/) {
344 ($ret) = trim_arg($ret);
347 flags => { map { $_, 1 } $flags =~ /./g },
350 cond => ppcond(\@pps),
354 warn "mysterious name [$name] in $file, line $.\n";
363 # Here's what two elements of the array look like:
371 # 'cond' => '(defined(HAS_NL_LANGINFO) && defined(PERL_LANGINFO_H))',
378 # 'name' => 'Perl_langinfo',
379 # 'ret' => 'const char *'
388 # 'cond' => '!(defined(HAS_NL_LANGINFO) && defined(PERL_LANGINFO_H))',
395 # 'name' => 'Perl_langinfo',
396 # 'ret' => 'const char *'
402 sub normalize_prototype # So that they can be compared more easily
405 $proto =~ s/\s* \* \s* / * /xg;
412 my @args = map { "@$_" } @{$f->{args}};
414 my $pTHX_ = exists $f->{flags}{T} ? "" : "pTHX_ ";
415 $proto = "$f->{ret} $f->{name}" . "($pTHX_" . join(', ', @args) . ')';
416 return normalize_prototype($proto);