Can g++ check the throw specifiers?
-
15-11-2019 - |
Pregunta
Two questions about this :
Is there a way to force
g++
to ignore thethrow
specifiers ?
(for example, as I remember, Visual Studio ignores the throw specifiers, different fromthrow()
)Is it possible to force
g++
to check if the throw specifiers are correct - what I mean is to check ( and this can be done by a one-pass compilers ) if the functions with throw specifiers call functions, that may throw by just watching theirs throw specifiers and watch for executingthrow
for exceptions, that will violate the specifiers ? (Note: this should not watch the functions without throw specifiers, because this could cause tons of warnings )
EDIT: I'll add some examples for my second question.
Suppose we have:
// sorry for the coding style here, but I don't want it to be unnecessary long
class A { /* .. */ };
class B : public A { /* .. */ };
class C { /* .. */ };
void no_throw_spec() { /* .. */ }
void no_throw_at_all() throw() { /* .. */ }
void throws_A() throw( A ) { /* .. */ }
// this is fine, don't do anything
void f()
{ no_throw_spec(); no_throw_at_all(); throws_A(); }
void g() throw()
{
no_throw_spec(); no_throw_at_all(); // OK
throws_A(); // warning here - throws_A() may throw A, but g() has throw()!
}
void h() throw( A )
{
no_throw_spec(); no_throw_at_all(); throws_A(); // OK
if( /* .. */ )
throw B(); // OK, B inherits A, it's OK
/* .. */
throw C(); // C does not inherit A, so WARNING!
}
Solución
gcc has an option
-fno-enforce-eh-specs
, see the documentation and check that it does what you want.I don't remember any way to statically check exception specifications with gcc.
Note that (dynamic) exception specifications are deprecated in C++0X which add a noexcept
exception specification replacing the empty exception specification case (it is also checked dynamically and has provisions helping to use it in templates).
Otros consejos
Yes, you can make g++ ignore throw by:
#define throw(x)
About the rest you need to change the compiler code or make your own script/program in build process that will check for those things, it can be easily done with regexp.
Edit:
about your comment, finding the hierarchy of exceptions is really easy. use regexp like:
class ([^ ]*) : ([^ ]*)
and input that to a hash, and later make hierarchical data.
To match exceptions in functions that throw them use:
([^\(\s]*)[\s]*([^\)])[\s]*(throw[\s]*\([^\)]*\)){((throw[\s]*[^;])|*)*}
its not tested and might have some errors, but good place to start