可以 g++检查抛出说明符?

发布于 2024-11-08 19:44:16 字数 1311 浏览 9 评论 0原文

关于此的两个问题:


编辑:我将为我的第二个问题添加一些示例。

假设我们有:

// 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!
}

Two questions about this :

  • Is there a way to force g++ to ignore the throw specifiers ?
    (for example, as I remember, Visual Studio ignores the throw specifiers, different from throw())

  • 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 executing throw 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!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-11-15 19:44:16
  • gcc 有一个选项 -fno-enforce-eh-specs,请参阅文档并检查它是否符合您的要求。

  • 我不记得任何使用 gcc 静态检查异常规范的方法。

请注意,(动态)异常规范在 C++0X 中已被弃用,它添加了一个 noexcept 异常规范来替换空的异常规范情况(它也是动态检查的,并且具有帮助在模板中使用它的规定)。

  • 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).

揪着可爱 2024-11-15 19:44:16

是的,您可以通过以下方式使 g++ 忽略抛出:

#define throw(x)

关于其余的内容,您需要更改编译器代码或在构建过程中制作自己的脚本/程序来检查这些事情,可以使用 regexp 轻松完成。

编辑:

关于您的评论,找到异常的层次结构非常容易。使用正则表达式,例如:

class ([^ ]*) : ([^ ]*)

并将其输入到哈希中,然后生成分层数据。
要匹配抛出它们的函数中的异常,请使用:

([^\(\s]*)[\s]*([^\)])[\s]*(throw[\s]*\([^\)]*\)){((throw[\s]*[^;])|*)*}

它未经测试,可能有一些错误,但很好的起点

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文