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 ################################################################################
21 eval { require File::Spec };
22 return $@ ? join('/', @_) : File::Spec->catfile(@_);
30 opendir DIR, $dir or die "cannot open directory $dir: $!\n";
31 my @files = grep { !-d && !/^\./ } readdir DIR; # no dirs or hidden files
34 return map { cat_file($dir, $_) } sort @files;
39 # Creates a hash with the keys being all symbols found in all the files in
40 # the input directory (default 'parts/todo'), and the values being the perl
41 # versions of each symbol.
43 my $dir = shift || 'parts/todo';
48 for $todo (all_files_in_dir($dir)) {
49 open TODO, $todo or die "cannot open $todo: $!\n";
57 /^\w+$/ or die "invalid identifier: $_\n";
58 exists $todo{$_} and die "duplicate identifier: $_ ($todo{$_} <=> $version)\n";
70 my($r, $v, $s) = parse_version($ver);
71 $r == 5 or die "only Perl revision 5 is supported\n";
72 my $bcdver = sprintf "0x%d%03d%03d", $r, $v, $s;
73 return "(PERL_BCDVERSION $op $bcdver)";
79 my $section = 'implementation';
81 my $vsec = join '|', qw( provides dontwarn implementation
82 xsubs xsinit xsmisc xshead xsboot tests );
86 open F, $file or die "$file: $!\n";
88 /[ \t]+$/ and warn "$file:$.: warning: trailing whitespace\n";
89 if ($section eq 'implementation') {
90 m!//! && !m!(?:=~|s/).*//! && !m!(?:ht|f)tp(?:s)://!
91 and warn "$file:$.: warning: potential C++ comment\n";
96 if (/^=($vsec)(?:\s+(.*))?/) {
100 $options{$section} = eval "{ $opt }";
101 $@ and die "$file:$.: invalid options ($opt) in section $section: $@\n";
105 push @{$data{$section}}, $_;
110 my @v = @{$data{$_}};
111 shift @v while @v && $v[0] =~ /^\s*$/;
112 pop @v while @v && $v[-1] =~ /^\s*$/;
113 $data{$_} = join '', @v;
116 unless (exists $data{provides}) {
117 $data{provides} = ($file =~ /(\w+)\.?$/)[0];
119 $data{provides} = [$data{provides} =~ /(\S+)/g];
121 if (exists $data{dontwarn}) {
122 $data{dontwarn} = [$data{dontwarn} =~ /(\S+)/g];
128 if (exists $data{tests} && (!exists $data{implementation} || $data{implementation} !~ /\S/)) {
129 $data{implementation} = '';
132 $data{implementation} =~ /\S/ or die "Empty implementation in $file\n";
136 for $p (@{$data{provides}}) {
137 if ($p =~ m#^/.*/\w*$#) {
138 my @tmp = eval "\$data{implementation} =~ ${p}gm";
139 $@ and die "invalid regex $p in $file\n";
140 @tmp or warn "no matches for regex $p in $file\n";
141 push @prov, do { my %h; grep !$h{$_}++, @tmp };
143 elsif ($p eq '__UNDEFINED__') {
144 my @tmp = $data{implementation} =~ /^\s*__UNDEFINED__[^\r\n\S]+(\w+)/gm;
145 @tmp or warn "no __UNDEFINED__ macros in $file\n";
154 if ($data{implementation} !~ /\b\Q$_\E\b/) {
155 warn "$file claims to provide $_, but doesn't seem to do so\n";
159 # scan for prototypes
160 my($proto) = $data{implementation} =~ /
161 ( ^ (?:[\w*]|[^\S\r\n])+
173 exists $proto{$_} and warn "$file: duplicate prototype for $_\n";
178 for $section (qw( implementation xsubs xsinit xsmisc xshead xsboot )) {
179 if (exists $data{$section}) {
180 $data{$section} =~ s/\{\s*version\s*(<|>|==|!=|>=|<=)\s*([\d._]+)\s*\}/expand_version($1, $2)/gei;
184 $data{provides} = \@prov;
185 $data{prototypes} = \%proto;
186 $data{OPTIONS} = \%options;
188 my %prov = map { ($_ => 1) } @prov;
189 my %dontwarn = exists $data{dontwarn} ? map { ($_ => 1) } @{$data{dontwarn}} : ();
190 my @maybeprov = do { my %h;
192 my($nop) = /^Perl_(.*)/;
193 not exists $prov{$_} ||
194 exists $dontwarn{$_} ||
196 (defined $nop && exists $prov{$nop} ) ||
197 (defined $nop && exists $dontwarn{$nop}) ||
200 $data{implementation} =~ /^\s*#\s*define\s+(\w+)/gm };
203 warn "$file seems to provide these macros, but doesn't list them:\n "
204 . join("\n ", @maybeprov) . "\n";
210 sub compare_prototypes
230 push @c, map "!($_)", @{$p->{pre}};
231 defined $p->{cur} and push @c, "($p->{cur})";
237 sub trim_arg # Splits the argument into type and name, returning the
241 my $remove = join '|', qw( NN NULLOK VOL );
243 $in eq '...' and return ($in);
246 my $name; # Work on the name
248 s/[*()]/ /g; # Get rid of this punctuation
249 s/ \[ [^\]]* \] / /xg; # Get rid of dimensions
250 s/\b(?:auto|const|extern|inline|register|static|volatile|restrict)\b//g;
252 s/^\s+//; s/\s+$//; # No leading, trailing space
254 if( /^\b (?:struct|union|enum) \s+ \w+ (?: \s+ ( \w+ ) )? $/x ) {
255 defined $1 and $name = $1; # Extract the name for one of these declarations
258 if( s/\b(?:char|double|float|int|long|short|signed|unsigned|void)\b//g ) {
259 /^ \s* (\w+) \s* $/x and $name = $1; # Similarly for these
261 elsif (/^ \s* " [^"]+ " \s+ (\w+) \s* $/x) { # A literal string (is special)
265 /^ \s* \w+ \s+ (\w+) \s* $/x and $name = $1; # Everything else.
269 $_ = $in; # Now work on the type.
271 # Get rid of the name if we found one
272 defined $name and s/\b$name\b//;
274 # these don't matter at all; note that const does matter
275 s/\b(?:auto|extern|inline|register|static|volatile|restrict)\b//g;
278 while (s/ \* \s+ \* /**/xg) {} # No spaces within pointer sequences
279 s/ \s* ( \*+ ) \s* / $1 /xg; # Normalize pointer sequences to be surrounded
281 s/^\s+//; s/\s+$//; # No leading, trailing spacd
282 s/\s+/ /g; # Collapse multiple space into one
296 open FILE, $file or die "$file: $!\n";
299 while (defined($line = <FILE>)) {
300 while ($line =~ /\\$/ && defined($l = <FILE>)) {
304 next if $line =~ /^\s*:/;
305 $line =~ s/^\s+|\s+$//gs;
306 my($dir, $args) = ($line =~ /^\s*#\s*(\w+)(?:\s*(.*?)\s*)?$/);
307 if (defined $dir and defined $args) {
309 /^ifdef$/ and do { push @pps, { pre => [], cur => "defined($args)" } ; last };
310 /^ifndef$/ and do { push @pps, { pre => [], cur => "!defined($args)" } ; last };
311 /^if$/ and do { push @pps, { pre => [], cur => $args } ; last };
312 /^elif$/ and do { push @{$pps[-1]{pre}}, $pps[-1]{cur}; $pps[-1]{cur} = $args; last };
313 /^else$/ and do { push @{$pps[-1]{pre}}, $pps[-1]{cur}; $pps[-1]{cur} = undef; last };
314 /^endif$/ and do { pop @pps ; last };
315 /^include$/ and last;
318 warn "unhandled preprocessor directive: $dir\n";
322 my @e = split /\s*\|\s*/, $line;
324 my($flags, $ret, $name, @args) = @e;
326 # Skip non-name entries, like
327 # PL_parser-E<gt>linestr
328 # which documents a struct entry rather than a function. We retain
329 # all other entries, so that our caller has full information, and
330 # may skip things like non-public functions.
331 next if $flags =~ /N/;
332 if ($name =~ /^[^\W\d]\w*$/) {
336 ($ret) = trim_arg($ret);
339 flags => { map { $_, 1 } $flags =~ /./g },
342 cond => ppcond(\@pps),
346 warn "mysterious name [$name] in $file, line $.\n";
355 # Here's what two elements of the array look like:
363 # 'cond' => '(defined(HAS_NL_LANGINFO) && defined(PERL_LANGINFO_H))',
370 # 'name' => 'Perl_langinfo',
371 # 'ret' => 'const char *'
380 # 'cond' => '!(defined(HAS_NL_LANGINFO) && defined(PERL_LANGINFO_H))',
387 # 'name' => 'Perl_langinfo',
388 # 'ret' => 'const char *'
394 sub normalize_prototype # So that they can be compared more easily
397 $proto =~ s/\s* \* \s* / * /xg;
404 my @args = map { "@$_" } @{$f->{args}};
406 my $pTHX_ = exists $f->{flags}{T} ? "" : "pTHX_ ";
407 $proto = "$f->{ret} $f->{name}" . "($pTHX_" . join(', ', @args) . ')';
408 return normalize_prototype($proto);
416 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
421 if ($r < 5 || ($r == 5 && $v < 6)) {
423 die "invalid version '$ver'\n";
427 $ver = sprintf "%d.%03d", $r, $v;
428 $s > 0 and $ver .= sprintf "_%02d", $s;
433 return sprintf "%d.%d.%d", $r, $v, $s;
440 if ($ver =~ /^(\d+)\.(\d+)\.(\d+)$/) {
443 elsif ($ver !~ /^\d+\.\d{3}(?:_\d{2})?$/) {
444 die "cannot parse version '$ver'\n";
450 my($r,$v,$s) = $ver =~ /(\d+)\.(\d{3})(\d{3})/;
455 if ($r < 5 || ($r == 5 && $v < 6)) {
457 die "cannot parse version '$ver'\n";