Allow to use the original name of the global functions

It is impossible to have two functions with the same name inside the same
program, but it is possible to provide a #define to allow the user code to use
the original function name for the wrapper function, so do it for convenience.

Remove the old changes adding explicit "_wrap_" prefix to the examples and the
tests and remove the few more now passing tests from the list of failing tests.
This commit is contained in:
Vadim Zeitlin 2016-04-21 00:35:00 +02:00
commit 4b5e5d0cc8
6 changed files with 33 additions and 11 deletions

View file

@ -192,7 +192,7 @@ Wrapping C functions and variables is obviously performed in a straightforward w
<p> <p>
For each C function declared in the interface file a wrapper function is created. Basically, the wrapper function performs a call to the original function, and returns its result. For each C function declared in the interface file a wrapper function with the prefix <tt>_wrap_</tt> is created. Basically, the wrapper function performs a call to the original function, and returns its result. For convenience, a <tt>#define func _wrap_func</tt> is also provided in the generated header file to make it possible to call the function under its original name. If this is undesirable, <tt>SWIG_NO_WRAPPER_ALIASES</tt> can be predefined before including the wrapper header to disable these defines.
</p> </p>
<p> <p>

View file

@ -5,7 +5,7 @@
int main(int argc, char **argv) { int main(int argc, char **argv) {
int a = 42; int a = 42;
int b = 105; int b = 105;
int g = _wrap_gcd(a, b); int g = gcd(a, b);
printf("The gcd of %d and %d is %d\n", a, b, g); printf("The gcd of %d and %d is %d\n", a, b, g);
printf("Foo = %f\n", Foo); printf("Foo = %f\n", Foo);
Foo = 3.1415926; Foo = 3.1415926;

View file

@ -54,8 +54,6 @@ FAILING_CPP_TESTS := \
arrays_global_twodim \ arrays_global_twodim \
char_strings \ char_strings \
constant_pointers \ constant_pointers \
c_backend_cpp_natural_std_string \
c_backend_cpp_exception \
default_arg_values \ default_arg_values \
default_args \ default_args \
default_constructor \ default_constructor \
@ -89,7 +87,6 @@ FAILING_CPP_TESTS := \
nested_class \ nested_class \
nested_scope \ nested_scope \
nested_template_base \ nested_template_base \
operator_overload \
overload_arrays \ overload_arrays \
smart_pointer_extend \ smart_pointer_extend \
smart_pointer_not \ smart_pointer_not \

View file

@ -85,12 +85,12 @@ int main(int argc, const char *argv[]) {
// getting, setting and calling function pointers isn't supported yet // getting, setting and calling function pointers isn't supported yet
#if 0 #if 0
SomeTypeForMemFnPtr func1 = _wrap_get_func1_ptr(); SomeTypeForMemFnPtr func1 = get_func1_ptr();
Foo_func_ptr_set(f, func1); Foo_func_ptr_set(f, func1);
assert(_wrap_test_func_ptr(f, 2) == 28); assert(test_func_ptr(f, 2) == 28);
SomeTypeForMemFnPtr func2 = _wrap_get_func2_ptr(); SomeTypeForMemFnPtr func2 = get_func2_ptr();
Foo_func_ptr_set(f, func2); Foo_func_ptr_set(f, func2);
assert(_wrap_test_func_ptr(f, 2) == -14); assert(test_func_ptr(f, 2) == -14);
#endif #endif
delete_Bar(b); delete_Bar(b);

View file

@ -2,7 +2,7 @@
#include "cpp_basic_template_function/cpp_basic_template_function_wrap.h" #include "cpp_basic_template_function/cpp_basic_template_function_wrap.h"
int main() { int main() {
assert(_wrap_GetMaxInt(3, 5) == 5); assert(GetMaxInt(3, 5) == 5);
return 0; return 0;
} }

View file

@ -107,6 +107,7 @@ class C:public Language {
File *f_wrappers_cxx; File *f_wrappers_cxx;
File *f_wrappers_types; File *f_wrappers_types;
File *f_wrappers_decl; File *f_wrappers_decl;
File *f_wrappers_aliases;
File *f_init; File *f_init;
String *empty_string; String *empty_string;
@ -416,6 +417,11 @@ public:
f_wrappers_types = NewString(""); f_wrappers_types = NewString("");
f_wrappers_decl = NewString(""); f_wrappers_decl = NewString("");
// We also define aliases for the global wrapper functions to allow calling them using their original names, but as this can result in problems (as usual
// when using the preprocessor), we provide a way to disable this by defining SWIG_NO_WRAPPER_ALIASES when compiling the generated code and so we use a
// separate section for this too.
f_wrappers_aliases = NIL;
{ {
cplusplus_output_guard cplusplus_output_guard
@ -434,6 +440,13 @@ public:
Dump(f_wrappers_h_body, f_wrappers_h); Dump(f_wrappers_h_body, f_wrappers_h);
Delete(f_wrappers_h_body); Delete(f_wrappers_h_body);
if (f_wrappers_aliases) {
Dump(f_wrappers_aliases, f_wrappers_h);
Delete(f_wrappers_aliases);
Printv(f_wrappers_h, "#endif /* SWIG_NO_WRAPPER_ALIASES */\n", NIL);
}
} // close wrapper header guard } // close wrapper header guard
// write all to the file // write all to the file
@ -928,12 +941,14 @@ ready:
* emit_wrapper_func_decl() * emit_wrapper_func_decl()
* *
* Declares the wrapper function, using the C types used for it, in the header. * Declares the wrapper function, using the C types used for it, in the header.
* Also emits a define allowing to use the function without the "_wrap_" prefix.
* The node here is a function declaration. * The node here is a function declaration.
* ---------------------------------------------------------------------- */ * ---------------------------------------------------------------------- */
void emit_wrapper_func_decl(Node *n, String *name) void emit_wrapper_func_decl(Node *n, String *name)
{ {
// C++ function wrapper proxy code // C++ function wrapper proxy code
String *wname = IS_SET_TO_ONE(n, "c:globalfun") ? Swig_name_wrapper(name) : Copy(name); bool const is_global = IS_SET_TO_ONE(n, "c:globalfun");
String *wname = is_global ? Swig_name_wrapper(name) : Copy(name);
String *preturn_type = get_wrapper_func_return_type(n); String *preturn_type = get_wrapper_func_return_type(n);
String *pproto = get_wrapper_func_proto(n); String *pproto = get_wrapper_func_proto(n);
String *wrapper_call = NewString(""); String *wrapper_call = NewString("");
@ -941,6 +956,16 @@ ready:
// add function declaration to the proxy header file // add function declaration to the proxy header file
Printv(f_wrappers_decl, preturn_type, " ", wname, "(", pproto, ");\n\n", NIL); Printv(f_wrappers_decl, preturn_type, " ", wname, "(", pproto, ");\n\n", NIL);
if (is_global) {
if (!f_wrappers_aliases) {
// Allocate it on demand.
f_wrappers_aliases = NewStringEmpty();
Printv(f_wrappers_aliases, "#ifndef SWIG_NO_WRAPPER_ALIASES\n", NIL);
}
Printf(f_wrappers_aliases, "#define %s %s\n", name, wname);
}
// cleanup // cleanup
Delete(wname); Delete(wname);
Delete(pproto); Delete(pproto);