Fail at compile time for multiple typemap substitution

Define a goto label in the typemap with a fixed name which will give a
multiple definition error if the typemap is substituted more than once
in a particular wrapper function.

This way we'll also catch this potential bug for target languages that
don't have a runme for the ignore_parameter testcase.
This commit is contained in:
Olly Betts 2023-07-10 14:22:36 +12:00
parent 33696cbf56
commit 0488f556a5
2 changed files with 14 additions and 18 deletions

View File

@ -2,15 +2,15 @@
%module ignore_parameter %module ignore_parameter
%typemap(in,numinputs=0) char* a (int unique = 0) { %typemap(in,numinputs=0) char* a %{
static const char* hi = "hello"; /* Catch if a target language substitutes this typemap more than once in
$1 = const_cast<char *>(hi); * the same wrapper method - this will lead to an error due to this label
unique++; * being redefined.
if (unique != 1) { */
fprintf(stderr, "in typemap applied more than once\n"); goto redefinition_error_means_in_typemap_substituted_more_than_once;
abort(); redefinition_error_means_in_typemap_substituted_more_than_once:
} $1 = const_cast<char *>("hello");
} %}
%typemap(in,numinputs=0) int bb "$1 = 101; called_argout = 0;" %typemap(in,numinputs=0) int bb "$1 = 101; called_argout = 0;"
%typemap(in,numinputs=0) double ccc "$1 = 8.8;" %typemap(in,numinputs=0) double ccc "$1 = 8.8;"

View File

@ -1,24 +1,20 @@
const ignore_parameter = require('ignore_parameter'); const ignore_parameter = require('ignore_parameter');
function check(a, b, argout) { function check(a, b) {
if (a !== b) throw new Error(`'${a}' != '${b}`); if (a !== b) throw new Error(`'${a}' != '${b}`);
if (argout) {
if (ignore_parameter.called_argout !== 1)
throw new Error('argout typemap not applied');
}
} }
check(ignore_parameter.jaguar(200, 0), "hello"); check(ignore_parameter.jaguar(200, 0), "hello");
check(ignore_parameter.lotus("foo", 1), 101, true); check(ignore_parameter.lotus("foo", 1), 101);
check(ignore_parameter.tvr("bar", 2), 8.8); check(ignore_parameter.tvr("bar", 2), 8.8);
check(ignore_parameter.ferrari(), 101, true); check(ignore_parameter.ferrari(), 101);
check(ignore_parameter.fiat(17), 17); check(ignore_parameter.fiat(17), 17);
car = new ignore_parameter.SportsCars(); car = new ignore_parameter.SportsCars();
check(car.daimler(200, 0), "hello"); check(car.daimler(200, 0), "hello");
check(car.astonmartin("foo", 1), 101, true); check(car.astonmartin("foo", 1), 101);
check(car.bugatti("bar", 2), 8.8); check(car.bugatti("bar", 2), 8.8);
check(car.lamborghini(), 101, true); check(car.lamborghini(), 101);
check(car.maseratti(289), 289); check(car.maseratti(289), 289);
check(car.audi(), 8.8); check(car.audi(), 8.8);