[perl] Fix C++11 enum class wrapping with -const

Fixes #630
This commit is contained in:
Olly Betts 2024-09-11 15:24:58 +12:00
parent cd40541bd1
commit 7e8eea4a59
7 changed files with 51 additions and 7 deletions

View File

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.3.0 (in progress) Version 4.3.0 (in progress)
=========================== ===========================
2024-09-11: olly
[Perl] #630 Fix wrapping of C++11 enum class when -const command line
option is specified.
2024-09-07: wsfulton 2024-09-07: wsfulton
#2875 Fix swig-4.1.0 regression using the %interface family of macros #2875 Fix swig-4.1.0 regression using the %interface family of macros
for multiple inheritance and common bases. for multiple inheritance and common bases.

View File

@ -263,3 +263,14 @@ private:
Val2 = 1172, Val2 = 1172,
}; };
%} %}
// Test handling of %rename of enum class and an enumerator.
%rename(Enum18) QEnum18;
%rename(Val1) QVal1;
%inline %{
enum class QEnum18
{
QVal1 = 1181,
Val2 = 1182,
};
%}

View File

@ -0,0 +1,3 @@
%module cpp11_strongly_typed_enumerations_perl_const
%include "cpp11_strongly_typed_enumerations.i"

View File

@ -23,6 +23,9 @@ CPP_TEST_CASES += \
memberin1 \ memberin1 \
director_nestedmodule \ director_nestedmodule \
CPP11_TEST_CASES += \
cpp11_strongly_typed_enumerations_perl_const
C_TEST_CASES += \ C_TEST_CASES += \
li_cstring \ li_cstring \
li_cdata_carrays \ li_cdata_carrays \
@ -34,7 +37,7 @@ include $(srcdir)/../common.mk
# none! # none!
# Custom tests - tests with additional commandline options # Custom tests - tests with additional commandline options
# none! cpp11_strongly_typed_enumerations_perl_const.cpptest: SWIGOPT += -const
# Rules for the different types of tests # Rules for the different types of tests
%.cpptest: %.cpptest:

View File

@ -0,0 +1,8 @@
use strict;
use warnings;
use Test::More tests => 4;
BEGIN { use_ok('cpp11_strongly_typed_enumerations_perl_const') }
require_ok('cpp11_strongly_typed_enumerations_perl_const');
is(cpp11_strongly_typed_enumerations_perl_const::Enum18::Val1, 1181);
is(cpp11_strongly_typed_enumerations_perl_const::Enum18::Val2, 1182);

View File

@ -1,6 +1,6 @@
use strict; use strict;
use warnings; use warnings;
use Test::More tests => 78; use Test::More tests => 80;
BEGIN { use_ok('cpp11_strongly_typed_enumerations') } BEGIN { use_ok('cpp11_strongly_typed_enumerations') }
require_ok('cpp11_strongly_typed_enumerations'); require_ok('cpp11_strongly_typed_enumerations');
@ -166,3 +166,5 @@ enumCheck(cpp11_strongly_typed_enumerations::globalTest1($cpp11_strongly_typed_e
enumCheck(cpp11_strongly_typed_enumerations::globalTest2($cpp11_strongly_typed_enumerations::Class1::Enum12_Val5c), 1121); enumCheck(cpp11_strongly_typed_enumerations::globalTest2($cpp11_strongly_typed_enumerations::Class1::Enum12_Val5c), 1121);
#enumCheck(cpp11_strongly_typed_enumerations::globalTest3($cpp11_strongly_typed_enumerations::Class1::Struct1::Enum12_Val5f), 3121); #enumCheck(cpp11_strongly_typed_enumerations::globalTest3($cpp11_strongly_typed_enumerations::Class1::Struct1::Enum12_Val5f), 3121);
$val = enumCheck($cpp11_strongly_typed_enumerations::Enum18_Val1, 1181);
$val = enumCheck($cpp11_strongly_typed_enumerations::Enum18_Val2, 1182);

View File

@ -107,7 +107,7 @@ static String *pcode = 0; /* Perl code associated with each class */
static int member_func = 0; /* Set to 1 when wrapping a member function */ static int member_func = 0; /* Set to 1 when wrapping a member function */
static String *func_stubs = 0; /* Function stubs */ static String *func_stubs = 0; /* Function stubs */
static String *const_stubs = 0; /* Constant stubs */ static String *const_stubs = 0; /* Constant stubs */
static int num_consts = 0; /* Number of constants */ static Node *const_stubs_enum_class = 0; /* Node for enum class if we're currently generating one */
static String *var_stubs = 0; /* Variable stubs */ static String *var_stubs = 0; /* Variable stubs */
static String *exported = 0; /* Exported symbols */ static String *exported = 0; /* Exported symbols */
static String *pragma_include = 0; static String *pragma_include = 0;
@ -585,10 +585,9 @@ public:
/* Emit package code for different classes */ /* Emit package code for different classes */
Printf(f_pm, "%s", pm); Printf(f_pm, "%s", pm);
if (num_consts > 0) { if (Len(const_stubs) > 0) {
/* Emit constant stubs */ /* Emit constant stubs */
Printf(f_pm, "\n# ------- CONSTANT STUBS -------\n\n"); Printf(f_pm, "\n# ------- CONSTANT STUBS -------\n\n");
Printf(f_pm, "package %s;\n\n", namespace_module);
Printf(f_pm, "%s", const_stubs); Printf(f_pm, "%s", const_stubs);
} }
@ -1120,8 +1119,22 @@ public:
"tie %__", iname, "_hash,\"", is_shadow(type), "\", $", "tie %__", iname, "_hash,\"", is_shadow(type), "\", $",
cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(type), ";\n", NIL); cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(type), ";\n", NIL);
} else if (do_constants) { } else if (do_constants) {
Printv(const_stubs, "sub ", name, " () { $", cmodule, "::", name, " }\n", NIL); char *dcolon = Strstr(name, "::");
num_consts++; if (!dcolon) {
if (Len(const_stubs) == 0 || const_stubs_enum_class != NULL) {
Printf(const_stubs, "package %s;\n", namespace_module);
const_stubs_enum_class = NULL;
}
Printv(const_stubs, "sub ", iname, " () { $", cmodule, "::", iname, " }\n", NIL);
} else {
// C++11 strongly-typed enum.
Node *parent = Getattr(n, "parentNode");
if (const_stubs_enum_class != parent) {
Printf(const_stubs, "package %s::%s;\n", namespace_module, Getattr(parent, "sym:name"));
const_stubs_enum_class = parent;
}
Printv(const_stubs, "sub ", Getattr(n, "enumvalueDeclaration:sym:name"), " () { $", cmodule, "::", iname, " }\n", NIL);
}
} else { } else {
Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL); Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL);
} }