Generate wrapper aliases only if requested and not by default

Defining the aliases by default results in conflicts when including
headers from multiple modules as e.g. SWIG_PendingException_get() is
defined in all of them, and could also easily result in other unwanted
clashes, so make this opt-in and update the examples and tests relying
on using the wrappers without the module prefix to define
SWIG_DEFINE_WRAPPER_ALIASES explicitly.
This commit is contained in:
Vadim Zeitlin 2021-10-20 01:57:20 +02:00
commit 2f6f6df211
14 changed files with 20 additions and 7 deletions

View file

@ -192,7 +192,7 @@ Wrapping C functions and variables is obviously performed in a straightforward w
<p>
For each C function declared in the interface file a wrapper function with a prefix, required to make its name different from the original one, is created. The prefix for the global functions is <tt>module_</tt>, i.e. the name of the SWIG module followed by underscore. If <tt>nspace</tt> feature is used, the prefix for a function defined in a namespace is <tt>namespace_</tt> -- note that it does <em>not</em> contain the module prefix, as it's not necessary to make a unique function name in this case. The wrapper function performs a call to the original function, and returns its result. For convenience, a <tt>#define func prefix_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.
For each C function declared in the interface file a wrapper function with a prefix, required to make its name different from the original one, is created. The prefix for the global functions is <tt>module_</tt>, i.e. the name of the SWIG module followed by underscore. If <tt>nspace</tt> feature is used, the prefix for a function defined in a namespace is <tt>namespace_</tt> -- note that it does <em>not</em> contain the module prefix, as it's not necessary to make a unique function name in this case. The wrapper function performs a call to the original function, and returns its result. For convenience, a <tt>#define func prefix_func</tt> can be provided in the generated header file to make it possible to call the function under its original name: predefine <tt>SWIG_DEFINE_WRAPPER_ALIASES</tt> before including the wrapper header to enable these defines, which are disabled by default to avoid accidental name clashes.
</p>
<p>

View file

@ -5,6 +5,7 @@
#include <stdio.h>
#include <assert.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "example_wrap.h"
static void show_exception(const char* prefix) {

View file

@ -1,5 +1,6 @@
#include <stdio.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "example_wrap.h"
int main(int argc, char **argv) {

View file

@ -1,4 +1,6 @@
#include <assert.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "c_backend_cpp_exception/c_backend_cpp_exception_wrap.h"
int main()

View file

@ -1,6 +1,8 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "c_backend_cpp_natural_std_string/c_backend_cpp_natural_std_string_wrap.h"
int main()

View file

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "char_strings/char_strings_wrap.h"
int main() {

View file

@ -1,3 +1,4 @@
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "cpp_basic/cpp_basic_wrap.h"
#include <assert.h>
#include <stdio.h>

View file

@ -1,4 +1,6 @@
#include <assert.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "cpp_basic_template_function/cpp_basic_template_function_wrap.h"
int main() {

View file

@ -1,6 +1,7 @@
#include <assert.h>
#include <stdlib.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "enums/enums_wrap.h"
int main() {

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "exception_order/exception_order_wrap.h"
int main() {

View file

@ -4,7 +4,7 @@
int main(int argc, const char *argv[])
{
init();
global_vars_init();
assert(strcmp(b_get(), "string b") == 0);
assert(x == 1234);

View file

@ -1,3 +1,4 @@
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "li_std_pair/li_std_pair_wrap.h"
#include <assert.h>

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#define SWIG_DEFINE_WRAPPER_ALIASES
#include "operator_overload/operator_overload_wrap.h"
#define assert(x,msg) if (!x) { printf("%d: %s\n", x, msg); exit(1); }

View file

@ -476,9 +476,8 @@ public:
f_wrappers_types = 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.
// We may 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), this is only done when SWIG_DEFINE_WRAPPER_ALIASES is defined, so use a separate section for this.
f_wrappers_aliases = NIL;
{
@ -504,7 +503,7 @@ public:
Dump(f_wrappers_aliases, f_wrappers_h);
Delete(f_wrappers_aliases);
Printv(f_wrappers_h, "#endif /* SWIG_NO_WRAPPER_ALIASES */\n", NIL);
Printv(f_wrappers_h, "#endif /* SWIG_DEFINE_WRAPPER_ALIASES */\n", NIL);
}
} // close wrapper header guard
@ -903,7 +902,7 @@ public:
if (!f_wrappers_aliases) {
// Allocate it on demand.
f_wrappers_aliases = NewStringEmpty();
Printv(f_wrappers_aliases, "#ifndef SWIG_NO_WRAPPER_ALIASES\n", NIL);
Printv(f_wrappers_aliases, "#ifdef SWIG_DEFINE_WRAPPER_ALIASES\n", NIL);
}
Printf(f_wrappers_aliases, "#define %s %s\n", name, wname);