Skip to content

Commit db33547

Browse files
committed
ParseXS: refactor: fetch_para(): simplify logic
Simplify the logic of the code which handles C-preprocessor directives where there's not the (usually required) blank line directly before and after an XSUB; e.g. #if X int foo(...) ... #endif This commit splits the logic into two halves: the first is responsible for handling conditionals directly *before* the XSUB, and the other half for ones during and directly after the XSUB. Then the second half is further simplified. I think this makes the code easier to understand. I *think* the new code is logically equivalent.
1 parent 9d970e5 commit db33547

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -735,28 +735,22 @@ sub fetch_para {
735735
# putative paragraph; otherwise treat it as internal.
736736

737737
my $type = $1;
738-
if ($type =~ /^if/) { # if, ifdef, ifndef
739-
if (@{$self->{line}}) {
740-
# increment level
741-
$if_level++;
742-
} else {
743-
$final = 1;
744-
}
745-
} elsif ($type eq "endif") {
746-
if ($if_level) { # are we in an if that was started in this paragraph?
747-
$if_level--; # yep- so decrement to end this if block
748-
} else {
749-
$final = 1;
750-
}
751-
} elsif (!$if_level) {
752-
# not in an #ifdef from this paragraph, thus
753-
# this directive should not be part of this paragraph.
738+
739+
if (!@{$self->{line}}) {
740+
# Treat a conditional starting the paragraph as a one-line
741+
# paragraph
754742
$final = 1;
755743
}
756-
}
757-
758-
if ($final and @{$self->{line}}) {
759-
return 1;
744+
else {
745+
# Handle conditionals appearing in, or just after, an XSUB
746+
747+
$if_level++ if $type =~ /^if/; # if, ifdef, ifndef
748+
# If we're in a conditional that didn't start in this paragraph,
749+
# return everything up to, but not including, this line, which
750+
# will instead form the first line of the *next* paragraph
751+
return 1 if !$if_level;
752+
$if_level-- if $type eq "endif";
753+
}
760754
}
761755

762756
push(@{ $self->{line} }, $self->{lastline});

0 commit comments

Comments
 (0)