Skip to content

Commit f775bec

Browse files
committed
ParseXS: fix alias/attr cv, add need_boot_cv field
Add a new field to the ExtUtils::ParseXS class: need_boot_cv and remove the seen_INTERFACE_or_MACRO field. The boot XSUB emitted by the XS parser will sometimes declare a 'cv' variable within a narrow scope when it knows that part of the boot code will need it, e.g. for: cv = newXS_deffile("Foo::foo", XS_Foo_foo); apply_attrs_string("Foo", cv, "x y", 0); Previously it would guess whether to to emit '{ CV *cv; ... }' based on flags indicating the presence of various keywords which were known to generate such code. This commit changes it so that instead, it sets a new flag, 'need_boot_cv', any time code is generated which uses 'cv'. This seems more logical to me. It also allows us to remove the flag indicating that INTERFACE: or INTERFACE_MACRO: was seen, since the only remaining use of that flag was for cv-emitting. This commit also fixes a bug whereby the cv declaration wasn't being emitted for the ALIAS and ATTR keywords (even though it uses it to call e.g. apply_attrs_string() as shown above). This was in fact *fairly* harmless, since the boot XSUB has a cv parameter anyway (the CV of the BOOT sub), and as long as nothing in the boot XSUB needs the original value held in cv, no harm was done. In the case of ALIAS, it checked for the existence of $pxs->{xsub_map_alias_name_to_value} as an indication of the presence of ALIAS, that field is undeffed after each each XSUB has been parsed. In the case of ATTR, I don't think it ever added cv..
1 parent d663961 commit f775bec

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,12 @@ BEGIN {
216216
'VERSIONCHECK_value', # Bool: most recent VERSIONCHECK: value. Defaults
217217
# to the value of the "-noversioncheck" switch.
218218

219-
'seen_INTERFACE_or_MACRO', # Bool: at least one INTERFACE/INTERFACE_MACRO
220-
# has been seen somewhere.
221-
222219
'seen_an_XSUB', # Bool: at least one XSUB has been encountered
223220

224221
# File-scoped code-emitting state:
225222

223+
'need_boot_cv', # must declare 'cv' within the boot function
224+
226225
'bootcode_early', # Array of code lines to emit early in boot XSUB:
227226
# typically newXS() calls
228227

@@ -837,9 +836,7 @@ EOF
837836
# XSANY.any_i32 = $value;
838837
# XSINTERFACE_FUNC_SET(cv, $value);
839838

840-
if ( defined $self->{xsub_map_alias_name_to_value}
841-
or defined $self->{seen_INTERFACE_or_MACRO})
842-
{
839+
if ($self->{need_boot_cv}) {
843840
print Q(<<"EOF");
844841
| [[
845842
| CV * cv;
@@ -886,9 +883,7 @@ EOF
886883

887884
# Emit closing scope for the 'CV *cv' declaration
888885

889-
if ( defined $self->{xsub_map_alias_name_to_value}
890-
or defined $self->{seen_INTERFACE_or_MACRO})
891-
{
886+
if ($self->{need_boot_cv}) {
892887
print Q(<<"EOF");
893888
| ]]
894889
EOF

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ sub boot_code {
519519
| cv = $newXS(\"$xname\", XS_$cname$file_arg$proto_arg);
520520
| XSANY.any_i32 = $value;
521521
EOF
522+
$pxs->{need_boot_cv} = 1;
522523
}
523524
}
524525
elsif (@{ $pxs->{xsub_attributes} }) {
@@ -528,6 +529,7 @@ EOF
528529
| cv = $newXS(\"$pname\", XS_$cname$file_arg$proto_arg);
529530
| apply_attrs_string("$pxs->{PACKAGE_name}", cv, "@{ $pxs->{xsub_attributes} }", 0);
530531
EOF
532+
$pxs->{need_boot_cv} = 1;
531533
}
532534
elsif ( $self->{seen_INTERFACE}
533535
or $self->{seen_INTERFACE_MACRO})
@@ -543,6 +545,7 @@ EOF
543545
| cv = $newXS(\"$yname\", XS_$cname$file_arg$proto_arg);
544546
| $pxs->{xsub_interface_macro_set}(cv,$value);
545547
EOF
548+
$pxs->{need_boot_cv} = 1;
546549
}
547550
}
548551
elsif ($newXS eq 'newXS_deffile'){
@@ -3110,8 +3113,6 @@ sub parse {
31103113
}
31113114

31123115
$self->{map_short_orig} = \%map;
3113-
3114-
$pxs->{seen_INTERFACE_or_MACRO} = 1; # global
31153116
1;
31163117
}
31173118

@@ -3163,8 +3164,6 @@ sub parse {
31633164
$self->{get_macro} = $pxs->{xsub_interface_macro} = $m1;
31643165
$self->{set_macro} = $pxs->{xsub_interface_macro_set} = $m2;
31653166

3166-
$pxs->{seen_INTERFACE_or_MACRO} = 1; # global
3167-
31683167
1;
31693168
}
31703169

dist/ExtUtils-ParseXS/t/001-basic.t

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,6 +4225,7 @@ EOF
42254225
"has Foo::buz" ],
42264226
[ 0, 0, qr{"Foo::biz",.*\n.*= 3;},
42274227
"has Foo::biz" ],
4228+
[ 0, 0, qr{\QCV * cv;}, "has cv declaration" ],
42284229
],
42294230
42304231
[
@@ -4379,6 +4380,7 @@ EOF
43794380
\s+\QXSINTERFACE_FUNC_SET(cv,f2);\E
43804381
}x,
43814382
"got f2 entries" ],
4383+
[ 0, 0, qr{\QCV * cv;}, "has cv declaration" ],
43824384
],
43834385
);
43844386
@@ -4437,6 +4439,7 @@ EOF
44374439
| C_ARGS: foo
44384440
| ATTRS: d(y( z))
44394441
EOF
4442+
[ 0, 0, qr{\QCV * cv;}, "has cv declaration" ],
44404443
[ 0, 0, qr{\Qapply_attrs_string("Foo", cv, "a\E\s+b\s+c\(x\)\s+\Qd(y( z))", 0);},
44414444
"has correct attrs arg" ],
44424445
],

0 commit comments

Comments
 (0)