Is there any way, short of putting an attribute on each function prototype, to let gcc know that C functions can never propagate exceptions, i.e. that all functions declared ins
You can always use -fno-exceptions
which will ensure that the c++ compiler does not generate exception propagation code.
When exception is raised it generate interrupt which unroll stack and override existing stack. It goes up to the point where try/except syntax is. This mean than you do not have any overhead if you do not use exceptions. Only overhead in memory/time is at try/catch blocks and stack unrolling at throw().
If your c functions does not generate exceptions your overhead will be only in space when you will call try/catch in your C++ but is same for any number of exceptions. (and small time overhead on initialising this small space whit constant).
GCC 4.5 seems to optimize those out for me automatically. Indeed, this line appears in the list of changes at http://gcc.gnu.org/gcc-4.5/changes.html :
- GCC now optimize exception handling code. In particular cleanup regions that are proved to not have any effect are optimized out.
Side note:
Are you sure telling the compiler "all these funcs never throw" is exactly what you want ?
It's not necessarily so that extern "C" ...
functions cannot propagate/trigger exceptions. Take a case in point:
class Foo {
public:
class Away {};
static void throwaway(void) { throw Away(); }
}
extern "C" {
void wrap_a_call(void (*wrapped)(void)) { wrapped(); }
}
int main(int argc, char **argv)
{
wrap_a_call(Foo::throwaway);
return 0;
}
Compiling and running this creates a C-linkage function wrap_a_call()
which, when called like above, will happily cause an exception:
$ ./test
terminate called after throwing an instance of 'Foo::Away'
Abort(coredump)
I.e. there can be "exception leakage" with extern "C"
(through invoking function pointers) and just because you're using/invoking extern "C"
functions in a particular place in C++ doesn't guarantee no exceptions can be thrown when invoking those.