是否可以做一个#define NORETURN(x)
与 Open Watcom C 编译器(不是 C++)一起使用的宏?
根据精美的手册,我有这个文件test.c
:
#include <stdlib.h>
#ifdef __GNUC__
#define STATIC_NORETURN(sig) static void sig __attribute__((__noreturn__))
#define EXTERN_NORETURN(sig) extern void sig __attribute__((__noreturn__))
#endif
#ifdef __WATCOMC__
#pragma aux noreturn aborts;
#define STATIC_NORETURN(sig) static void __pragma("noreturn") sig
#define EXTERN_NORETURN(sig) extern void __pragma("noreturn") sig
#endif
STATIC_NORETURN(my_static_example(void));
EXTERN_NORETURN(my_extern_example(void));
static void my_static_example(void) {
exit(0);
}
void my_extern_example(void) {
my_static_example();
}
它使用 GCC 编译没有任何错误或警告:
$ gcc -Wall -Wextra -pedantic -std=gnu99 -c test.c
但不使用 Open Watcom 1.9:
$ wine wcc386 -q -wx test.c
test.c(14): Error! E1009: Expecting ')' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found ')'
test.c(14): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(15): Error! E1023: Storage class of parameter must be register or unspecified
test.c(15): Error! E1009: Expecting ')' but found 'noreturn'
test.c(15): Error! E1024: Declared symbol '__pragma' is not in parameter list
test.c(15): Error! E1009: Expecting ',' but found 'noreturn'
test.c(15): Error! E1026: Invalid declarator
test.c(15): Error! E1009: Expecting ',' but found ')'
test.c(15): Error! E1024: Declared symbol 'my_extern_example' is not in parameter list
test.c(17): Error! E1023: Storage class of parameter must be register or unspecified
test.c(17): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(17): Error! E1076: Missing semicolon at end of declaration
test.c(22): Warning! W131: No prototype found for function 'my_static_example'
test.c(14): Warning! W202: Symbol '__pragma' has been defined, but not referenced
仔细阅读后,我认为手册说__pragma
出于某种原因仅适用于 C++ 代码。但我不确定我是否理解正确。是否有任何 C 的等价物不需要在#pragma
每个函数定义的位置使用?我试图避免使用 ifdef,因此最好在一个中心位置定义一个可移植的 NORETURN 宏。