CppSharp
CppSharp copied to clipboard
Simplify and optimize the generated C++ by using -femit-all-decls
Clang has a flag named -femit-all-decls
which generates symbols for any inlined and any used implicit functions. Combined with -O
, it removes private ones. This allows for much simpler generated C++ which simply allocates classes and gets most needed symbols. There are some complications, however, to think about:
-
-O
excludes protected functions too which means they either need special code or-O
must be dropped which will bloat the binary with private functions; - Free functions must be checked for symbols as well;
- This would limit the symbols to compilation with Clang only.
This Godbolt workspace demonstrates the approach as well as its problems.
What we seem to be doing right now works regardless of the compiler because the C++ standard requires it (as far as I know) and we are only exporting the symbols we actually need.
Usually compilers implement different versions with different names for flags like this and gathering the commands for all the compilers and expecting them to have the same behavior and work exactly as we want seems very hard and pointless when we already have a way to do it. Some compilers may export more symbols than we need, and some will do the opposite. I also don't see how this would give us faster performance, if that's what you actually meant when you said optimize
.
I agree with @josetr on this one.
We might have an issue in our current code where constructors of abstract specializations of templates don't get symbols. In case the problem's real, the approach above can solve it as seen as seen here. There are a few conditions to meet for actual results, however:
- Clang must use optimizations no more than
-O
; - GCC must use no optimizations or get
-fkeep-inline-functions
too; - MSVC seems to generate all symbols with any optimization but some research to make sure is required.
In other words, point 3 from the description is not obligatory under the conditions above.