GCC comp_except_specs 和异常处理/解析

发布于 2024-10-06 20:49:44 字数 536 浏览 3 评论 0原文

我有点不知所措...我已经开始深入研究 GCC G++ 并尝试在函数抛出异常规范中未定义的异常时抛出警告/错误,反之亦然(你知道,就像爪哇)。

我感觉我走进了一个全新的星球,里面充满了任意宏,这些宏接收所有不同类型的树,将它们吐出来,但我不知道如何与它们交互......

我找到了 comp_ except_specs 比较两个异常类型并返回它们是否相等;但是,仅传递从 build_throw 返回的异常和从 cp_parser_exception_specification_opt 返回的树会导致内部编译器错误。

如果您遵循其中任何一条,那么您很可能比我对此事有更多的了解。我不是在寻找“答案”,虽然这很好,但任何有关这方面的信息将非常感激!

编辑: 我的目标是改变编译器(不知道为什么我当时没有提到这一点......)以合并 Java 风格的异常规范错误。我最终确实想出了一个好方法来实现它——我想我不记得是怎么实现的了,那已经是很久以前的事了。

I'm a bit over my head here... I've begun digging into the GCC G++ and am trying to throw warning/errors if a function throws exceptions not defined in an exception-specification or vice-versa (you know, like java).

I feel like I've walked into a whole new planet filled with arbitrary macros that take in tree's of all different types, spits them out and I've no clue how to interface with them...

I've located comp_except_specs which compares two exception types and returns whether they're equivalent; however, just passing the returned exception from build_throw and the tree returned from cp_parser_exception_specification_opt causes an internal compiler error.

If you've followed any of that, chances are you have more knowledge on this matter than I do. I'm not looking for the "answer" although that'd be nice but any information on this would be extremely appreciated!

Edit:
My goal was to alter the compiler (not sure why I didn't mention that at the time...) to incorporated Java style exception-specification errors. I eventually did come up with a good way to implement it - thought I don't remember how, was too long ago now.

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

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

发布评论

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

评论(3

篱下浅笙歌 2024-10-13 20:49:44

我不想让你失望,但据我所知,类似 Java 的异常检查在 C++ 中是不可能的,甚至作为 GCC 特定的扩展也是如此。

这是一个疏忽,因为异常是在很晚的阶段未经深思熟虑就附加到语言上的。更糟糕的是,看起来相似但执行完全不同且通常适得其反的事情是以异常规范的形式添加的。

考虑到这次惨败及其在类 Java 检查所需语法方面的影响,以及 C++ 社区中异常失败和疲劳的普遍感觉,我们可能永远不会在 C++ 中获得适当的异常检查。

I hate to disappoint you, but but as far as I know Java-like exception checking isn't possible in C++, not even as a GCC-specific extension.

This was an oversight caused by the fact that exceptions were bolted onto the language in a very late stage, without due deliberation. Worse, something that looks similar, but does something completely different and usually counter-productive, was added in the form of exception specifications.

Given that fiasco and its implications in terms of e.g. required syntax for Java-like checking, and a general feeling of exception defeat and fatigue in the C++ community, we probably won't get proper exception checking in C++ ever.

夏花。依旧 2024-10-13 20:49:44

编辑除了说 g++ 的源代码很复杂之外,我不太确定你实际上在问什么。

下面的旧答案(似乎与实际问题没有太大关系,但我会保留它,以防有一天有人用谷歌搜索它并发现它有用)

正如另一个答案所说,C++ 没有 Java 风格的检查异常 [1]。事实上,C++ 标准的下一个迭代 C++11(以前的 C++0x)弃用了异常规范,而是添加了一个“noexcept”关键字,可以用来代替旧的“throw()”。参见例如

http://www.gotw.ca/publications/mill22.htm

< a href="https://stackoverflow.com/questions/5756304/no except-vs-throws-nothing">"noexcept" 与 "抛出:无"

http://www.boost.org/development/requirements.html#Exception-specation

[1] 当然,还有是使用Java并且不喜欢检查异常的人,一些框架(Spring IIRC?)将标准异常包装在非检查异常中(继承自RuntimeException)。

EDIT I'm not really sure what you're actually asking, beyond stating that the source code of g++ is complicated.

Old answer below (which doesn't have that much to do with the actual question, it seems, but I'll leave it in case somebody googles it up someday and finds it useful)

As another answer says, C++ doesn't have Java-style checked exceptions [1]. In fact, the next iteration of the C++ standard, C++11 (previously C++0x) deprecated exception specifications, and instead adds a "noexcept" keyword that can be used instead of the old "throw()". See e.g.

http://www.gotw.ca/publications/mill22.htm

"noexcept" vs "Throws: nothing"

http://www.boost.org/development/requirements.html#Exception-specification

[1] Of course, there are people who use Java and don't like checked exceptions, and some frameworks (Spring IIRC?) wrap standard exceptions in unchecked ones (inheriting from RuntimeException).

初心 2024-10-13 20:49:44

请把你的问题说得更具体一些,这个问题太宽泛了。

异常规范在 C++ 中几乎毫无用处。编译器不会检查它。我听说过静态分析工具可以做到这一点。不过我还没有真正遇到过。

确实很遗憾,与未定义或可疑的行为相比,异常是处理错误的更好方法。这只是 C++ 的又一错误,而 C++0x 只会加剧它。

我相信 G++ 黑客攻击超出了本网站的范围。 GCC/G++ 邮件列表更适合于此。

Please be more specific in your question, this is too broad.

Exception specifications are pretty much useless in C++ as it is. The compiler doesn't check against it. I've heard about static analysis tools that do. However I haven't actually encountered one yet.

It's a shame really, exceptions are a superior way to handle errors compared to undefined or dubious behavior. It's just one more thing C++ got it wrong and C++0x just aggravates it.

And G++ hacking I believe, is waaaay out of scope of this site. GCC/G++ mailing lists would be more appropriate for this.

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