模拟 GCC 的 __builtin_unreachable?
我收到大量关于开关的警告,这些开关仅部分覆盖了所切换的枚举范围。因此,我希望所有这些开关都有一个“默认值”,并在这种情况下放置 __builtin_unreachable (GCC 内置),以便编译器知道该情况不可到达。
然而,我发现 GCC4.3 还不支持该内置函数。有什么好的方法来模拟该功能吗?我考虑过取消引用空指针,但这可能会产生其他不良影响/警告等。你有更好的主意吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
即将发布的 2023 年 C 标准修订版(C23、ISO/IEC 9899:2023)将包含一个新的宏
unreachable
,其效果与 gcc 的
__builtin_unreachable
相同。在较旧的 C 标准中,您可以调用声明为
_Noreturn
的内联函数,以将调用之后的任何内容标记为不可访问。编译器可以丢弃此类函数之后的任何代码。如果函数本身是静态的(并且确实返回),编译器通常也会内联该函数。下面是一个示例:请注意,如果标记为
_Noreturn
的函数确实返回,则您会调用未定义的行为。确保该函数永远不会被调用。The upcoming 2023 revision of the C standard (C23, ISO/IEC 9899:2023) is going to have a new macro
unreachable
with the effect of gcc's
__builtin_unreachable
.On older C standards, you may be able to 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 isstatic
(and does return), the compiler will usually also inline the function. Here is an example:Notice that you invoke undefined behavior if a function marked
_Noreturn
indeed returns. Be sure that said function will never be called.嗯,类似的东西(因为 __builtin_unreachable() 出现在 4.5 中):
Hmm, something like (since __builtin_unreachable() appeared in 4.5):
abort
(留下核心转储)或throw
(允许备用数据捕获)能否满足您的需求?您真的想要不覆盖完整枚举的 switch 语句吗?我几乎总是尝试列出所有可能的情况(无操作),没有默认情况,这样如果添加新的枚举,gcc 就会警告我,因为可能需要处理它们,而不是让它默默地(在编译期间)落下进入默认状态。
Would
abort
(leaving a core dump) orthrow
(allowing for alternate data capture) accommodate your needs?Do you really want to have switch statements that don't cover the full enumeration? I nearly always try to list all the possible cases (to no-op) with no default case so that gcc will warn me if new enumerations are added, as it may be required to handle them rather than letting it silently (during compile) fall into the default.
保持简单:
或者,更好的是:
keep it simple:
or, better yet:
编辑:
由于您希望编译器忽略无法访问的代码,因此以下是最简单的方法。
编译器会优化
x = x;
指令,尤其是当它无法访问时。用法如下:如果将
__builtin_unreachable()
放在foo()
的开头,则编译器会为未实现的生成链接器错误运算符=
。我在 gcc 3.4.6(64 位)中运行了这些测试。Edit:
Since you want to have unreachable code to be omitted by compiler, below is the simplest way.
Compiler optimizes away
x = x;
instruction especially when it's unreachable. Here is the usage:If you put
__builtin_unreachable()
in the beginning offoo()
then compiler generates a linker error for unimplementedoperator =
. I ran these tests in gcc 3.4.6 (64-bit).