diff options
author | Lukas Mai <l.mai@web.de> | 2011-09-17 18:31:00 +0200 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2011-09-17 11:48:40 -0700 |
commit | 5a677310673fcfab45f919bc0ea9b84374ff22f6 (patch) | |
tree | 01285098d84207c4bf68f151dc7d6d6e65c6bb1a /utils | |
parent | 2a014a4b51424c9ea9e46c78e7b3840cc28e4aaa (diff) | |
download | perl-5a677310673fcfab45f919bc0ea9b84374ff22f6.tar.gz |
don't redefine macrosubs in .ph
h2ph generates code of the form
unless(defined(&FOO)) {
sub FOO () {42;}
}
for a C macro like '#define FOO 42'.
The problem with that: 'sub' happens at compile time, 'unless' at runtime.
So the sub is unconditionally defined first, then the unless runs with
an empty body. This behavior was introduced in commit
3d271ce79d39df56470393916b3d33ff26db2c8b (the syntax errors there were
fixed in commit 4a8e146e38ec2045f1f817a7cb578e1b1f80f39f).
There are already two code paths there, one for indentation level > 0
(eval), the other for toplevel definitions (straight sub). This patch
unifies them to always use 'eval' / runtime definitions. This change
shortens the code and makes the conditions actually work.
It moves the check for '#define FOO FOO' further up because I don't
see why it only needs to be done if indentation == 0. I changed the
comparison to use a whitespace-agnostic regex because
'" \&$name" eq $new' looks too brittle (the redundant \ doesn't help).
Diffstat (limited to 'utils')
-rw-r--r-- | utils/h2ph.PL | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/utils/h2ph.PL b/utils/h2ph.PL index 4545d6d1f3..e0b5e3aa9f 100644 --- a/utils/h2ph.PL +++ b/utils/h2ph.PL @@ -151,24 +151,22 @@ while (defined (my $file = next_file())) { } else { s/^\s+//; expr(); + $new = 1 if $new eq ''; + + # Shunt around such directives as `#define FOO FOO': + next if $new =~ /^\s*&\Q$name\E\s*\z/; + $new = reindent($new); $args = reindent($args); - if ($t ne '') { - $new =~ s/(['\\])/\\$1/g; #']); - - if ($opt_h) { - print OUT $t,"eval \"\\n#line $eval_index $outfile\\n\" . 'sub $name () {",$new,";}' unless defined(\&$name);\n"; - $eval_index++; - } else { - print OUT $t,"eval 'sub $name () {",$new,";}' unless defined(\&$name);\n"; - } - } else { - # Shunt around such directives as `#define FOO FOO': - next if " \&$name" eq $new; + $new =~ s/(['\\])/\\$1/g; #']); - print OUT $t,"unless(defined(\&$name)) {\n sub $name () {\t",$new,";}\n}\n"; + print OUT $t, 'eval '; + if ($opt_h) { + print OUT "\"\\n#line $eval_index $outfile\\n\" . "; + $eval_index++; } + print OUT "'sub $name () {$new;}' unless defined(&$name);\n"; } } elsif (/^(include|import|include_next)\s*([<\"])(.*)[>\"]/) { $incl_type = $1; |