c - 如何在 Watcom C 中定义 NORETURN 宏

标签 c c-preprocessor watcom noreturn

是否可以使用 Open Watcom C 编译器(而非 C++)执行 #define NORETURN(x) 宏?

基于 the fine manual我有这个文件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++ 代码中工作。但我不确定我是否理解正确。是否有不需要在每个函数定义处使用 #pragma 的 C 等效项?我试图避免使用 ifdef,所以最好在一个中心位置定义一个可移植的 NORETURN 宏。

最佳答案

阅读 https://open-watcom.github.io/open-watcom-v2-wikidocs/cguide.html它是:

__declspec( noreturn )
    indicates to the C/C++ compiler that function does not return.
    Example:
    ...

兼容stdnoreturn.h ,别想出自己的风格。做:

// possibly in some noreturn.h
// maybe even name it stdnoreturn.h
#if __STDC_VERSION__ >= 201110L
#include <stdnoreturn.h>
#elif defined(__WATCOMC__)
#define noreturn __declspec(noreturn)
#else
#error
#endif

#include <stdlib.h>

static noreturn void my_static_example(void) {
    exit(0);
}

noreturn void my_extern_example(void) {
    my_static_example();
}

wcc386 -q -wx test.c 上使用 2.0 版 编译良好。

关于c - 如何在 Watcom C 中定义 NORETURN 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540145/

相关文章:

c++ - 在没有预处理器的情况下扩展 C/C++ 函数宏

C预处理器: dynamic const char

c - Watcom 内联汇编文档

无法释放 C 中的一些内存

c++ - C/C++ 只比较一次

c - 为什么 x 被提升为 unsigned int?

C, strcat 之后的 char[] 格式错误的 int

c++ - 这个命名空间的目的是什么?

c - 打开 Watcom 错误 E1127 "Type required in parameter list"

c - 在 DOS 中为 "Daylight Savings Time"