六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 26|回复: 0

__attribute__ noreturn

[复制链接]

升级  14.54%

1227

主题

1227

主题

1227

主题

榜眼

Rank: 8Rank: 8

积分
3727
 楼主| 发表于 2013-1-26 14:39:51 | 显示全部楼层 |阅读模式
This attribute tells the compiler that the function won't ever return, and this can be used to suppress errors about code paths not being reached. The C library functions abort() and exit() are both declared with this attribute:
extern void exit(int)   __attribute__((noreturn));extern void abort(void) __attribute__((noreturn));Once tagged this way, the compiler can keep track of paths through the code and suppress errors that won't ever happen due to the flow of control never returning after the function call.
In this example, two nearly-identical C source files refer to an "exitnow()" function that never returns, but without the __attribute__ tag, the compiler issues a warning. The compiler is correct here, because it has no way of knowing that control doesn't return.
$ cat test1.cextern void exitnow();int foo(int n){        if ( n > 0 ){                exitnow();/* control never reaches this point */}        else                return 0;}$ cc -c -Wall test1.ctest1.c: In function `foo':test1.c:9: warning: this function may return with or without a valueBut when we add __attribute__, the compiler suppresses the spurious warning:
$ cat test2.cextern void exitnow() __attribute__((noreturn));int foo(int n){        if ( n > 0 )                exitnow();        else                return 0;}$ cc -c -Wall test2.cno warnings!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表