Skip to content

Commit 43263bc

Browse files
committed
ParseXS: disallow const on non-C++ methods
It's possible for an XSUB's name to include a class, in which case the XS parser will treat it as a C++ method and declare a THIS variable. It's also possible for an XSUB to have a postfix 'const' modifier to mimic similar in C++ method declarations. For example this XS declaration: void X::Y::foo(...) const causes this C++ variable to be declared: const X__Y * THIS = ...; The 'const' is supposed to be an optional modifier to a C++ XSUB but the parser currently allows it it on plain XSUBs, e.g. void foo(...) const which leads to 'uninit var' warnings during parsing, and to badly-formed C code or weird errors about missing typemap entries. This commit makes it an error to have 'const' on a non-C++ XSUB.
1 parent cf92bef commit 43263bc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,17 @@ sub parse {
877877
unless $func_header =~
878878
/^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
879879

880-
my ($class, $name, $params_text) = ($1, $2, $3);
881-
$class = "$4 $class" if $4;
880+
my ($class, $name, $params_text, $const) = ($1, $2, $3, $4);
881+
882+
if (defined $const) {
883+
if (defined $class) {
884+
$class = "$const $class";
885+
}
886+
else {
887+
$pxs->blurt("const modifier only allowed on XSUBs which are C++ methods");
888+
undef $const;
889+
}
890+
}
882891

883892
if ($return_type->{static} and !defined $class)
884893
{

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,16 @@ EOF
12021202
[ 0, 0, qr/\QTHIS->hhh(ddd)/, "autocall" ],
12031203
],
12041204

1205+
[
1206+
"C++: only const",
1207+
[
1208+
'void',
1209+
'foo() const',
1210+
],
1211+
[ 1, 0, qr/\Qconst modifier only allowed on XSUBs which are C++ methods/,
1212+
"got expected err" ],
1213+
],
1214+
12051215
[
12061216
"",
12071217
[

0 commit comments

Comments
 (0)