Skip to content

Instantly share code, notes, and snippets.

@snogglethorpe
Created November 18, 2013 01:51
Show Gist options
  • Save snogglethorpe/7521146 to your computer and use it in GitHub Desktop.
Save snogglethorpe/7521146 to your computer and use it in GitHub Desktop.
SWIG overload-resolution bug test case
// SWIG overload-resolution bug test case
//
// Miles Bader <snogglethorpe@gmail.com>
//
// Instructions to reproduce, on a Debian system with the "lua5.1" and
// "liblua5.1-0-dev" packages installed:
//
// Compile with:
//
// swig -o ovlbug.cc -c++ -lua ovlbug.swg
// g++ -o ovlbug.so -shared -fPIC -I/usr/include/lua5.1 ovlbug.cc
//
// Test with:
//
// lua5.1 -e 'require "ovlbug".non_overloaded_fun ({1, 2, 3, 4})'
// [works correctly, the INPUT typemap is used]
//
// lua5.1 -e 'require "ovlbug".overloaded_fun (1)'
// [works correctly, this overload case has no INPUT typemap]
//
// lua5.1 -e 'require "ovlbug".overloaded_fun ({1, 2, 3, 4})'
// [FAILS, because it incorrectly ignores the INPUT typemap for
// overload resolution]
//
//
// This test cases demonstrates a bug in SWIG overload-resolution
// wrappers. This test uses Lua, although I don't know if this is a
// Lua-backend specific bug or a general one.
//
// Basically, the two functions "non_overloaded_fun" and
// "overloaded_fun" here both accept arguments of type "const int
// array[4]", for which we provide a SWIG INPUT typemap to convert
// from Lua tables. This works correctly for "non_overloaded_fun",
// but not for "overloaded_fun".
//
// The reason is that although SWIG correctly uses the INPUT typemap
// when actually calling the functions, it _ignores_ the typemap in
// the additional wrapper function used for overload resolution.
//
//
// This bug still exists as of SWIG 2.0.11
//
%module ovlbug;
%{
#include <iostream>
void non_overloaded_fun (const int array[4])
{
std::cout << "non_overloaded_fun: int array[4] = " << array[0] << "..." << std::endl;
}
void overloaded_fun (const int array[4])
{
std::cout << "overloaded_fun: int array[4] = " << array[0] << "..." << std::endl;
}
void overloaded_fun (int num)
{
std::cout << "overloaded_fun: num = " << num << std::endl;
}
%}
%include <typemaps.i>
%apply (int INPUT[ANY]) {(const int array[4])};
// This function can be called with a Lua table:
//
void non_overloaded_fun (const int array[4]);
// ... but this function can't be, because the SWIG
// overload-resolution wrapper does type-checking incorrectly for
// arguments with INPUT typemaps:
//
void overloaded_fun (const int array[4]);
void overloaded_fun (int num);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment