Commit graph

4,376 commits

Author SHA1 Message Date
Vadim Zeitlin
d89a95a48c Use std/std_vector.i instead of a poor copy in c/std_vector.i
At the very least, this gives us a working vector<bool> and allows
"li_std_vector" unit test to pass. It is also just nice to reuse the existing
typemaps instead of having another copy of them.

As C doesn't have UTL, some parts of std_vector.i had to be excluded from it
however.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
29563ec98e Fix typemaps for bool
The existing typemaps didn't make much sense, simplify (there doesn't seem to
be any point in using $1_basetype when dealing with "bool") them and treat
"const bool&" as "bool", not as pointer, as this is much more convenient in C.

This also allows another unit test to pass.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
8eba14c6e0 Resolve typedefs in parameter types after applying ctype typemap
Typedefs were not resolved for non-object types in spite of
SwigType_typedef_resolve_all() call as it didn't affect "c_parm_type".

Actually it is not even clear if resolving typedefs is a good idea as using
them probably makes the generated code more clear, but at least in the case of
nested typedefs we do need to do it as "Class::Type" can't appear in C code.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
1912fd3a42 Suppress a warning in abstract_typedef unit test
Use an explicit cast to cast from derived to base, there is no better solution
currently.
2016-09-15 01:27:41 +02:00
Vadim Zeitlin
bca00c65dd Implement cdata typemaps for C
Just use the struct directly in the C wrappers.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
a6f53d18ea Fix memory leak in std::vector example
Delete the object we allocate.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
55f7d51979 Fix memory leak in the exception example
Delete the object we allocate.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
dfac9ce325 Show how to handle a custom exception class in the example
Define "throws" typemap for it.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
10d25c7327 Drop longjmp-based exception handling approach
Using longjmp was incompatible with using C++ objects in the code using the
wrappers, and using this C API from C++ to avoid ABI incompatibilities between
different C++ compilers is one of the main reasons for using this module.

Also, this required using a separate SwigObj instead of just using the real
object pointer which inevitably resulted in memory leaks whenever a non owned
object was returned from anywhere, e.g. from a member accessor or any method
returning pointer or reference.

Abandon the attempts to recreate C++ exceptions in C and just use a very
simple approach allowing to pass an error message out of band after any
function call in a global variable. An alternative could be to add a special
"out" error parameter to each and every function, but this risked being too
verbose, especially for the functions which don't really throw, and the calls
to SWIG_PendingException_get() won't need to be made explicitly when using a
C++ wrapper around the generated C API in the future.

This simplifies both the module and the generated code, in particular we don't
need any runtime code at all any more and there is no need for an extra level
of indirection for every object.

It also makes a couple more tests pass.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
bda731cd8f Change naming convention for wrapped ctors and dtor
Use Foo_{new,delete}() instead of {new,delete}_Foo() to ensure that all
methods of the class Foo start with the corresponding prefix.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
4ba57e577a Fix obvious memory leaks in the std_vector example
Delete the class when we're done with it.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
4a0ed9cb42 Declare printf() before using it in std_vector example
Just add missing #include <stdio.h>.
2016-09-15 01:27:40 +02:00
Vadim Zeitlin
75d9cb4769 Remove C-specific cpp_basic_global_var_built_in unit test
There is no need for it, the common global_vars unit test is more extensive
and passes too now.
2016-09-15 01:27:39 +02:00
Vadim Zeitlin
862abb42e6 Allow suppressing progress messages when running C test suite
This makes the warnings more noticeable.
2016-09-15 01:27:39 +02:00
Vadim Zeitlin
8e30abf7ab Don't use "this" method parameter for disambiguating overloads
When building the unique suffix for each member of the overloaded functions
set, don't use the first "this" parameter of the object methods in it as it's
the same for all of them and so is completely useless for disambiguation
purposes and just results in unnecessarily long and ugly names.

Use "_const" suffix for the methods differing by their const-ness only, this
is necessary now that we don't use "_pFoo" or "_pcFoo" in their names.

This makes it superfluous to check for c:objstruct in
functionWrapperAppendOverloaded() (and checking for it there was not enough
neither, as the changes in the test suite show, sometimes the "this" parameter
type still found its way into the generated wrappers).
2016-09-15 01:27:39 +02:00
Vadim Zeitlin
1dfbd492b9 Add more warning-suppressing casts to special_variables test
Avoid harmless but annoying warning about assigning a literal string to
a non-const char pointer.
2016-09-15 01:27:39 +02:00
Vadim Zeitlin
e068aa8cc9 Simplify ctors/dtor handling by reusing base class code
Just use our custom object creation/destruction code, but let the base class
do everything else.

This uncovered complete lack of support for ctors/dtors defined using %extend,
but this is not new and will have to remain broken for now.
2016-09-15 01:27:39 +02:00
Vadim Zeitlin
160074a025 Suppress warnings about no typemaps being defined in C test suite
In many (although perhaps not all, so don't suppress this warning
unconditionally) cases, there is no need for special typemaps in C, the
parameters can be just used directly, so this warning is harmless but it was
given many times during the test suite execution, distracting from more
important messages.
2016-04-21 15:23:27 +02:00
Vadim Zeitlin
e2bb89ccb3 Replace unknown enums with int in the wrappers
An enum can be declared outside of the code wrapped by SWIG, just use int
instead of it then.
2016-04-21 01:37:42 +02:00
Vadim Zeitlin
4b5e5d0cc8 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.
2016-04-21 01:37:42 +02:00
Vadim Zeitlin
36fdd4e78f Wrap unknown type as opaque pointers
Don't give errors for the unknown types, instead wrap them using the mangled
name of the type.

This is the expected (even if not particularly useful) SWIG behaviour and
allows many more tests to pass.
2016-04-21 01:37:42 +02:00
Vadim Zeitlin
2ec9d9da6e Don't try to export variables in namespace scope directly
The generated code, which used C++ scope operator "::" in a C header, didn't
compile and couldn't work anyhow.

Just use the accessor functions for not really global variables.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
e39265ba13 Handle primitive types when expanding "resolved_type" in typemaps
Although the default typemaps only use "resolved_type" for non-simple types,
the generic SWIGTYPE typemap can also be applied explicitly to other types as
"apply_strings" and "char_binary" unit tests do.

In this case just use the original type unchanged if we can, this is enough to
make these tests pass.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
59c131ea08 Fix broken anonymous_bitfield C checking code
For some reason the test expected another bit field be changed when setting
the given one, which was just plain wrong and prevented the test from passing.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
b062a63cd1 Fix abstract_typedef C test code
Don't refer to the typedef name (especially with a typo in it...), it is not
supposed to be exported.

Also fix the write() method call: use the right class for it and pass it the
correct arguments.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
b7bd0165e8 Remove more tests which pass now
All tests detected as passing by "make check-failing".
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
19268d0865 Add check-failing target to check that failing tests still do fail
This is useful to remove the tests which pass after the latest fixes from the
list of the failing tests.
2016-04-21 01:37:41 +02:00
Vadim Zeitlin
3764ee1764 Make anonymous_bitfield test compile
It still doesn't pass, but at least include the correct header from it.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
af9dd72bb6 Fix function name in cpp_basic_template_function test
Use "_wrap_" prefix to make the test compile and pass.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
2edbbfd87f Fix cpp_basic_global_var_built_in unit test
Use the correct header name in it and remove it from the list of the failing
tests.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
7bb7fe135a Let cpp_basic unit test pass without method pointers support
This was the only part of the test that didn't work, test at least the rest of
it.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
e8c6bfbc8f Remove completely broken handling of member function pointers
Member function pointers can't possibly be represented as function pointers,
they have a strictly bigger size and attempting to do it resulted in code
which, with a lot of bad casts, compiled, but crashed during run-time.

The proper solution is to represent C++ method pointers with an appropriate
opaque type, but for now this remains broken -- just make it explicitly broken
instead of pretending that it works when it actually has no chance to.

Let "namespace_spaces" unit test pass, as it's not really related to the
function pointers, by adding an explicit SWIGC test to it.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
11fbdba9de Remove cpp_basic_template_class from the list of failing tests
It passes now.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
e28bd6bf3a Fix cpp_basic_class_var_pub_member_built_in test compilation
Use the correct header name in the test.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
4d774887cc Only syntax check generated header when not using run test
There is no need to syntax check the header if it's going to be really used,
so this is a small optimization.

It also separates building of the wrappers from testing them better.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
87d4a0da71 Reuse c_compile rule in C unit test suite makefile
No real changes, just reuse the commands defined in the C section of the top
level Examples/Makefile instead of (imperfectly, i.e. CFLAGS was forgotten
here) duplicating them in Examples/test-suite/c/Makefile.
2016-04-21 01:37:40 +02:00
Vadim Zeitlin
7a392fd21f Fix typemaps for "void*"
Pass void pointers directly to C, don't handle them objects which was done by
default previously because we didn't define any specific typemaps for them.

