模拟GCC的__builtin_unreachable? [英] Emulating GCC's __builtin_unreachable?

查看:1431
本文介绍了模拟GCC的__builtin_unreachable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个整体很多的关于开关的警告,只是部分覆盖了已切换枚举的范围。因此,我想有一个默认的所有这些开关,并在这种情况下放置 __ builtin_unreachable (GCC内置),以便编译器知道情况是不可达。

I get a whole lot of warnings about switches that only partially covers the range of an enumeration switched over. Therefor, I would like to have a "default" for all those switches and put __builtin_unreachable (GCC builtin) in that case, so that the compiler know that case is not reachable.

但是,我了解到GCC4.3不支持该内置。有什么好的方法来模拟这个功能吗?我想到取消引用一个空指针,但可能有其他不良的效果/警告等。你有什么更好的主意吗?

However, I came to know that GCC4.3 does not support that builtin yet. Is there any good way to emulate that functionality? I thought about dereferencing a null pointer instead, but that may have other undesirable effects/warnings and such. Do you have any better idea?

推荐答案

您可以调用声明为 _Noreturn 的内联函数该调用为不可达。编译器允许在这样的函数后抛出任何代码。如果函数本身 static (并且返回),编译器通常也会内联函数。下面是一个例子:

You can call an inline function declared _Noreturn to mark anything after that call as unreachable. The compiler is allowed to throw out any code after such a function. If the function itself is static (and does return), the compiler will usually also inline the function. Here is an example:

static _Noreturn void unreachable() {
    return; /* intentional */
}

/* ... */

foo();
bar(); /* should better not return */
unreachable();
baz(); /* compiler will know this is not reachable */

请注意, _Noreturn 确实会返回。请确保不会调用该函数。

Notice that you invoke undefined behavior if a function marked _Noreturn indeed returns. Be sure that said function will never be called.

这篇关于模拟GCC的__builtin_unreachable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