This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regression test for 34394ecd - SVs that were only on the tmps stack leaked.
[perl5.git] / Porting / expand-macro.pl
CommitLineData
d1f3479f
NC
1#!perl -w
2use strict;
3
d5f33267 4use Pod::Usage;
a93e78e3 5use Getopt::Std;
d5f33267 6$Getopt::Std::STANDARD_HELP_VERSION = 1;
a93e78e3 7
d5f33267
MHM
8my $trysource = "try.c";
9my $tryout = "try.i";
d1f3479f 10
d5f33267 11getopts('fF:ekvI:', \my %opt) or pod2usage();
a93e78e3 12
d5f33267 13my($expr, @headers) = @ARGV ? splice @ARGV : "-";
d1f3479f 14
d5f33267 15pod2usage "-f and -F <tool> are exclusive\n" if $opt{f} and $opt{F};
a93e78e3 16
d1f3479f 17foreach($trysource, $tryout) {
a93e78e3 18 unlink $_ if $opt{e};
d1f3479f
NC
19 die "You already have a $_" if -e $_;
20}
21
d5f33267
MHM
22if ($expr eq '-') {
23 warn "reading from stdin...\n";
24 $expr = do { local $/; <> };
25}
26
27my($macro, $args) = $expr =~ /^\s*(\w+)((?:\s*\(.*\))?)\s*;?\s*$/s
28 or pod2usage "$expr doesn't look like a macro-name or macro-expression to me";
29
30if (!(@ARGV = @headers)) {
d1f3479f
NC
31 open my $fh, '<', 'MANIFEST' or die "Can't open MANIFEST: $!";
32 while (<$fh>) {
33 push @ARGV, $1 if m!^([^/]+\.h)\t!;
34 }
ed690650 35 push @ARGV, 'config.h' if -f 'config.h';
d1f3479f
NC
36}
37
be4f373d 38my $header;
d1f3479f 39while (<>) {
e25a7dc2 40 next unless /^#\s*define\s+$macro\b/;
d1f3479f 41 my ($def_args) = /^#\s*define\s+$macro\(([^)]*)\)/;
d5f33267 42 if (defined $def_args && !$args) {
d1f3479f 43 my @args = split ',', $def_args;
a93e78e3 44 print "# macro: $macro args: @args in $_\n" if $opt{v};
d1f3479f
NC
45 my $argname = "A0";
46 $args = '(' . join (', ', map {$argname++} 1..@args) . ')';
47 }
be4f373d 48 $header = $ARGV;
d1f3479f
NC
49 last;
50}
be4f373d 51die "$macro not found\n" unless defined $header;
d1f3479f
NC
52
53open my $out, '>', $trysource or die "Can't open $trysource: $!";
54
d5f33267
MHM
55my $sentinel = "$macro expands to";
56
d1f3479f
NC
57print $out <<"EOF";
58#include "EXTERN.h"
59#include "perl.h"
4784c5e8
NC
60EOF
61
c3c0aa28 62print $out qq{#include "$header"\n}
4784c5e8
NC
63 unless $header eq 'perl.h' or $header eq 'EXTERN.h';
64
65print $out <<"EOF";
be4f373d 66#line 4 "$sentinel"
d1f3479f
NC
67$macro$args
68EOF
69
70close $out or die "Can't close $trysource: $!";
71
a93e78e3 72print "doing: make $tryout\n" if $opt{v};
d1f3479f
NC
73system "make $tryout" and die;
74
a93e78e3 75# if user wants 'indent' formatting ..
27c6397c
NC
76my $out_fh;
77
78if ($opt{f} || $opt{F}) {
79 # a: indent is a well behaved filter when given 0 arguments, reading from
80 # stdin and writing to stdout
81 # b: all our braces should be balanced, indented back to column 0, in the
82 # headers, hence everything before our #line directive can be ignored
83 #
84 # We can take advantage of this to reduce the work to indent.
85
86 my $indent_command = $opt{f} ? 'indent' : $opt{F};
87
88 if (defined $opt{I}) {
89 $indent_command .= " $opt{I}";
90 }
91 open $out_fh, '|-', $indent_command or die $?;
92} else {
93 $out_fh = \*STDOUT;
94}
a93e78e3 95
d1f3479f
NC
96open my $fh, '<', $tryout or die "Can't open $tryout: $!";
97
98while (<$fh>) {
27c6397c 99 print $out_fh $_ if /$sentinel/o .. 1;
d1f3479f
NC
100}
101
a93e78e3
JC
102unless ($opt{k}) {
103 foreach($trysource, $tryout) {
104 die "Can't unlink $_" unless unlink $_;
105 }
d1f3479f 106}
d5f33267
MHM
107
108__END__
109
110=head1 NAME
111
112expand-macro.pl - expand C macros using the C preprocessor
113
114=head1 SYNOPSIS
115
116 expand-macro.pl [options] [ < macro-name | macro-expression | - > [headers] ]
117
118 options:
119 -f use 'indent' to format output
120 -F <tool> use <tool> to format output (instead of -f)
121 -e erase try.[ic] instead of failing when they're present (errdetect)
122 -k keep them after generating (for handy inspection)
123 -v verbose
124 -I <indent-opts> passed into indent
125
126=cut