This allows to reenable another test.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
85c9b87f9b Fix ctype typemap for bool& too
Don't add an extra pointer, this is already done at the code level currently.

This makes the "bools" unit test pass.
2016-04-20 18:46:12 +02:00
Vadim Zeitlin
c8e4abd16e Use wrapper functions for global variables of non-C types
Don't try to export the variables whose type can't be represented in C
directly, this can't work.

Instead, just let use the default implementation to generate wrapper functions
for them.

Update "cpp_basic_global_var_class" unit test accordingly and remove it from
the list of the failing tests.
2016-04-20 18:45:42 +02:00
Vadim Zeitlin
c73acc44a7 Show the number of passed tests in "make check"
This is especially useful when some tests are disabled by using
FAILING_XXX_TESTS variables.
2016-04-20 01:53:23 +02:00
Vadim Zeitlin
668f2b4a85 Syntax check headers by C module when running the unit tests suite
Catch errors in the generated headers even when we don't have a run test.

Unfortunately this uncovered that almost no unit tests actually pass, so many,
many more of them had to be disabled.
2016-04-18 20:30:49 +02:00
Vadim Zeitlin
d98ef6ca07 Remove C-specific enum unit tests
These tests are just a small subset of the full language-independent tests and
it doesn't make much sense to keep them now that the full tests pass.
2016-04-16 00:48:17 +02:00
Vadim Zeitlin
44d68197d6 Hack for "true" and "false" used as enum element values
This is not a full solution by far, but should address the most common case.

This also allows "cpp_enum" unit test to pass.
2016-04-16 00:45:58 +02:00
Vadim Zeitlin
2a4bc0e7e5 Fix function names in the cpp_enum C test
This was just an error in the test.
2016-04-16 00:45:41 +02:00
Vadim Zeitlin
3d21bb2c96 Hack around parser bug with char enum element values
Enum element somehow lose the single quotes around them, compensate for it in
C module code (other modules use module-specific constvalue feature to work
around it, but it seems better to do it in SWIG itself rather than leaving the
user code to deal with it).

This finally makes the "enums" unit test pass.
2016-04-16 00:25:50 +02:00
Vadim Zeitlin
168e16ec67 Hack to support variables of anonymous enum types
Declare the variable as int as this is how variables of enum types are stored
(although this would have to be revised when support for C++11 base enum type
is added) and, although ugly, this at least allows the generated code to
compile.

This fixes some (but not all yet) errors in the "enums" and "cpp_enum" unit
tests.
2016-04-16 00:25:10 +02:00
Vadim Zeitlin
fb4d700279 Represent enums as themselves in generated code
Define enum type and enum values as the elements of this enum instead of using
preprocessor #defines for them.

This fixes a couple of enum-related unit tests.
2016-04-16 00:10:48 +02:00
Vadim Zeitlin
e1a4b02f69 No real changes, just convert files to Unix EOLs
Some tests and examples files as well as the C manual chapter used DOS EOLs,
get rid of them for consistency with all the other text files.
2016-04-16 00:10:22 +02:00
Vadim Zeitlin
c05f03d485 Fix esoteric regression in the previous commit
Implement the check for duplicate SWIG_derives_from<> specializations which
could happen if a template type with default parameters was exported both with
implicit default values used for these parameters and the same values
specified explicitly.

This makes the code more complicated and is very rarely needed (in addition to
the above, the template class must also have a base class to trigger the bug),
but it does happen and used to work, so restore this after breaking it in the
last commit and reenable the template_default2 unit test.
2016-04-16 00:06:35 +02:00
Vadim Zeitlin
818b399c9e Drastically simplify and optimize object creation and destruction
Don't populate the typenames array for each and every created object and don't
search it when deleting every object, this is O(N) in number of classes and is
completely impractical for any big library where N can easily be several
thousands. Use destructor function which will correctly destroy the object
instead.

Also don't store each created objects in the global registry, there doesn't
seem to be any point in it.

And, in fact, don't bother with the typenames at all, just use another
pseudo-virtual function for checking whether a class inherits from a class
with the given name.

There is unfortunately one problem with the new approach: it doesn't work when
the same C++ type is wrapped under different names as this results in multiple
specializations of SWIG_derives_from<> for this type. But this is a relatively
rare situation (but which does arise in template_default2 unit test, which had
to be disabled) and could be fixed in the future by completely resolving the
type, including the default template parameters values, and checking if
SWIG_derives_from had been already specialized for it. In the meanwhile, this
regression is not a big deal compared to the advantages of the new approach.
2016-04-15 23:25:32 +02:00