Include required headers for %imported modules

We need the types from the imported modules, so #include the header
generated for it.

Unfortunately we have to half-guess the name used for that header, as
it's not available anywhere (and can't really be, as it could be changed
by a command line option used for another SWIG invocation that was used
to compile that module), but this seems to work well enough in practice.

In particular, this fixes failures in multi cpp tests, so that we don't
need FAILING_MULTI_CPP_TESTS any longer.
This commit is contained in:
Vadim Zeitlin 2021-11-25 02:50:37 +01:00
parent f7f59f4648
commit 17fbb0a528
3 changed files with 35 additions and 9 deletions

View File

@ -89,12 +89,6 @@ FAILING_CPP_TESTS := \
cpp11_rvalue_reference3 \
cpp11_type_aliasing \
FAILING_MULTI_CPP_TESTS := \
clientdata_prop \
imports \
import_stl \
template_typedef_import \
# Ignore warnings about failing to apply typemaps because none are defined:
# usually there is no need for special typemaps in C.
char_binary.cpptest director_binary_string.cpptest li_typemaps.cpptest li_typemaps_apply.cpptest long_long_apply.cpptest: SWIGOPT += -w453
@ -202,7 +196,7 @@ valuewrapper_const.cpptest \
valuewrapper_opaque.cpptest \
: SWIGOPT += -nocxx
%.multicpptest: SWIGOPT += -nocxx
%.multicpptest: SWIGOPT += -namespace $*
%.cpptest: SWIGOPT += -namespace $*

View File

@ -1,5 +1,5 @@
#if !defined(SWIGGO)
// Prevent Go from generating a Go module import - this test is not set up as true multiple modules
#if !defined(SWIGC) && !defined(SWIGGO)
// Prevent C/Go from generating a C include/Go module import - this test is not set up as true multiple modules
%module import_fragments_a
#endif

View File

@ -1361,6 +1361,38 @@ public:
return SWIG_OK;
}
/* -----------------------------------------------------------------------
* importDirective()
* ------------------------------------------------------------------------ */
virtual int importDirective(Node *n) {
// When we import another module, we need access to its declarations in our header, so we must include the header generated for that module. Unfortunately
// there doesn't seem to be any good way to get the name of that header, so we try to guess it from the header name of this module. This is obviously not
// completely reliable, but works reasonably well in practice and it's not clear what else could we do, short of requiring some C-specific %import attribute
// specifying the name of the header explicitly.
// First, find the top node.
for (Node* top = parentNode(n); top; top = parentNode(top)) {
if (Checkattr(top, "nodeType", "top")) {
// Get its header name.
scoped_dohptr header_name(Copy(Getattr(top, "outfile_h")));
// Strip the output directory from the file name, as it should be common to all generated headers.
Replace(header_name, SWIG_output_directory(), "", DOH_REPLACE_FIRST);
// And replace our module name with the name of the one being imported.
Replace(header_name, module_name, imported_module_name, DOH_REPLACE_FIRST);
// Finally inject inclusion of this header.
Printv(Swig_filebyname("cheader"), "#include \"", header_name.get(), "\"\n", NIL);
break;
}
}
return Language::importDirective(n);
}
/* -----------------------------------------------------------------------
* globalvariableHandler()
* ------------------------------------------------------------------------ */